aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org>2020-04-30 13:55:21 +0300
committerSergey Poznyakoff <gray@gnu.org>2020-04-30 13:55:21 +0300
commitda4c5a335f0637315b94271e35532aaba0d4c794 (patch)
treeab498372b20da9afdb87ca0466222f59c26b19dd
parent3284f879c364a2a7b6d9c1227b21c6ed29246d28 (diff)
downloadmangemanche-da4c5a335f0637315b94271e35532aaba0d4c794.tar.gz
mangemanche-da4c5a335f0637315b94271e35532aaba0d4c794.tar.bz2
nagios: exclude hosts that don't resolve; push a sorted IP list
-rw-r--r--lib/App/Mangemanche/Command/nagios.pm13
1 files changed, 12 insertions, 1 deletions
diff --git a/lib/App/Mangemanche/Command/nagios.pm b/lib/App/Mangemanche/Command/nagios.pm
index 016ba6d..7c28038 100644
--- a/lib/App/Mangemanche/Command/nagios.pm
+++ b/lib/App/Mangemanche/Command/nagios.pm
@@ -12,12 +12,23 @@ sub new {
$class->SUPER::new($com, $agent,
'progname|p=s@' => 'prognames',
'config|c=s' => 'nagiosconf');
}
+sub valid_host {
+ my ($self,$s) = @_;
+ return 1 if $s =~ /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}
+ (?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/x;
+ unless (gethostbyname($s)) {
+ $self->error("warning: $s does not resolve; skipping");
+ return 0;
+ }
+ return 1;
+}
+
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';
@@ -33,13 +44,13 @@ sub run {
if ($service->register && $self->check_command($service)) {
my @addr = map { $_->address } (@{$service->host_name});
@iph{@addr} = (1) x @addr
}
}
- my @iplist = keys %iph;
+ my @iplist = sort grep { $self->valid_host($_) } 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);
}
}

Return to:

Send suggestions and report system problems to the System administrator.