File Coverage

blib/lib/SNMP/LogParserDriver.pm
Criterion Covered Total %
statement 12 53 22.6
branch 0 20 0.0
condition n/a
subroutine 4 15 26.6
pod 11 11 100.0
total 27 99 27.2


line stmt bran cond sub pod time code
1             #
2             # $Id: LogParserDriver.pm,v 1.3 2006/09/25 06:28:44 nito Exp $
3             #
4             # Simple class that counts he number of lines of a file
5             #
6             # Nito@Qindel.ES -- 7/9/2006
7             package SNMP::LogParserDriver;
8              
9 1     1   1167 use warnings;
  1         2  
  1         37  
10 1     1   6 use strict;
  1         3  
  1         24  
11 1     1   1326 use Log::Log4perl qw(:easy);;
  1         61256  
  1         7  
12 1     1   1699 use Data::Dumper;
  1         6304  
  1         568  
13              
14             =head1 NAME
15              
16             SNMP:LogParserDriver
17              
18             =head1 SYNOPSIS
19              
20             This is base class for the usage of the logparser
21             parsing. Usually you will implement a subclass of
22             this class implementing the following methods:
23              
24             =over 8
25              
26             =item * new
27              
28              
29             You can define whatever new parameters you want
30             in this method, but usually only the object variable
31             B (the regular expression to match in the file)
32             is needed
33              
34             =item * evalBegin
35              
36              
37             This is an extract of code which initializes all the variables
38             that you might need during the run of logparser
39              
40             =item * evalIterate
41              
42              
43             This method will be invoked for each line of the log file.
44              
45             =item * evalEnd
46              
47              
48             This method usually records in the B hash reference
49             the key, value pairs that you want ot record in the output.
50              
51             =back
52              
53             =head1 DESCRIPTION
54              
55             For a more detailed description on how to use this class
56             please check B.
57              
58             This document is a detailed reference of the different
59             methods provided in this class
60              
61             =head1 METHODS
62              
63             =head2 new
64              
65             Receives (at the moment) no arguments. It creates an object
66             of SNMP::LogParserDriver
67              
68             =cut
69              
70             # Class constructor
71             sub new {
72 0     0 1   my $class = shift;
73 0           my $self = {};
74 0           $self = {
75             name => undef,
76             logfile => undef,
77             pattern => undef,
78             savespace => undef,
79             workspace => undef,
80             properties => undef,
81             logger => undef
82             };
83 0           bless ($self, $class);
84 0           $self->name($class);
85 0           $self->pattern('^.*$');
86 0 0         if(!(Log::Log4perl->initialized())) {
87 0           Log::Log4perl->easy_init($ERROR);
88             }
89 0           $self->logger(Log::Log4perl->get_logger("logparser.$class"));
90 0           $self->{logger}->info("Created object of class $class");
91 0           return $self;
92             }
93              
94             =head2 name
95              
96             Permits to retrieve or set the name of the object as in
97              
98             print "name: ".$self->name;
99              
100             or
101              
102             $self->name("myname");
103              
104             =cut
105              
106             # The name of this object
107             sub name {
108 0     0 1   my $self = shift;
109 0 0         $self->{name} = shift if (@_);
110 0           return $self->{name};
111             }
112              
113             =head2 logfile
114              
115             Permits to retrieve or set the logfile of the object as in
116              
117             print "logfile: ".$self->logfile;
118              
119             or
120              
121             $self->logfile("/var/log/logfile");
122              
123             =cut
124              
125             # The log file that should be parsed
126             sub logfile {
127 0     0 1   my $self = shift;
128 0 0         $self->{logfile} = shift if (@_);
129 0           return $self->{logfile};
130             }
131              
132             =head2 pattern
133              
134             Permits to retrieve or set the regular expression pattern of the object as in
135              
136             print "pattern: ".$self->pattern;
137              
138             or
139              
140             $self->pattern("^myregex$");
141              
142             =cut
143              
144             # The regular expression that should be matched
145             sub pattern {
146 0     0 1   my $self = shift;
147 0 0         $self->{pattern} = shift if (@_);
148 0           return $self->{pattern};
149             }
150              
151             =head2 savespace
152              
153             Permits to retrieve or set the savespace of the object as in
154              
155             print "savespace: ".Dumper($self->savespace);
156              
157             or
158              
159             $self->savespace(\%mysavespace);
160              
161             The savespace is usually a reference to a hash file. The
162             value of the savespace variable will be restored in the beginning
163             of each invocation of B and saved at the end of the
164             invocation. Tipically things like counters are saved in each
165             invocation of the savespace.
166              
167             =cut
168              
169             # The savespace that should be saved across sessions
170             sub savespace {
171 0     0 1   my $self = shift;
172 0 0         $self->{savespace} = shift if (@_);
173 0           return $self->{savespace};
174             }
175              
176             =head2 workspace
177              
178             Permits to retrieve or set a variable that will be maintained across iterative parsings
179             of the log, but will not be maintained across logparser invocations
180              
181             print "workspace: ".Dumper($self->workspace);
182              
183             or
184              
185             $self->workspace(\%workspace);
186              
187             The workpace is usually a reference to a hash file. The
188             value of the workspace variable will only last for each invocation
189             of B. Tipically things like local variable are saved in
190             in the workspace.
191              
192             =cut
193              
194             # A variable that will be maintained across iterative parsings
195             # of the log, but will not be maintained across logparser invocations
196             sub workspace {
197 0     0 1   my $self = shift;
198 0 0         $self->{workspace} = shift if (@_);
199 0           return $self->{workspace};
200             }
201              
202             =head2 properties
203              
204             Permits to retrieve or set the properties of the object as in
205              
206             print "properties: ".$self->properties;
207              
208             or
209              
210             $self->properties(\%myproperties);
211              
212             The properties of an object are the key/value pairs that will
213             be stored in the B of the B
214             file.
215              
216             =cut
217              
218             # The properties that should be set in the output file
219             sub properties {
220 0     0 1   my $self = shift;
221 0 0         $self->{properties} = shift if (@_);
222 0           return $self->{properties};
223             }
224              
225             =head2 logger
226              
227             Permits to retrieve or set the logger of the object as in
228              
229             print $self->logger->debug("debug message");
230              
231             or
232              
233             $logger = Log::Log4perl->get_logger(LOG_TAG);
234             $self->logger($logger);
235              
236             =cut
237              
238             # Sets the logger object to allow for logging
239             sub logger {
240 0     0 1   my $self = shift;
241 0 0         $self->{logger} = shift if (@_);
242 0           return $self->{logger};
243             }
244              
245              
246             =head2 evalBegin
247              
248             This method is usually overriden in the child class.
249             This method will be invoked before any line of the
250             file is parsed.
251              
252             Typically counters will be initialised here if they
253             have not been initialised before:
254              
255             $self->{savespace}{counter} = 0 if (!exists($self->{savespace}{counter}));
256              
257             =cut
258              
259             # This will be invoked before the first parsing of the log
260             sub evalBegin {
261 0     0 1   my $self = shift;
262 0 0         $self->{savespace}{counter} = 0 if (!exists($self->{savespace}{counter}));
263             }
264              
265             =head2 evalIterate
266              
267             This method is usually overriden in the child class.
268             This method will be invoked for each line of the
269             log that should be parsed.
270              
271             It receives as an argument the line of the log to be parsed.
272              
273             One typical implementation could be:
274              
275             sub evalIterate {
276             my $self = shift;
277             my ($line) = @_;
278             my $pattern = $self->{pattern};
279             if ($line =~ /$pattern/) {
280             $self->{savespace}{counter} ++;
281             }
282             }
283              
284              
285             =cut
286              
287             # This will be invoked whenever the pattern matches
288             # the log line parsed
289             # Input:
290             # - The line to be parsed
291             # Output:
292             # - 1 if the line has matched the regular expression and 0 otherwise
293             sub evalIterate {
294 0     0 1   my $self = shift;
295 0           my ($line) = @_;
296 0           my $pattern = $self->{pattern};
297 0 0         if ($line =~ /$pattern/) {
298 0           $self->{savespace}{counter} ++;
299             }
300             }
301              
302             =head2 evalEnd
303              
304             This method is usually overriden in the child class.
305             This method will be invoked after all the lines
306             have been parsed.
307              
308             Typically the properties elements will be initialised here
309              
310             =cut
311              
312             # This will be invoked after the last log line has been parsed
313             sub evalEnd {
314 0     0 1   my $self = shift;
315 0           $self->properties($self->savespace());
316             }
317              
318             1;
319              
320              
321             =head1 REQUIREMENTS AND LIMITATIONS
322              
323             =head1 BUGS
324              
325             =head1 TODO
326              
327             =over 8
328              
329             =item * document logger.
330              
331             =back
332              
333             =head1 SEE ALSO
334              
335             =head1 AUTHOR
336              
337             Nito at Qindel dot ES -- 7/9/2006
338              
339             =head1 COPYRIGHT & LICENSE
340              
341             Copyright 2007 by Qindel Formacion y Servicios SL, all rights reserved.
342              
343             This program is free software; you can redistribute it and/or modify it
344             under the same terms as Perl itself.
345              
346             =cut