#!/bin/sh
# read rc file if exists
if [ -e ~/.thruk  ]; then
    . ~/.thruk
fi

if [ ! -f $(dirname $0)/../thruk.conf ]; then
  export PERL5LIB=$PERL5LIB:/usr/lib/thruk/perl5:/usr/share/thruk/lib;
  if [ -z $CATALYST_CONFIG ]; then export CATALYST_CONFIG='/etc/thruk'; fi
fi

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

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

use strict;
use warnings;
use Getopt::Long;
use Pod::Usage;
use Thruk::Utils;

our $VERSION = '1.56';
my $branch   = '';
$branch      = Thruk::Utils::get_git_name() unless $branch ne '';

my $options = {
    'verbose'  => 0,
    'backends' => [],
};
Getopt::Long::Configure('no_ignore_case');
Getopt::Long::Configure('bundling');
GetOptions (
   "h|help"             => \$options->{'help'},
   "v|verbose"          => \$options->{'verbose'},
   "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'},
   "<>"                 => sub { $options->{'url'} = "".shift },
) or pod2usage( { -verbose => 2, -message => 'error in options' } );

if($options->{'version'}) { print "Thruk Version $VERSION".($branch ne '' ? '~'.$branch : '')."\n"; exit 0; }
pod2usage( { -verbose => 2, -exit => 1 } ) if $options->{'help'};
pod2usage( { -verbose => 2, -exit => 1 } ) if !$options->{'action'} and !$options->{'url'} and !$options->{'listbackends'};

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

1;

##############################################

=head1 NAME

thruk - Command Line Utility for Thruk Monitoring Gui

=head1 SYNOPSIS

Usage: thruk [options] | url]

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 this action:
                                  - url=<url>        output url result
                                  - report=<nr>      generate report
                                  - reportmail=<nr>  send report by mail

  -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.

=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>     request this url. Url can be shortened and the
                    leading /thruk/cgi-bin/ can be stripped.

    - report=<nr>   generate report by specifying its id.

=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.

=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, Sven Nierlein, <sven@nierlein.de>

=cut
