From 12374e2c418f8bd2eed9af4ace8eae55ecdfa383 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Sat, 26 May 2012 09:54:03 +0200 Subject: Add -U / --as-contact option Only operative on services visible to . Most useful for -ls --- Build.PL | 3 ++- Changelog | 6 ++++++ bin/icli | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 60 insertions(+), 5 deletions(-) diff --git a/Build.PL b/Build.PL index bd33bf0..8e97184 100644 --- a/Build.PL +++ b/Build.PL @@ -6,7 +6,7 @@ use Module::Build; my $build = Module::Build->subclass( code => q( - sub ACTION_testauthor { shift->generic_tast(type => 'author') } + sub ACTION_testauthor { shift->generic_task(type => 'author') } ) )->new( build_requires => { @@ -24,6 +24,7 @@ my $build = Module::Build->subclass( 'Carp' => 0, 'Date::Format' => 0, 'Getopt::Long' => 0, + 'List::MoreUtils' => 0, 'Term::ANSIColor' => 0, 'Term::Size' => 0, }, diff --git a/Changelog b/Changelog index e31c6e8..14a2ccb 100644 --- a/Changelog +++ b/Changelog @@ -1,3 +1,9 @@ +git HEAD + + * Add -U / --as-contact option to only operate on services visible to a + certain contact + * New dependency List::MoreUtils + icli 0.41 - Sun Apr 15 2012 * Add "icli host/service" as "icli -h host -s service" shortcut diff --git a/bin/icli b/bin/icli index 25729e9..ed04bd2 100755 --- a/bin/icli +++ b/bin/icli @@ -10,6 +10,7 @@ use 5.010; use Carp qw(croak); use Date::Format; use Getopt::Long qw/:config bundling/; +use List::MoreUtils qw(any firstval); use Term::ANSIColor; use Term::Size; @@ -26,6 +27,7 @@ my $verbosity = 1; my $recheck = 0; my $force_recheck = 0; my $acknowledge = 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 ); @@ -118,7 +120,7 @@ sub split_by_words { my @words = split( / /, $str ); my @ret; - while ( grep { length($_) > $max_w } @words ) { + while ( any { length($_) > $max_w } @words ) { for my $i ( 0 .. $#words ) { my $word = $words[$i]; @@ -261,16 +263,29 @@ sub filter_service { return 0; } + if ($as_contact and not has_contact($s, $as_contact)) { + return 0; + } + return 1; } +sub has_contact { + my ($s, $contact) = @_; + + my $conf_s = firstval { $_->{service_description} eq $s->{service_description } } + @{ $config->{services}{$s->{host_name}}}; + + return any { $_ eq $contact } @{$conf_s->{contacts}}; +} + sub read_objects_line { my ( $line, $ref ) = @_; if ( $line =~ / ^ (?:define \s )? (? \w+) \s+ { /x ) { $context = $+{context}; } - elsif ( $line =~ / ^ \t (? [^=\t]+ ) [=\t] (? .*) $ /x ) { + elsif ( $line =~ / ^ \t (? [^=\t]+ ) [=\t] \s* (? .*) $ /x ) { $cache->{ $+{key} } = $+{value}; } elsif ( $line =~ / ^ \t } $ /x ) { @@ -311,6 +326,19 @@ sub read_objects_line { # TODO } + when ('host') { + ${$ref}->{hosts}->{ $cache->{host_name} } = $cache; + } + when ('service') { + push( + @{ ${$ref}->{services}->{ $cache->{host_name} } }, + $cache + ); + } + when ('contactgroup') { + ${$ref}->{contactgroups}->{ $cache->{contactgroup_name} } + = [split(m{, *}, $cache->{members})]; + } when ( [ qw[ @@ -351,6 +379,17 @@ sub enhance_status { } } } + HOST: for my $h ( keys %{ $config->{services} } ) { + for my $s ( @{ $config->{services}->{$h} } ) { + if ($s->{contacts}) { + $s->{contacts} =~ s{^ *}{}o; + $s->{contacts} = [split(m{, *}, $s->{contacts})]; + } + for my $group (split(m{, *}, $s->{contact_groups})) { + push(@{$s->{contacts}}, @{$config->{contactgroups}{$group}}); + } + } + } } sub service_state { @@ -773,6 +812,7 @@ GetOptions( '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, 'V|version' => sub { say "icli version $VERSION"; exit 0 }, 'x|cut-mode=s' => sub { $cut_mode = substr( $_[1], 0, 1 ) }, @@ -786,7 +826,7 @@ enhance_status(); for my $arg (@ARGV) { my ( $host, $service ) = split( qr{/}, $arg ); - if ( not grep { $host } @for_hosts ) { + if ( not any { $host } @for_hosts ) { push( @for_hosts, $host ); } if ($service) { @@ -810,7 +850,7 @@ foreach my $group (@for_groups) { foreach my $host ( split /,/, $config->{'hostgroups'}->{$group}->{'members'} ) { - if ( not grep { $_ eq $host } @list_hosts ) { + if ( not any { $_ eq $host } @list_hosts ) { push( @list_hosts, $host ); } } @@ -944,6 +984,14 @@ Schedule an immediate recheck of all selected services 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, +most useful for B<-ls>. + +NOTE: This is meant to help find out which services a user has access to. It is +NOT intended as a way to restrict access and should never be used that way. + =item B<-s>|B<--service> I Limit selection to I (comma separated lists). Can be combined with -- cgit v1.2.3