blib/lib/MRTG/Parse.pm untested

filter: Statementstmt 0.0% 0 / 133 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 211 CC 21 SCAR 48.4
CC
21
Cov
0%
parser
210.0
traffic_output
72.0
mrtg_parse
2.0
CRAP 126.0
1 #############################################################################
2 # MRTG::Parse - v0.03 #
3 # #
4 # This module parses and utilizes the logfiles produced by MRTG #
5 # A full documentation is attached to this sourcecode in POD format. #
6 # #
7 # Copyright (C) 2005 Mario Fuerderer <mario@codehack.org> #
8 # #
9 # This library is free software; you can redistribute it and/or #
10 # modify it under the terms of the GNU Lesser General Public #
11 # License as published by the Free Software Foundation; either #
12 # version 2.1 of the License, or (at your option) any later version. #
13 # #
14 # This library is distributed in the hope that it will be useful, #
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU #
17 # Lesser General Public License for more details. #
18 # #
19 # You should have received a copy of the GNU Lesser General Public #
20 # License along with this library; if not, write to the Free Software #
21 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #
22 # #
23 # Mario Fuerderer <mariof@cpan.org) #
24 # #
25 #############################################################################
26  
27 package MRTG::Parse;
28  
29 use 5.006;
30 use strict;
31 use warnings;
32 use Time::Local;
33  
34 require Exporter;
35  
36 our @ISA = qw(Exporter);
37 our @EXPORT = qw(mrtg_parse);
38  
39 our $VERSION = '0.03';
40  
41  
42 ###########################################
43 # This subroutine receives the needed #
44 # arguments and passes them to the main #
45 # parser() subroutine. #
46 ###########################################
47 sub mrtg_parse {
48 my $logfile = $_[0];
49 my $period = $_[1];
50 my $unit = $_[2];
51  
52 # Start the parser and save total incoming and outgoing bytes to @array_traffic_total
53 my @array_traffic_total = &parser($logfile, $period);
54 # Convert the bytes into the right unit (you may call it "prefix multiplier").
55 my @array_traffic_total_unit_in = &traffic_output($array_traffic_total[0], $unit);
56 my @array_traffic_total_unit_out = &traffic_output($array_traffic_total[1], $unit);
57 my @array_traffic_total_unit_sum = &traffic_output($array_traffic_total[0] + $array_traffic_total[1], $unit);
58  
59 my @return;
60 push(@return, join(' ', @array_traffic_total_unit_in));
61 push(@return, join(' ', @array_traffic_total_unit_out));
62 push(@return, join(' ', @array_traffic_total_unit_sum));
63  
64 return @return;
65 }
66  
67  
68 ###########################################
69 # Our main subroutine, which parses the #
70 # logfile with the values passed through #
71 # mrtg_parse(). #
72 ###########################################
73 sub parser {
74 my $logfile = $_[0];
75 my $period = $_[1];
76  
77 my $parse_type;
78 my $start_date;
79 my $end_date;
80 my $start_epoch;
81 my $end_epoch;
82 my $weekday;
83 my $month;
84 my $day;
85 my $time;
86 my $year;
87  
88 # Decide whether we should parse for static time period...
89 if ($period eq "day" || $period eq "month" || $period eq "year") {
90 ($weekday, $month, $day, $time, $year) = split(/ +/, localtime(time()));
91 $parse_type = "static";
92 # ...or an individual one.
93 } elsif ($period =~ /\d{8}-\d{8}/) {
94 ($start_date, $end_date) = split(/-/, $period);
95  
96 unless ($start_date - $end_date =~ /-/) {
97 die("Error: time periods are not in the right order!\n");
98 }
99
100 # Calculate start and end date to seconds since the system epoch
101 $start_date =~ /(\d{4})(\d{2})(\d{2})/;
102 my $start_year = $1;
103 my $start_month = $2;
104 my $start_day = $3;
105 $start_epoch = timelocal("00", "00", "00", $start_day, $start_month-1, $start_year);
106  
107 $end_date =~ /(\d{4})(\d{2})(\d{2})/;
108 my $end_year = $1;
109 my $end_month = $2;
110 my $end_day = $3;
111 $end_epoch = timelocal("00", "00", "00", $end_day, $end_month-1, $end_year);
112  
113 $parse_type = "individual";
114 } else {
115 die("Error: time period is not present or in an unkown format!\n");
116 }
117  
118 open(LOG, "$logfile") or die "Error: Could not open MRTG-Logfile ($logfile)";
119  
120 # Set any counter to zero before we start parsing.
121 my $traffic_total_in = 0;
122 my $traffic_total_out = 0;
123 my $last_date = 0;
124 my $counter = 0;
125  
126 while (my $line=<LOG>) {
127 # We want to ignore the first line, because it's just the sum of the rest.
128 unless ($counter == 0) {
129 my $date;
130 my @column = split(/\s+/, $line);
131 # Build the match-pattern
132 my $pattern;
133 if ($parse_type eq "static") {
134 $date = scalar localtime($column[0]);
135 if ($period eq "day") {
136 $pattern = ".+ $month.+$day .+$year";
137 } elsif($period eq "month") {
138 $pattern = ".+$month.+$year";
139 } elsif($period eq "year") {
140 $pattern = ".+$year";
141 }
142 # Check if the current line matches out pattern.
143 if ($date =~ /^$pattern$/) {
144 my $traffic_in = $column[1];
145 my $traffic_out = $column[2];
146
147 my $time_range;
148
149 unless ($last_date == 0) {
150 # Calculate the time difference between the current and the previously processd line.
151 $time_range = $last_date - $column[0];
152 } else {
153 # Set time range to zero if it's the first line.
154 $time_range = 0;
155 }
156
157 # Multiply the bytes with the time range.
158 $traffic_total_in += $traffic_in * $time_range;
159 $traffic_total_out += $traffic_out * $time_range;
160
161 # Set the $last_date variable to the new value.
162 $last_date = $column[0];
163 }
164 } elsif ($parse_type eq "individual") {
165
166 $date = $column[0];
167  
168 if (($date > $start_epoch) && ($date < $end_epoch)) {
169 my $traffic_in = $column[1];
170 my $traffic_out = $column[2];
171  
172 my $time_range;
173  
174 unless ($last_date == 0) {
175 $time_range = $last_date - $column[0];
176 } else {
177 $time_range = 0;
178 }
179  
180 $traffic_total_in += $traffic_in * $time_range;
181 $traffic_total_out += $traffic_out * $time_range;
182  
183 $last_date = $column[0];
184 }
185
186 }
187 }
188 $counter++;
189 }
190  
191 my @array;
192 push(@array, $traffic_total_in);
193 push(@array, $traffic_total_out);
194 return @array;
195  
196 close LOG;
197 }
198  
199  
200 ###########################################
201 # This is just to get the right unit... #
202 ###########################################
203 sub traffic_output {
204 my $bytes = $_[0];
205 my $desired_unit = $_[1];
206
207 my @array;
208 my $unit;
209 my $total_count;
210  
211 my @unit_array = qw(B KB MB GB TB);
212 my $temp_unit = $bytes;
213 my $counter = 0;
214 my $check = "false";
215  
216 # Run through the @unit_array
217 foreach my $unit_entry (@unit_array) {
218 # We don't need to divide for the first entry, because it's already in byte
219 unless ($counter == 0) {
220 $temp_unit = ($temp_unit / 1024);
221 unless ($desired_unit) {
222 # If our value is lower than 1, we should stop here in order to retain an adequate unit.
223 if ($temp_unit < 1) {
224 last;
225 }
226 }
227 }
228  
229 $total_count = $temp_unit;
230 $unit = $unit_entry;
231  
232 if ($desired_unit) {
233 if ($desired_unit eq $unit_entry) {
234 $check = "true";
235 last;
236 }
237 }
238  
239 $counter++;
240 }
241
242 push(@array, $total_count);
243 push(@array, $unit);
244 if ($desired_unit && $check eq "true") {
245 return @array;
246 } elsif (defined($desired_unit) && $check ne "true") {
247 die("Erorr: $desired_unit is a non valid unit!\n");
248 } else {
249 return @array;
250 }
251 }
252  
253  
254 __END__