From 94bdfd9b99e031b6e7bdace43a433f06aebcbbb0 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Sat, 31 May 2014 20:31:38 +0200 Subject: icli: Use generic -a/--action option for actions --- bin/icli | 210 ++++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 121 insertions(+), 89 deletions(-) (limited to 'bin') diff --git a/bin/icli b/bin/icli index 41cc8e6..79e6ed2 100755 --- a/bin/icli +++ b/bin/icli @@ -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 +=item B<-a>|B<--action> I[:I] + +Run I on all matching services. I is a colon-separated list of +action arguments and depends on the action in question. I may also be +a one or two letter shortcut. + +The following actions are supported: -Acknowledge all matching services with string I. This creates a sticky +=over + +=item a|acknowledge I + +Acknowledge service problems with string I. 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 Read config from I @@ -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 Only operate on service visible to I. Doesn't work for B<-lh> yet, -- cgit v1.2.3