4 # an example in not so hot perl programming....
5 # based around GraphMaker from Fabrizio Pivari
6 # A graph maker perl script
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");
22 NUMBERYCELLGRIDSIZE
=> '8',
25 XCELLGRIDSIZE
=> '18',
27 Data
=> './graphmaker.dat',
28 Graph
=> './graphmaker.gif',
32 Title
=> 'GraphMaker 2.1',
47 &GetOptions
("conf=s","help") || &printusage
;
50 if ($opt_help) {&printusage
};
52 open (CNF
, $opt_conf) || die;
54 s/\t/ /g; #replace tabs by space
55 next if /^\s*\#/; #ignore comment lines
56 next if /^\s*$/; #ignore empty lines
59 if (/\s*$elem\s*:\s*(.*)/) { $option{$elem}=$1; }
63 #########################################
67 # number datapoints/24 hours is 1440 (minutes)
69 # Split into N graphs where each graph has max of 240 datapoints (4 hours)
75 $YCELLGRIDSIZE = $YGRIDSIZE/$option{'NUMBERYCELLGRIDSIZE'};
80 #$XGRIDSIZE = ($option{'XMAX'}*$option{'XCELLGRIDSIZE'});
81 #$XGRIDSIZE = (240*$option{'XCELLGRIDSIZE'});
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;
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
105 # LINE COLOUR HELP BY VAR $colour
109 if ($option{'Transparent'} eq "yes") {$im->transparent($bg)};
110 $im->filledRectangle(0,0,$XGIF,$YGIF,$bg);
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);
123 $posis=$num - ($num/60)*60;
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;
146 if ($odd_even eq 1) {$middle = $option{'XCELLGRIDSIZE'}/2 +0.5;}
147 else {$middle = $option{'XCELLGRIDSIZE'}/2 +0.5;}
150 # open (DATA,$option{'Data'}) || die "cant open $option{'Data'}";
151 # nextdata becomes Y on reading of second data set....
153 @datafiles=("./in.dat" , "./out.dat" );
154 foreach ( @datafiles )
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";
169 if ($option{'Average'} eq 1) {$m+=$2;$i++;}
170 if ($count eq 0){$XOLD=$1;$YOLD=$2;$count=1;next}
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'};
184 #$im->line($XINIT,$YGRAPH,$X,$Y,$orange);
185 $im->line($xspaceold,$yspaceold,$xspace,$yspace,$green);
189 $im->line($xspaceold,$yspaceold,$xspace,$yspace,$red);
196 $im->filledRectangle($xspaceold,$yspaceold,
197 $xspaceold+$middle,$YGRAPH,$colour);
198 $im->rectangle($xspaceold,$yspaceold,
199 $xspaceold+$middle,$YGRAPH,$fg);
203 $im->filledRectangle($xspaceold-$middle,$yspaceold,
204 $xspaceold+$middle,$YGRAPH,$colour);
205 $im->rectangle($xspaceold-$middle,$yspaceold,
206 $xspaceold+$middle,$YGRAPH,$fg);
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);
229 $im->filledRectangle($xspace-$middle,$yspace,
230 $xspace+$middle,$YGRAPH,$colour);
231 $im->rectangle($xspace-$middle,$yspace,
232 $xspace+$middle,$YGRAPH,$fg);
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)
256 $im->setStyle($acolour,$acolour,$acolour,$acolour,$bg,$bg,$bg,$bg);
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
;
274 } # end of subroutine main
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