aboutsummaryrefslogtreecommitdiff
path: root/lib/App/Mangemanche/Command/nagios.pm
blob: fc21a37e9f47ea1ac2a1ebd4e45764a1ba292e71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package App::Mangemanche::Command::nagios;
use strict;
use warnings;
use parent 'App::Mangemanche::Command';
use App::Mangemanche::Command ':exit_codes';
use File::Spec;
use File::Basename;
use Nagios::Config;

sub new {
    my ($class, $com, $agent) = @_;

    bless $class->SUPER::new($com, $agent,
			     'progname|p=s@' => 'prognames',
			     'config|c=s' => 'nagiosconf');
}

sub run {
    my $self = shift;
    $self->SUPER::run;
    $self->usage_error("extra parameters") if @ARGV;
    my $cf = $self->{options}{nagiosconf} // '/etc/nagios/nagios.cfg';
    push @{$self->{options}{prognames}}, 'ping903q';
    $self->{cfg} = Nagios::Config->new(filename => $cf);

    my %iph;
    foreach my $host ($self->{cfg}->list_hosts) {
	if ($host->register && $self->check_command($host)) {
	    $iph{$host->address} = 1;
	}
    }
    foreach my $service ($self->{cfg}->list_services) {
	if ($service->register && $self->check_command($service)) {
	    my @addr = map { $_->address } (@{$service->host_name});
	    @iph{@addr} = (1) x @addr
	}
    }

    my @iplist = keys %iph;
    my $n = @iplist;
    $self->error("info: collected $n addresses");
    unless ($self->agent->set_ip_list([@iplist])) {
	$self->abend(EX_FAIL, $self->agent->error_message);
    }
}

sub check_command {
    my ($self, $host) = @_;
    my $chk = $host->check_command or return;
    $chk =~ s/!.*//;
    my $def = $self->{cfg}->find_object($chk) or return;
    return unless $def->can('command_line');
    my ($cmd) =  split(/\s+/, $def->command_line);
    return grep {
		File::Spec->file_name_is_absolute($_)
		    ? $cmd eq $_
		    : basename($cmd) eq $_
	    } @{$self->{options}{prognames}};
}

1;
=head1 NAME

nagios - extract monitored IP addresses from the Nagios configuration

=head1 SYNOPSIS

B<nagios>
[B<-c> I<FILE>]
[B<-p> I<PROG>]
[B<--config=>I<FILE>]
[B<--progname=>I<PROG>]

=head1 DESCRIPTION

Scans Nagios configuration for services that use B<ping903q> and extracts
IP addresses of associated hosts.  At the end of the run, the extracted
addresses are posted to the mutable list of IP addresses monitored by
the running B<ping903> server.

Default nagios configuration file is F</etc/nagios/nagios.conf>.

=head1 OPTIONS

=over 4

=item B<-c>, B<--config=>I<FILE>

Read Nagios configuration from I<FILE>.

=item B<-p>, B<--progname=>I<PROG>

Look for I<PROG> in addition to B<ping903q>.

=back

=head1 SEE ALSO

L<ping903>,
L<mangemache>,
L<ipadd>,
L<ipdel>.

=cut

Return to:

Send suggestions and report system problems to the System administrator.