MFC: An off-by-one malloc size was corrupting the installer's memory,
[dragonfly.git] / contrib / ipfilter / perl / Isbgraph
blobc68b672322f02c5ed2208fd2b79574af6a81fc2a
1 #!/usr/local/bin/perl
3 # isbgraph
4 # an example in not so hot perl programming....
5 # based around GraphMaker from Fabrizio Pivari
6 # A graph maker perl script
8 use GD;
9 use Getopt::Long;
10 $hr=0;
12 sub main{
14 $opt_conf="./graphmaker.cnf";
16 @elem=("NUMBERYCELLGRIDSIZE","MAXYVALUE","MINYVALUE","XCELLGRIDSIZE","XMAX",
17 "Data","Graph","Bar","Average","Graphnum","Title","Transparent","Rbgcolour",
18 "Gbgcolour","Bbgcolour","Rfgcolour","Gfgcolour","Bfgcolour","Rcolour",
19 "Gcolour","Bcolour","Racolour","Gacolour","Bacolour");
21 %option=(
22 NUMBERYCELLGRIDSIZE => '8',
23 MAXYVALUE => '7748',
24 MINYVALUE => '6500',
25 XCELLGRIDSIZE => '18',
26 XMAX => '1000',
27 Data => './graphmaker.dat',
28 Graph => './graphmaker.gif',
29 Bar => '1',
30 Average => '1',
31 Graphnum => '1',
32 Title => 'GraphMaker 2.1',
33 Transparent => 'yes',
34 Rbgcolour => '255',
35 Gbgcolour => '255',
36 Bbgcolour => '255',
37 Rfgcolour => '0',
38 Gfgcolour => '0',
39 Bfgcolour => '0',
40 Rcolour => '0',
41 Gcolour => '0',
42 Bcolour => '255',
43 Racolour => '255',
44 Gacolour => '255',
45 Bacolour => '0');
47 &GetOptions("conf=s","help") || &printusage ;
50 if ($opt_help) {&printusage};
52 open (CNF, $opt_conf) || die;
53 while (<CNF>) {
54 s/\t/ /g; #replace tabs by space
55 next if /^\s*\#/; #ignore comment lines
56 next if /^\s*$/; #ignore empty lines
57 foreach $elem (@elem)
59 if (/\s*$elem\s*:\s*(.*)/) { $option{$elem}=$1; }
62 close(CNF);
63 #########################################
67 # number datapoints/24 hours is 1440 (minutes)
69 # Split into N graphs where each graph has max of 240 datapoints (4 hours)
72 $barset=0;
73 $m=0;
74 $YGRIDSIZE = 400;
75 $YCELLGRIDSIZE = $YGRIDSIZE/$option{'NUMBERYCELLGRIDSIZE'};
76 $XINIT = 30;
77 $XEND = 8;
78 $YINIT =20;
79 $YEND = 20;
80 #$XGRIDSIZE = ($option{'XMAX'}*$option{'XCELLGRIDSIZE'});
81 #$XGRIDSIZE = (240*$option{'XCELLGRIDSIZE'});
82 $XGRIDSIZE = 620;
83 $XGIF = $XGRIDSIZE + $XINIT + $XEND;
84 $XGRAPH = $XGRIDSIZE + $XINIT;
85 $YGIF = $YGRIDSIZE + $YEND + $YINIT;
86 $YGRAPH = $YGRIDSIZE + $YINIT;
87 $RANGE=$option{'MAXYVALUE'}-$option{'MINYVALUE'};
88 $SCALE=$YGRIDSIZE/$RANGE;
90 # NEW IMAGE
91 $im=new GD::Image($XGIF,$YGIF);
93 $white=$im->colorAllocate(255,255,255);
94 $black=$im->colorAllocate(0,0,0);
95 $pink=$im->colorAllocate(255,153,153);
96 $red=$im->colorAllocate(255,0,0);
97 $blue=$im->colorAllocate(0,0,255);
98 $green=$im->colorAllocate(0,192,51);
99 $orange=$im->colorAllocate(255,102,0);
100 $pink=$im->colorAllocate(255,153,153);
101 $teal=$im->colorAllocate(51,153,153);
102 # gif background is $bg
103 $bg=$white;
104 $fg=$blue;
105 # LINE COLOUR HELP BY VAR $colour
106 $colour=$red;
107 $acolour=$yellow;
108 # GRID
109 if ($option{'Transparent'} eq "yes") {$im->transparent($bg)};
110 $im->filledRectangle(0,0,$XGIF,$YGIF,$bg);
112 # Dot style
113 # vertical markers on Y axis grid
114 $im->setStyle($fg,$bg,$bg,$bg);
115 for $i (0..$option{'XMAX'})
117 $xspace= $XINIT+$option{'XCELLGRIDSIZE'}*$i +$i;
118 # $im->line($xspace,$YINIT,$xspace,$YGRAPH,gdStyled);
119 $num = $i+1;
121 use integer;
123 $posis=$num - ($num/60)*60;
125 if ($posis eq 0)
127 $outhr=0;
128 $hr=($hr + 1) ;
129 $outhr=$hr+$option{'Graphnum'}*4;
130 # shift minutes coords to correct stat hour!
131 $im->string(gdMediumBoldFont,$xspace-3,$YGRAPH,"$outhr",$fg);
134 } # end of scan over X values (minutes)
136 $YCELLVALUE=($option{'MAXYVALUE'}-$option{'MINYVALUE'})/$option{'NUMBERYCELLGRIDSIZE'};
137 for $i (0..$option{'NUMBERYCELLGRIDSIZE'})
139 $num=$option{'MINYVALUE'}+$YCELLVALUE*($option{'NUMBERYCELLGRIDSIZE'}-$i);
140 $im->string(gdMediumBoldFont,0,$YINIT+$YCELLGRIDSIZE*$i -6,"$num",$fg);
142 $im->string(gdSmallFont,$XGRIDSIZE/2-80,0,$option{'Title'},$fg);
144 $odd_even = $option{'XCELLGRIDSIZE'}%2;
145 #odd
146 if ($odd_even eq 1) {$middle = $option{'XCELLGRIDSIZE'}/2 +0.5;}
147 else {$middle = $option{'XCELLGRIDSIZE'}/2 +0.5;}
149 # start reading data
150 # open (DATA,$option{'Data'}) || die "cant open $option{'Data'}";
151 # nextdata becomes Y on reading of second data set....
152 $nextdata="N";
153 @datafiles=("./in.dat" , "./out.dat" );
154 foreach ( @datafiles )
156 $m=0;
157 $count=0;
158 $i=0;
159 $fname=$_;
161 print "fname $fname\n";
162 # change entry for red in colour table to green for packets LEAVING target host
164 open (DATA,$_) || die "cant open $_";
165 print "$nextdata nextdata\n";
166 while (<DATA>)
168 /(.*):(.*)/;
169 if ($option{'Average'} eq 1) {$m+=$2;$i++;}
170 if ($count eq 0){$XOLD=$1;$YOLD=$2;$count=1;next}
171 $X=$1; $Y=$2;
172 # +($X-1) are the pixel of the line
173 $xspace= $XINIT+$option{'XCELLGRIDSIZE'}*($X-1) +($X-1);
174 $xspaceold= $XINIT+$option{'XCELLGRIDSIZE'}*($XOLD-1) +($XOLD-1);
175 $yspace= $YGRAPH-($Y-$option{'MINYVALUE'})*$SCALE;
176 $yspaceold= $YGRAPH-($YOLD-$option{'MINYVALUE'})*$SCALE;
177 $barset=$option{'Bar'};
178 if ($barset eq 0)
181 if($nextdata eq "Y")
184 #$im->line($XINIT,$YGRAPH,$X,$Y,$orange);
185 $im->line($xspaceold,$yspaceold,$xspace,$yspace,$green);
187 else
189 $im->line($xspaceold,$yspaceold,$xspace,$yspace,$red);
192 else
194 if ($1 eq 2)
196 $im->filledRectangle($xspaceold,$yspaceold,
197 $xspaceold+$middle,$YGRAPH,$colour);
198 $im->rectangle($xspaceold,$yspaceold,
199 $xspaceold+$middle,$YGRAPH,$fg);
201 else
203 $im->filledRectangle($xspaceold-$middle,$yspaceold,
204 $xspaceold+$middle,$YGRAPH,$colour);
205 $im->rectangle($xspaceold-$middle,$yspaceold,
206 $xspaceold+$middle,$YGRAPH,$fg);
209 $XOLD=$X; $YOLD=$Y;
211 } # end of while DATA loop
213 $im->line(500,40,530,40,$red);
214 $im->line(500,60,530,60,$green);
215 $im->string(gdSmallFont,535,35,"Packets IN",$fg);
216 $im->string(gdSmallFont,535,55,"Packets OUT",$fg);
218 if ($option{'Bar'} ne 0)
220 if ($X eq $option{'XMAX'})
222 $im->filledRectangle($xspace-$middle,$yspace,
223 $xspace,$YGRAPH,$colour);
224 $im->rectangle($xspace-$middle,$yspace,
225 $xspace,$YGRAPH,$fg);
227 else
229 $im->filledRectangle($xspace-$middle,$yspace,
230 $xspace+$middle,$YGRAPH,$colour);
231 $im->rectangle($xspace-$middle,$yspace,
232 $xspace+$middle,$YGRAPH,$fg);
235 close (DATA);
238 $nextdata="Y";
239 # TOP LEFT is 0,0 on GIF (image)
240 # origin of plot is xinit,yinit
241 # print "little line\n";
242 $im->line($xspace,$yspace,$xspace,$YGRAPH,$blue);
243 $im->line($xspace,$YGRAPH,$XINIT,$YGRAPH,$blue);
244 # (0,0) in cartesian space time=0 minutes, rate 0 packets/s
245 $im->line($XINIT,$YGRAPH,$XINIT,$YGRAPH,$blue);
246 $im->line($XINIT,$YGRAPH,$XINIT,$YGRAPH,$green);
248 } # close foreach loop on data file names
253 if ($option{'Average'} eq 1)
255 # Line style
256 $im->setStyle($acolour,$acolour,$acolour,$acolour,$bg,$bg,$bg,$bg);
257 $m=$m/$i;
258 $ym=$YGRAPH-($m-$option{'MINYVALUE'})*$SCALE;
259 $im->line($XINIT,$ym,$XGRAPH,$ym,gdStyled)
261 $im->line($XINIT,$YINIT,$XINIT,$YGRAPH,$fg);
262 $im->line($XINIT,$YINIT,$XGRAPH,$YINIT,$fg);
263 $im->line($XGRAPH,$YINIT,$XGRAPH,$YGRAPH,$fg);
264 $im->line($XINIT,$YGRAPH,$XGRAPH,$YGRAPH,$fg);
266 $im->string(gdSmallFont,$XGIF-335,$YGIF - 12,"Time of Day (hours)",$fg);
267 open (GRAPH,">$option{'Graph'}") || die "Error: Grafico.gif - $!\n";
268 print GRAPH $im -> gif;
269 close (GRAPH);
274 } # end of subroutine main
276 main;
277 exit(0);
279 sub printusage {
280 print <<USAGEDESC;
282 usage:
283 graphmaker [-options ...]
285 where options include:
286 -help print out this message
287 -conf file the configuration file (default graphmaker.cnf)
289 If you want to know more about this tool, you might want
290 to read the docs. They came together with graphmaker!
292 Home: http://www.geocities.com/CapeCanaveral/Lab/3469/graphmaker.html
294 USAGEDESC
295 exit(1);