jablonka.czprosek.czf

hotsanic

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

 

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;
7622simandl $name =~ s/\//-/g;
77 $device =~ s/\//-/g;
78 $dbfile =~ s/\//-/g;
791simandl } else {
80 if (index ($device,"RRCP")>=0) {
81 my (undef,$host,$comm,$port)=split /:/,$device;
82 $host =~ s/\./:/g;
83 ($name=$host."_".$port) =~ s/\./_/g;
84 $name =~ s/:/_/g;
85 $device=$host.":p".$port;
86 $dbfile=$host."_".$port;
87 $dbfile=~ s/\./:/g;
88 
8919simandl } else {
90 $name=$device;
91 $name =~ s/:/_/g;
92 $dbfile=$name;
93 $name =~ s/\./_/g;
94 }
951simandl }
96 # if database exists, create images...
97 print "creating images for $device ...\n";
98 if ( -e "./rrd/$dbfile.rrd") {
99 if ((! defined $maxin) || ($maxin eq "") || ($maxin == 0)) {
100 print " Maximum input speed not set!\n Please check settings-file!\n setting speed to 100 MBit\n";
101 $maxin=12500000;
102 }
103 if ((! defined $maxout) || ($maxout eq "") || ($maxout == 0)) {
104 print " Maximum output speed not set!\n Please check settings-file!\n setting speed to 100 MBit\n";
105 $maxout=12500000;
106 }
107 $maxin*=$MULT;
108 $maxout*=$MULT;
109 $IOMIN=abs($SCALE)*$MULT;
110 
111 foreach $range ("1h","6h","1day","1week","1month","1year") {
112 my ($descr,$file,$build,$fullrange,$DATESTRING)=HotSaNICdiagram::get_diagram_properties($range);
113 if ($range eq "1h") { draw_hourly($dbfile,$name,$device,$descr,$file,$DATESTRING,$STYLE,$STYLETOTAL,$MULT,$MULTTOTAL,$IOMIN,$maxin,$maxout,$IMGFMT); }
114 else { draw_other($range,$dbfile,$name,$device,$descr,$file,$DATESTRING,$build,$fullrange,$STYLE,$STYLETOTAL,$MULT,$MULTTOTAL,$IOMIN,$maxin,$maxout,$IMGFMT); }
115 }
116 
117 print "creating weathermap file for $device ...\n";
118 
119 &rrd2wmwrite($dbfile,"./rrd/",$OUTDIR);
120 
121 }
122 else { print " No database $dbfile found\n Please check if the daemon is running.\n";next; }
123 print "\n";
124 }
125exit 0;
126 
127sub draw_hourly {
128 
129 my($dbfile,$name,$device,$descr,$file,$DATESTRING,$STYLE,$STYLETOTAL,$MULT,$MULTTOTAL,$IOMIN,$maxin,$maxout,$IMGFMT)=@_;
130 
131 my $FILENAME="$OUTDIR/$name-$file.$IMGFMT";
132 
133 my $DATE=time;
134 
135 $MULTTOTAL=3600*$MULTTOTAL;
136 
137# the scaring ;) definition of "wrong1":
138# if TIME < now { if in+out unknown { INF else 0 } else 0 }
139# CDEF:wrong1=TIME,$DATE,LT,maxin,maxout,+,UN,INF,0,IF,0,IF \
140 
141 $name =~ s/:/\\:/g;
142 $dbfile =~ s/:/\\:/g;
143 my ($prints,$xs,$ys)=RRDs::graph $TEMPNAME, "-i", "-b","1024", "-v","$STYLE/s",
144 "-s","-3600", "-w",$CONFIG{"WIDTH"}, "-h",$CONFIG{"HEIGHT"}, "-a", uc($IMGFMT),
145 "-u",$IOMIN, "-l",(-$IOMIN), "--title", "traffic on $device - last hour ($DATESTRING)",@FEATURES,
146 "DEF:dbavgin=rrd/$dbfile.rrd:in:AVERAGE",
147 "DEF:dbmaxin=rrd/$dbfile.rrd:in:MAX",
148 "DEF:dbavgout=rrd/$dbfile.rrd:out:AVERAGE",
149 "DEF:dbmaxout=rrd/$dbfile.rrd:out:MAX",
150 "CDEF:avgin=dbavgin,$MULT,*",
151 "CDEF:maxin=dbmaxin,$MULT,*",
152 "CDEF:totin=dbavgin,$MULTTOTAL,*",
153 "CDEF:avgout=dbavgout,$MULT,*",
154 "CDEF:maxout=dbmaxout,$MULT,*",
155 "CDEF:totout=dbavgout,$MULTTOTAL,*",
156 "CDEF:wrong1=TIME,$DATE,LT,maxin,maxout,+,UN,INF,0,IF,0,IF",
157 "CDEF:wrong2=0,wrong1,-",
158 "CDEF:avgin_draw=avgin,$SWAPIO,*",
159 "CDEF:avgout_draw=avgout,$SWAPOI,*",
160 "AREA:avgin_draw#d0d0d0:",
161 "LINE1:avgin_draw#$COLOR_IN_LINE:input\\g",
162 "GPRINT:avgin:AVERAGE: (avg\\:%7.2lf %s /\\g",
163 "GPRINT:maxin:MAX: max\\:%7.2lf %s /\\g",
164 "GPRINT:maxin:LAST: current\\:%7.2lf %s $STYLE/s)\\n",
165 "AREA:avgout_draw#d0d0d0:",
166 "LINE1:avgout_draw#$COLOR_OUT_LINE:output\\g",
167 "GPRINT:avgout:AVERAGE: (avg\\:%7.2lf %s /\\g",
168 "GPRINT:maxout:MAX: max\\:%7.2lf %s /\\g",
169 "GPRINT:maxout:LAST: current\\:%7.2lf %s $STYLE/s)\\n",
170 "AREA:wrong1#$COLOR_WRONGDATA:data unknown (device may be down)\\n",
171 "AREA:wrong2#$COLOR_WRONGDATA:",
172 "HRULE:$maxin#$COLOR_MAXSPEED:Maximum transmission speed without compression (in\\: $maxin / out\\: $maxout $STYLE/s)\\n",
173 "HRULE:-$maxout#$COLOR_MAXSPEED",
174 "GPRINT:totin:AVERAGE: totaals for last hour\\:%7.2lf %s $STYLETOTAL input",
175 "GPRINT:totout:AVERAGE: / %7.2lf %s $STYLETOTAL output",
176 "HRULE:0#000000";
177 
178 rename $TEMPNAME,$FILENAME;
179 print " hour ",$xs,"x",$ys," ",$FILENAME,"\n";
180 }
181 
182sub draw_other {
183 
184 my($range,$dbfile,$name,$device,$descr,$file,$DATESTRING,$build,$fullrange,$STYLE,$STYLETOTAL,$MULT,$MULTTOTAL,$IOMIN,$maxin,$maxout,$IMGFMT)=@_;
185 
186 my $FILENAME="$OUTDIR/$name-$file.$IMGFMT";
187 
188 my $DATE=time;
189 
190 $MULTTOTAL=$fullrange*$MULTTOTAL;
191 
192 my $make=$FORCE;
193 if (! -e "$FILENAME") { $make++; }
194 else {
195 my (undef,undef,undef,undef,undef,undef,undef,undef,undef,$mtime,undef,undef,undef) = stat( "$FILENAME" );
196 if ($DATE > ($mtime+$build)) { $make++; }
197 }
198 if ($make>0) {
199 $name =~ s/:/\\:/g;
200 $dbfile =~ s/:/\\:/g;
201 
202 my ($prints,$xs,$ys)=RRDs::graph $TEMPNAME, "-i", "-b","1024", "-v","$STYLE/s",
203 "-s","-$range", "-w",$CONFIG{"WIDTH"}, "-h",$CONFIG{"HEIGHT"}, "-a", uc($IMGFMT),
204 "-u",$IOMIN, "-l",(-$IOMIN), "--title", "traffic on $device - last $descr ($DATESTRING)",@FEATURES,
205 "DEF:dbminin=rrd/$dbfile.rrd:in:MIN",
206 "DEF:dbmaxin=rrd/$dbfile.rrd:in:MAX",
207 "DEF:dbavgin=rrd/$dbfile.rrd:in:AVERAGE",
208 "DEF:dbminout=rrd/$dbfile.rrd:out:MIN",
209 "DEF:dbmaxout=rrd/$dbfile.rrd:out:MAX",
210 "DEF:dbavgout=rrd/$dbfile.rrd:out:AVERAGE",
211 "CDEF:minin=dbminin,$MULT,*",
212 "CDEF:maxin=dbmaxin,$MULT,*",
213 "CDEF:avgin=dbavgin,$MULT,*",
214 "CDEF:totin=dbavgin,$MULTTOTAL,*",
215 "CDEF:minout=dbminout,$MULT,*",
216 "CDEF:maxout=dbmaxout,$MULT,*",
217 "CDEF:avgout=dbavgout,$MULT,*",
218 "CDEF:totout=dbavgout,$MULTTOTAL,*",
219 "CDEF:wrong1=maxin,maxout,+,UN,INF,UNKN,IF",
220 "CDEF:wrong2=0,wrong1,-",
221 "CDEF:minin_draw=minin,$SWAPIO,*",
222 "CDEF:maxin_draw=maxin,$SWAPIO,*",
223 "CDEF:avgin_draw=avgin,$SWAPIO,*",
224 "CDEF:minout_draw=minout,$SWAPOI,*",
225 "CDEF:maxout_draw=maxout,$SWAPOI,*",
226 "CDEF:avgout_draw=avgout,$SWAPOI,*",
227 "AREA:maxin_draw#$COLOR_IN_AREA:min/max in\\g",
228 "LINE1:maxin_draw#$COLOR_BORDER:\\g",
229 "AREA:minin_draw#ffffff:\\g",
230 "LINE1:minin_draw#$COLOR_BORDER:\\g",
231 "GPRINT:minin:MIN: (%7.2lf %s /\\g",
232 "GPRINT:maxin:MAX: %7.2lf %s $STYLE/s)",
233 "LINE2:avgin_draw#$COLOR_IN_LINE:avg in\\g",
234 "GPRINT:avgin:MIN: (min\\:%7.2lf %s /\\g",
235 "GPRINT:avgin:AVERAGE: avg\\:%7.2lf %s /\\g",
236 "GPRINT:avgin:MAX: max\\:%7.2lf %s $STYLE/s)\\n",
237 "AREA:maxout_draw#$COLOR_OUT_AREA:min/max out\\g",
238 "LINE1:maxout_draw#$COLOR_BORDER:\\g",
239 "AREA:minout_draw#ffffff:\\g",
240 "LINE1:minout_draw#$COLOR_BORDER:\\g",
241 "GPRINT:minout:MIN: (%7.2lf %s /\\g",
242 "GPRINT:maxout:MAX: %7.2lf %s $STYLE/s)",
243 "LINE2:avgout_draw#$COLOR_OUT_LINE:avg out\\g",
244 "GPRINT:avgout:MIN: (min\\:%7.2lf %s /\\g",
245 "GPRINT:avgout:AVERAGE: avg\\:%7.2lf %s /\\g",
246 "GPRINT:avgout:MAX: max\\:%7.2lf %s $STYLE/s)\\n",
247 "AREA:wrong1#$COLOR_WRONGDATA:data unknown (device may be down)\\n",
248 "AREA:wrong2#$COLOR_WRONGDATA:",
249 "HRULE:$maxin#$COLOR_MAXSPEED:Maximum transmission speed without compression (in\\: $maxin / out\\: $maxout $STYLE/s)",
250 "HRULE:-$maxout#$COLOR_MAXSPEED",
251 "GPRINT:totin:AVERAGE: totals for last $descr \\:%7.2lf %s $STYLETOTAL input",
252 "GPRINT:totout:AVERAGE: / %7.2lf %s $STYLETOTAL output",
253 "HRULE:0#000000";
254 
255 rename $TEMPNAME,$FILENAME;
256 printf " %-7s %d"."x%d %s\n",$descr,$xs,$ys,$FILENAME;
257 }
258 }
259 
260sub rrd2wmwrite() {
261 
262my ($dbname, $logdir, $outdir) = @_;
263 
264my $dbnameweb = $dbname;
265$dbnameweb =~ s/:/_/g;
266$dbnameweb =~ s/\./_/g;
26722simandl$dbnameweb =~ s/\//-/g;
2681simandl 
269my $file = $outdir."/".$dbnameweb."_wm.html";
270 
271 
272open (WMFILE,">$file");
273 
274my $cf = "MAX";
275 
276print WMFILE rrd2wm($dbname, $logdir, $cf, "now");
277print WMFILE rrd2wm($dbname, $logdir, $cf, "hour");
278print WMFILE rrd2wm($dbname, $logdir, $cf, "6h");
279print WMFILE rrd2wm($dbname, $logdir, $cf, "day");
280print WMFILE rrd2wm($dbname, $logdir, $cf, "week");
281print WMFILE rrd2wm($dbname, $logdir, $cf, "month");
282print WMFILE rrd2wm($dbname, $logdir, $cf, "year");
283 
284$cf = "CURRENT";
285 
286print WMFILE rrd2wm($dbname, $logdir, $cf, "now");
287print WMFILE rrd2wm($dbname, $logdir, $cf, "hour");
288print WMFILE rrd2wm($dbname, $logdir, $cf, "6h");
289print WMFILE rrd2wm($dbname, $logdir, $cf, "day");
290print WMFILE rrd2wm($dbname, $logdir, $cf, "week");
291print WMFILE rrd2wm($dbname, $logdir, $cf, "month");
292print WMFILE rrd2wm($dbname, $logdir, $cf, "year");
293 
294$cf = "AVERAGE";
295 
296print WMFILE rrd2wm($dbname, $logdir, $cf, "now");
297print WMFILE rrd2wm($dbname, $logdir, $cf, "hour");
298print WMFILE rrd2wm($dbname, $logdir, $cf, "6h");
299print WMFILE rrd2wm($dbname, $logdir, $cf, "day");
300print WMFILE rrd2wm($dbname, $logdir, $cf, "week");
301print WMFILE rrd2wm($dbname, $logdir, $cf, "month");
302print WMFILE rrd2wm($dbname, $logdir, $cf, "year");
303 
304close(WMFILE);
305}
306 
307 
308sub rrd2wm() {
309 
310my ($dbname, $logdir, $cf, $period) = @_;
311my $stepnum = 0;
312my $stepsize = 0;
313 
314my $cfp = $cf;
315if($cf eq "CURRENT") { $cf = "MAX"; };
316 
317if ($period eq "now") { $stepnum = 1 ; $stepsize = 1; };
318if ($period eq "hour") { $stepnum = 6 ; $stepsize = 600; };
319if ($period eq "6h") { $stepnum = 6 ; $stepsize = 3600; };
320if ($period eq "day") { $stepnum = 24 ; $stepsize = 3600; };
321if ($period eq "week") { $stepnum = (7 * 24) ; $stepsize = 3600; };
322if ($period eq "month") { $stepnum = (30 * 24) ; $stepsize = 3600; };
323if ($period eq "year") { $stepnum = (365* 24) ; $stepsize = 3600; };
324 
325my $logfile = "$dbname.rrd";
326my $ltime = localtime;
327my $time = time;
328 
329#this will make sure that we will have a value in case of "now"
330$time = int(($time-2)/10) * 10;
331 
332my $endtime = int($time/$stepsize) * $stepsize;
333my $starttime = $endtime - ($stepsize * ($stepnum - 1));
334my ($start,$step,$names,$data) = RRDs::fetch ("$logdir$logfile", "$cf", "-r $stepsize", "-s $starttime", "-e $endtime");
335my $ERR=RRDs::error;
336 
337my $pocetdat = $#$data ;
338my $celkem_in = 0;
339my $celkem_out = 0;
340my $max_in = 0;
341my $max_out = 0;
342my $val = 0;
343if($pocetdat < 0) {
344$pocetdat = 1;
345 
346 } else {
347$pocetdat += 1;
348 
349foreach my $line (@$data) {
350 my $lasttime = $start;
351 $start += $step;
352 my $pozice = 0;
353 foreach $val (@$line) {
354 $pozice += 1;
355 if (!$val) { $val=0; };
356 if ($pozice == 1) {
357 $celkem_in += $val;
358 if ($val > $max_in) {$max_in = $val};
359 };
360 if ($pozice == 2) {
361 $celkem_out += $val;
362 if ($val > $max_out) {$max_out = $val};
363 };
364 }
365} #foreach
366} #else
367 
368 
369$max_in = int($max_in);
370$max_out = int($max_out);
371my $avg_in = int($celkem_in / $pocetdat);
372my $avg_out = int($celkem_out / $pocetdat);
373$period = substr($period,0,1);
374 
375if($cfp eq "AVERAGE") { return "<!-- avgin $period $avg_in --><!-- avgout $period $avg_out -->\n"; }
376 
377if($cfp eq "MAX") { return "<!-- maxin $period $max_in --><!-- maxout $period $max_out -->\n"; }
378 
379if($cfp eq "CURRENT") { return "<!-- cuin $period $max_in --><!-- cuout $period $max_out -->\n"; }
380} # sub

Powered by WebSVN 2.2.1