Permalink
Browse files

cli tool: restructured arguments

  • Loading branch information...
1 parent a96d35d commit 5298aa7d521f799d335c8dcac3331ce2116c363f @sni committed Apr 15, 2012
View
@@ -1,3 +1,4 @@
+error.log
notes.txt
.DS_Store
\.swp
View
@@ -124,7 +124,7 @@
.\" ========================================================================
.\"
.IX Title "thruk 3"
-.TH thruk 3 "2012-04-10" "perl v5.10.1" "User Contributed Perl Documentation"
+.TH thruk 3 "2012-04-15" "perl v5.10.1" "User Contributed Perl Documentation"
.\" For nroff, turn off justification. Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.if n .ad l
@@ -133,15 +133,28 @@
thruk \- Command Line Utility for Thruk Monitoring Gui
.SH "SYNOPSIS"
.IX Header "SYNOPSIS"
-thruk [ \-h |\-\-help ]
- [ \-v | \-\-verbose ]
- [ \-a | \-\-auth=<auth> ]
- [ \-c | \-\-credential=<credential> ]
- [ \-r | \-\-remote\-url=<remote\-url> ]
- [ \-\-local ]
- [ \-b | \-\-backend=<backend> ]
- [ \-l | \-\-list\-backends ]
- [ \-u | \-\-url=<url> ]
+Usage: thruk [options] |A\*^\ [url]
+.PP
+Options:
+ \-h, \-\-help Show this help message and exit
+ \-v, \-\-verbose Print verbose output
+.PP
+.Vb 6
+\& \-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
+\&
+\& \-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\*(Aqt try to connect fastcgi daemon first.
+.Ve
.SH "DESCRIPTION"
.IX Header "DESCRIPTION"
This script gives access to various parts of the Thruk gui and allows to generate
@@ -167,25 +180,21 @@ script has the following arguments
\& used. Specify the key of the backend. Use <\-l> to get a list
\& of backends.
.Ve
-.IP "\fB\-r\fR \fIremote-url\fR, \fB\-\-remote\-url\fR=\fIremote-url\fR" 4
-.IX Item "-r remote-url, --remote-url=remote-url"
-.Vb 4
-\& 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.
-.Ve
-.IP "\fB\-\-local\fR" 4
-.IX Item "--local"
-.Vb 2
-\& Don\*(Aqt contact remote server. Normally remote\-url is tried first for
-\& faster access.
-.Ve
.IP "\fB\-l\fR , \fB\-\-list\-backends\fR" 4
.IX Item "-l , --list-backends"
.Vb 1
\& get list of available backends
.Ve
+.IP "\fB\-a\fR \fIaction\fR, \fB\-\-action\fR=\fIaction\fR" 4
+.IX Item "-a action, --action=action"
+.Vb 1
+\& 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.
+.Ve
.IP "\fB\-a\fR \fIauthname\fR, \fB\-\-auth\fR=\fIauthname\fR" 4
.IX Item "-a authname, --auth=authname"
.Vb 1
@@ -196,33 +205,47 @@ script has the following arguments
.Vb 1
\& use this key for authentication when using remoteurl
.Ve
-.IP "\fB\-u\fR \fIurl\fR, \fB\-\-url\fR=\fIurl\fR" 4
-.IX Item "-u url, --url=url"
+.IP "\fB\-r\fR \fIremote-url\fR, \fB\-\-remote\-url\fR=\fIremote-url\fR" 4
+.IX Item "-r remote-url, --remote-url=remote-url"
+.Vb 4
+\& 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.
+.Ve
+.IP "\fB\-\-local\fR" 4
+.IX Item "--local"
.Vb 2
-\& request this url. Url can be shortened and the
-\& leading /thruk/cgi\-bin/ can be stripped.
+\& Don\*(Aqt contact remote server. Normally remote\-url is tried first for
+\& faster access.
.Ve
.SH "RETURN VALUE"
.IX Header "RETURN VALUE"
-thruk returns 0 on success or 1 otherwise
+thruk returns 0 on success and >= 1 otherwise
.SH "EXAMPLES"
.IX Header "EXAMPLES"
-export the event log as excel file:
+List available backends
+.PP
+.Vb 1
+\& %> thruk \-l
+.Ve
+.PP
+Export the event log as excel file:
.PP
.Vb 1
-\& %> thruk \-a thrukadmin \-u \*(Aqshowlog.cgi?view_mode=xls\*(Aq > eventlog.xls
+\& %> thruk \-A thrukadmin \-a \*(Aqurl=/thruk/cgi\-bin/showlog.cgi?view_mode=xls\*(Aq > eventlog.xls
.Ve
.PP
-export all services into an excel file:
+Urls can be shortened. Export all services into an excel file:
.PP
.Vb 1
-\& %> thruk \-a thrukadmin \-u \*(Aqstatus.cgi?view_mode=xls&host=all\*(Aq > allservices.xls
+\& %> thruk \*(Aqstatus.cgi?view_mode=xls&host=all\*(Aq > allservices.xls
.Ve
.PP
-export all services into an excel file:
+Export all services into an excel file:
.PP
.Vb 1
-\& %> thruk \-a thrukadmin \-u \*(Aqavail.cgi?host=all&timeperiod=last7days&csvoutput=1\*(Aq > all_host_availability.csv
+\& %> thruk \-A thrukadmin \-a \*(Aqurl=avail.cgi?host=all&timeperiod=last7days&csvoutput=1\*(Aq > all_host_availability.csv
.Ve
.SH "AUTHOR"
.IX Header "AUTHOR"
@@ -79,21 +79,38 @@ sub _read_secret {
##############################################
sub _run {
my($self) = @_;
- my $result;
+ my($result, $response);
_debug("_run(): ".Dumper($self->{'opt'}));
unless($self->{'opt'}->{'local'}) {
- $result = $self->_request($self->{'opt'}->{'credential'}, $self->{'opt'}->{'remoteurl'}, $self->{'opt'});
+ ($result,$response) = $self->_request($self->{'opt'}->{'credential'}, $self->{'opt'}->{'remoteurl'}, $self->{'opt'});
}
+ if(!defined $result and $self->{'opt'}->{'remoteurl'} !~ m|/localhost/|mx) {
+ print STDERR "remote command failed:\n".Dumper($response);
+ return 1;
+ }
+
unless(defined $result) {
my($c, $failed) = $self->_dummy_c();
if($failed) {
- print "command failed";
+ print STDERR "command failed";
return 1;
}
$result = $self->_from_local($c, $self->{'opt'})
}
- binmode STDOUT;
- print STDOUT $result->{'output'} if defined $result->{'output'};
+
+ # no output?
+ if(!defined $result->{'output'}) {
+ return $result->{'rc'};
+ }
+
+ # with output
+ if($result->{'rc'} == 0) {
+ binmode STDOUT;
+ print STDOUT $result->{'output'};
+ } else {
+ binmode STDERR;
+ print STDERR $result->{'output'};
+ }
return $result->{'rc'};
}
@@ -112,25 +129,21 @@ sub _request {
_debug(" -> success");
my $data_str = $response->decoded_content;
my $data = decode_json($data_str);
- return $data;
+ return($data, $response);
} else {
_debug(" -> failed: ".Dumper($response));
}
- return;
+ return(undef, $response);
}
##############################################
sub _dummy_c {
my($self) = @_;
_debug("_dummy_c()");
- my $olduser = $ENV{'REMOTE_USER'};
- $ENV{'REMOTE_USER'} = 'dummy';
require Catalyst::Test;
Catalyst::Test->import('Thruk');
my($res, $c) = ctx_request('/thruk/cgi-bin/remote.cgi');
- defined $olduser ? $ENV{'REMOTE_USER'} = $olduser : delete $ENV{'REMOTE_USER'};
- my $failed = 0;
- $failed = 1 unless $res->code == 200;
+ my $failed = ( $res->code == 200 ? 0 : 1 );
return($c, $failed);
}
@@ -190,16 +203,38 @@ sub _run_commands {
'output' => '',
'rc' => 0,
};
+
+ # which command to run?
+ my $action = $opt->{'action'};
+ if(defined $opt->{'url'} and $opt->{'url'} ne '') {
+ $action = 'url='.$opt->{'url'};
+ }
if(defined $opt->{'listbackends'}) {
+ $action = 'listbackends';
+ }
+
+ # list backends
+ if($action eq 'listbackends') {
$data->{'output'} = _listbackends($c);
+ return $data;
}
- if(defined $opt->{'url'}) {
- if($opt->{'url'} =~ m|^\w+\.cgi|gmx) {
- $opt->{'url'} = '/thruk/cgi-bin/'.$opt->{'url'};
+ # request url
+ if($action =~ /^url=(.*)$/mx) {
+ my $url = $1;
+ if($url =~ m|^\w+\.cgi|gmx) {
+ $url = '/thruk/cgi-bin/'.$url;
}
- $data->{'output'} = _request_url($c, $opt->{'url'})
+ $data->{'output'} = _request_url($c, $url)
+ }
+
+ # report
+ if($action =~ /^report=(.*)$/mx) {
+ my $nr = $1;
+ my $pdf_file = Thruk::Utils::Reports::generate_report($c, $nr);
+ $data->{'output'} = read_file($pdf_file);
}
+
return $data;
}
@@ -246,7 +246,9 @@ sub get_objects_by_name {
$id = $objects->{'hostgroup_name'}->{$name2};
}
if(defined $id) {
- return [$self->get_object_by_id($id)];
+ my $obj = $self->get_object_by_id($id);
+ confess("corrupt objects") unless defined $obj;
+ return [$obj];
}
return [];
}
@@ -255,26 +257,32 @@ sub get_objects_by_name {
my $objs = {};
my $tid = $self->{'objects'}->{'byname'}->{'templates'}->{$type}->{$name};
if(defined $tid) {
- $objs->{$tid} = $self->get_object_by_id($tid);
+ my $obj = $self->get_object_by_id($tid);
+ confess("corrupt objects") unless defined $obj;
+ $objs->{$tid} = $obj;
}
# existing object
unless($templates_only) {
if(defined $self->{'objects'}->{'byname'}->{$type}->{$name}) {
my $id = $self->{'objects'}->{'byname'}->{$type}->{$name};
unless(ref $id) {
- $objs->{$id} = $self->get_object_by_id($id);
+ my $obj = $self->get_object_by_id($id);
+ confess("corrupt objects") unless defined $obj;
+ $objs->{$id} = $obj;
} else {
for my $subtype (keys %{$id}) {
for my $subid (values %{$id->{$subtype}}) {
- $objs->{$subid} = $self->get_object_by_id($id);
+ my $obj = $self->get_object_by_id($subid);
+ confess("corrupt objects") unless defined $obj;
+ $objs->{$subid} = $obj;
}
}
}
}
}
-
- return [ values %{$objs} ];
+ my @objects = values %{$objs};
+ return \@objects;
}
@@ -1182,10 +1190,13 @@ sub _update_obj_in_index {
# set uniq id
$obj->set_uniq_id($objects);
+ # by id
+ $objects->{'byid'}->{$obj->{'id'}} = $obj;
+
# by template name
if(defined $tname) {
my $existing_id = $objects->{'byname'}->{'templates'}->{$obj->{'type'}}->{$tname};
- if(defined $existing_id) {
+ if(defined $existing_id and $existing_id eq $obj->{'id'}) {
my $orig = $self->get_object_by_id($existing_id);
if(defined $orig) {
push @{$self->{'errors'}}, "duplicate ".$obj->{'type'}." template definition $tname in ".Thruk::Utils::Conf::_link_obj($obj)."\n -> already defined in ".Thruk::Utils::Conf::_link_obj($orig);
@@ -1226,7 +1237,7 @@ sub _update_obj_in_index {
# single primary key
$pname = $primary if defined $primary;
my $existing_id = $objects->{'byname'}->{$obj->{'type'}}->{$pname};
- if(defined $existing_id and $existing_id ne $obj->get_id()) {
+ if(defined $existing_id and $existing_id ne $obj->{'id'}) {
my $orig = $self->get_object_by_id($existing_id);
if(!defined $orig) {
push @{$self->{'errors'}},
@@ -1241,9 +1252,6 @@ sub _update_obj_in_index {
}
if($found or defined $primary) {
- # by id
- $objects->{'byid'}->{$obj->{'id'}} = $obj;
-
# by type
if(!defined $obj->{'conf'}->{'register'} or $obj->{'conf'}->{'register'} != 0) {
push @{$objects->{'bytype'}->{$obj->{'type'}}}, $obj->{'id'};
@@ -522,7 +522,7 @@ sub set_uniq_id {
# make sure id is uniq
my $nr = 5;
- while(defined $objects->{'byid'}->{$self->{'id'}}) {
+ while(defined $objects->{'byid'}->{$self->{'id'}} and $objects->{'byid'}->{$self->{'id'}} != $self) {
$self->{'id'} = $self->_make_id(++$nr);
}
return $self->{'id'};
Oops, something went wrong. Retry.

0 comments on commit 5298aa7

Please sign in to comment.