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

filter: Statementstmt 41.2% 92 / 223 Branchbran 10.3% 6 / 58 Conditioncond 17.7% 8 / 45 MC/DCmcdc 0.0% 0 / 23 Subroutinesub 66.6% 18 / 27 Podpod 0.0% 0 / 10 totaltotal 32.1% 124 / 386 CC 51 SCAR 43.3
CC
51
Cov
33%
parse
185.0
Start
90.0
getLogFileTails
42.0
CRAP 75.9
« blib/lib/Net/Peep/Notifier.pm ↑ Summary blib/lib/Net/Peep/Mail.pm »
1 package Net::Peep::Client::Logparser;
2  
3 2 require 5.00503;
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 # use warnings; # commented out for 5.005 compatibility
6 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use File::Tail;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Sys::Hostname;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Net::Peep::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Net::Peep::Client::Logparser::Event;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Net::Peep::BC;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Net::Peep::Notifier;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Net::Peep::Notification;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 require Exporter;
17  
18 2 ▶use vars qw{ @ISA %EXPORT_TAGS @EXPORT_OK @EXPORT $VERSION };
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 2 @ISA = qw(Exporter Net::Peep::Client);
21 2 %EXPORT_TAGS = ( 'all' => [ qw( INTERVAL MAX_INTERVAL ADJUST_AFTER ) ] );
22 2 @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
23 2 @EXPORT = qw( );
24 2 $VERSION = do { my @r = (q$Revision: 1.11 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
25  
26 # These are in seconds and are the parameters for File::Tail
27  
28 # File Tail uses the idea of intervals and predictions to try to keep
29 # blocking time at a maximum. These three parameters are the ones that
30 # people will want to tune for performance vs. load. The smaller the
31 # interval, the higher the load but faster events are picked up.
32  
33 # The interval that File::Tail waits before checking the log
34 2 ▶use constant INTERVAL => 0.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 # The maximum interval that File::Tail will wait before checking the
36 # log
37 2 ▶use constant MAX_INTERVAL => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 # The time after which File::Tail adjusts its predictions
39 2 ▶use constant ADJUST_AFTER => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 2 ▶use constant DEFAULT_PID_FILE => "/var/run/logparser.pid";
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod undocumented
44  
45 1 my $self = shift;
46 1 ▶ my $class = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
47 1 my $this = $class->SUPER::new();
48 1 bless $this, $class;
49 1 $this->{'EVENTS'} = [];
50 1 $this->name('logparser');
51 1 $this;
52  
53 } # end sub new
54  
55 0 ▶sub getLogFiles {
Subroutine getLogFiles: not called CC=3 SCAR=24.8
Pod undocumented
56  
57 0 my $self = shift;
58 0 my $conf = $self->conf();
59 0 ▶ my $logfiles = $conf->optionExists('logfile') ? $conf->getOption('logfile') : '';
Branch: 1 branch
BranchTrueFalseTotal
$conf->optionExists('logfile') ? : 0 0 0
60 0 my @logfiles = split ',\s*', $logfiles;
61 0 ▶ return wantarray ? @logfiles : [@logfiles];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
62  
63 } # sub getLogFiles
64  
65 0 ▶sub getLogFileTails {
Subroutine getLogFileTails: not called CC=6 SCAR=37.4
Pod undocumented
66  
67 0 my $self = shift;
68 0 ▶ if ( ! exists $self->{"__LOGFILETAILS"} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"__LOGFILETAILS"}) 0 0 0
69  
70 0 my @logfiles = $self->getLogFiles();
71 0 my @tailfiles;
72 0 for my $logfile (@logfiles) {
73 0 ▶ if (-e $logfile) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $logfile) { } 0 0 0
74 0 my $tail;
75 0 eval { $tail =
76 File::Tail->new(
77 name => $logfile,
78 interval => INTERVAL,
79 maxinterval => MAX_INTERVAL,
80 adjustafter => ADJUST_AFTER
81 );
82 };
83 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
84 0 chomp $@;
85 0 $self->logger()->log("Warning: Error creating tail of logfile '$logfile': $@");
86 } else {
87 0 push @tailfiles, $tail;
88 }
89 } else {
90 0 $self->logger()->log("Warning: Can't tail the log file '$logfile': It doesn't exist.");
91 }
92 }
93  
94 0 $self->{"__LOGFILETAILS"} = \@tailfiles;
95  
96 }
97  
98 0 ▶ return wantarray ? @{$self->{"__LOGFILETAILS"}} : $self->{"__LOGFILETAILS"};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
99  
100 } # sub getLogFileTails
101  
102 0 ▶sub Start {
Subroutine Start: not called CC=9 SCAR=45.0
Pod undocumented
103  
104 0 my $self = shift;
105  
106 0 my $events = '';
107 0 my $logfile = '';
108 0 my $pidfile = DEFAULT_PID_FILE;
109 0 my @groups = ();
110 0 my @exclude = ();
111  
112 0 my %options = (
113 'events=s' => \$events,
114 'logfile=s' => \$logfile,
115 'pidfile=s' => \$pidfile,
116 'groups=s' => \@groups,
117 'exclude=s' => \@exclude );
118  
119 0 ▶ $self->initialize(%options) || $self->pods();
Branch: 1 branch
BranchTrueFalseTotal
$self->initialize(%options) or $self->pods 0 0 0
120  
121 # register a parser for the logparser section of the configuration file
122  
123 0 $self->logger()->debug(9,"Registering parser ...");
124 0 ▶ $self->parser(sub { my @text = @_; $self->parse(@text); });
Subroutine __ANON__: not called CC=1 SCAR=6.9
125 0 $self->logger()->debug(9,"\tParser registered ...");
126  
127 # have the client parse the configuration file and
128 # get the configuration object which should be populated with the
129 # standard command-line options and configuration information
130 0 my $conf = $self->configure();
131  
132 0 ▶ unless ($conf->getOption('autodiscovery')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($conf->getOption("autodiscovery")) 0 0 0
133 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
134 unless $conf->optionExists('server') && $conf->optionExists('port') &&
135 $conf->getOption('server') && $conf->getOption('port');
136 }
137  
138 0 my @gotgroups = $self->getGroups();
139 0 my @gotexclude = $self->getExcluded();
140 0 $self->logger()->debug(1,"Recognized event groups are [@gotgroups]");
141 0 $self->logger()->debug(1,"Excluded event groups are [@gotexclude]");
142  
143 # Check whether the pidfile option was set. If not, use the default
144 0 ▶ unless ($conf->optionExists('pidfile')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($conf->optionExists("pidfile")) 0 0 0
145 0 $self->logger()->debug(3,"No pid file specified. Using default [" . DEFAULT_PID_FILE . "]");
146 0 $conf->setOption('pidfile', DEFAULT_PID_FILE);
147 }
148  
149 0 $self->logger()->log("Scanning logs:");
150  
151 0 for my $logfile ($self->getLogFiles()) {
152 0 $self->logger()->log("\t$logfile");
153 }
154  
155 # Register a callback for the main loop
156 0 $self->logger()->debug(9,"Registering callback ...");
157 0 ▶ $self->callback(sub { $self->loop(); });
Subroutine __ANON__: not called CC=1 SCAR=6.9
158 0 $self->logger()->debug(9,"\tCallback registered ...");
159  
160 0 $self->MainLoop();
161  
162 0 return 1;
163  
164 } # end sub Start
165  
166 0 ▶sub loop {
Subroutine loop: not called CC=5 SCAR=34.0
Pod undocumented
167  
168 0 my $self = shift;
169  
170 0 select(STDOUT);
171  
172 0 $| = 1; # autoflush
173  
174 0 my $nfound;
175  
176 0 my @logFileTails = $self->getLogFileTails();
177  
178 # call the peck method which, the first time it is called, will
179 # instantiate a Net::Peep::BC object as necessary
180  
181 0 $self->peck();
182  
183 0 while (1) {
184  
185 0 $nfound = File::Tail::select(undef,undef,undef,60,@logFileTails);
186 # hmmm ... don't quite understand what interval does ... [collin]
187 0 ▶ unless ($nfound) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($nfound) 0 0 0
188 0 for my $filetail (@logFileTails) {
189 0 $filetail->interval;
190 }
191 }
192  
193 0 for my $filetail (@logFileTails) {
194 0 ▶ $self->tail($filetail->read) unless $filetail->predict;
Branch: 1 branch
BranchTrueFalseTotal
unless $filetail->predict 0 0 0
195 }
196 }
197  
198 0 return 1;
199  
200 } # end sub loop
201  
202 0 ▶sub peck {
Subroutine peck: not called CC=3 SCAR=24.8
Pod undocumented
203  
204 0 my $self = shift;
205  
206 0 my $configuration = $self->conf();
207  
208 0 ▶ unless (exists $self->{"__PEEP"}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"__PEEP"}) 0 0 0
209 0 ▶ if ($configuration->getOptions()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($configuration->getOptions) { } 0 0 0
210 0 $self->{"__PEEP"} = Net::Peep::BC->new( $self->name(), $configuration );
211 } else {
212 0 confess "Error: Expecting options to have been parsed by now.";
213 }
214 }
215  
216 0 return $self->{"__PEEP"};
217  
218 } # end sub peck
219  
220 0 ▶sub tail {
Subroutine tail: not called CC=4 SCAR=30.0
Pod undocumented
221  
222 0 my $self = shift;
223 0 my $line = shift;
224
225 0 chomp $line;
226
227 0 $self->logger()->debug(9,"Checking [$line] ...");
228
229 0 my $conf = $self->conf();
230
231 0 my $found = 0;
232
233 # filter the events based on which groups or option letters
234 # are specified
235 0 my @events = grep $self->filter($_), $self->events();
236
237 0 for my $event (@events) {
238
239 # if we've already matched an event ignore the remaining events
240
241 0 ▶ unless ($found) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found) 0 0 0
242
243 0 my $name = $event->name();
244 0 my $location = $event->location();
245 0 my $priority = $event->priority();
246 0 my $status = $event->notification();
247 0 my $regex = $event->regex();
248
249 0 $self->logger()->debug(9,"\tTrying to match regex [$regex] for event [$name]");
250
251 0 ▶ if ($line =~ /$regex/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /$regex/) 0 0 0
252
253 0 $self->logger()->debug(5,"$name: $line");
254
255 0 $self->peck()->send(
256 'logparser',
257 'type' => 0,
258 'sound' => $name,
259 'location' => $location,
260 'priority' => $priority,
261 'volume' => 255
262 );
263
264 0 my $notifier = new Net::Peep::Notifier;
265 0 my $notification = new Net::Peep::Notification;
266
267 0 $notification->client($self->name());
268 0 $notification->hostname($Net::Peep::Notifier::HOSTNAME);
269 0 $notification->status($status);
270 0 $notification->datetime(time());
271 0 $notification->message("${Net::Peep::Notifier::HOSTNAME}: $name: $line");
272
273 0 $notifier->notify($notification);
274
275 0 $found++;
276
277 }
278 }
279 }
280
281 0 return 1;
282  
283 } # end sub tail
284  
285 1 ▶sub parse {
Subroutine parse: called CC=24 SCAR=52.2
Pod undocumented
286  
287 1 my $self = shift;
288 1 ▶ my $client = $self->name() || confess "Cannot parse logparser events: Client name attribute not set.";
Condition50%1/2
$self->name || confess('Cannot parse logparser events: Client name attribute not set.')
l!l
10
MC/DC0%0/1
$self->name || confess('Cannot parse logparser events: Client name attribute not set.')
$self->name
Truth table50%1/2
A
A= $self->name
Aresult
11
00
289 1 my @text = @_;
290  
291 1 ▶ my $conf = $self->conf() || confess "Cannot parse logparser events: Configuration object not found.";
Condition50%1/2
$self->conf || confess('Cannot parse logparser events: Configuration object not found.')
l!l
10
MC/DC0%0/1
$self->conf || confess('Cannot parse logparser events: Configuration object not found.')
$self->conf
Truth table50%1/2
A
A= $self->conf
Aresult
11
00
292  
293 1 $self->logger()->debug(1,"\t\tParsing events for client [$client] ...");
294  
295 1 $self->tempParseDefaults(@text);
296  
297 1 my @events = $self->getConfigSection('events',@text);
298  
299 1 ▶ my @version = $self->conf()->versionExists()
Branch: 1 branch
BranchTrueFalseTotal
$self->conf->versionExists ? : 1 0 1
300 ? split /\./, $self->conf()->getVersion()
301 : ();
302  
303 1 ▶ if (@version && $version[0] >= 0 && $version[1] >= 4 && $version[2] > 3) {
Branch: 2 branches
BranchTrueFalseTotal
if (@version and $version[0] >= 0 and $version[1] >= 4 and $version[2] > 3) { } 1 0 1
elsif (@version and $version[0] >= 0 and $version[1] >= 4 and $version[2] > 1) { } 0 0 0
Condition33%3/9
@version and $version[0] >= 0 and $version[1] >= 4 and $version[2] > 3
!ll&&!rl&&r
001
@version and $version[0] >= 0 and $version[1] >= 4
!ll&&!rl&&r
001
@version and $version[0] >= 0
!ll&&!rl&&r
001
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 table20%1/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
304  
305 1 ▶ while (my $line = shift @events) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = shift @events) 10 1 11
306 10 ▶ next if $line =~ /^\s*#/ || $line =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*#/ or $line =~ /^\s*$/ 0 10 10
Condition33%1/3
$line =~ /^\s*#/ or $line =~ /^\s*$/
l!l&&r!l&&!r
0010
MC/DC0%0/2
$line =~ /^\s*#/ or $line =~ /^\s*$/
$line =~ /^\s*#/ $line =~ /^\s*$/
Truth table33%1/3
A or B
A= $line =~ /^\s*#/B= $line =~ /^\s*$/
ABresult
1X1
011
000
307  
308 10 my $name;
309 10 ▶ if ($line =~ /^\s*([\w-]+)\s+([\w-]+)\s+(\d+)\s+(\d+)\s+(\w+)\s+"(.*)"\s+([\w\-\.]+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*([\w-]+)\s+([\w-]+)\s+(\d+)\s+(\d+)\s+(\w+)\s+"(.*)"\s+([\w\-\.]+)/) 10 0 10
310  
311 10 my $event = new Net::Peep::Client::Logparser::Event;
312 10 $event->name($1);
313 10 $event->group($2);
314 10 $event->location($3);
315 10 $event->priority($4);
316 10 $event->notification($5);
317 10 $event->regex($6);
318 10 $event->hosts($7);
319
320 10 $self->addEvent($event);
321 10 $self->logger()->debug(1,"\t\t\tClient event [$1] added.");
322  
323 }
324  
325 }
326  
327 ▶ } elsif (@version && $version[0] >= 0 && $version[1] >= 4 && $version[2] > 1) {
Condition0%0/9
@version and $version[0] >= 0 and $version[1] >= 4 and $version[2] > 1
!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] > 1
@version $version[0] >= 0 $version[1] >= 4 $version[2] > 1
Truth table0%0/5
A and B and C and D
A= @versionB= $version[0] >= 0C= $version[1] >= 4D= $version[2] > 1
ABCDresult
0XXX0
10XX0
110X0
11100
11111
328  
329 0 ▶ while (my $line = shift @events) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = shift @events) 0 0 0
330 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
331  
332 0 my $name;
333 0 ▶ if ($line =~ /^\s*([\w-]+)\s+([\w-]+)\s+([a-zA-Z])\s+(\d+)\s+(\d+)\s+"(.*)"/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*([\w-]+)\s+([\w-]+)\s+([a-zA-Z])\s+(\d+)\s+(\d+)\s+"(.*)"/) 0 0 0
334  
335 0 my $event = new Net::Peep::Client::Logparser::Event;
336 0 $event->name($1);
337 0 $event->group($2);
338 0 $event->letter($3);
339 0 $event->location($4);
340 0 $event->priority($5);
341 0 $event->regex($7);
342
343 0 $self->addEvent($event);
344 0 $self->logger()->debug(1,"\t\t\tClient event [$1] added.");
345  
346 }
347  
348 }
349  
350 } else {
351  
352 0 ▶ while (my $line = shift @events) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = shift @events) 0 0 0
353 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
354
355 0 my $name;
356 0 ▶ if ($line =~ /([\w-]+)\s+([a-zA-Z])\s+(\d+)\s+(\d+)\s+"(.*)"/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /([\w-]+)\s+([a-zA-Z])\s+(\d+)\s+(\d+)\s+"(.*)"/) 0 0 0
357  
358 0 my $event = new Net::Peep::Client::Logparser::Event;
359 0 $event->name($1);
360 0 $event->letter($3);
361 0 $event->location($4);
362 0 $event->priority($5);
363 0 $event->regex($7);
364
365 0 $self->addEvent($event);
366 0 $self->logger()->debug(1,"\t\t\tClient event [$1] added.");
367  
368 }
369  
370 }
371  
372 }
373  
374 1 return @text;
375  
376 } # end sub parse
377  
378 10 ▶sub addEvent {
Subroutine addEvent: called CC=2 SCAR=7.0
Pod undocumented
379  
380 10 my $self = shift;
381 10 ▶ my $event = shift || confess "Cannot add logparser event: No event was provided.";
Condition50%1/2
shift() || confess('Cannot add logparser event: No event was provided.')
l!l
100
MC/DC0%0/1
shift() || confess('Cannot add logparser event: No event was provided.')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
382 10 push @{$self->{'EVENTS'}}, $event;
383 10 return 1;
384  
385 } # end sub addEvent
386  
387 0 ▶sub events {
Subroutine events: not called CC=2 SCAR=17.9
Pod undocumented
388  
389 # return an array of events identified by calls to the event
390 # method
391  
392 0 my $self = shift;
393 0 ▶ return wantarray ? @{$self->{'EVENTS'}} : $self->{'EVENTS'};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
394  
395 } # events
396  
397 2 1;
398  
399 __END__
« blib/lib/Net/Peep/Notifier.pm ↑ Summary blib/lib/Net/Peep/Mail.pm »