blib/lib/Net/Peep/Client/Sysmonitor.pm

filter: Statementstmt 14.3% 47 / 328 Branchbran 0.0% 0 / 132 Conditioncond 0.0% 0 / 125 MC/DCmcdc 0.0% 0 / 66 Subroutinesub 42.8% 12 / 28 Podpod 0.0% 0 / 14 totaltotal 8.5% 59 / 693 CC 116 SCAR 73.5
CC
116
Cov
8%
loop
3540.0
Start
506.0
parse
72.0
CRAP 1552.4
↑ Summary blib/lib/Net/Peep/Notifier.pm »
1 package Net::Peep::Client::Sysmonitor;
2  
3 1 require 5.00503;
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 # use warnings; # commented out for 5.005 compatibility
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Net::Peep::Notifier;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Net::Peep::Notification;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Net::Peep::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Net::Peep::Client::Sysmonitor::Uptime;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Net::Peep::Client::Sysmonitor::Proc;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Net::Peep::Client::Sysmonitor::Disk;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Proc::ProcessTable;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Filesys::DiskFree;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 require Exporter;
17  
18 1 ▶use vars qw{ @ISA %EXPORT_TAGS @EXPORT_OK @EXPORT $VERSION };
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 @ISA = qw( Exporter Net::Peep::Client );
21 1 %EXPORT_TAGS = ( 'all' => [ qw( ) ] );
22 1 @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
23 1 @EXPORT = qw( );
24 1 $VERSION = do { my @r = (q$Revision: 1.7 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
25  
26 1 ▶use constant DEFAULT_PID_FILE => "/var/run/sysmonitor.pid";
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
29  
30 0 my $self = shift;
31 0 ▶ my $class = ref($self) || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
32 0 my $this = $class->SUPER::new();
33 0 bless $this, $class;
34 0 $this->{'UPTIMES'} = [];
35 0 $this->{'PROCS'} = [];
36 0 $this->{'DISKS'} = [];
37 0 $this->name('sysmonitor');
38 0 $this;
39  
40 } # end sub new
41  
42 0 ▶sub Start {
Subroutine Start: not called CC=22 SCAR=62.3
Pod undocumented
43  
44 0 my $self = shift;
45  
46 # command-line options
47  
48 0 my $loadsound = '';
49 0 my $userssound = '';
50 0 my $loadloc = '';
51 0 my $usersloc = '';
52 0 my $sleep = 60;
53 0 my $maxload = 2.0;
54 0 my $maxusers = 5;
55 0 my $pidfile = '/var/run/sysmonitor.pid';
56  
57 0 my %options = (
58 'loadsound=s' => \$loadsound, # the load sound
59 'userssound=s' => \$userssound, # The users sound
60 'loadloc=s' => \$loadloc, # The location of the load sound
61 'usersloc=s' => \$usersloc, # The location of the users sound
62 'sleep=s' => \$sleep, # sleep time
63 'maxload=s' => \$maxload, # What to consider a high load
64 'maxusers=s' => \$maxusers, # What to consider a high number of users
65 'pidfile=s' => \$pidfile, # Path to write the pid out to
66 );
67  
68 # let the client know what command-line options to expect
69 # and ask the client to parse the command-line
70 0 ▶ $self->initialize(%options) || $self->pods();
Branch: 1 branch
BranchTrueFalseTotal
$self->initialize(%options) or $self->pods 0 0 0
71  
72 # register a parser for the sysmonitor section of the configuration file
73  
74 0 $self->logger()->debug(9,"Registering parser ...");
75 0 ▶ $self->parser(sub { my @text = @_; $self->parse(@text); });
Subroutine __ANON__: not called CC=1 SCAR=6.9
76 0 $self->logger()->debug(9,"\tParser registered ...");
77  
78 # have the client parse the configuration file and
79 # get the configuration object which should be populated with the
80 # standard command-line options and configuration information
81 0 my $conf = $self->configure();
82  
83 0 ▶ unless ($conf->getOption('autodiscovery')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($conf->getOption("autodiscovery")) 0 0 0
84 0 ▶ $self->pods("Error: Without autodiscovery you must provide a server and port option.")
Branch: 1 branch
BranchTrueFalseTotal
unless $conf->optionExists("server") and $conf->optionExists("port") and $conf->getOption("server") and $conf->getOption("port") 0 0 0
Condition0%0/9
$conf->optionExists("server") and $conf->optionExists("port") and $conf->getOption("server") and $conf->getOption("port")
!ll&&!rl&&r
000
$conf->optionExists("server") and $conf->optionExists("port") and $conf->getOption("server")
!ll&&!rl&&r
000
$conf->optionExists("server") and $conf->optionExists("port")
!ll&&!rl&&r
000
MC/DC0%0/4
$conf->optionExists("server") and $conf->optionExists("port") and $conf->getOption("server") and $conf->getOption("port")
$conf->optionExists("server") $conf->optionExists("port") $conf->getOption("server") $conf->getOption("port")
Truth table0%0/5
A and B and C and D
A= $conf->optionExists("server")B= $conf->optionExists("port")C= $conf->getOption("server")D= $conf->getOption("port")
ABCDresult
0XXX0
10XX0
110X0
11100
11111
85 unless $conf->optionExists('server') && $conf->optionExists('port') &&
86 $conf->getOption('server') && $conf->getOption('port');
87 }
88  
89 # Check whether the pidfile option was set. If not, use the default
90 0 ▶ unless ($conf->optionExists('pidfile')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($conf->optionExists("pidfile")) 0 0 0
91 0 $self->logger()->debug(3,"No pid file specified. Using default [" . DEFAULT_PID_FILE . "]");
92 0 $conf->setOption('pidfile', DEFAULT_PID_FILE);
93 }
94  
95 # use the options defined in the configuration file if they were
96 # not explicitly defined from the command-line
97 0 ▶ $loadsound = $conf->getOption('loadsound') if ! $loadsound && $conf->optionExists('loadsound');
Branch: 1 branch
BranchTrueFalseTotal
if not $loadsound and $conf->optionExists("loadsound") 0 0 0
Condition0%0/3
not $loadsound and $conf->optionExists("loadsound")
!ll&&!rl&&r
000
MC/DC0%0/2
not $loadsound and $conf->optionExists("loadsound")
not $loadsound $conf->optionExists("loadsound")
Truth table0%0/3
not(A) and B
A= not $loadsoundB= $conf->optionExists("loadsound")
ABresult
0X0
100
111
98 0 ▶ $userssound = $conf->getOption('userssound') if ! $userssound && $conf->optionExists('userssound');
Branch: 1 branch
BranchTrueFalseTotal
if not $userssound and $conf->optionExists("userssound") 0 0 0
Condition0%0/3
not $userssound and $conf->optionExists("userssound")
!ll&&!rl&&r
000
MC/DC0%0/2
not $userssound and $conf->optionExists("userssound")
not $userssound $conf->optionExists("userssound")
Truth table0%0/3
not(A) and B
A= not $userssoundB= $conf->optionExists("userssound")
ABresult
0X0
100
111
99 0 ▶ $loadloc = $conf->getOption('loadloc') if ! $loadloc && $conf->optionExists('loadloc');
Branch: 1 branch
BranchTrueFalseTotal
if not $loadloc and $conf->optionExists("loadloc") 0 0 0
Condition0%0/3
not $loadloc and $conf->optionExists("loadloc")
!ll&&!rl&&r
000
MC/DC0%0/2
not $loadloc and $conf->optionExists("loadloc")
not $loadloc $conf->optionExists("loadloc")
Truth table0%0/3
not(A) and B
A= not $loadlocB= $conf->optionExists("loadloc")
ABresult
0X0
100
111
100 0 ▶ $usersloc = $conf->getOption('usersloc') if ! $usersloc && $conf->optionExists('usersloc');
Branch: 1 branch
BranchTrueFalseTotal
if not $usersloc and $conf->optionExists("usersloc") 0 0 0
Condition0%0/3
not $usersloc and $conf->optionExists("usersloc")
!ll&&!rl&&r
000
MC/DC0%0/2
not $usersloc and $conf->optionExists("usersloc")
not $usersloc $conf->optionExists("usersloc")
Truth table0%0/3
not(A) and B
A= not $userslocB= $conf->optionExists("usersloc")
ABresult
0X0
100
111
101 0 ▶ $sleep = $conf->getOption('sleep') if ! $sleep && $conf->optionExists('sleep');
Branch: 1 branch
BranchTrueFalseTotal
if not $sleep and $conf->optionExists("sleep") 0 0 0
Condition0%0/3
not $sleep and $conf->optionExists("sleep")
!ll&&!rl&&r
000
MC/DC0%0/2
not $sleep and $conf->optionExists("sleep")
not $sleep $conf->optionExists("sleep")
Truth table0%0/3
not(A) and B
A= not $sleepB= $conf->optionExists("sleep")
ABresult
0X0
100
111
102 0 ▶ $maxload = $conf->getOption('maxload') if ! $maxload && $conf->optionExists('maxload');
Branch: 1 branch
BranchTrueFalseTotal
if not $maxload and $conf->optionExists("maxload") 0 0 0
Condition0%0/3
not $maxload and $conf->optionExists("maxload")
!ll&&!rl&&r
000
MC/DC0%0/2
not $maxload and $conf->optionExists("maxload")
not $maxload $conf->optionExists("maxload")
Truth table0%0/3
not(A) and B
A= not $maxloadB= $conf->optionExists("maxload")
ABresult
0X0
100
111
103 0 ▶ $maxusers = $conf->getOption('maxusers') if ! $maxusers && $conf->optionExists('maxusers');
Branch: 1 branch
BranchTrueFalseTotal
if not $maxusers and $conf->optionExists("maxusers") 0 0 0
Condition0%0/3
not $maxusers and $conf->optionExists("maxusers")
!ll&&!rl&&r
000
MC/DC0%0/2
not $maxusers and $conf->optionExists("maxusers")
not $maxusers $conf->optionExists("maxusers")
Truth table0%0/3
not(A) and B
A= not $maxusersB= $conf->optionExists("maxusers")
ABresult
0X0
100
111
104  
105 0 $self->logger()->debug(9,"loadsound=[$loadsound] userssound=[$userssound] loadloc=[$loadloc] usersloc=[$usersloc] sleep=[$sleep] maxload=[$maxload] maxusers=[$maxusers] ...");
106  
107 # Register a callback from the main loop
108  
109 0 $self->logger()->debug(9,"Registering callback ...");
110 0 ▶ $self->callback(sub { $self->loop(); });
Subroutine __ANON__: not called CC=1 SCAR=6.9
111 0 $self->logger()->debug(9,"\tCallback registered ...");
112  
113 0 $self->MainLoop($sleep);
114  
115 0 return 1;
116  
117 } # end sub Start
118  
119 0 ▶sub loop {
Subroutine loop: not called CC=59 SCAR=81.7
Pod undocumented
120  
121 0 my $self = shift;
122  
123 0 ▶ my $conf = $self->conf() || confess "Cannot execute loop: Configuration object not found.";
Condition0%0/2
$self->conf || confess('Cannot execute loop: Configuration object not found.')
l!l
00
MC/DC0%0/1
$self->conf || confess('Cannot execute loop: Configuration object not found.')
$self->conf
Truth table0%0/2
A
A= $self->conf
Aresult
11
00
124  
125 0 my ($loadsound,$userssound,$loadloc,$usersloc,$sleep,$maxload,$maxusers);
126  
127 0 ▶ $sleep = $conf->optionExists('sleep')
Branch: 1 branch
BranchTrueFalseTotal
$conf->optionExists('sleep') ? : 0 0 0
128 ? $conf->getOption('sleep') : undef;
129  
130 0 my @uptimes;
131  
132 0 ▶ my @version = $self->conf()->versionExists()
Branch: 1 branch
BranchTrueFalseTotal
$self->conf->versionExists ? : 0 0 0
133 ? split /\./, $self->conf()->getVersion()
134 : ();
135  
136 0 my ($users,$load);
137  
138 0 my ($checkload,$checkusers) = (1,1);
139  
140 0 my $notifier = new Net::Peep::Notifier;
141  
142 0 ▶ if (@version && $version[0] >= 0 && $version[1] >= 4 && $version[2] > 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (@version and $version[0] >= 0 and $version[1] >= 4 and $version[2] > 3) { } 0 0 0
Condition0%0/9
@version and $version[0] >= 0 and $version[1] >= 4 and $version[2] > 3
!ll&&!rl&&r
000
@version and $version[0] >= 0 and $version[1] >= 4
!ll&&!rl&&r
000
@version and $version[0] >= 0
!ll&&!rl&&r
000
MC/DC0%0/4
@version and $version[0] >= 0 and $version[1] >= 4 and $version[2] > 3
@version $version[0] >= 0 $version[1] >= 4 $version[2] > 3
Truth table0%0/5
A and B and C and D
A= @versionB= $version[0] >= 0C= $version[1] >= 4D= $version[2] > 3
ABCDresult
0XXX0
10XX0
110X0
11100
11111
143 # get information from the new configuration file format
144 0 @uptimes = $self->uptimes();
145 0 for my $uptime (@uptimes) {
146 0 ▶ if ($uptime->name() eq 'maxload') {
Branch: 2 branches
BranchTrueFalseTotal
if ($uptime->name eq 'maxload') { } 0 0 0
elsif ($uptime->name eq 'maxusers') { } 0 0 0
147 0 $maxload = $uptime->value();
148 0 $loadloc = $uptime->location();
149 0 $loadsound = $uptime->state();
150 0 $load = $uptime;
151 0 my $hosts = $uptime->hosts();
152 0 ▶ unless ($self->filter($uptime,1)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->filter($uptime, 1)) 0 0 0
153 0 $checkload = 0;
154 }
155 } elsif ($uptime->name() eq 'maxusers') {
156 0 $maxusers = $uptime->value();
157 0 $usersloc = $uptime->location();
158 0 $userssound = $uptime->state();
159 0 $users = $uptime;
160 0 my $hosts = $uptime->hosts();
161 0 ▶ unless ($self->filter($uptime,1)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->filter($uptime, 1)) 0 0 0
162 0 $checkusers = 0;
163 }
164 } else {
165 # do nothing
166 }
167 }
168 } else {
169 # get information from the old configuration file format
170 0 ▶ $loadsound = $conf->optionExists('loadsound')
Branch: 1 branch
BranchTrueFalseTotal
$conf->optionExists('loadsound') ? : 0 0 0
171 ? $conf->getOption('loadsound') : undef;
172 0 ▶ $userssound = $conf->optionExists('userssound')
Branch: 1 branch
BranchTrueFalseTotal
$conf->optionExists('userssound') ? : 0 0 0
173 ? $conf->getOption('userssound') : undef;
174 0 ▶ $loadloc = $conf->optionExists('loadloc')
Branch: 1 branch
BranchTrueFalseTotal
$conf->optionExists('loadloc') ? : 0 0 0
175 ? $conf->getOption('loadloc') : undef;
176 0 ▶ $usersloc = $conf->optionExists('usersloc')
Branch: 1 branch
BranchTrueFalseTotal
$conf->optionExists('usersloc') ? : 0 0 0
177 ? $conf->getOption('usersloc') : undef;
178 0 ▶ $maxload = $conf->optionExists('maxload')
Branch: 1 branch
BranchTrueFalseTotal
$conf->optionExists('maxload') ? : 0 0 0
179 ? $conf->getOption('maxload') : undef;
180 0 ▶ $maxusers = $conf->optionExists('maxusers')
Branch: 1 branch
BranchTrueFalseTotal
$conf->optionExists('maxusers') ? : 0 0 0
181 ? $conf->getOption('maxusers') : undef;
182 }
183  
184 0 ▶ confess "Error: You didn't define the sleep time [$sleep], ".
Branch: 1 branch
BranchTrueFalseTotal
unless $sleep > 0 and $loadsound and $userssound and $maxload and $maxusers 0 0 0
Condition0%0/12
$sleep > 0 and $loadsound and $userssound and $maxload and $maxusers
!ll&&!rl&&r
000
$sleep > 0 and $loadsound and $userssound and $maxload
!ll&&!rl&&r
000
$sleep > 0 and $loadsound and $userssound
!ll&&!rl&&r
000
$sleep > 0 and $loadsound
!ll&&!rl&&r
000
MC/DC0%0/5
$sleep > 0 and $loadsound and $userssound and $maxload and $maxusers
$sleep > 0 $loadsound $userssound $maxload $maxusers
Truth table0%0/6
A and B and C and D and E
A= $sleep > 0B= $loadsoundC= $userssoundD= $maxloadE= $maxusers
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
185 "load sound [$loadsound], user sound [$userssound], max load [$maxload], ".
186 "or max users [$maxusers] properly.\n".
187 " You may want to check peep.conf.\n"
188 unless $sleep > 0
189 and $loadsound and $userssound
190 and $maxload and $maxusers;
191
192 0 my ($in, $avg, $nusers, $uptime);
193 0 ▶ confess "Error: Can't find uptime: $!" unless $uptime = `which uptime`;
Branch: 1 branch
BranchTrueFalseTotal
unless $uptime = `which uptime` 0 0 0
194 0 chomp($uptime);
195 0 ▶ confess "Error: $uptime returned no output: $!" unless $in = `$uptime`;
Branch: 1 branch
BranchTrueFalseTotal
unless $in = `$uptime` 0 0 0
196
197 0 $self->logger()->debug(3,"$uptime: $in");
198
199 0 ▶ $nusers = $1 if $in =~ /(\d+) users/;
Branch: 1 branch
BranchTrueFalseTotal
if $in =~ /(\d+) users/ 0 0 0
200 0 ▶ $avg = $1 if $in =~ /load average. ([\d\.]+)/;
Branch: 1 branch
BranchTrueFalseTotal
if $in =~ /load average. ([\d\.]+)/ 0 0 0
201
202 0 ▶ if ($checkload) {
Branch: 1 branch
BranchTrueFalseTotal
if ($checkload) 0 0 0
203  
204 0 $self->logger()->debug(6,"The 1 minute load average is [$avg]. (Max load is [$maxload].)");
205 0 $self->logger()->debug(6,"The number of users is [$nusers]. (Max users is [$maxusers].)");
206 # Scale relative to maximum value with max volume being 255
207 0 ▶ my $loadvol = $maxload < $avg ? 255.0 : int(255.0 * $avg / $maxload);
Branch: 1 branch
BranchTrueFalseTotal
$maxload < $avg ? : 0 0 0
208 0 ▶ if ($maxload > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($maxload > 0) 0 0 0
209 0 $self->peck()->send(
210 $self->name(),
211 'type' => 1,
212 'sound' => $loadsound,
213 'location' => $loadloc,
214 'volume' => $loadvol,
215 );
216 }
217
218 0 ▶ if (@version && $version[0] >= 0 && $version[1] >= 4 && $version[2] > 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (@version and $version[0] >= 0 and $version[1] >= 4 and $version[2] > 3) 0 0 0
Condition0%0/9
@version and $version[0] >= 0 and $version[1] >= 4 and $version[2] > 3
!ll&&!rl&&r
000
@version and $version[0] >= 0 and $version[1] >= 4
!ll&&!rl&&r
000
@version and $version[0] >= 0
!ll&&!rl&&r
000
MC/DC0%0/4
@version and $version[0] >= 0 and $version[1] >= 4 and $version[2] > 3
@version $version[0] >= 0 $version[1] >= 4 $version[2] > 3
Truth table0%0/5
A and B and C and D
A= @versionB= $version[0] >= 0C= $version[1] >= 4D= $version[2] > 3
ABCDresult
0XXX0
10XX0
110X0
11100
11111
219 # check whether we should send notifications based on load
220 0 ▶ if ($avg > $maxload) {
Branch: 1 branch
BranchTrueFalseTotal
if ($avg > $maxload) 0 0 0
221 0 my $notification = new Net::Peep::Notification;
222 0 $notification->client($self->name());
223 0 $notification->hostname($Net::Peep::Notifier::HOSTNAME);
224 0 $notification->status($load->notification());
225 0 $notification->datetime(time());
226 0 $notification->message("${Net::Peep::Notifier::HOSTNAME}: load: ".
227 "1 minute Load average is [$avg]. It should be at most [$maxload].");
228 0 $notifier->notify($notification);
229 }
230 }
231 }
232  
233 0 ▶ if ($checkusers) {
Branch: 1 branch
BranchTrueFalseTotal
if ($checkusers) 0 0 0
234  
235 0 ▶ my $uservol = $maxusers < $nusers ? 255.0 : int(255.0 * $nusers / $maxusers);
Branch: 1 branch
BranchTrueFalseTotal
$maxusers < $nusers ? : 0 0 0
236 0 ▶ if ($maxusers > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($maxusers > 0) 0 0 0
237 0 $self->peck()->send(
238 $self->name(),
239 'type' => 1,
240 'sound' => $userssound,
241 'location' => $usersloc,
242 'volume' => $uservol,
243 );
244 }
245
246 0 ▶ if (@version && $version[0] >= 0 && $version[1] >= 4 && $version[2] > 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (@version and $version[0] >= 0 and $version[1] >= 4 and $version[2] > 3) 0 0 0
Condition0%0/9
@version and $version[0] >= 0 and $version[1] >= 4 and $version[2] > 3
!ll&&!rl&&r
000
@version and $version[0] >= 0 and $version[1] >= 4
!ll&&!rl&&r
000
@version and $version[0] >= 0
!ll&&!rl&&r
000
MC/DC0%0/4
@version and $version[0] >= 0 and $version[1] >= 4 and $version[2] > 3
@version $version[0] >= 0 $version[1] >= 4 $version[2] > 3
Truth table0%0/5
A and B and C and D
A= @versionB= $version[0] >= 0C= $version[1] >= 4D= $version[2] > 3
ABCDresult
0XXX0
10XX0
110X0
11100
11111
247 # check whether we should send notifications based on users
248 0 ▶ if ($nusers > $maxusers) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nusers > $maxusers) 0 0 0
249 0 $self->logger()->debug(6,"Sending notification based on load observation.");
250 0 my $notification = new Net::Peep::Notification;
251 0 $notification->client($self->name());
252 0 $notification->hostname($Net::Peep::Notifier::HOSTNAME);
253 0 $notification->status($users->notification());
254 0 $notification->datetime(time());
255 0 $notification->message("${Net::Peep::Notifier::HOSTNAME}: maxusers: ".
256 "Number of users is [$nusers]. It should be at most [$maxusers].");
257 0 $notifier->notify($notification);
258 }
259 }
260 }
261
262 # now for the procs ...
263 0 my @procs = grep $self->filter($_,1), $self->procs();
264  
265 0 my $table = new Proc::ProcessTable;
266  
267 0 for my $proc (@procs) {
268 0 my $name = $proc->name();
269 0 my $hosts = $proc->hosts();
270 0 my $max = $proc->max();
271 0 my $min = $proc->min();
272 0 my $count = 0;
273 0 my $p = $table->table();
274 0 $self->logger()->debug(6,"Checking process [$name] ...");
275 0 ▶ map { $count++ if $_->cmndline() =~ /$name/ } @$p;
Branch: 1 branch
BranchTrueFalseTotal
if $_->cmndline =~ /$name/ 0 0 0
276 0 $self->logger()->debug(6,"\t[$count] processes whose command line matches [$name] are running.");
277 0 ▶ if ( ( $max ne 'inf' && $count > $max ) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($max ne "inf" and $count > $max or $count < $min) 0 0 0
Condition0%0/6
$max ne "inf" and $count > $max or $count < $min
l!l&&r!l&&!r
000
$max ne "inf" and $count > $max
!ll&&!rl&&r
000
MC/DC0%0/3
$max ne "inf" and $count > $max or $count < $min
$max ne "inf" $count > $max $count < $min
Truth table0%0/5
A and B or C
A= $max ne "inf"B= $count > $maxC= $count < $min
ABCresult
11X1
0X11
1011
0X00
1000
278 ( $count < $min ) ) {
279
280 0 ▶ $self->logger()->debug(6,"\tSending notification because too many [$name] processes are running.")
Branch: 1 branch
BranchTrueFalseTotal
if $max ne "inf" and $count > $max 0 0 0
Condition0%0/3
$max ne "inf" and $count > $max
!ll&&!rl&&r
000
MC/DC0%0/2
$max ne "inf" and $count > $max
$max ne "inf" $count > $max
Truth table0%0/3
A and B
A= $max ne "inf"B= $count > $max
ABresult
0X0
100
111
281 if $max ne 'inf' && $count > $max;
282 0 ▶ $self->logger()->debug(6,"\tSending notification because too few [$name] processes are running.")
Branch: 1 branch
BranchTrueFalseTotal
if $count < $min 0 0 0
283 if $count < $min;
284 0 $self->peck()->send(
285 $self->name(),
286 'type' => 0,
287 'sound' => $proc->event(),
288 'location' => 128, # FIXIT: This should be taken from peep.conf
289 'priority' => 0,
290 'volume' => 255
291 );
292
293 0 my $notification = new Net::Peep::Notification;
294 0 $notification->client($self->name());
295 0 $notification->hostname($Net::Peep::Notifier::HOSTNAME);
296 0 $notification->status($proc->notification());
297 0 $notification->datetime(time());
298 0 ▶ if ($max ne 'inf' && $count > $max) {
Branch: 2 branches
BranchTrueFalseTotal
if ($max ne 'inf' and $count > $max) { } 0 0 0
elsif ($count < $min) { } 0 0 0
Condition0%0/3
$max ne "inf" and $count > $max
!ll&&!rl&&r
000
MC/DC0%0/2
$max ne "inf" and $count > $max
$max ne "inf" $count > $max
Truth table0%0/3
A and B
A= $max ne "inf"B= $count > $max
ABresult
0X0
100
111
299 0 $notification->message("${Net::Peep::Notifier::HOSTNAME}: $name: ".
300 "There are [$count] instances of processes that match the pattern [$name]\n".
301 "whereas at most [$max] should be running.");
302 } elsif ($count < $min) {
303 0 $notification->message("${Net::Peep::Notifier::HOSTNAME}: $name: ".
304 "There are [$count] instances of processes that match the pattern [$name]\n".
305 "whereas a minimum of [$min] should be running.");
306 } else {
307 0 confess "Error: Bad evaluation of process count for [$name].";
308 }
309 0 $notifier->notify($notification);
310 }
311 }
312  
313 # now for the disk utilization
314 0 my @disks = grep $self->filter($_,1), $self->disks();
315 0 my $handle = new Filesys::DiskFree;
316 0 $handle->df();
317 0 my @d = $handle->disks();
318 0 for my $disk (@disks) {
319 0 my $name = $disk->name();
320 0 my $hosts = $disk->hosts();
321 0 my $name = $disk->name();
322 0 my $max = $disk->max();
323 0 $self->logger()->debug(6,"Checking disk pattern [$name] ...");
324 0 my $regex = quotemeta($name);
325 0 for my $d (@d) {
326 0 my $mount = $handle->mount($d);
327 0 my $used = $handle->used($d);
328 0 my $total = $handle->total($d);
329 0 my $device = $handle->device($d);
330 0 my $utilization = ( $used / $total ) * 100;
331 0 $self->logger()->debug(8,"\tChecking disk [$mount] on device [$device] ...");
332 0 ▶ if ($device =~ /$regex/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($device =~ /$regex/) 0 0 0
333 0 $self->logger()->debug(8,"\t\tDisk [$mount] matches.");
334 0 ▶ if ( $utilization > $max ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($utilization > $max) 0 0 0
335 0 $self->logger()->debug(6,sprintf "\t\tDisk [$mount] is at [%5.2f\%] capacity. It should be at most [$max\%].", $utilization);
336 0 $self->peck()->send(
337 $self->name(),
338 'type' => 0,
339 'sound' => $disk->event(),
340 'location' => 128, # FIXIT: This should be taken from peep.conf
341 'priority' => 0,
342 'volume' => 255
343 );
344 0 my $notification = new Net::Peep::Notification;
345 0 $notification->client($self->name());
346 0 $notification->hostname($Net::Peep::Notifier::HOSTNAME);
347 0 $notification->status($disk->notification());
348 0 $notification->datetime(time());
349 0 $notification->message(sprintf "${Net::Peep::Notifier::HOSTNAME}: $name: ".
350 "Device [$device] mounted at [$mount] is at [%5.2f\%] capacity.\n".
351 "It should be at most [$max\%].", $utilization);
352 0 $notifier->notify($notification);
353 }
354 }
355 }
356 }
357
358 } # end sub loop
359  
360 0 ▶sub parse {
Subroutine parse: not called CC=8 SCAR=42.8
Pod undocumented
361  
362 0 my $self = shift;
363 0 ▶ my $client = $self->name() || confess "Cannot parse logparser events: Client name attribute not set.";
Condition0%0/2
$self->name || confess('Cannot parse logparser events: Client name attribute not set.')
l!l
00
MC/DC0%0/1
$self->name || confess('Cannot parse logparser events: Client name attribute not set.')
$self->name
Truth table0%0/2
A
A= $self->name
Aresult
11
00
364 0 my @text = @_;
365  
366 0 ▶ my $conf = $self->conf() || confess "Cannot parse logparser events: Configuration object not found.";
Condition0%0/2
$self->conf || confess('Cannot parse logparser events: Configuration object not found.')
l!l
00
MC/DC0%0/1
$self->conf || confess('Cannot parse logparser events: Configuration object not found.')
$self->conf
Truth table0%0/2
A
A= $self->conf
Aresult
11
00
367  
368 0 $self->logger()->debug(1,"\t\tParsing events for client [$client] ...");
369  
370 0 ▶ my @version = $self->conf()->versionExists()
Branch: 1 branch
BranchTrueFalseTotal
$self->conf->versionExists ? : 0 0 0
371 ? split /\./, $self->conf()->getVersion()
372 : ();
373  
374 0 ▶ if (@version && $version[0] >= 0 && $version[1] >= 4 && $version[2] > 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (@version and $version[0] >= 0 and $version[1] >= 4 and $version[2] > 3) { } 0 0 0
Condition0%0/9
@version and $version[0] >= 0 and $version[1] >= 4 and $version[2] > 3
!ll&&!rl&&r
000
@version and $version[0] >= 0 and $version[1] >= 4
!ll&&!rl&&r
000
@version and $version[0] >= 0
!ll&&!rl&&r
000
MC/DC0%0/4
@version and $version[0] >= 0 and $version[1] >= 4 and $version[2] > 3
@version $version[0] >= 0 $version[1] >= 4 $version[2] > 3
Truth table0%0/5
A and B and C and D
A= @versionB= $version[0] >= 0C= $version[1] >= 4D= $version[2] > 3
ABCDresult
0XXX0
10XX0
110X0
11100
11111
375  
376 0 $self->tempParseDefaults(@text);
377  
378 0 my @uptime = $self->getConfigSection('uptime',@text);
379 0 my @procs = $self->getConfigSection('procs',@text);
380 0 my @disk = $self->getConfigSection('disk',@text);
381  
382 0 $self->parseUptime(@uptime);
383 0 $self->parseProcs(@procs);
384 0 $self->parseDisk(@disk);
385  
386 } else {
387  
388 0 $self->tempParseOldConfig(@text);
389  
390 }
391  
392 } # end sub parse
393  
394 0 ▶sub tempParseOldConfig {
Subroutine tempParseOldConfig: not called CC=8 SCAR=42.8
Pod undocumented
395  
396 0 my $self = shift;
397 0 my @text = @_;
398  
399 0 ▶ my $conf = $self->conf() || confess "Cannot parse old-style config file: Configuration object not found.";
Condition0%0/2
$self->conf || confess('Cannot parse old-style config file: Configuration object not found.')
l!l
00
MC/DC0%0/1
$self->conf || confess('Cannot parse old-style config file: Configuration object not found.')
$self->conf
Truth table0%0/2
A
A= $self->conf
Aresult
11
00
400  
401 0 ▶ while (my $line = shift @text) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = shift @text) 0 0 0
402  
403 0 ▶ next if $line =~ /^\s*#/ || $line =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*#/ or $line =~ /^\s*$/ 0 0 0
Condition0%0/3
$line =~ /^\s*#/ or $line =~ /^\s*$/
l!l&&r!l&&!r
000
MC/DC0%0/2
$line =~ /^\s*#/ or $line =~ /^\s*$/
$line =~ /^\s*#/ $line =~ /^\s*$/
Truth table0%0/3
A or B
A= $line =~ /^\s*#/B= $line =~ /^\s*$/
ABresult
1X1
011
000
404 0 ▶ if ($line =~ /^\s*([\w\-]+)\s+(\S+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*([\w\-]+)\s+(\S+)/) 0 0 0
405 0 my ($option,$value) = ($1,$2);
406 0 ▶ if ($conf->optionExists($option)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conf->optionExists($option)) { } 0 0 0
407 0 $self->logger()->debug(6,"Not setting option [$option]: It has already been set (possibly from the command-line).");
408 } else {
409 0 $self->logger()->debug(6,"Setting option [$option] to value [$value].");
410 0 ▶ $conf->setOption($option,$value) unless $conf->optionExists($option);
Branch: 1 branch
BranchTrueFalseTotal
unless $conf->optionExists($option) 0 0 0
411 }
412 }
413  
414 }
415  
416 } # end sub tempParseOldConfig
417  
418 0 ▶sub parseUptime {
Subroutine parseUptime: not called CC=6 SCAR=37.4
Pod undocumented
419  
420 0 my $self = shift;
421 0 my @text = @_;
422  
423 0 ▶ my $client = $self->name() || confess "Cannot process uptime definitions: No client name found.";
Condition0%0/2
$self->name || confess('Cannot process uptime definitions: No client name found.')
l!l
00
MC/DC0%0/1
$self->name || confess('Cannot process uptime definitions: No client name found.')
$self->name
Truth table0%0/2
A
A= $self->name
Aresult
11
00
424  
425 0 $self->logger()->debug(1,"\t\tParsing uptime settings for client [$client] ...");
426  
427 0 ▶ while (my $line = shift @text) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = shift @text) 0 0 0
428 0 ▶ next if $line =~ /^\s*$/ or $line =~ /^\s*#/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*$/ or $line =~ /^\s*#/ 0 0 0
Condition0%0/3
$line =~ /^\s*$/ or $line =~ /^\s*#/
l!l&&r!l&&!r
000
MC/DC0%0/2
$line =~ /^\s*$/ or $line =~ /^\s*#/
$line =~ /^\s*$/ $line =~ /^\s*#/
Truth table0%0/3
A or B
A= $line =~ /^\s*$/B= $line =~ /^\s*#/
ABresult
1X1
011
000
429  
430 0 ▶ if ($line =~ /^\s*([\w]+)\s+([\w]+)\s+([\d\.]+)\s+(\d+)\s+(\d+)\s+(info|warn|crit)\s+([\w\-\.]+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*([\w]+)\s+([\w]+)\s+([\d\.]+)\s+(\d+)\s+(\d+)\s+(info|warn|crit)\s+([\w\-\.]+)/) 0 0 0
431  
432 0 my ($name,$state,$value,$location,$priority,$notification,$hosts) =
433 ($1,$2,$3,$4,$5,$6,$7);
434  
435 0 my $uptime = new Net::Peep::Client::Sysmonitor::Uptime;
436  
437 0 $uptime->name($name);
438 0 $uptime->state($state);
439 0 $uptime->value($value);
440 0 $uptime->location($location);
441 0 $uptime->priority($priority);
442 0 $uptime->notification($notification);
443 0 $uptime->hosts($hosts);
444 0 $self->addUptime($uptime);
445 0 $self->logger()->debug(1,"\t\t\tClient uptime setting [$name] added.");
446  
447 }
448  
449 }
450  
451 0 return @text;
452  
453 } # end sub parseUptime
454  
455 0 ▶sub parseProcs {
Subroutine parseProcs: not called CC=6 SCAR=37.4
Pod undocumented
456  
457 0 my $self = shift;
458 0 my @text = @_;
459
460 0 ▶ my $client = $self->name() || confess "Cannot process proc definitions: No client name found.";
Condition0%0/2
$self->name || confess('Cannot process proc definitions: No client name found.')
l!l
00
MC/DC0%0/1
$self->name || confess('Cannot process proc definitions: No client name found.')
$self->name
Truth table0%0/2
A
A= $self->name
Aresult
11
00
461  
462 0 $self->logger()->debug(1,"\t\tParsing proc list for client [$client] ...");
463
464 0 ▶ while (my $line = shift @text) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = shift @text) 0 0 0
465 0 ▶ next if $line =~ /^\s*$/ or $line =~ /^\s*#/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*$/ or $line =~ /^\s*#/ 0 0 0
Condition0%0/3
$line =~ /^\s*$/ or $line =~ /^\s*#/
l!l&&r!l&&!r
000
MC/DC0%0/2
$line =~ /^\s*$/ or $line =~ /^\s*#/
$line =~ /^\s*$/ $line =~ /^\s*#/
Truth table0%0/3
A or B
A= $line =~ /^\s*$/B= $line =~ /^\s*#/
ABresult
1X1
011
000
466
467 0 ▶ if ($line =~ /^\s*([\w\-\.]+)\s+([\w\-]+)\s+(\d+|inf)\s+(\d+)\s+(\d+)\s+(\d+)\s+(info|warn|crit)\s+(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*([\w\-\.]+)\s+([\w\-]+)\s+(\d+|inf)\s+(\d+)\s+(\d+)\s+(\d+)\s+(info|warn|crit)\s+(.*)$/) 0 0 0
468
469 0 my ($name,$event,$max,$min,$location,$priority,$notification,$hosts) =
470 ($1,$2,$3,$4,$5,$6,$7,$8);
471
472 0 my @hosts = split /\s*,\s*/, $hosts;
473
474 0 my $proc = new Net::Peep::Client::Sysmonitor::Proc;
475 0 $proc->name($name);
476 0 $proc->event($event);
477 0 $proc->max($max);
478 0 $proc->min($min);
479 0 $proc->location($location);
480 0 $proc->priority($priority);
481 0 $proc->notification($notification);
482 0 $proc->hosts($hosts);
483
484 0 $self->addProc($proc);
485 0 $self->logger()->debug(1,"\t\t\tClient proc [$name] added.");
486
487 }
488
489 }
490
491 0 return @text;
492  
493 } # end sub parseProcs
494  
495 0 ▶sub parseDisk {
Subroutine parseDisk: not called CC=6 SCAR=37.4
Pod undocumented
496  
497 0 my $self = shift;
498 0 my @text = @_;
499  
500 0 ▶ my $client = $self->name() || confess "Cannot process disk definitions: No client name found.";
Condition0%0/2
$self->name || confess('Cannot process disk definitions: No client name found.')
l!l
00
MC/DC0%0/1
$self->name || confess('Cannot process disk definitions: No client name found.')
$self->name
Truth table0%0/2
A
A= $self->name
Aresult
11
00
501  
502 0 $self->logger()->debug(1,"\t\tParsing disks for client [$client] ...");
503  
504 0 ▶ while (my $line = shift @text) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = shift @text) 0 0 0
505 0 ▶ next if $line =~ /^\s*$/ or $line =~ /^\s*#/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*$/ or $line =~ /^\s*#/ 0 0 0
Condition0%0/3
$line =~ /^\s*$/ or $line =~ /^\s*#/
l!l&&r!l&&!r
000
MC/DC0%0/2
$line =~ /^\s*$/ or $line =~ /^\s*#/
$line =~ /^\s*$/ $line =~ /^\s*#/
Truth table0%0/3
A or B
A= $line =~ /^\s*$/B= $line =~ /^\s*#/
ABresult
1X1
011
000
506
507 0 ▶ if ($line =~ /^\s*(\w+)\s+([\w-]+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(info|crit|warn)\s+([\w\-\.]+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*(\w+)\s+([\w-]+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(info|crit|warn)\s+([\w\-\.]+)/) 0 0 0
508  
509 0 my ($name,$event,$max,$location,$priority,$notification,$hosts) =
510 ($1,$2,$3,$4,$5,$6,$7);
511  
512 0 my $disk = new Net::Peep::Client::Sysmonitor::Disk;
513  
514 0 $disk->name($name);
515 0 $disk->event($event);
516 0 $disk->max($max);
517 0 $disk->location($location);
518 0 $disk->priority($priority);
519 0 $disk->notification($notification);
520 0 $disk->hosts($hosts);
521
522 0 $self->addDisk($disk);
523 0 $self->logger()->debug(1,"\t\t\tClient disk [$name] added.");
524  
525 }
526  
527 }
528  
529 0 return @text;
530  
531 } # end sub parseDisk
532  
533 0 ▶sub addUptime {
Subroutine addUptime: not called CC=2 SCAR=17.9
Pod undocumented
534  
535 0 my $self = shift;
536 0 ▶ my $uptime = shift || confess "Cannot add sysmonitor uptime: No uptime was provided.";
Condition0%0/2
shift() || confess('Cannot add sysmonitor uptime: No uptime was provided.')
l!l
00
MC/DC0%0/1
shift() || confess('Cannot add sysmonitor uptime: No uptime was provided.')
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
537 0 push @{$self->{'UPTIMES'}}, $uptime;
538 0 return 1;
539  
540 } # end sub addUptime
541  
542 0 ▶sub uptimes {
Subroutine uptimes: not called CC=2 SCAR=17.9
Pod undocumented
543  
544 # returns the list of uptimes built with the addUptime method
545 0 my $self = shift;
546 0 ▶ return wantarray ? @{$self->{'UPTIMES'}} : $self->{'UPTIMES'};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
547  
548 } # end sub uptimes
549  
550 0 ▶sub addProc {
Subroutine addProc: not called CC=2 SCAR=17.9
Pod undocumented
551  
552 0 my $self = shift;
553 0 ▶ my $proc = shift || confess "Cannot add sysmonitor proc: No proc was provided.";
Condition0%0/2
shift() || confess('Cannot add sysmonitor proc: No proc was provided.')
l!l
00
MC/DC0%0/1
shift() || confess('Cannot add sysmonitor proc: No proc was provided.')
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
554 0 push @{$self->{'PROCS'}}, $proc;
555 0 return 1;
556  
557 } # end sub addProc
558  
559 0 ▶sub procs {
Subroutine procs: not called CC=2 SCAR=17.9
Pod undocumented
560  
561 # returns the list of procs built with the addProc method
562 0 my $self = shift;
563 0 ▶ return wantarray ? @{$self->{'PROCS'}} : $self->{'PROCS'};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
564  
565 } # end sub procs
566  
567 0 ▶sub addDisk {
Subroutine addDisk: not called CC=2 SCAR=17.9
Pod undocumented
568  
569 0 my $self = shift;
570 0 ▶ my $disk = shift || confess "Cannot add sysmonitor disk: No disk was provided.";
Condition0%0/2
shift() || confess('Cannot add sysmonitor disk: No disk was provided.')
l!l
00
MC/DC0%0/1
shift() || confess('Cannot add sysmonitor disk: No disk was provided.')
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
571 0 push @{$self->{'DISKS'}}, $disk;
572 0 return 1;
573  
574 } # end sub addDisk
575  
576 0 ▶sub disks {
Subroutine disks: not called CC=2 SCAR=17.9
Pod undocumented
577  
578 # returns the list of disks built with the addDisk method
579 0 my $self = shift;
580 0 ▶ return wantarray ? @{$self->{'DISKS'}} : $self->{'DISKS'};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
581  
582 } # end sub disks
583  
584 1 1;
585  
586 __END__
↑ Summary blib/lib/Net/Peep/Notifier.pm »