Initial import from FreeBSD RELENG_4:
[dragonfly.git] / contrib / ipfilter / perl / Isbgraph
1 #!/usr/local/bin/perl
2
3 # isbgraph
4 # an example in not so hot perl programming....
5 # based around GraphMaker from Fabrizio Pivari
6 # A graph maker perl script
7
8 use GD;
9 use Getopt::Long;
10 $hr=0;
11
12 sub main{
13
14 $opt_conf="./graphmaker.cnf";
15
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");
20
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');
46
47 &GetOptions("conf=s","help") || &printusage ;
48
49
50 if ($opt_help) {&printusage};
51
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)
58    {
59    if (/\s*$elem\s*:\s*(.*)/) { $option{$elem}=$1; }
60    }
61 }
62 close(CNF);
63 #########################################
64 #
65 #
66 #
67 #       number datapoints/24 hours is 1440 (minutes)
68 #
69 #       Split into N graphs where each graph has max of 240 datapoints (4 hours)
70
71
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;
89
90 # NEW IMAGE
91    $im=new GD::Image($XGIF,$YGIF);
92
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);
111
112 # Dot style
113 # vertical markers on Y axis grid
114    $im->setStyle($fg,$bg,$bg,$bg);
115    for $i (0..$option{'XMAX'})
116       {
117       $xspace= $XINIT+$option{'XCELLGRIDSIZE'}*$i +$i;
118      # $im->line($xspace,$YINIT,$xspace,$YGRAPH,gdStyled);
119       $num = $i+1;
120       
121       use integer;
122      {
123       $posis=$num - ($num/60)*60;
124     }
125       if ($posis eq 0)
126          {
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);
132          }
133
134       } # end of scan over X values (minutes)
135
136    $YCELLVALUE=($option{'MAXYVALUE'}-$option{'MINYVALUE'})/$option{'NUMBERYCELLGRIDSIZE'};
137    for $i (0..$option{'NUMBERYCELLGRIDSIZE'})
138       {
139       $num=$option{'MINYVALUE'}+$YCELLVALUE*($option{'NUMBERYCELLGRIDSIZE'}-$i);
140       $im->string(gdMediumBoldFont,0,$YINIT+$YCELLGRIDSIZE*$i -6,"$num",$fg);
141       }
142    $im->string(gdSmallFont,$XGRIDSIZE/2-80,0,$option{'Title'},$fg);
143
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;}
148
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 )
155 {
156    $m=0;
157    $count=0;
158    $i=0;
159    $fname=$_;
160     
161   print "fname $fname\n";
162 #  change entry for red in colour table to green for packets LEAVING target host
163
164    open (DATA,$_) || die "cant open $_";
165    print "$nextdata nextdata\n";
166    while (<DATA>)
167       {
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)
179    {
180
181      if($nextdata eq "Y")
182      {
183         
184          #$im->line($XINIT,$YGRAPH,$X,$Y,$orange);
185          $im->line($xspaceold,$yspaceold,$xspace,$yspace,$green);
186      }
187      else
188      {
189          $im->line($xspaceold,$yspaceold,$xspace,$yspace,$red);
190      } 
191     }
192       else
193          {
194          if ($1 eq 2)
195             {
196             $im->filledRectangle($xspaceold,$yspaceold,
197                                  $xspaceold+$middle,$YGRAPH,$colour);
198             $im->rectangle($xspaceold,$yspaceold,
199                            $xspaceold+$middle,$YGRAPH,$fg);
200             }
201          else
202             {
203             $im->filledRectangle($xspaceold-$middle,$yspaceold,
204                                  $xspaceold+$middle,$YGRAPH,$colour);
205             $im->rectangle($xspaceold-$middle,$yspaceold,
206                            $xspaceold+$middle,$YGRAPH,$fg);
207             }
208          }
209       $XOLD=$X; $YOLD=$Y;
210
211       }  # end of while DATA loop
212
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);
217      
218    if ($option{'Bar'} ne 0)
219       {
220       if ($X eq $option{'XMAX'})
221          {
222          $im->filledRectangle($xspace-$middle,$yspace,
223                               $xspace,$YGRAPH,$colour);
224          $im->rectangle($xspace-$middle,$yspace,
225                         $xspace,$YGRAPH,$fg);
226          }
227       else
228          {
229          $im->filledRectangle($xspace-$middle,$yspace,
230                               $xspace+$middle,$YGRAPH,$colour);
231          $im->rectangle($xspace-$middle,$yspace,
232                         $xspace+$middle,$YGRAPH,$fg);
233          }
234       }
235    close (DATA);
236
237
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);
247
248 } # close foreach loop on data file names
249
250
251
252
253    if ($option{'Average'} eq 1)
254       {
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)
260       }
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);
265
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);
270
271
272
273
274 } # end of subroutine main
275
276 main;
277 exit(0);
278
279 sub printusage {
280     print <<USAGEDESC;
281
282 usage:
283         graphmaker [-options ...]
284
285 where options include:
286     -help                        print out this message
287     -conf  file                  the configuration file (default graphmaker.cnf)
288
289 If you want to know more about this tool, you might want
290 to read the docs. They came together with graphmaker!
291
292 Home: http://www.geocities.com/CapeCanaveral/Lab/3469/graphmaker.html
293
294 USAGEDESC
295     exit(1);
296 }
297