1 | 1 | simandl | #!/usr/bin/env perl |
2 | | | use strict; |
3 | | | use warnings; |
4 | | | use diagnostics; |
5 | | | |
6 | | | use lib "../../lib"; |
7 | | | use HotSaNICparser; |
8 | | | use HotSaNICdiagram; |
9 | | | use RRDs; |
10 | | | |
11 | | | # read global settings |
12 | | | # |
13 | | | my $MODNAME=HotSaNICparser::get_module_name(); |
14 | | | my %CONFIG=HotSaNICparser::get_config("../.."); |
15 | | | |
16 | | | my $OUTDIR=$CONFIG{"WEBDIR"}."/".lc $MODNAME; |
17 | | | if ( ! -e $OUTDIR ) { mkdir "$OUTDIR",0755; } |
18 | | | |
19 | | | my $IMGFMT=lc $CONFIG{"IMAGEFORMAT"}; |
20 | | | my $TEMPNAME="$OUTDIR/temp.$IMGFMT"; |
21 | | | |
22 | | | my (@ITEMS,$dbfile,$device,$maxin,$maxout,$name,$IOMIN,$range,$descr,$file,$build,$fullrange); |
23 | | | my $SWAPIO=1; |
24 | | | my $SCALE=-1000; |
25 | | | my @FEATURES=(); |
26 | | | my $FORCE=@ARGV; |
27 | | | |
28 | | | my $COLOR_IN_LINE="00a000"; |
29 | | | my $COLOR_IN_AREA="c0ffc0"; |
30 | | | my $COLOR_OUT_LINE="c00000"; |
31 | | | my $COLOR_OUT_AREA="ffc0c0"; |
32 | | | my $COLOR_BORDER="a0a0a0"; |
33 | | | my $COLOR_MAXSPEED="ff00ff"; |
34 | | | my $COLOR_WRONGDATA="ffffa0"; |
35 | | | |
36 | | | my $STYLE="bytes"; |
37 | | | my $STYLETOTAL="bytes"; |
38 | | | my $MULT=1; |
39 | | | my $MULTTOTAL=1; |
40 | | | |
41 | | | # read module-specific settings |
42 | | | # |
43 | | | foreach (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 | | | |
62 | | | if ($SCALE eq "") { $SCALE=-1000; } |
63 | | | if ($SCALE > 0) { push @FEATURES,"--rigid"; } |
64 | | | |
65 | | | my $SWAPOI=-$SWAPIO; |
66 | | | |
67 | | | # generate diagrams |
68 | | | # |
69 | | | foreach (@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 | 22 | simandl | $name =~ s/\//-/g; |
77 | | | $device =~ s/\//-/g; |
78 | | | $dbfile =~ s/\//-/g; |
79 | 1 | simandl | } 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 | | | |
89 | 19 | simandl | } else { |
90 | | | $name=$device; |
91 | | | $name =~ s/:/_/g; |
92 | | | $dbfile=$name; |
93 | | | $name =~ s/\./_/g; |
94 | | | } |
95 | 1 | simandl | } |
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 | | | } |
125 | | | exit 0; |
126 | | | |
127 | | | sub 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 | | | |
182 | | | sub 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 | | | |
260 | | | sub rrd2wmwrite() { |
261 | | | |
262 | | | my ($dbname, $logdir, $outdir) = @_; |
263 | | | |
264 | | | my $dbnameweb = $dbname; |
265 | | | $dbnameweb =~ s/:/_/g; |
266 | | | $dbnameweb =~ s/\./_/g; |
267 | 22 | simandl | $dbnameweb =~ s/\//-/g; |
268 | 1 | simandl | |
269 | | | my $file = $outdir."/".$dbnameweb."_wm.html"; |
270 | | | |
271 | | | |
272 | | | open (WMFILE,">$file"); |
273 | | | |
274 | | | my $cf = "MAX"; |
275 | | | |
276 | | | print WMFILE rrd2wm($dbname, $logdir, $cf, "now"); |
277 | | | print WMFILE rrd2wm($dbname, $logdir, $cf, "hour"); |
278 | | | print WMFILE rrd2wm($dbname, $logdir, $cf, "6h"); |
279 | | | print WMFILE rrd2wm($dbname, $logdir, $cf, "day"); |
280 | | | print WMFILE rrd2wm($dbname, $logdir, $cf, "week"); |
281 | | | print WMFILE rrd2wm($dbname, $logdir, $cf, "month"); |
282 | | | print WMFILE rrd2wm($dbname, $logdir, $cf, "year"); |
283 | | | |
284 | | | $cf = "CURRENT"; |
285 | | | |
286 | | | print WMFILE rrd2wm($dbname, $logdir, $cf, "now"); |
287 | | | print WMFILE rrd2wm($dbname, $logdir, $cf, "hour"); |
288 | | | print WMFILE rrd2wm($dbname, $logdir, $cf, "6h"); |
289 | | | print WMFILE rrd2wm($dbname, $logdir, $cf, "day"); |
290 | | | print WMFILE rrd2wm($dbname, $logdir, $cf, "week"); |
291 | | | print WMFILE rrd2wm($dbname, $logdir, $cf, "month"); |
292 | | | print WMFILE rrd2wm($dbname, $logdir, $cf, "year"); |
293 | | | |
294 | | | $cf = "AVERAGE"; |
295 | | | |
296 | | | print WMFILE rrd2wm($dbname, $logdir, $cf, "now"); |
297 | | | print WMFILE rrd2wm($dbname, $logdir, $cf, "hour"); |
298 | | | print WMFILE rrd2wm($dbname, $logdir, $cf, "6h"); |
299 | | | print WMFILE rrd2wm($dbname, $logdir, $cf, "day"); |
300 | | | print WMFILE rrd2wm($dbname, $logdir, $cf, "week"); |
301 | | | print WMFILE rrd2wm($dbname, $logdir, $cf, "month"); |
302 | | | print WMFILE rrd2wm($dbname, $logdir, $cf, "year"); |
303 | | | |
304 | | | close(WMFILE); |
305 | | | } |
306 | | | |
307 | | | |
308 | | | sub rrd2wm() { |
309 | | | |
310 | | | my ($dbname, $logdir, $cf, $period) = @_; |
311 | | | my $stepnum = 0; |
312 | | | my $stepsize = 0; |
313 | | | |
314 | | | my $cfp = $cf; |
315 | | | if($cf eq "CURRENT") { $cf = "MAX"; }; |
316 | | | |
317 | | | if ($period eq "now") { $stepnum = 1 ; $stepsize = 1; }; |
318 | | | if ($period eq "hour") { $stepnum = 6 ; $stepsize = 600; }; |
319 | | | if ($period eq "6h") { $stepnum = 6 ; $stepsize = 3600; }; |
320 | | | if ($period eq "day") { $stepnum = 24 ; $stepsize = 3600; }; |
321 | | | if ($period eq "week") { $stepnum = (7 * 24) ; $stepsize = 3600; }; |
322 | | | if ($period eq "month") { $stepnum = (30 * 24) ; $stepsize = 3600; }; |
323 | | | if ($period eq "year") { $stepnum = (365* 24) ; $stepsize = 3600; }; |
324 | | | |
325 | | | my $logfile = "$dbname.rrd"; |
326 | | | my $ltime = localtime; |
327 | | | my $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 | | | |
332 | | | my $endtime = int($time/$stepsize) * $stepsize; |
333 | | | my $starttime = $endtime - ($stepsize * ($stepnum - 1)); |
334 | | | my ($start,$step,$names,$data) = RRDs::fetch ("$logdir$logfile", "$cf", "-r $stepsize", "-s $starttime", "-e $endtime"); |
335 | | | my $ERR=RRDs::error; |
336 | | | |
337 | | | my $pocetdat = $#$data ; |
338 | | | my $celkem_in = 0; |
339 | | | my $celkem_out = 0; |
340 | | | my $max_in = 0; |
341 | | | my $max_out = 0; |
342 | | | my $val = 0; |
343 | | | if($pocetdat < 0) { |
344 | | | $pocetdat = 1; |
345 | | | |
346 | | | } else { |
347 | | | $pocetdat += 1; |
348 | | | |
349 | | | foreach 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); |
371 | | | my $avg_in = int($celkem_in / $pocetdat); |
372 | | | my $avg_out = int($celkem_out / $pocetdat); |
373 | | | $period = substr($period,0,1); |
374 | | | |
375 | | | if($cfp eq "AVERAGE") { return "<!-- avgin $period $avg_in --><!-- avgout $period $avg_out -->\n"; } |
376 | | | |
377 | | | if($cfp eq "MAX") { return "<!-- maxin $period $max_in --><!-- maxout $period $max_out -->\n"; } |
378 | | | |
379 | | | if($cfp eq "CURRENT") { return "<!-- cuin $period $max_in --><!-- cuout $period $max_out -->\n"; } |
380 | | | } # sub |