summaryrefslogtreecommitdiffabout
path: root/lib
authorSergey Poznyakoff <gray@gnu.org>2020-03-02 11:29:49 (GMT)
committer Sergey Poznyakoff <gray@gnu.org>2020-03-02 11:29:49 (GMT)
commitf75c3e02447ceb9a201c1107ecb0d7844b8000d2 (patch) (side-by-side diff)
tree256a94f50d94b14a6d7b2d8c801004aa6a9e10c5 /lib
parente52a09add15b01feaa24466361f2c5f4c6bf1807 (diff)
downloadmangemanche-f75c3e02447ceb9a201c1107ecb0d7844b8000d2.tar.gz
mangemanche-f75c3e02447ceb9a201c1107ecb0d7844b8000d2.tar.bz2
Improve "nagios" subcommand (deep scan of hosts and services)
* lib/App/Ping903/Command.pm: Remove unused module. * lib/App/Ping903/Command/ident.pm: New command. * lib/App/Ping903/Command/inspect.pm: Document. * lib/App/Ping903/Command/nagios.pm: Rewrite using modified Magios::Config. * lib/Net/Ping903.pm (get_id): New command.
Diffstat (limited to 'lib') (more/less context) (ignore whitespace changes)
-rw-r--r--lib/App/Ping903/Command.pm1
-rw-r--r--lib/App/Ping903/Command/ident.pm54
-rw-r--r--lib/App/Ping903/Command/inspect.pm28
-rw-r--r--lib/App/Ping903/Command/nagios.pm122
-rw-r--r--lib/Net/Ping903.pm15
5 files changed, 116 insertions, 104 deletions
diff --git a/lib/App/Ping903/Command.pm b/lib/App/Ping903/Command.pm
index f6cb2c6..516f1a6 100644
--- a/lib/App/Ping903/Command.pm
+++ b/lib/App/Ping903/Command.pm
@@ -2,7 +2,6 @@ package App::Ping903::Command;
use strict;
use warnings;
use Getopt::Long;
-use Pod::Man;
use Pod::Usage;
use Pod::Find qw(pod_where);
use Carp;
diff --git a/lib/App/Ping903/Command/ident.pm b/lib/App/Ping903/Command/ident.pm
new file mode 100644
index 0000000..d985f29
--- a/dev/null
+++ b/lib/App/Ping903/Command/ident.pm
@@ -0,0 +1,54 @@
+package App::Ping903::Command::ident;
+use strict;
+use warnings;
+use parent 'App::Ping903::Command';
+use App::Ping903::Command ':exit_codes';
+
+sub run {
+ my $self = shift;
+ $self->SUPER::run;
+ $self->usage_error("extra parameters") if @ARGV > 1;
+
+ my $id = $self->agent->get_id(@ARGV);
+ unless ($id) {
+ $self->abend(EX_FAIL, $self->agent->error_message);
+ }
+
+ foreach my $kw (sort keys %$id) {
+ my $val = $id->{$kw} or next;
+ print "$kw ";
+ if (ref($val) eq 'ARRAY') {
+ print join(' ', @$val);
+ } elsif (JSON::is_bool($val)) {
+ print $val ? "on" : "off";
+ } else {
+ print $val
+ }
+ print "\n";
+ }
+}
+
+1;
+
+=head1 NAME
+
+ident - display ping903 server identification
+
+=head1 SYNOPSIS
+
+B<ident>
+[I<PARAM>]
+
+=head1 DESCRIPTION
+
+Queries the running instance of B<ping903> about identification parameters
+and prints them on screen. The identification parameters are: package name,
+package version an running instance PID.
+
+If given a single argument I<PARAM>, only the requested parameter is
+displayed.
+
+=head1 SEE ALSO
+
+L<ping903>(1), L<ping903.conf>(5).
+
diff --git a/lib/App/Ping903/Command/inspect.pm b/lib/App/Ping903/Command/inspect.pm
index 464a1b6..9347e54 100644
--- a/lib/App/Ping903/Command/inspect.pm
+++ b/lib/App/Ping903/Command/inspect.pm
@@ -7,8 +7,9 @@ use App::Ping903::Command ':exit_codes';
sub run {
my $self = shift;
$self->SUPER::run;
+ $self->usage_error("extra parameters") if @ARGV > 1;
- my $cfg = $self->agent->get_config;
+ my $cfg = $self->agent->get_config(@ARGV);
unless ($cfg) {
$self->abend(EX_FAIL, $self->agent->error_message);
}
@@ -32,10 +33,33 @@ sub run {
}
1;
+
=head1 NAME
inspect - inspect ping903 server configuration
+=head1 SYNOPSIS
+
+B<inspect>
+[I<PARAM>]
+
=head1 DESCRIPTION
-Text
+Queries the running instance of B<ping903> about its configuration and
+prints it on screen in its configuration file format.
+
+If given a single argument I<PARAM>, only the requested configuration
+parameter is displayed.
+
+=head1 NOTES
+
+Depending on the B<ping903> server and the user settings, the configuration
+returned can be censored.
+
+The IP list is always formatted as a here-document, containing both immutable
+and mutable IPs from the server configuration.
+
+=head1 SEE ALSO
+
+L<ping903>(1), L<ping903.conf>(5).
+
diff --git a/lib/App/Ping903/Command/nagios.pm b/lib/App/Ping903/Command/nagios.pm
index 2c8405c..b6c8204 100644
--- a/lib/App/Ping903/Command/nagios.pm
+++ b/lib/App/Ping903/Command/nagios.pm
@@ -5,7 +5,7 @@ use parent 'App::Ping903::Command';
use App::Ping903::Command ':exit_codes';
use File::Spec;
use File::Basename;
-use Text::Locus;
+use Nagios::Config;
sub new {
my ($class, $com, $agent) = @_;
@@ -21,115 +21,41 @@ sub run {
$self->usage_error("extra parameters") if @ARGV;
my $cf = $self->{options}{nagiosconf} // '/etc/nagios/nagios.cfg';
push @{$self->{options}{prognames}}, 'ping903q';
- $self->{commands} = [];
- $self->scanconf($cf, new Text::Locus('<cli>',0));
- if (my $n = @{$self->{iplist}}) {
- $self->error("info: collected $n addresses");
- unless ($self->agent->set_ip_list($self->{iplist})) {
- $self->abend(EX_FAIL, $self->agent->error_message);
- }
- }
-}
+ $self->{cfg} = Nagios::Config->new(filename => $cf);
-sub scanconf {
- my ($self, $cf, $loc) = @_;
-# print "$loc: parsing $cf\n";
- open(my $fh, '<', $cf) or
- $self->abend(EX_FAIL, "$loc: can't open $cf: $!");
- my $line = 0;
- while (<$fh>) {
- ++$line;
- chomp;
- s/^\s+//;
- s/\s+$//;
- next if /^(#.*)$/;
-
- if (/cfg_file\s*=\s*(.+)/) {
- $self->scanconf($1, new Text::Locus($cf, $line));
- } elsif (/cfg_dir\s*=\s*(.+)/) {
- my $loc = new Text::Locus($cf, $line);
- foreach my $file (glob File::Spec->catfile($1, '*.cfg')) {
- $self->scanconf($file, $loc);
- }
- } elsif (/define\s+(command|service|host)\s+\{/) {
- $self->${\ "parse_$1"}($fh, $cf, \$line);
+ my %iph;
+ foreach my $host ($self->{cfg}->list_hosts) {
+ if ($host->register && $self->check_command($host)) {
+ $iph{$host->address} = 1;
}
- #...
}
- close $fh;
-}
-
-sub parse_host {
- my ($self, $fh, $file, $lref) = @_;
- my ($host_name,$address);
- while (<$fh>) {
- ++$$lref;
- chomp;
- s/^\s+//;
- s/\s+$//;
- next if /^(#.*)$/;
- last if /^\s*}/;
- if (/^host_name\s+(.+)/) {
- $host_name = $1;
- } elsif (/^address\s+(.+)/) {
- $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
}
}
- if ($host_name) {
- $self->{hosts}{$host_name} = $address || $host_name;
+
+ 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 parse_command {
- my ($self, $fh, $file, $lref) = @_;
- my ($command_name,$matches);
- while (<$fh>) {
- ++$$lref;
- chomp;
- s/^\s+//;
- s/\s+$//;
- next if /^(#.*)$/;
- last if /^\s*}/;
- if (/^command_name\s+(.+)/) {
- $command_name = $1;
- } elsif (/^command_line\s+(.+)/) {
- my ($cmd) = split(/\s+/, $1);
- $matches = grep {
+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}};
- }
- }
- if ($matches && $command_name) {
-# print "FOUND $command_name\n";
- push @{$self->{commands}}, $command_name;
- }
-}
-
-
-sub parse_service {
- my ($self, $fh, $file, $lref) = @_;
- my ($hostname,$matches,$loc);
- while (<$fh>) {
- ++$$lref;
- chomp;
- s/^\s+//;
- s/\s+$//;
- next if /^(#.*)$/;
- last if /^\s*}/;
- if (/^host_name\s+(.+)/) {
- $hostname = $1;
- $loc = new Text::Locus($file, $$lref);
- } elsif (/check_command\s+(.+)/) {
- my ($cmd) = split(/!/,$1);
- $matches = grep { $cmd eq $_ } @{$self->{commands}}
- }
- }
- if ($matches) {
-# print "$loc: $hostname -> $self->{hosts}{$hostname}\n";
- push @{$self->{iplist}}, $self->{hosts}{$hostname};
- }
}
1;
diff --git a/lib/Net/Ping903.pm b/lib/Net/Ping903.pm
index a23f61a..2ca2006 100644
--- a/lib/Net/Ping903.pm
+++ b/lib/Net/Ping903.pm
@@ -16,9 +16,18 @@ sub new {
}
sub get_config {
- my ($self, $param) = @_;
- my $url = "$self->{baseurl}/config";
- $url .= '/' . $param if $param;
+ my $self = shift;
+ return $self->get_info_from_json('config', @_);
+}
+
+sub get_id {
+ my $self = shift;
+ return $self->get_info_from_json('id', @_);
+}
+
+sub get_info_from_json {
+ my $self = shift;
+ my $url = join('/', ($self->{baseurl}, @_));;
my $resp = $self->{ua}->get($url);
$self->response($resp);
unless ($resp->is_success) {

Return to:

Send suggestions and report system problems to the System administrator.