jablonka.czprosek.czf

hotsanic

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

Powered by WebSVN 2.2.1