File Coverage

lib/RTG/Report.pm
Criterion Covered Total %
statement 42 124 33.8
branch 20 86 23.2
condition 2 15 13.3
subroutine 8 11 72.7
pod 8 8 100.0
total 80 244 32.7


line stmt bran cond sub pod time code
1             package RTG::Report;
2              
3 6     6   129565 use strict;
  6         16  
  6         233  
4 6     6   28 use warnings;
  6         12  
  6         293  
5              
6             our $VERSION = '1.17';
7              
8             # built-in modules
9 6     6   5429 use English qw( -no_match_vars );
  6         26469  
  6         40  
10              
11             sub new {
12 5     5 1 3862 my ( $class, $name ) = @_;
13 5         20 my $self = { name => $name };
14 5         15 bless( $self, $class );
15 5         149 return $self;
16             };
17              
18             sub formatted_header {
19              
20 0     0 1 0 my $self = shift;
21 0         0 my ($report_units, $no_rates, $utilization, $no_95th) = @_;
22              
23 0 0       0 my $unit = $report_units == 1000 ? 'Kbytes'
    0          
    0          
24             : $report_units == 1000000 ? 'Mbytes'
25             : $report_units == 1000000000 ? 'Gbytes'
26             : '?bytes';
27              
28 0         0 my @headers = (
29             'Pod', 'RID', 'Router',
30             'IID', 'Connection/Interface', 'Speed', 'Description',
31             "In $unit",
32             "Out $unit",
33             );
34              
35             # redefine $unit b/c every use of unit below here is rate
36 0 0       0 $unit = $report_units == 1000 ? 'Kbit/s'
    0          
    0          
37             : $report_units == 1000000 ? 'Mbit/s'
38             : $report_units == 1000000000 ? 'Gbit/s'
39             : '?bit/s';
40              
41 0 0       0 if ( !$no_rates ) {
42 0         0 push @headers,
43             "Avg In $unit",
44             "Avg Out $unit",
45             "Max In $unit",
46             "Max Out $unit";
47             };
48              
49 0 0       0 if ( $utilization ) {
50 0         0 push @headers,
51             'Util Avg In %',
52             'Util Avg Out %',
53             'Util Max In %',
54             'Util Max Out %';
55             }
56              
57 0 0       0 if ( !$no_95th ) { push @headers, "95th In $unit", "95th Out $unit"; };
  0         0  
58              
59 0         0 push @headers, 'Start Date', 'End Date';
60              
61 0         0 return @headers;
62             };
63              
64             sub get_interface_stats {
65              
66 0     0 1 0 my $self = shift;
67 0         0 my $args_ref = shift;
68            
69 0         0 my $db = $args_ref->{'db'};
70 0         0 my $table = $args_ref->{'table'};
71 0         0 my $range = $args_ref->{'range'};
72 0         0 my $iid = $args_ref->{'iid'};
73 0         0 my $debug = $args_ref->{'debug'};
74              
75 0         0 my $query="SELECT counter, UNIX_TIMESTAMP(dtime) AS unix_time
76             FROM $table
77             WHERE $range
78             AND id=$iid";
79             # ORDER BY dtime"; # sort in perl, it is faster
80              
81 0         0 my $db_err = 0;
82 0 0       0 my @rows = $db->query($query)->hashes or $db_err++;
83 0 0       0 if ( $db_err ) {
84 0 0       0 print "no results found $table for id=$iid from $range\n" if $debug;
85             #warn $db->error;
86 0 0       0 warn "query: $query\n" if $debug;
87 0         0 return { rate_peak=>0,rate_avg=>0,rate_95th=>0,bytes=>0};
88             };
89              
90 0         0 my ($bytes_total, $last_timestamp, $num_rate_samples,
91             $rate_avg_total, $rate_95th, $rate_avg, $rate_peak, @rate );
92 0         0 $bytes_total = $rate_peak = $rate_avg = $rate_95th = $last_timestamp = 0;
93              
94 0         0 foreach my $row ( sort { $a->{'unix_time'} <=> $b->{'unix_time'} } @rows ) {
  0         0  
95              
96 0         0 my $counter = $row->{'counter'};
97 0         0 my $timestamp = $row->{'unix_time'};
98              
99 0         0 $self->timestamp_sanity($last_timestamp, $timestamp, $table, $iid, $counter);
100              
101 0 0       0 if ( $timestamp == $last_timestamp ) {
102             #warn "** duplicate timestamps: ".
103             # "table: $table id: $iid count: $counter ts: $timestamp lts: $last_timestamp\n";
104 0         0 $bytes_total += $counter;
105 0         0 next;
106             };
107              
108 0         0 $bytes_total += $counter;
109              
110             # to calculate rates, we must have an interval. Skip the first row of
111             # data since we have no start time to calculate against.
112 0 0       0 if ( $last_timestamp == 0 ) { $last_timestamp = $timestamp; next; };
  0         0  
  0         0  
113              
114 0         0 $num_rate_samples++;
115 0         0 my $counter_bits = $counter * 8; # convert octets/bytes to bits
116              
117 0         0 my $interval = $timestamp - $last_timestamp; # the interval is not fixed
118 0         0 my $rate_cur = $counter_bits/$interval; # calc the rate of this sample
119 0         0 push @rate, $rate_cur;
120 0         0 $rate_avg_total += $rate_cur;
121 0 0       0 if ($rate_cur > $rate_peak) { $rate_peak = $rate_cur; } # calc max rate
  0         0  
122              
123 0         0 $last_timestamp = $timestamp;
124             }
125 0 0       0 warn "There were $num_rate_samples rate samples in the period.\n" if $debug;
126              
127             # calculate 95th percentile
128 0 0       0 if ( $num_rate_samples ) {
129 0         0 @rate = sort { $a <=> $b } @rate;
  0         0  
130 0         0 $rate_95th = @rate[ int( $num_rate_samples * 0.95 + 0.5) ];
131             };
132              
133             # calculate average rate
134 0 0 0     0 if ( $num_rate_samples && $num_rate_samples !=0 ) {
135 0         0 $rate_avg = sprintf("%.0f", $rate_avg_total/$num_rate_samples);
136             };
137              
138             return {
139 0         0 bytes => $bytes_total,
140             rate_peak => sprintf("%.0f", $rate_peak),
141             rate_avg => $rate_avg,
142             rate_95th => $rate_95th,
143             };
144             }
145              
146             sub get_the_date {
147              
148 0     0 1 0 my ($self, $bump) = @_;
149              
150 0         0 my $time = time;
151             # warn "time: " . time . "\n" if $debug;
152              
153 0 0       0 $bump = $bump ? $bump * 86400 : 0;
154 0         0 my $offset_time = time - $bump;
155             # warn "selected time: $offset_time\n" if $debug;
156              
157             # load Date::Format to get the time2str function
158 0         0 eval { require Date::Format };
  0         0  
159 0 0       0 if ( ! $EVAL_ERROR) {
160              
161 0         0 my $ss = Date::Format::time2str( "%S", ( $offset_time ) );
162 0         0 my $mn = Date::Format::time2str( "%M", ( $offset_time ) );
163 0         0 my $hh = Date::Format::time2str( "%H", ( $offset_time ) );
164 0         0 my $dd = Date::Format::time2str( "%d", ( $offset_time ) );
165 0         0 my $mm = Date::Format::time2str( "%m", ( $offset_time ) );
166 0         0 my $yy = Date::Format::time2str( "%Y", ( $offset_time ) );
167 0         0 my $lm = Date::Format::time2str( "%m", ( $offset_time - 2592000 ) );
168              
169             # warn "get_the_date: $yy/$mm/$dd $hh:$mn\n" if $debug;
170 0         0 return $dd, $mm, $yy, $lm, $hh, $mn, $ss;
171             }
172              
173 0         0 die "Date::Format is not installed!\n";
174             }
175              
176             sub is_arrayref {
177              
178 6     6 1 20 my ( $self, $should_be_arrayref, $debug ) = @_;
179              
180 6         9 my $error;
181              
182 6 50       21 return if ! defined $should_be_arrayref;
183              
184 6         10 eval {
185             # simply accessing it will generate an exception.
186 6 50       25 if ( $should_be_arrayref->[0] ) {
187 5 50       17 print "is_arrayref is a arrayref!\n" if $debug;
188             }
189             };
190 6 100       41 $@ ? return : return 1;
191             }
192              
193             sub should_i_skip_it {
194              
195 2     2 1 17 my ($self, $if_desc, $if_name, $skip_desc, $skip_name) = @_;
196              
197             ##### Due to use of Config::Std #####
198             # if one description is present in the config file, we get a string.
199             # If more than one is present, we get an arrayref
200              
201 2 50       8 if ( $skip_desc ) {
202 2 50       8 if ( $self->is_arrayref($skip_desc) ) {
203 2         16 foreach my $desc ( @$skip_desc ) {
204 2 50 33     7 return 1 if ( $desc eq 'blank' && $if_desc eq '' );
205 2 50       9 return 1 if ( $desc eq $if_desc );
206             }
207             }
208             else {
209 0 0 0     0 return 1 if ( $skip_desc eq 'blank' && $if_desc eq '' );
210 0 0       0 return 1 if ( $skip_desc eq $if_desc );
211             }
212             };
213              
214 2 50       13 if ( $skip_name ) {
215 2 50       5 if ( $self->is_arrayref($skip_name) ) {
216 2         4 foreach my $name ( @$skip_name ) {
217 3 50 33     10 return 1 if ( $name eq 'blank' && $if_name eq '' );
218 3 100       45 return 1 if $if_name =~ /$name/i;
219             }
220             }
221             else {
222 0 0 0     0 return 1 if ( $skip_name eq 'blank' && $if_name eq '' );
223 0 0       0 return 1 if ( $skip_name eq $if_name );
224             }
225             };
226 1         8 return;
227             };
228              
229             sub status {
230 1     1 1 5 my ($self, $mess, $nocr, $debug) = @_;
231              
232 1 50       4 if ($debug) {
233 0         0 print $mess;
234 0 0       0 print "\n" unless $nocr;
235             }
236 1 50       9 return $nocr ? $mess : "$mess\n";
237             };
238              
239             sub timestamp_sanity {
240              
241 2     2 1 14 my ($self, $last_timestamp, $timestamp, $table, $iid, $counter) = @_;
242              
243             # timestamps should never go backwards, we sorted them
244 2 100       7 if ( $last_timestamp > $timestamp ) {
245 1         247 print "* Bad Sample " .
246             "table: $table id: $iid count: $counter ts: $timestamp lts: $last_timestamp\n";
247 1         25 die "The last timestamp ($last_timestamp) is newer than the current one($timestamp)!\n";
248             };
249              
250 1 50       5 if ( $timestamp - $last_timestamp < 0 ) {
251 0         0 die "timestamp value ($timestamp) wandered into the forest alone! Report this error!\n";
252             };
253              
254 1 50       6 if ( $counter < 0 ) {
255 0         0 die "** Invalid Counter Value $counter\n" .
256             "table: $table id: $iid count: $counter ts: $timestamp lts: $last_timestamp\n";
257             # print "*** stmt: $query\n";
258             }
259              
260 1         6 return 1;
261             };
262              
263              
264             1;
265             __END__;