diff options
author | Daniel Friesel <derf@finalrewind.org> | 2014-05-31 20:31:38 +0200 |
---|---|---|
committer | Daniel Friesel <derf@finalrewind.org> | 2014-05-31 20:31:38 +0200 |
commit | 94bdfd9b99e031b6e7bdace43a433f06aebcbbb0 (patch) | |
tree | 5656956f18fbe6cf5f42ec19863d088a4b6cd127 | |
parent | 84892a8583ade34cd97ac92b6ea1fb98f5dfc8d1 (diff) |
icli: Use generic -a/--action option for actions
-rw-r--r-- | Changelog | 7 | ||||
-rwxr-xr-x | bin/icli | 210 |
2 files changed, 128 insertions, 89 deletions
@@ -1,3 +1,10 @@ +git HEAD + + * NOT BACKWARDS COMPATIBLE: Add -a/--action option to invoke generic + actions on selected services. This replaces -a/--acknowledge with + -a acknowledge, -r/--recheck with -a recheck and -u/--force-recheck + wiht -a force_recheck + icli 0.47 - Thu May 29 2014 * Remove autodie dependency, improve error messages @@ -23,18 +23,17 @@ my $config_file = App::Icli::ConfigData->config('object_file'); my $status_file = App::Icli::ConfigData->config('status_file'); my $rw_file = App::Icli::ConfigData->config('command_file'); my $context; -my $colours = 1; -my $list_type = 's'; -my $verbosity = 1; -my $overview = 0; -my $recheck = 0; -my $force_recheck = 0; -my $match_output = undef; -my $acknowledge = undef; -my $as_contact = undef; -my $term_width = Term::Size::chars(); -my $cut_mode = 'b'; +my $colours = 1; +my $list_type = 's'; +my $verbosity = 1; +my $overview = 0; +my $match_output = undef; +my $action = undef; +my $as_contact = undef; +my $term_width = Term::Size::chars(); +my $cut_mode = 'b'; my ( @for_hosts, @for_groups, @for_services, @list_hosts, @list_services ); +my @action_args; my @filters; sub have_host { @@ -436,6 +435,77 @@ sub enhance_status { } } +sub parse_action { + if ( not $action ) { + return; + } + + my %actionmap = ( + a => 'acknowledge', + r => 'recheck', + R => 'force_recheck', + ); + + ( $action, @action_args ) = split( /:/, $action ); + $list_type = q{}; + + if ( exists $actionmap{$action} ) { + $action = $actionmap{$action}; + } + elsif ( length($action) <= 2 ) { + say STDERR "Note: Action shortcut '${action}' is unknown"; + } +} + +sub compute_hostlist { + for my $arg (@ARGV) { + my ( $host, $service ) = split( qr{/}, $arg ); + + if ( not any { $host } @for_hosts ) { + push( @for_hosts, $host ); + } + if ($service) { + push( @for_services, $service ); + } + } + + foreach my $host (@for_hosts) { + if ( not exists $data->{services}->{$host} ) { + die("Unknown host: ${host}\n"); + } + } + + @list_hosts = @for_hosts; + @list_services = @for_services; + + foreach my $group (@for_groups) { + if ( not exists $config->{'hostgroups'}->{$group} ) { + die("Unknown hostgroup: ${group}\n"); + } + foreach + my $host ( split /,/, $config->{'hostgroups'}->{$group}->{'members'} ) + { + if ( not any { $_ eq $host } @list_hosts ) { + push( @list_hosts, $host ); + } + } + } + + if ( @list_hosts == 0 ) { + @list_hosts = sort keys %{ $data->{hosts} }; + } + + if (@list_services) { + @list_hosts + = grep { have_service_multi( $_, @list_services ) } @list_hosts; + } + + if ( $list_type eq 'h' ) { + @list_hosts + = grep { filter_host( $data->{'hosts'}->{$_} ) } @list_hosts; + } +} + sub service_state { my ($s) = @_; my $checked = $s->{has_been_checked}; @@ -901,18 +971,17 @@ sub acknowledge_service { my ( $host, $service ) = @_; dispatch_command( 'ACKNOWLEDGE_SVC_PROBLEM', $host, $service, 2, 1, 1, - 'cli', $acknowledge ); - say "Acknowledged $host/$service: $acknowledge"; + 'cli', $action_args[0] ); + say "Acknowledged $host/$service: $action_args[0]"; } sub action_on_host { my ($h) = @_; - if ($recheck) { - recheck_host_all($h); - } - elsif ($force_recheck) { - force_recheck_host_all($h); + given ($action) { + when ('recheck') { recheck_host_all($h) } + when ('force_recheck') { force_recheck_host_all($h) } + default { say STDERR "Cannot run action '${action}' on a host" } } } @@ -923,19 +992,15 @@ sub action_on_service { return; } - if ($recheck) { - recheck_service( $h, $s ); - } - if ($force_recheck) { - force_recheck_service( $h, $s ); - } - if ($acknowledge) { - acknowledge_service( $h, $s ); + given ($action) { + when ('recheck') { recheck_service( $h, $s ) } + when ('force_recheck') { force_recheck_service( $h, $s ) } + when ('acknowledge') { acknowledge_service( $h, $s ) } } } GetOptions( - 'a|acknowledge=s' => sub { $acknowledge = $_[1]; $list_type = q{} }, + 'a|action=s' => \$action, 'c|config=s' => \$config_file, 'C|no-colours' => sub { $colours = 0 }, 'f|status-file=s' => \$status_file, @@ -944,12 +1009,10 @@ GetOptions( 'h|host=s' => sub { push( @for_hosts, split( /,/, $_[1] ) ) }, 'l|list=s' => sub { $list_type = substr( $_[1], 0, 1 ) }, 'm|match=s' => sub { $match_output = qr{$_[1]}i }, - 'o|overview' => \$overview, - 'r|recheck' => sub { $recheck = 1; $list_type = q{} }, - 's|service=s' => sub { push( @for_services, split( /,/, $_[1] ) ) }, - 'u|force-recheck' => sub { $force_recheck = 1; $list_type = q{} }, - 'U|as-contact=s' => \$as_contact, - 'v|verbose+' => \$verbosity, + 'o|overview' => \$overview, + 's|service=s' => sub { push( @for_services, split( /,/, $_[1] ) ) }, + 'U|as-contact=s' => \$as_contact, + 'v|verbose+' => \$verbosity, 'V|version' => sub { say "icli version $VERSION"; exit 0 }, 'x|cut-mode=s' => sub { $cut_mode = substr( $_[1], 0, 1 ) }, 'z|filter=s' => sub { push( @filters, split( /,/, $_[1] ) ) }, @@ -958,51 +1021,8 @@ GetOptions( read_objects( $status_file, \$data, 'icinga status_file', '--status-file' ); read_objects( $config_file, \$config, 'icinga object_cache_file', '--config' ); enhance_status(); - -for my $arg (@ARGV) { - my ( $host, $service ) = split( qr{/}, $arg ); - - if ( not any { $host } @for_hosts ) { - push( @for_hosts, $host ); - } - if ($service) { - push( @for_services, $service ); - } -} - -foreach my $host (@for_hosts) { - if ( not exists $data->{services}->{$host} ) { - die("Unknown host: ${host}\n"); - } -} - -@list_hosts = @for_hosts; -@list_services = @for_services; - -foreach my $group (@for_groups) { - if ( not exists $config->{'hostgroups'}->{$group} ) { - die("Unknown hostgroup: ${group}\n"); - } - foreach - my $host ( split /,/, $config->{'hostgroups'}->{$group}->{'members'} ) - { - if ( not any { $_ eq $host } @list_hosts ) { - push( @list_hosts, $host ); - } - } -} - -if ( @list_hosts == 0 ) { - @list_hosts = sort keys %{ $data->{hosts} }; -} - -if (@list_services) { - @list_hosts = grep { have_service_multi( $_, @list_services ) } @list_hosts; -} - -if ( $list_type eq 'h' ) { - @list_hosts = grep { filter_host( $data->{'hosts'}->{$_} ) } @list_hosts; -} +parse_action(); +compute_hostlist(); if ($overview) { if ( $list_type eq 'h' ) { @@ -1027,7 +1047,7 @@ elsif ( $list_type eq 'd' ) { display_downtime($downtime); } } -elsif ( $recheck or $force_recheck or $acknowledge ) { +elsif ($action) { foreach my $host (@list_hosts) { if ( not @list_services and not @filters ) { @@ -1089,14 +1109,34 @@ aneurysm/{Libraries,Websites} >> with shell expansion). =over -=item B<-a>|B<--acknowledge> I<comment> +=item B<-a>|B<--action> I<action>[:I<args>] + +Run I<action> on all matching services. I<args> is a colon-separated list of +action arguments and depends on the action in question. I<action> may also be +a one or two letter shortcut. + +The following actions are supported: -Acknowledge all matching services with string I<comment>. This creates a sticky +=over + +=item a|acknowledge I<comment> + +Acknowledge service problems with string I<comment>. This creates a sticky acknwoledgment with notification and no expire time. The comment will not be persistent. Note: Acknowledgement of host problems is not yet supported. +=item r|recheck + +Schedule an immediate recheck + +=item R|force_recheck + +Schedule a forced, immediate recheck + +=back + =item B<-c>|B<--config> I<config> Read config from I<file> @@ -1141,14 +1181,6 @@ By default (or when used with C<< -ls >>) the number of all hosts and services When used with C<< -lh >>, lists all hosts with the number of ok / warning / ... checks on each host. -=item B<-r>|B<--recheck> - -Schedule an immediate recheck of all selected services - -=item B<-u>|B<--force-recheck> - -Schedule a forced, immediate recheck of all selected services - =item B<-U>|B<--as-contact> I<name> Only operate on service visible to I<name>. Doesn't work for B<-lh> yet, |