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