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

filter: Statementstmt 32.4% 50 / 154 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 59.0% 13 / 22 Podpod 0.0% 0 / 7 totaltotal 25.3% 63 / 249 CC 24 SCAR 37.8
CC
24
Cov
24%
Start
132.0
parse
56.0
loop
12.0
CRAP 43.8
« blib/lib/Net/Peep/Client.pm ↑ Summary blib/lib/Net/Peep/Conf.pm »
1 package Net::Peep::Client::Pinger;
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 Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use File::Tail;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Net::Ping::External qw(ping);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Net::Peep::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Net::Peep::Client::Pinger::Host;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Net::Peep::BC;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Net::Peep::Notifier;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Net::Peep::Notification;
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( INTERVAL MAX_INTERVAL ADJUST_AFTER ) ] );
22 1 @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
23 1 @EXPORT = qw( );
24 1 $VERSION = do { my @r = (q$Revision: 1.5 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
25  
26 1 ▶use constant DEFAULT_PID_FILE => "/var/run/pinger.pid";
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use constant DEFAULT_INTERVAL => 60; # ping every 60 seconds
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
30  
31 0 my $self = shift;
32 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
33 0 my $this = $class->SUPER::new('pinger');
34 0 bless $this, $class;
35 0 $this->name('pinger');
36 0 $this->{'HOSTS'} = [];
37 0 $this;
38  
39 } # end sub new
40  
41 0 ▶sub Start {
Subroutine Start: not called CC=11 SCAR=48.8
Pod undocumented
42  
43 0 my $self = shift;
44  
45 0 my $interval = DEFAULT_INTERVAL;
46 0 my $pidfile = DEFAULT_PID_FILE;
47 0 my $hosts = '';
48  
49 0 my $events = '';
50 0 my $logfile = '';
51 0 my $pidfile = DEFAULT_PID_FILE;
52 0 my @groups = ();
53 0 my @exclude = ();
54  
55 0 my %options = (
56 'events=s' => \$events,
57 'interval=s' => \$interval,
58 'events=s' => \$events,
59 'pidfile=s' => \$pidfile,
60 'groups=s' => \@groups,
61 'exclude=s' => \@exclude );
62  
63 0 ▶ $self->initialize(%options) || $self->pods();
Branch: 1 branch
BranchTrueFalseTotal
$self->initialize(%options) or $self->pods 0 0 0
64  
65 0 $self->logger()->debug(9,"Registering parser ...");
66 0 ▶ $self->parser(sub { my @text = @_; $self->parse(@text); });
Subroutine __ANON__: not called CC=1 SCAR=6.9
67 0 $self->logger()->debug(9,"\tParser registered ...");
68  
69 # have the client parse the configuration file and
70 # get the configuration object which should be populated with the
71 # standard command-line options and configuration information
72 0 my $conf = $self->configure();
73  
74 0 ▶ unless ($conf->getOption('autodiscovery')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($conf->getOption("autodiscovery")) 0 0 0
75 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
76 unless $conf->optionExists('server') && $conf->optionExists('port') &&
77 $conf->getOption('server') && $conf->getOption('port');
78 }
79  
80 0 my @groups = $self->getGroups();
81 0 my @exclude = $self->getExcluded();
82 0 $self->logger()->debug(1,"Recognized event groups are [@groups]");
83 0 $self->logger()->debug(1,"Excluded event groups are [@exclude]");
84  
85 # Check whether the pidfile option was set. If not, use the default
86 0 ▶ unless ($conf->optionExists('pidfile') && $conf->getOption('pidfile')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($conf->optionExists("pidfile") and $conf->getOption("pidfile")) 0 0 0
Condition0%0/3
$conf->optionExists("pidfile") and $conf->getOption("pidfile")
!ll&&!rl&&r
000
MC/DC0%0/2
$conf->optionExists("pidfile") and $conf->getOption("pidfile")
$conf->optionExists("pidfile") $conf->getOption("pidfile")
Truth table0%0/3
A and B
A= $conf->optionExists("pidfile")B= $conf->getOption("pidfile")
ABresult
0X0
100
111
87 0 $self->logger()->debug(3,"No pid file specified. Using default [" . DEFAULT_PID_FILE . "]");
88 0 $conf->setOption('pidfile', DEFAULT_PID_FILE);
89 }
90  
91 # Check whether the interval option was set. If not, use the default
92 0 ▶ unless ($conf->optionExists('interval') && $conf->getOption('interval')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($conf->optionExists("interval") and $conf->getOption("interval")) 0 0 0
Condition0%0/3
$conf->optionExists("interval") and $conf->getOption("interval")
!ll&&!rl&&r
000
MC/DC0%0/2
$conf->optionExists("interval") and $conf->getOption("interval")
$conf->optionExists("interval") $conf->getOption("interval")
Truth table0%0/3
A and B
A= $conf->optionExists("interval")B= $conf->getOption("interval")
ABresult
0X0
100
111
93 0 $self->logger()->debug(3,"No interval specified. Using default [" . DEFAULT_INTERVAL . "]");
94 0 $conf->setOption('interval', DEFAULT_INTERVAL);
95 }
96  
97 0 $self->logger()->debug(9,"Registering callback ...");
98 0 ▶ $self->callback(sub { $self->loop(); });
Subroutine __ANON__: not called CC=1 SCAR=6.9
99 0 $self->logger()->debug(9,"\tCallback registered ...");
100 0 $self->MainLoop($interval);
101  
102 0 return 1;
103  
104 } # end sub Start
105  
106 0 ▶sub loop {
Subroutine loop: not called CC=3 SCAR=24.8
Pod undocumented
107  
108 0 my $self = shift;
109  
110 0 my $conf = $self->conf();
111  
112 0 my @hosts = grep $self->filter($_), $self->hosts();
113  
114 0 my $notifier = new Net::Peep::Notifier;
115  
116 0 for my $host (@hosts) {
117  
118 0 my $object = $host->host();
119 0 my $name = $object->name();
120 0 my $ip = $object->ip();
121 0 $self->logger()->debug(7,"Pinging host [$name] with IP [$ip] ...");
122 0 my $alive = ping( host => $ip );
123 0 ▶ if ($alive) {
Branch: 1 branch
BranchTrueFalseTotal
if ($alive) { } 0 0 0
124 0 $self->logger()->debug(7,"\tHost [$name] is alive.");
125 } else {
126 0 $self->logger()->debug(7,"\tHost [$name] is unresponsive!");
127 0 my $eventname = $host->event();
128 0 my $location = $host->location();
129 0 my $priority = $host->priority();
130 0 my $status = $host->notification();
131 # send an event to the Peep server
132 0 $self->peck()->send(
133 'pinger',
134 'type' => 0,
135 'sound' => $eventname,
136 'location' => $location,
137 'priority' => $priority,
138 'volume' => 255
139 );
140  
141 0 my $notification = new Net::Peep::Notification;
142  
143 0 $notification->client('pinger');
144 0 $notification->hostname($Net::Peep::Notifier::HOSTNAME);
145 0 $notification->status($status);
146 0 $notification->datetime(time());
147 0 $notification->message("${Net::Peep::Notifier::HOSTNAME}: Host [$name] with IP address [$ip] is unresponsive.");
148
149 0 $notifier->notify($notification);
150  
151 }
152  
153 }
154  
155 } # end sub loop
156  
157 0 ▶sub peck {
Subroutine peck: not called CC=3 SCAR=24.8
Pod undocumented
158  
159 0 my $self = shift;
160  
161 0 my $conf = $self->conf();
162  
163 0 ▶ unless (exists $self->{"__PEEP"}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"__PEEP"}) 0 0 0
164 0 ▶ if ($conf->getOption('config')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conf->getOption('config')) { } 0 0 0
165 0 $self->{"__PEEP"} = Net::Peep::BC->new( $self->name(), $conf );
166 } else {
167 0 confess "Error: Expecting options to have been parsed by now.";
168 }
169 }
170  
171 0 return $self->{"__PEEP"};
172  
173 } # end sub peck
174  
175 0 ▶sub parse {
Subroutine parse: not called CC=7 SCAR=40.3
Pod undocumented
176  
177 0 my $self = shift;
178 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
179 0 my @text = @_;
180  
181 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
182  
183 0 $self->logger()->debug(1,"\t\tParsing events for client [$client] ...");
184  
185 0 $self->tempParseDefaults(@text);
186  
187 0 my @hosts = $self->getConfigSection('hosts',@text);
188  
189 0 ▶ while (my $line = shift @hosts) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = shift @hosts) 0 0 0
190 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
191  
192 0 my $name;
193 0 ▶ if ($line =~ /^\s*([\w\-\.]+)\s+([\w\-]+)\s+([\w\-]+)\s+(\d+)\s+(\d+)\s+(info|warn|crit)\s+([\w\-\.]+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*([\w\-\.]+)\s+([\w\-]+)\s+([\w\-]+)\s+(\d+)\s+(\d+)\s+(info|warn|crit)\s+([\w\-\.]+)/) 0 0 0
194  
195 0 my ($name,$event,$group,$location,$priority,$notification,$hosts) =
196 ($1,$2,$3,$4,$5,$6,$7);
197 0 my $host = new Net::Peep::Client::Pinger::Host;
198 0 $host->name($name);
199 0 $host->event($event);
200 0 $host->group($group);
201 0 $host->location($location);
202 0 $host->priority($priority);
203 0 $host->notification($notification);
204 0 $host->hosts($hosts);
205
206 0 $self->addHost($host);
207 0 $self->logger()->debug(1,"\t\t\tClient host [$host] added.");
208  
209 }
210  
211 }
212
213  
214 } # end sub parse
215  
216 0 ▶sub addHost {
Subroutine addHost: not called CC=2 SCAR=17.9
Pod undocumented
217  
218 0 my $self = shift;
219 0 ▶ my $host = shift || confess "Cannot add pinger host: No host was provided.";
Condition0%0/2
shift() || confess('Cannot add pinger host: No host was provided.')
l!l
00
MC/DC0%0/1
shift() || confess('Cannot add pinger host: No host was provided.')
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
220 0 push @{$self->{'HOSTS'}}, $host;
221 0 return 1;
222  
223 } # end sub addHost
224  
225 0 ▶sub hosts {
Subroutine hosts: not called CC=2 SCAR=17.9
Pod undocumented
226  
227 # returns the list of hosts built with the addHost method
228 0 my $self = shift;
229 0 ▶ return wantarray ? @{$self->{'HOSTS'}} : $self->{'HOSTS'};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
230  
231 } # end sub hosts
232  
233 1 1;
234  
235 __END__
« blib/lib/Net/Peep/Client.pm ↑ Summary blib/lib/Net/Peep/Conf.pm »