Debian Codegraph @

bin/mrtg-traffic-sum

raw
1 #!/usr/bin/perl -w
2
3 # Changes in revision 1.9
4
5 # Added an option for --range, which may be "current" for month-to-date or "previous" for last month
6 # Changed default date range to 00:00:00 on 1st of month to 23:59:59 at end of month
7 # Changed the default units to MB, more relevant to non-datacenter users :-)
8 # Added the option for units=GB, to allow for previous behaviour of this script
9 # Changed default minimum to 0, to report any info that is available by default
10 # Updated help section with the above changes, fixed a couple of typos
11
12
13 use strict;
14 use Getopt::Long;
15 use Pod::Usage;
16
17 BEGIN {
18 # Automatic OS detection ... do NOT touch
19 if ( $^O =~ /^(?:(ms)?(dos|win(32|nt)?))/i ) {
20 $main::OS = 'NT';
21 $main::SL = '\\';
22 $main::PS = ';';
23 } elsif ( $^O =~ /^VMS$/i ) {
24 $main::OS = 'VMS';
25 $main::SL = '.';
26 $main::PS = ':';
27 } else {
28 $main::OS = 'UNIX';
29 $main::SL = '/';
30 $main::PS = ':';
31 }
32 }
33
34 use FindBin;
35 use lib "${FindBin::Bin}";
36 use lib "${FindBin::Bin}${main::SL}..${main::SL}lib${main::SL}mrtg2";
37 use MRTG_lib "2.090017";
38 use POSIX qw(mktime);
39
40 '$Revision: 1.9 $ ' =~ /Revision: (\S*)/;
41 my $Revision = $1;
42
43 my $sendmail = "/usr/lib/sendmail";
44 $sendmail = "/usr/sbin/sendmail" if -x "/usr/sbin/sendmail";
45
46 # main loop
47 my %opt = (
48 'catch' => '(?:description|port name):</td>\s*<td>\s*([^<\s]+[^<]*?)\s*</td',
49 # 'catch' => 'td>\s*([^<\s]+COLO[^<]*?)\s*</td',
50 'min' => 0,
51 'range' => "previous",
52 'units' => "MB"
53 );
54 sub walklog ($$$$$);
55 sub sumlog($$$);
56 sub mailout($$);
57
58 sub main()
59 {
60 # parse options
61 GetOptions(\%opt, 'min=i','help|h', 'catch=s', 'email=s','version','range=s','units=s','man') or exit(1);
62 if($opt{help}) { pod2usage(1) }
63 if($opt{man}) { pod2usage(-exitstatus => 0, -verbose => 2) }
64 if($opt{version}) { print "mrtg-traffic-sum $Revision\n"; exit(0) }
65 my $cfgfile = shift @ARGV;
66 pod2usage(1) unless $cfgfile and -r $cfgfile;
67 my %rcfg;
68 my %cfg;
69 my @routers;
70 my @target;
71 my $report = "Subject: Traffic total for '$cfgfile' ($Revision) ";
72 readcfg($cfgfile,\@routers,\%cfg,\%rcfg);
73 cfgcheck(\@routers, \%cfg, \%rcfg, \@target);
74 walklog \@routers, \%cfg, \%rcfg,\$report,\%opt;
75 if ($opt{email}) {
76 mailout \$report,$opt{email};
77 } else {
78 print $report;
79 }
80 }
81
82 main;
83
84 sub walklog ($$$$$){
85 my $routers = shift;
86 my $cfg = shift;
87 my $rcfg = shift;
88 my $report = shift;
89 my $opt = shift;
90 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = localtime(time);
91 my $start = mktime(0, 0, 0, 1, $mon-1, $year, 0, 0, 0);
92 my $end = mktime(0, 0, 0, 1, $mon, $year, 0, 0, 0)-1;
93 if ($opt{range} eq "previous") {
94 $start = mktime(0, 0, 0, 1, $mon-1, $year, 0, 0, 0);
95 $end = mktime(0, 0, 0, 1, $mon, $year, 0, 0, 0)-1;
96 }
97 if ($opt{range} eq "current") {
98 $start = mktime(0, 0, 0, 1, $mon, $year, 0, 0, 0);
99 $end = mktime(0, 0, 0, 1, $mon+1, $year, 0, 0, 0)-1;
100 }
101
102 my($m,$y) = (localtime($start))[4,5];
103 $y += 1900;
104 $m ++;
105 $$report .= sprintf "%04d/%02d\n\n", $y,$m;
106
107 $$report .= "Start: ".localtime($start)."\n";
108 $$report .= "End: ".localtime($end)."\n";
109 if ($opt{'units'} eq "GB") {
110 $$report .= sprintf "%-40s %15s\n","Interface","In+Out in GB";
111 } else {
112 $$report .= sprintf "%-40s %15s\n","Interface","In+Out in MB";
113 }
114 $$report .= ("-" x 78)."\n";
115 foreach my $r (@$routers) {
116 my $label;
117 if ($rcfg->{pagetop}{$r} =~ /$opt{catch}/i ) {
118 $label = $1;
119 $label .= " $2" if $2;
120 my ($in,$out) = sumlog $cfg->{logdir}.$rcfg->{directory}{$r}."$r.log",$start,$end;
121 my $sum = ($in+$out)/(10**6);
122 if ($opt{'units'} eq "GB") {
123 $sum = ($in+$out)/(10**9);
124 } else {
125 $sum = ($in+$out)/(10**6);
126 }
127 next if $sum < $opt{'min'};
128 $$report .= sprintf "%-40s %15.0f\n",$label,$sum;
129 }
130 }
131 }
132
133 sub sumlog ($$$) {
134 my $log = shift;
135 my $start = shift;
136 my $end = shift;
137 my $in = 0.0;
138 my $out = 0.0;
139 if (open L, "<",$log){
140 my @this=($end+1,0,0);
141 my @prev;
142 while (<L>) {
143 chomp;
144 @prev = @this;
145 @this = (split /\s+/)[0,1,2];
146
147 if ($prev[0] <= $end) {
148 my $diff = $prev[0] - $this[0];
149 $in += $diff * $prev[1];
150 $out += $diff * $prev[2];
151 }
152 last if $this[0] <= $start;
153 }
154 close L;
155 } else {
156 warn "WARN Skipping $log\n";
157 }
158 return $in,$out;
159 }
160
161 sub mailout ($$) {
162 my $data = shift;
163 my $rec = shift;
164 open S, "|-" or exec $sendmail,'-f',$rec,$rec;
165 print S <<MESSAGE;
166 From: $rec
167 To: $rec
168 $$data
169 MESSAGE
170 close S;
171
172 }
173 1
174
175 __END__
176
177 =head1 NAME
178
179 mrtg-traffic-sum - Builds monthly traffic summary from mrtg log files
180
181 =head1 SYNOPSIS
182
183 B<mrtg-traffic-sum> [I<options>...] B<config-file>
184
185 --man show man-page and exit
186 -h, --help display this help and exit
187 --version output version information and exit
188 --catch=regexp filter out things that match this in PageTop
189 --email=address email the result
190 --min=gigabytes minimal number of gigabites required for report
191 --range=<when> Specify "current" for month-to-date, "previous" for last complete month
192 --units=[GB|MB] Display results in gigabytes (default is MB)
193
194 =head1 DESCRIPTION
195
196 The mrtg-traffic-sum goes through the mrtg logs for the targets in the
197 the config file specified and builds the traffic total for the last
198 month in Giga Bytes. (Note in communications Giga is 10^9).
199
200 The result of this analysis can be sent via email to an address of
201 your choice using the B<--email> option.
202
203 With the B<--catch> option you can specify a regular expression which
204 will be matched against the contents of the PageTop settings. The
205 regular expression should return a value into $1 and possibly into
206 $2. This will then be used as description for the appropriate traffic
207 sum. By default the catch is set to:
208
209 (?:description|port name):</td>\s*<td>\s*([^< ]*?[^<]*?)\s*</td
210
211 By default, all traffic which is over one megabyte gets reported. Use the
212 B<--min> switch to change this number.
213
214 =head1 COPYRIGHT
215
216 Copyright (c) 2002 by Tobias Oetiker. All rights reserved.
217
218 =head1 LICENSE
219
220 This program is free software; you can redistribute it and/or modify
221 it under the terms of the GNU General Public License as published by
222 the Free Software Foundation; either version 2 of the License, or
223 (at your option) any later version.
224
225 This program is distributed in the hope that it will be useful,
226 but WITHOUT ANY WARRANTY; without even the implied warranty of
227 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
228 GNU General Public License for more details.
229
230 You should have received a copy of the GNU General Public License
231 along with this program; if not, write to the Free Software
232 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
233
234 =head1 AUTHOR
235
236 Tobias Oetiker E<lt>[email protected]<gt>,
237 Keith Dunnett E<lt>[email protected]<gt>
238
239 =head1 HISTORY
240
241 2002-07-13 to Initial Version
242 2009-12-38 kd New features
243
244 =cut
245
246 # Emacs Configuration
247 #
248 # Local Variables:
249 # mode: cperl
250 # eval: (cperl-set-style "PerlStyle")
251 # mode: flyspell
252 # mode: flyspell-prog
253 # End:
254 #
255 # vi: sw=4 et