jablonka.czprosek.czf

hotsanic

Subversion Repositories:
[/] [trunk/] [modules/] [traffic/] [diagrams.pl] - Blame information for rev 19

 

Line No. Rev Author Line
11simandl#!/usr/bin/env perl
2use strict;
3use warnings;
4use diagnostics;
5 
6use lib "../../lib";
7use HotSaNICparser;
8use HotSaNICdiagram;
9use RRDs;
10 
11# read global settings
12#
13my $MODNAME=HotSaNICparser::get_module_name();
14my %CONFIG=HotSaNICparser::get_config("../..");
15 
16my $OUTDIR=$CONFIG{"WEBDIR"}."/".lc $MODNAME;
17if ( ! -e $OUTDIR ) { mkdir "$OUTDIR",0755; }
18 
19my $IMGFMT=lc $CONFIG{"IMAGEFORMAT"};
20my $TEMPNAME="$OUTDIR/temp.$IMGFMT";
21 
22my (@ITEMS,$dbfile,$device,$maxin,$maxout,$name,$IOMIN,$range,$descr,$file,$build,$fullrange);
23my $SWAPIO=1;
24my $SCALE=-1000;
25my @FEATURES=();
26my $FORCE=@ARGV;
27 
28my $COLOR_IN_LINE="00a000";
29my $COLOR_IN_AREA="c0ffc0";
30my $COLOR_OUT_LINE="c00000";
31my $COLOR_OUT_AREA="ffc0c0";
32my $COLOR_BORDER="a0a0a0";
33my $COLOR_MAXSPEED="ff00ff";
34my $COLOR_WRONGDATA="ffffa0";
35 
36my $STYLE="bytes";
37my $STYLETOTAL="bytes";
38my $MULT=1;
39my $MULTTOTAL=1;
40 
41# read module-specific settings
42#
43foreach (HotSaNICparser::read_settings(".")) {
44 my ($var,$value)=HotSaNICparser::parse_line($_);
45 if ($var eq "STYLE") {
46 $STYLE=lc $value;
47 if ($STYLE eq "bits") { $MULT=8; }
48 }
49 if ($var eq "STYLETOTAL") {
50 $STYLETOTAL=lc $value;
51 if ($STYLETOTAL eq "bits") { $MULTTOTAL=8; }
52 }
53 if ($var eq "SWAPIO") {
54 if (index("1 yes true",lc $value) >=0 ) { $SWAPIO=-1; }
55 }
56 if ($var eq "SCALE") { $SCALE=$value; }
57 if ($var eq "DEV") {
58 push @ITEMS,$value;
59 }
60 }
61 
62if ($SCALE eq "") { $SCALE=-1000; }
63if ($SCALE > 0) { push @FEATURES,"--rigid"; }
64 
65my $SWAPOI=-$SWAPIO;
66 
67# generate diagrams
68#
69foreach (@ITEMS) {
70 ($device,$maxin,$maxout)=split /,/;
71 if (index ($device,"SNMP")>=0) {
72 my (undef,$host,$comm,$oid)=split /:/,$device;
73 ($name=$host."_".$oid) =~ s/\./_/g;
74 $device=$host.":".$oid;
75 $dbfile=$host."_".$oid;
76 } else {
77 if (index ($device,"RRCP")>=0) {
78 my (undef,$host,$comm,$port)=split /:/,$device;
79 $host =~ s/\./:/g;
80 ($name=$host."_".$port) =~ s/\./_/g;
81 $name =~ s/:/_/g;
82 $device=$host.":p".$port;
83 $dbfile=$host."_".$port;
84 $dbfile=~ s/\./:/g;
85 
8619simandl } else {
87 $name=$device;
88 $name =~ s/:/_/g;
89 $dbfile=$name;
90 $name =~ s/\./_/g;
91 }
921simandl }
93 # if database exists, create images...
94 print "creating images for $device ...\n";
95 if ( -e "./rrd/$dbfile.rrd") {
96 if ((! defined $maxin) || ($maxin eq "") || ($maxin == 0)) {
97 print " Maximum input speed not set!\n Please check settings-file!\n setting speed to 100 MBit\n";
98 $maxin=12500000;
99 }
100 if ((! defined $maxout) || ($maxout eq "") || ($maxout == 0)) {
101 print " Maximum output speed not set!\n Please check settings-file!\n setting speed to 100 MBit\n";
102 $maxout=12500000;
103 }
104 $maxin*=$MULT;
105 $maxout*=$MULT;
106 $IOMIN=abs($SCALE)*$MULT;
107 
108 foreach $range ("1h","6h","1day","1week","1month","1year") {
109 my ($descr,$file,$build,$fullrange,$DATESTRING)=HotSaNICdiagram::get_diagram_properties($range);
110 if ($range eq "1h") { draw_hourly($dbfile,$name,$device,$descr,$file,$DATESTRING,$STYLE,$STYLETOTAL,$MULT,$MULTTOTAL,$IOMIN,$maxin,$maxout,$IMGFMT); }
111 else { draw_other($range,$dbfile,$name,$device,$descr,$file,$DATESTRING,$build,$fullrange,$STYLE,$STYLETOTAL,$MULT,$MULTTOTAL,$IOMIN,$maxin,$maxout,$IMGFMT); }
112 }
113 
114 print "creating weathermap file for $device ...\n";
115 
116 &rrd2wmwrite($dbfile,"./rrd/",$OUTDIR);
117 
118 }
119 else { print " No database $dbfile found\n Please check if the daemon is running.\n";next; }
120 print "\n";
121 }
122exit 0;
123 
124sub draw_hourly {
125 
126 my($dbfile,$name,$device,$descr,$file,$DATESTRING,$STYLE,$STYLETOTAL,$MULT,$MULTTOTAL,$IOMIN,$maxin,$maxout,$IMGFMT)=@_;
127 
128 my $FILENAME="$OUTDIR/$name-$file.$IMGFMT";
129 
130 my $DATE=time;
131 
132 $MULTTOTAL=3600*$MULTTOTAL;
133 
134# the scaring ;) definition of "wrong1":
135# if TIME < now { if in+out unknown { INF else 0 } else 0 }
136# CDEF:wrong1=TIME,$DATE,LT,maxin,maxout,+,UN,INF,0,IF,0,IF \
137 
138 $name =~ s/:/\\:/g;
139 $dbfile =~ s/:/\\:/g;
140 my ($prints,$xs,$ys)=RRDs::graph $TEMPNAME, "-i", "-b","1024", "-v","$STYLE/s",
141 "-s","-3600", "-w",$CONFIG{"WIDTH"}, "-h",$CONFIG{"HEIGHT"}, "-a", uc($IMGFMT),
142 "-u",$IOMIN, "-l",(-$IOMIN), "--title", "traffic on $device - last hour ($DATESTRING)",@FEATURES,
143 "DEF:dbavgin=rrd/$dbfile.rrd:in:AVERAGE",
144 "DEF:dbmaxin=rrd/$dbfile.rrd:in:MAX",
145 "DEF:dbavgout=rrd/$dbfile.rrd:out:AVERAGE",
146 "DEF:dbmaxout=rrd/$dbfile.rrd:out:MAX",
147 "CDEF:avgin=dbavgin,$MULT,*",
148 "CDEF:maxin=dbmaxin,$MULT,*",
149 "CDEF:totin=dbavgin,$MULTTOTAL,*",
150 "CDEF:avgout=dbavgout,$MULT,*",
151 "CDEF:maxout=dbmaxout,$MULT,*",
152 "CDEF:totout=dbavgout,$MULTTOTAL,*",
153 "CDEF:wrong1=TIME,$DATE,LT,maxin,maxout,+,UN,INF,0,IF,0,IF",
154 "CDEF:wrong2=0,wrong1,-",
155 "CDEF:avgin_draw=avgin,$SWAPIO,*",
156 "CDEF:avgout_draw=avgout,$SWAPOI,*",
157 "AREA:avgin_draw#d0d0d0:",
158 "LINE1:avgin_draw#$COLOR_IN_LINE:input\\g",
159 "GPRINT:avgin:AVERAGE: (avg\\:%7.2lf %s /\\g",
160 "GPRINT:maxin:MAX: max\\:%7.2lf %s /\\g",
161 "GPRINT:maxin:LAST: current\\:%7.2lf %s $STYLE/s)\\n",
162 "AREA:avgout_draw#d0d0d0:",
163 "LINE1:avgout_draw#$COLOR_OUT_LINE:output\\g",
164 "GPRINT:avgout:AVERAGE: (avg\\:%7.2lf %s /\\g",
165 "GPRINT:maxout:MAX: max\\:%7.2lf %s /\\g",
166 "GPRINT:maxout:LAST: current\\:%7.2lf %s $STYLE/s)\\n",
167 "AREA:wrong1#$COLOR_WRONGDATA:data unknown (device may be down)\\n",
168 "AREA:wrong2#$COLOR_WRONGDATA:",
169 "HRULE:$maxin#$COLOR_MAXSPEED:Maximum transmission speed without compression (in\\: $maxin / out\\: $maxout $STYLE/s)\\n",
170 "HRULE:-$maxout#$COLOR_MAXSPEED",
171 "GPRINT:totin:AVERAGE: totaals for last hour\\:%7.2lf %s $STYLETOTAL input",
172 "GPRINT:totout:AVERAGE: / %7.2lf %s $STYLETOTAL output",
173 "HRULE:0#000000";
174 
175 rename $TEMPNAME,$FILENAME;
176 print " hour ",$xs,"x",$ys," ",$FILENAME,"\n";
177 }
178 
179sub draw_other {
180 
181 my($range,$dbfile,$name,$device,$descr,$file,$DATESTRING,$build,$fullrange,$STYLE,$STYLETOTAL,$MULT,$MULTTOTAL,$IOMIN,$maxin,$maxout,$IMGFMT)=@_;
182 
183 my $FILENAME="$OUTDIR/$name-$file.$IMGFMT";
184 
185 my $DATE=time;
186 
187 $MULTTOTAL=$fullrange*$MULTTOTAL;
188 
189 my $make=$FORCE;
190 if (! -e "$FILENAME") { $make++; }
191 else {
192 my (undef,undef,undef,undef,undef,undef,undef,undef,undef,$mtime,undef,undef,undef) = stat( "$FILENAME" );
193 if ($DATE > ($mtime+$build)) { $make++; }
194 }
195 if ($make>0) {
196 $name =~ s/:/\\:/g;
197 $dbfile =~ s/:/\\:/g;
198 
199 my ($prints,$xs,$ys)=RRDs::graph $TEMPNAME, "-i", "-b","1024", "-v","$STYLE/s",
200 "-s","-$range", "-w",$CONFIG{"WIDTH"}, "-h",$CONFIG{"HEIGHT"}, "-a", uc($IMGFMT),
201 "-u",$IOMIN, "-l",(-$IOMIN), "--title", "traffic on $device - last $descr ($DATESTRING)",@FEATURES,
202 "DEF:dbminin=rrd/$dbfile.rrd:in:MIN",
203 "DEF:dbmaxin=rrd/$dbfile.rrd:in:MAX",
204 "DEF:dbavgin=rrd/$dbfile.rrd:in:AVERAGE",
205 "DEF:dbminout=rrd/$dbfile.rrd:out:MIN",
206 "DEF:dbmaxout=rrd/$dbfile.rrd:out:MAX",
207 "DEF:dbavgout=rrd/$dbfile.rrd:out:AVERAGE",
208 "CDEF:minin=dbminin,$MULT,*",
209 "CDEF:maxin=dbmaxin,$MULT,*",
210 "CDEF:avgin=dbavgin,$MULT,*",
211 "CDEF:totin=dbavgin,$MULTTOTAL,*",
212 "CDEF:minout=dbminout,$MULT,*",
213 "CDEF:maxout=dbmaxout,$MULT,*",
214 "CDEF:avgout=dbavgout,$MULT,*",
215 "CDEF:totout=dbavgout,$MULTTOTAL,*",
216 "CDEF:wrong1=maxin,maxout,+,UN,INF,UNKN,IF",
217 "CDEF:wrong2=0,wrong1,-",
218 "CDEF:minin_draw=minin,$SWAPIO,*",
219 "CDEF:maxin_draw=maxin,$SWAPIO,*",
220 "CDEF:avgin_draw=avgin,$SWAPIO,*",
221 "CDEF:minout_draw=minout,$SWAPOI,*",
222 "CDEF:maxout_draw=maxout,$SWAPOI,*",
223 "CDEF:avgout_draw=avgout,$SWAPOI,*",
224 "AREA:maxin_draw#$COLOR_IN_AREA:min/max in\\g",
225 "LINE1:maxin_draw#$COLOR_BORDER:\\g",
226 "AREA:minin_draw#ffffff:\\g",
227 "LINE1:minin_draw#$COLOR_BORDER:\\g",
228 "GPRINT:minin:MIN: (%7.2lf %s /\\g",
229 "GPRINT:maxin:MAX: %7.2lf %s $STYLE/s)",
230 "LINE2:avgin_draw#$COLOR_IN_LINE:avg in\\g",
231 "GPRINT:avgin:MIN: (min\\:%7.2lf %s /\\g",
232 "GPRINT:avgin:AVERAGE: avg\\:%7.2lf %s /\\g",
233 "GPRINT:avgin:MAX: max\\:%7.2lf %s $STYLE/s)\\n",
234 "AREA:maxout_draw#$COLOR_OUT_AREA:min/max out\\g",
235 "LINE1:maxout_draw#$COLOR_BORDER:\\g",
236 "AREA:minout_draw#ffffff:\\g",
237 "LINE1:minout_draw#$COLOR_BORDER:\\g",
238 "GPRINT:minout:MIN: (%7.2lf %s /\\g",
239 "GPRINT:maxout:MAX: %7.2lf %s $STYLE/s)",
240 "LINE2:avgout_draw#$COLOR_OUT_LINE:avg out\\g",
241 "GPRINT:avgout:MIN: (min\\:%7.2lf %s /\\g",
242 "GPRINT:avgout:AVERAGE: avg\\:%7.2lf %s /\\g",
243 "GPRINT:avgout:MAX: max\\:%7.2lf %s $STYLE/s)\\n",
244 "AREA:wrong1#$COLOR_WRONGDATA:data unknown (device may be down)\\n",
245 "AREA:wrong2#$COLOR_WRONGDATA:",
246 "HRULE:$maxin#$COLOR_MAXSPEED:Maximum transmission speed without compression (in\\: $maxin / out\\: $maxout $STYLE/s)",
247 "HRULE:-$maxout#$COLOR_MAXSPEED",
248 "GPRINT:totin:AVERAGE: totals for last $descr \\:%7.2lf %s $STYLETOTAL input",
249 "GPRINT:totout:AVERAGE: / %7.2lf %s $STYLETOTAL output",
250 "HRULE:0#000000";
251 
252 rename $TEMPNAME,$FILENAME;
253 printf " %-7s %d"."x%d %s\n",$descr,$xs,$ys,$FILENAME;
254 }
255 }
256 
257sub rrd2wmwrite() {
258 
259my ($dbname, $logdir, $outdir) = @_;
260 
261my $dbnameweb = $dbname;
262$dbnameweb =~ s/:/_/g;
263$dbnameweb =~ s/\./_/g;
264 
265my $file = $outdir."/".$dbnameweb."_wm.html";
266 
267 
268open (WMFILE,">$file");
269 
270my $cf = "MAX";
271 
272print WMFILE rrd2wm($dbname, $logdir, $cf, "now");
273print WMFILE rrd2wm($dbname, $logdir, $cf, "hour");
274print WMFILE rrd2wm($dbname, $logdir, $cf, "6h");
275print WMFILE rrd2wm($dbname, $logdir, $cf, "day");
276print WMFILE rrd2wm($dbname, $logdir, $cf, "week");
277print WMFILE rrd2wm($dbname, $logdir, $cf, "month");
278print WMFILE rrd2wm($dbname, $logdir, $cf, "year");
279 
280$cf = "CURRENT";
281 
282print WMFILE rrd2wm($dbname, $logdir, $cf, "now");
283print WMFILE rrd2wm($dbname, $logdir, $cf, "hour");
284print WMFILE rrd2wm($dbname, $logdir, $cf, "6h");
285print WMFILE rrd2wm($dbname, $logdir, $cf, "day");
286print WMFILE rrd2wm($dbname, $logdir, $cf, "week");
287print WMFILE rrd2wm($dbname, $logdir, $cf, "month");
288print WMFILE rrd2wm($dbname, $logdir, $cf, "year");
289 
290$cf = "AVERAGE";
291 
292print WMFILE rrd2wm($dbname, $logdir, $cf, "now");
293print WMFILE rrd2wm($dbname, $logdir, $cf, "hour");
294print WMFILE rrd2wm($dbname, $logdir, $cf, "6h");
295print WMFILE rrd2wm($dbname, $logdir, $cf, "day");
296print WMFILE rrd2wm($dbname, $logdir, $cf, "week");
297print WMFILE rrd2wm($dbname, $logdir, $cf, "month");
298print WMFILE rrd2wm($dbname, $logdir, $cf, "year");
299 
300close(WMFILE);
301}
302 
303 
304sub rrd2wm() {
305 
306my ($dbname, $logdir, $cf, $period) = @_;
307my $stepnum = 0;
308my $stepsize = 0;
309 
310my $cfp = $cf;
311if($cf eq "CURRENT") { $cf = "MAX"; };
312 
313if ($period eq "now") { $stepnum = 1 ; $stepsize = 1; };
314if ($period eq "hour") { $stepnum = 6 ; $stepsize = 600; };
315if ($period eq "6h") { $stepnum = 6 ; $stepsize = 3600; };
316if ($period eq "day") { $stepnum = 24 ; $stepsize = 3600; };
317if ($period eq "week") { $stepnum = (7 * 24) ; $stepsize = 3600; };
318if ($period eq "month") { $stepnum = (30 * 24) ; $stepsize = 3600; };
319if ($period eq "year") { $stepnum = (365* 24) ; $stepsize = 3600; };
320 
321my $logfile = "$dbname.rrd";
322my $ltime = localtime;
323my $time = time;
324 
325#this will make sure that we will have a value in case of "now"
326$time = int(($time-2)/10) * 10;
327 
328my $endtime = int($time/$stepsize) * $stepsize;
329my $starttime = $endtime - ($stepsize * ($stepnum - 1));
330my ($start,$step,$names,$data) = RRDs::fetch ("$logdir$logfile", "$cf", "-r $stepsize", "-s $starttime", "-e $endtime");
331my $ERR=RRDs::error;
332 
333my $pocetdat = $#$data ;
334my $celkem_in = 0;
335my $celkem_out = 0;
336my $max_in = 0;
337my $max_out = 0;
338my $val = 0;
339if($pocetdat < 0) {
340$pocetdat = 1;
341 
342 } else {
343$pocetdat += 1;
344 
345foreach my $line (@$data) {
346 my $lasttime = $start;
347 $start += $step;
348 my $pozice = 0;
349 foreach $val (@$line) {
350 $pozice += 1;
351 if (!$val) { $val=0; };
352 if ($pozice == 1) {
353 $celkem_in += $val;
354 if ($val > $max_in) {$max_in = $val};
355 };
356 if ($pozice == 2) {
357 $celkem_out += $val;
358 if ($val > $max_out) {$max_out = $val};
359 };
360 }
361} #foreach
362} #else
363 
364 
365$max_in = int($max_in);
366$max_out = int($max_out);
367my $avg_in = int($celkem_in / $pocetdat);
368my $avg_out = int($celkem_out / $pocetdat);
369$period = substr($period,0,1);
370 
371if($cfp eq "AVERAGE") { return "<!-- avgin $period $avg_in --><!-- avgout $period $avg_out -->\n"; }
372 
373if($cfp eq "MAX") { return "<!-- maxin $period $max_in --><!-- maxout $period $max_out -->\n"; }
374 
375if($cfp eq "CURRENT") { return "<!-- cuin $period $max_in --><!-- cuout $period $max_out -->\n"; }
376} # sub

Powered by WebSVN 2.2.1