| 1 | #----------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 2 | # Monitor::Simple | ||||||||||||||||||||||||||||
| 3 | # Author: Martin Senger <martin.senger@gmail.com> | ||||||||||||||||||||||||||||
| 4 | # For copyright and disclaimer se below. | ||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||
| 6 | # ABSTRACT: Simple monitoring of applications and services | ||||||||||||||||||||||||||||
| 7 | # PODNAME: Monitor::Simple | ||||||||||||||||||||||||||||
| 8 | #----------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | package Monitor::Simple; | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 40 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 40 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 40 | our $VERSION = '0.2.8'; # VERSION | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | # values returned by plugins (compatible with Nagios' | ||||||||||||||||||||||||||||
| 18 | 40 | ▶ | use constant { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 40 | RETURN_OK => 0, | |||||||||||||||||||||||||||
| 20 | RETURN_WARNING => 1, | ||||||||||||||||||||||||||||
| 21 | RETURN_CRITICAL => 2, | ||||||||||||||||||||||||||||
| 22 | RETURN_UNKNOWN => 3, | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | # ...and this is returned if the plugin cannot be started | ||||||||||||||||||||||||||||
| 25 | RETURN_FATAL => -1, | ||||||||||||||||||||||||||||
| 26 | 40 | }; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | # values used in configuration file for notifiers (in attribute "on") | ||||||||||||||||||||||||||||
| 29 | 40 | ▶ | use constant { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 40 | NOTIFY_OK => 'ok', | |||||||||||||||||||||||||||
| 31 | NOTIFY_WARNING => 'w', | ||||||||||||||||||||||||||||
| 32 | NOTIFY_CRITICAL => 'c', | ||||||||||||||||||||||||||||
| 33 | NOTIFY_UNKNOWN => 'u', | ||||||||||||||||||||||||||||
| 34 | NOTIFY_ALL => 'all', | ||||||||||||||||||||||||||||
| 35 | NOTIFY_ERRORS => 'err', | ||||||||||||||||||||||||||||
| 36 | NOTIFY_NONE => 'none', | ||||||||||||||||||||||||||||
| 37 | 40 | }; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 40 | ▶ | use Monitor::Simple::Log; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | 40 | ▶ | use Monitor::Simple::Config; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 40 | ▶ | use Monitor::Simple::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | 40 | ▶ | use Monitor::Simple::Utils; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | 40 | ▶ | use Monitor::Simple::Output; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | 40 | ▶ | use Monitor::Simple::Notifier; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 40 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 47 | 40 | ▶ | use Log::Log4perl qw(:easy); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 48 | 40 | ▶ | use Parallel::ForkManager; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 49 | 40 | ▶ | use IO::CaptureOutput qw(capture_exec); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 50 | 40 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 51 | 40 | ▶ | use Time::HiRes qw(time); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 40 | my $default_npp = 10; # maximum number of child processes in parallel | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | # ----------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 57 | # The main loop checking all services as defined in the given | ||||||||||||||||||||||||||||
| 58 | # configuration. | ||||||||||||||||||||||||||||
| 59 | # | ||||||||||||||||||||||||||||
| 60 | # Recognized arguments: | ||||||||||||||||||||||||||||
| 61 | # { config_file => $config_file, | ||||||||||||||||||||||||||||
| 62 | # npp => <integer>, | ||||||||||||||||||||||||||||
| 63 | # outputter => instance of Monitor::Simple::Output, | ||||||||||||||||||||||||||||
| 64 | # filter => hashref (keys are service IDS) or | ||||||||||||||||||||||||||||
| 65 | # arrayref with service IDs, or | ||||||||||||||||||||||||||||
| 66 | # scalar with just one service ID | ||||||||||||||||||||||||||||
| 67 | # nonotif => boolean | ||||||||||||||||||||||||||||
| 68 | # } | ||||||||||||||||||||||||||||
| 69 | # ----------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 70 | 75 | ▶ | sub check_services { | ||||||||||||||||||||||||||
|
Subroutine
check_services: called CC=18 SCAR=30.8
Pod
documented
| |||||||||||||||||||||||||||||
| 71 | 75 | my ($self, $args) = @_; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | # recognized arguments | ||||||||||||||||||||||||||||
| 74 | 75 | my $config; | |||||||||||||||||||||||||||
| 75 | my $config_file; | ||||||||||||||||||||||||||||
| 76 | 75 | ▶ | if ($args->{config_file}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 75 | $config_file = $args->{config_file}; | |||||||||||||||||||||||||||
| 78 | 75 | $config = Monitor::Simple::Config->get_config ($config_file); | |||||||||||||||||||||||||||
| 79 | } else { | ||||||||||||||||||||||||||||
| 80 | 0 | LOGDIE ("check_services: Missing argument 'config_file'. Cannot do anything.\n"); | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | 75 | ▶ | my $npp = ($args->{npp} || $default_npp); | ||||||||||||||||||||||||||
|
Condition33%1/3
$args->{'npp'} || $default_npp
MC/DC0%0/2
$args->{'npp'} || $default_npp
$args->{'npp'}
$default_npp
Truth table33%1/3
A || B
A= $args->{'npp'}B= $default_npp
| |||||||||||||||||||||||||||||
| 83 | 75 | ▶ | if ($npp < 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 0 | LOGWARN ("check_services: Argument 'npp' must be positive. Replaced by default value $default_npp.\n"); | |||||||||||||||||||||||||||
| 85 | 0 | $npp = $default_npp; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | 75 | ▶ | my $outputter = ($args->{outputter} || Monitor::Simple::Output->new (config => $config)); | ||||||||||||||||||||||||||
|
Condition33%1/3
$args->{'outputter'} || 'Monitor::Simple::Output'->new('config', $config)
MC/DC0%0/2
$args->{'outputter'} || 'Monitor::Simple::Output'->new('config', $config)
$args->{'outputter'}
'Monitor::Simple::Output'->new('config', $config)
Truth table33%1/3
A || B
A= $args->{'outputter'}B= 'Monitor::Simple::Output'->new('config', $config)
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | # optional filter tells which only services to check; filter can be | ||||||||||||||||||||||||||||
| 90 | # hashref or arrayref or scalar (all with service name(s) we wish | ||||||||||||||||||||||||||||
| 91 | # to check) | ||||||||||||||||||||||||||||
| 92 | 75 | my $filter = $args->{filter}; | |||||||||||||||||||||||||||
| 93 | 75 | ▶ | if ($filter) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 75 | ▶ | if (ref ($filter) eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 95 | 7 | $filter = { map { $_ => 1 } @$filter }; | |||||||||||||||||||||||||||
| 96 | } elsif (ref ($filter) eq 'HASH') { | ||||||||||||||||||||||||||||
| 97 | # already done | ||||||||||||||||||||||||||||
| 98 | } else { | ||||||||||||||||||||||||||||
| 99 | 54 | $filter = { $filter => 1 }; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | 75 | ▶ | $filter = undef unless keys %$filter > 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | # before the main checking loop starts | ||||||||||||||||||||||||||||
| 105 | 75 | $outputter->header(); | |||||||||||||||||||||||||||
| 106 | 75 | my $notifier = Monitor::Simple::Notifier->new (config => $config, | |||||||||||||||||||||||||||
| 107 | cfgfile => $config_file); | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | # main loop | ||||||||||||||||||||||||||||
| 110 | 75 | INFO ('--- Checking started ---'); | |||||||||||||||||||||||||||
| 111 | 75 | my $start_time = time(); | |||||||||||||||||||||||||||
| 112 | 75 | my $pm = new Parallel::ForkManager ($npp); | |||||||||||||||||||||||||||
| 113 | 75 | foreach my $service (@{ $config->{services} }) { | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | # filtering of services | ||||||||||||||||||||||||||||
| 116 | 407 | ▶ | next if $filter and not exists $filter->{ $service->{id} }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$filter and not exists $$filter{$service->{"id"}}
MC/DC50%1/2
$filter and not exists $$filter{$service->{"id"}}
$filter
not exists $$filter{$service->{"id"}}
Truth table66%2/3
A and not(B)
A= $filterB= not exists $$filter{$service->{"id"}}
| |||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | # this does the fork and for the parent branch and continues the foreach loop | ||||||||||||||||||||||||||||
| 119 | 119 | ▶ | $pm->start and next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | # this is the child branch: execute an external plugin... | ||||||||||||||||||||||||||||
| 122 | 14 | my $command = $service->{plugin}->{command}; | |||||||||||||||||||||||||||
| 123 | 14 | ▶ | unless (File::Spec->file_name_is_absolute ($command)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 14 | my $plugins_dir = $config->{general}->{'plugins-dir'}; | |||||||||||||||||||||||||||
| 125 | 14 | ▶ | if ($plugins_dir) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 14 | $command = File::Spec->catfile ($plugins_dir, $command); | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | my @command = ($command, | ||||||||||||||||||||||||||||
| 130 | Monitor::Simple::Config->create_plugin_args ($config_file, | ||||||||||||||||||||||||||||
| 131 | $config, | ||||||||||||||||||||||||||||
| 132 | 14 | $service->{id})); | |||||||||||||||||||||||||||
| 133 | 14 | DEBUG ("Started: " . join (' ', @command)); | |||||||||||||||||||||||||||
| 134 | 14 | my ($stdout, $stderr, $success, $exit_code) = capture_exec (@command); | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | # ...and make the result report | ||||||||||||||||||||||||||||
| 137 | 14 | ▶ | print STDERR "$stderr\n" if $stderr; # do not hide standard errors, if any | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 14 | my ($code, $msg) = Monitor::Simple::Utils->process_exit ($command, $exit_code, $stdout); | |||||||||||||||||||||||||||
| 139 | 14 | $outputter->out ($service->{id}, $code, $msg); | |||||||||||||||||||||||||||
| 140 | 14 | ▶ | unless (exists $args->{nonotif} and $args->{nonotif}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $args->{"nonotif"} and $args->{"nonotif"}
MC/DC0%0/2
exists $args->{"nonotif"} and $args->{"nonotif"}
exists $args->{"nonotif"}
$args->{"nonotif"}
Truth table33%1/3
A and B
A= exists $args->{"nonotif"}B= $args->{"nonotif"}
| |||||||||||||||||||||||||||||
| 141 | $notifier->notify ( { service => $service->{id}, | ||||||||||||||||||||||||||||
| 142 | 14 | code => $code, | |||||||||||||||||||||||||||
| 143 | msg => $msg } ); | ||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | 14 | $pm->finish; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | # here the parent branch continue when all child process have been | ||||||||||||||||||||||||||||
| 149 | # started; we need to wait untill all these processes finish - | ||||||||||||||||||||||||||||
| 150 | # otherwise we would create a "zombie" process in out operating | ||||||||||||||||||||||||||||
| 151 | # system | ||||||||||||||||||||||||||||
| 152 | 61 | $pm->wait_all_children(); | |||||||||||||||||||||||||||
| 153 | 61 | $outputter->footer(); | |||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 61 | INFO ('--- Checking finished [' . (time() - $start_time) . ' s] ---'); | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 40 | 1; # End of Monitor::Simple | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | __END__ | ||||||||||||||||||||||||||||