#!/bin/bash

# read rc files if exist
[ -e ~/.thruk   ] && . ~/.thruk
[ -e ~/.profile ] && . ~/.profile

if [ -e $(dirname $0)/../lib/Thruk.pm ]; then
  export PERL5LIB="$PERL5LIB:$(dirname $0)/../lib";
  if [ -z $CATALYST_CONFIG ]; then export CATALYST_CONFIG="$(dirname $0)/../"; fi
elif [ ! -z $OMD_ROOT ]; then
  export PERL5LIB=$OMD_ROOT/share/thruk/lib:$PERL5LIB
  if [ -z $CATALYST_CONFIG ]; then export CATALYST_CONFIG="$OMD_ROOT/etc/thruk"; fi
else
  export PERL5LIB=$PERL5LIB:/usr/share/thruk/lib:/usr/lib/thruk/perl5;
  if [ -z $CATALYST_CONFIG ]; then export CATALYST_CONFIG='/etc/thruk'; fi
fi

eval 'exec perl -x $0 ${1+"$@"} ;'
    if 0;

#! -*- perl -*-
# vim: expandtab:ts=4:sw=4:syntax=perl
#line 24

###################################################
# clean up env
use Thruk::Utils::INC;
BEGIN {
    Thruk::Utils::INC::clean();
}

use strict;
use warnings;
use Getopt::Long qw//;

our $VERSION = '1.80';
my $branch       = '3';

my $options = {
    'verbose'  => 1,
    'backends' => [],
    'url'      => [],
};
Getopt::Long::Configure('no_ignore_case');
Getopt::Long::Configure('bundling');
Getopt::Long::GetOptions (
   "h|help"             => \$options->{'help'},
   "v|verbose"          => sub { $options->{'verbose'}++ },
   "q|quiet"            => \$options->{'quiet'},
   "V|version"          => \$options->{'version'},
   "r|remote-url=s"     => \$options->{'remoteurl'},
     "local"            => \$options->{'local'},
   "c|credential=s"     => \$options->{'credential'},
   "l|list-backends"    => \$options->{'listbackends'},
   "b|backend=s"        =>  $options->{'backends'},
   "A|auth=s"           => \$options->{'auth'},
   "a|action=s"         => \$options->{'action'},
   "y|yes"              => \$options->{'yes'},
   "f|force"            => \$options->{'force'},
   "i|all-inclusive"    => \$options->{'all_inclusive'},
   "<>"                 => sub { push @{$options->{'url'}}, "".shift },
) or do {
    print "usage: $0 [<options>] <action>\nsee --help for detailed help.\n";
    exit 3;
};
if($options->{'help'}) {
    require Pod::Usage;
    Pod::Usage::pod2usage( { -verbose => 2, -exit => 3 } );
}
if($options->{'version'}) {
    require Thruk::Config;
    $branch = Thruk::Config::get_git_name() unless $branch ne '';
    print "Thruk Version $VERSION".($branch ne '' ? '~'.$branch : '')."\n"; exit 0;
}
$options->{'verbose'} = 0 if $options->{'quiet'};
if(!$options->{'action'} and scalar @{$options->{'url'}} == 0 and !$options->{'listbackends'}) {
    print "usage: $0 [options]\nsee --help for complete help.\n";
    exit 3;
}

require Thruk::Utils::CLI;
my $cli = Thruk::Utils::CLI->new($options);
exit $cli->_run();

1;
__END__
##############################################

=head1 NAME

thruk - Command Line Utility for Thruk Monitoring Gui

=head1 SYNOPSIS

  Usage: thruk [options] [url|files...]

  Options:
  -h, --help                    Show this help message and exit
  -v, --verbose                 Print verbose output
  -V, --version                 Print version

  -b, --backend=<backend>       Comma seperated list of backends/sites
                                which should be used for querys.
  -l, --list-backends           List available backends/sites.
  -a, --action=<action>         Perform given action. see below for a list.

  -A, --auth=<auth>             Set username for querys

  -c, --credential=<pw>         Credential when connecting to remote
                                Thruk installations specified by the
                                remote-url.
  -r, --remote-url=<url>        URL to remote installation.
      --local                   Don't try to connect fastcgi daemon first.

  -y, --yes                     answer yes to all given questions.
  -f, --force                   some actions need to be forced

=head1 DESCRIPTION

This script gives access to various parts of the Thruk gui and allows to generate
excel reports, csv exports or any other url by using command line and bypassing the
webserver.

=head1 OPTIONS

script has the following arguments

=over 4

=item B<-h> , B<--help>

    print help and exit

=item B<-v> , B<--verbose>

    print verbose output too

=item B<-V> , B<--version>

    print version and exit

=item B<-b> I<backend>, B<--backend>=I<backend>

    specify backend(s), otherwise the default backends will be
    used. Specify the key of the backend. Use <-l> to get a list
    of backends.

=item B<-l> , B<--list-backends>

    get list of available backends

=item B<-a> I<action>, B<--action>=I<action>

    perform selected action:
      - url=<url>                   output url result
        -i / --all-inclusive        includes all css, javascript and images in the resulting html page
      - report=<nr>                 generate report
      - reportmail=<nr>             send report by mail

    Logcache: ( MySQL / MongoDB )
      - logcacheimport(=blocksize)  initial logcache import. Blocksize is in seconds. Defaults is one day.
      - logcacheupdate              update logcache
      - logcacheauthupdate          update logcache authorization information
      - logcachestats               print logcache statistics
      - logcacheclean(=days)        clean everything older than specified number of days. Default is one year.
      - logcacheoptimize            optimize and repair logcache (mysql only)

    Internal Commands
      - installcron                 update cronjobs for downtimes and reports
                                    (only needed during package installation)
      - uninstallcron               remove all thruk cronjobs
                                    (only needed during package deinstallation)
      - compile                     compile templates for faster access
      - bpd                         start business process daemon

    Testing Commands
      - listbackends                list all backends
      - listhosts                   list hostnames
      - listhostgroups              list hostgroupnames
      - command <host> <service>    print used command line for this host/service


=item B<-A> I<authname>, B<--auth>=I<authname>

    use this username for authentication

=item B<-c> I<credential>, B<--credential>=I<credential>

    use this key for authentication when using remoteurl

=item B<-r> I<remote-url>, B<--remote-url>=I<remote-url>

    Connect to this remote instance instead of the local one.
    Defaults to http://localhost/thruk/cgi-bin/remote.cgi.
    Retrieving information from the fcgi daemon saves time to
    startup and allows to connect to remote located sites.

=item B<--local>

    Don't contact remote server. Normally remote-url is tried first for
    faster access.

=item B<-y> , B<--yes>

    answer yes to a given questions during commands.

=back

=head1 RETURN VALUE

thruk returns 0 on success and >= 1 otherwise

=head1 EXAMPLES

List available backends

  %> thruk -l

Export the event log as excel file:

  %> thruk -A thrukadmin -a 'url=/thruk/cgi-bin/showlog.cgi?view_mode=xls' > eventlog.xls

Urls can be shortened.
Export all services into an excel file:

  %> thruk 'status.cgi?view_mode=xls&host=all' > allservices.xls

Export service availability data into a csv file:

  %> thruk -A thrukadmin -a 'url=avail.cgi?host=all&timeperiod=last7days&csvoutput=1' > all_host_availability.csv

Generate pdf report

  %> thruk -a 'report=1' > report.pdf

Generate pdf report and send it by mail

  %> thruk -a 'reportmail=1'

=head1 AUTHOR

2012-2013, Sven Nierlein, <sven@nierlein.de>

=cut
