Simple.pm

filter: Statementstmt 96.8% 92 / 95 Branchbran 66.6% 16 / 24 Conditioncond 41.6% 5 / 12 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 100% 17 / 17 Podpod 100% 1 / 1 totaltotal 84.0% 132 / 157 CC 18 SCAR 24.8
CC
18
Cov
86%
check_services
21.7
BEGIN
1.0
BEGIN
1.0
CRAP 12.0
« Simple/Notifier.pm ↑ Summary Simple/Output.pm »
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
BranchTrueFalseTotal
if ($args->{'config_file'}) { } 75 0 75
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
l!l&&r!l&&!r
0750
MC/DC0%0/2
$args->{'npp'} || $default_npp
$args->{'npp'} $default_npp
Truth table33%1/3
A || B
A= $args->{'npp'}B= $default_npp
ABresult
1X1
011
000
83 75 ▶ if ($npp < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($npp < 1) 0 75 75
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)
l!l&&r!l&&!r
7500
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)
ABresult
1X1
011
000
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
BranchTrueFalseTotal
if ($filter) 75 0 75
94 75 ▶ if (ref ($filter) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $filter eq 'ARRAY') { } 7 68 75
elsif (ref $filter eq 'HASH') { } 14 54 68
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
BranchTrueFalseTotal
unless keys %$filter > 0 0 75 75
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
BranchTrueFalseTotal
if $filter and not exists $$filter{$service->{"id"}} 288 119 407
Condition66%2/3
$filter and not exists $$filter{$service->{"id"}}
!ll&&!rl&&r
0119288
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"}}
ABresult
0X0
100
111
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
BranchTrueFalseTotal
$pm->start and next 105 14 119
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
BranchTrueFalseTotal
unless ("File::Spec"->file_name_is_absolute($command)) 14 0 14
124 14 my $plugins_dir = $config->{general}->{'plugins-dir'};
125 14 ▶ if ($plugins_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plugins_dir) 14 0 14
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
BranchTrueFalseTotal
if $stderr 0 14 14
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
BranchTrueFalseTotal
unless (exists $args->{"nonotif"} and $args->{"nonotif"}) 14 0 14
Condition33%1/3
exists $args->{"nonotif"} and $args->{"nonotif"}
!ll&&!rl&&r
1400
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"}
ABresult
0X0
100
111
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__
« Simple/Notifier.pm ↑ Summary Simple/Output.pm »