#!/usr/bin/perl
use CGI_Lite;
use lib qw(/usr/local/rrdtool/lib/perl);
use RRDs;
use Getopt::Long;
use GD;
################################################################
# Configuration parameters
$WGET = "/usr/local/bin/wget -qO -";
$CONFIG = "/weathermap/weathermap.conf";
$OUTPUT = "historic.png";
$DEBUG = 0;
$WIDTH = 880;
$HEIGHT = 750;
$rrdtool="/usr/local/rrdtool/bin/rrdtool";
#################################################################
$cgi=new CGI_Lite;
%in = $cgi->parse_form_data;
&read_config($CONFIG);
if($background){
open (PNG,"$background" ) || die "$background: $!\n";
$map = newFromPng GD::Image(PNG) || die "newFromPng failed.";
close PNG;
} else {
$map=new GD::Image($WIDTH,$HEIGHT);
}
&alloc_colors;
foreach $link (keys %target){
$data = $target{$link};
open(LOG, "$data" );
($input{$link},$output{$link})=&get_data;
close(LOG);
}
foreach $link (keys %target){
$outrate=(int(($output{$link}/$maxbytes{$link}+0.005)*100)>100) ? 100:in
t(($output{$link}/$maxbytes{$link})*100);
$inrate=(int(($input{$link}/$maxbytes{$link}+0.005)*100)>100) ? 100:int(
($input{$link}/$maxbytes{$link})*100);
if($output{$link} != 0 && $outrate == 0) { $outrate=1 }
if($input{$link} != 0 && $inrate == 0) { $inrate=1 }
# draw lines...
$width{$link}=sqrt($bandwidth{$link})/4;
&draw_arrow(
$xpos{$nodea{$link}},
$ypos{$nodea{$link}},
&middle($xpos{$nodea{$link}},$xpos{$nodeb{$link}}),
&middle($ypos{$nodea{$link}},$ypos{$nodeb{$link}}),
$width{$link}, 1, &select_color($outrate));
&draw_arrow(
$xpos{$nodea{$link}},
$ypos{$nodea{$link}},
&middle($xpos{$nodea{$link}},$xpos{$nodeb{$link}}),
&middle($ypos{$nodea{$link}},$ypos{$nodeb{$link}}),
$width{$link}, 0, $black);
&label(&middle($xpos{$nodea{$link}},&middle($xpos{$nodea{$link}},$xpos{$
nodeb{$link}})),
&middle($ypos{$nodea{$link}},&middle($ypos{$nodea{$link}},$ypos{
$nodeb{$link}})),
$outrate . "%", 0);
&draw_arrow(
$xpos{$nodeb{$link}},
$ypos{$nodeb{$link}},
&middle($xpos{$nodea{$link}},$xpos{$nodeb{$link}}),
&middle($ypos{$nodea{$link}},$ypos{$nodeb{$link}}),
$width{$link}, 1, &select_color($inrate));
&draw_arrow(
$xpos{$nodeb{$link}},
$ypos{$nodeb{$link}},
&middle($xpos{$nodea{$link}},$xpos{$nodeb{$link}}),
&middle($ypos{$nodea{$link}},$ypos{$nodeb{$link}}),
$width{$link}, 0, $black);
&label(&middle($xpos{$nodeb{$link}},&middle($xpos{$nodea{$link}},$xpos{$
nodeb{$link}})),
&middle($ypos{$nodeb{$link}},&middle($ypos{$nodea{$link}},$ypos{
$nodeb{$link}})),
$inrate . "%", 0);
}
foreach(keys %xpos){
&label($xpos{$_},$ypos{$_},$label{$_}, 3);
}
&annotation;
# print image...
open(PNG,">$OUTPUT" )||die("$OUTPUT: $!\n" );
print PNG $map->png;
close PNG;
# print labels
sub label{
my($xpos,$ypos,$label,$pad)=@_;
my($strwidth)=(gdLargeFont->width*length($label))-3;
my($strheight)=(gdLargeFont->height)-3;
if ($strwidth > 0){
$map->filledRectangle(
$xpos-$strwidth/2-$pad-1, $ypos-$strheight/2-$pad+1,
$xpos+$strwidth/2+$pad, $ypos+$strheight/2+$pad-1,
$black);
$map->filledRectangle(
$xpos-$strwidth/2-$pad, $ypos-$strheight/2-$pad+2,
$xpos+$strwidth/2+$pad-1, $ypos+$strheight/2+$pad-2,
$white);
$map->string(gdLargeFont,
$xpos-$strwidth/2-1, $ypos-$strheight/2-1,
$label, $black);
}
}
# print annotation
sub annotation{
my($title)="Traffic load";
$strwidth=gdLargeFont->width*length($label{$_});
$strheight=gdLargeFont->height;
$map->string(gdLargeFont, 570, 555, "Etat du r├®seau le $in{'jour'} $in{
'mois'} $in{'annee'} a $in{'heure'}h et $in{'minute'}min", $black);
$map->filledRectangle($keyxpos,$keyypos,
$keyxpos+gdLargeFont->width*length($title)+10,
$keyypos+gdLargeFont->height*($scales+1)+10,
$gray);
$map->rectangle($keyxpos,$keyypos,
$keyxpos+gdLargeFont->width*length($title)+10,
$keyypos+gdLargeFont->height*($scales+1)+10,
$black);
$map->string(gdLargeFont,
$keyxpos+4,
$keyypos+4,
"Traffic load", $black);
my($i)=1;
foreach(sort {$scale_low{$a}<=>$scale_low{$b}} keys %scale_low){
$map->filledRectangle(
$keyxpos+6,
$keyypos+gdLargeFont->height*$i+8,
$keyxpos+6+16,
$keyypos+gdLargeFont->height*$i+gdLargeFont->height+6,
$color{$_});
$map->string(gdLargeFont,
$keyxpos+6+20,
$keyypos+gdLargeFont->height*$i+8,
"$scale_low{$_}-$scale_high{$_}%", $black);
$i++
}
}
sub select_color {
my($rate)=($_[0]>100) ? 100:$_[0];
if($rate=="0" ){return($darkgray)}
foreach(sort {$scale_high{$a}<=>$scale_high{$b}} keys %scale_high){
if($scale_low{$_}<=$rate && $rate<=$scale_high{$_}){
return($color{$_});
}
}
}
sub alloc_colors {
$white=$map->colorAllocate(255,255,255);
$gray=$map->colorAllocate(248,248,248);
$black=$map->colorAllocate(0,0,0);
$darkgray=$map->colorAllocate(128,128,128);
foreach(keys %scale_red){
$color{$_} = $map->colorAllocate($scale_red{$_},$scale_green{$_
,$scale_blue{$_});
}
}
sub read_config {
my($config)=shift;
my($node,$link);
$scales=0;
open(CONF,$config) or die "$config: $!\n";
while(<CONF> ){
if(/^\s*BACKGROUND\s+(\S+)/i){
if(-s "$1" ){
$background=$1;
}
}
if(/^\s*WIDTH\s+(\d+)/i){
if("$1" ne "" ){
$WIDTH=$1;
}
}
if(/^\s*HEIGHT\s+(\d+)/i){
if("$1" ne "" ){
$HEIGHT=$1;
}
}
if(/^\s*NODE\s+(\w+)/i){
$node=$1;
}
if(/^\s*POSITION\s+(\d+)\s+(\d+)/i){
$xpos{$node}=$1;
$ypos{$node}=$2;
}
if(/^\s*LABEL\s+(\S+)/i){
$label{$node}=$1;
}
if(/^\s*LINK\s+(\S+)/i){
$link=$1;
}
if(/^\s*NODES\s+(\S+)\s+(\S+)/i){
$nodea{$link}=$1;
$nodeb{$link}=$2;
}
if(/^\s*TARGET\s+(\S+)/i){
$target{$link}=$1;
}
if(/^\s*BANDWIDTH\s+(\d+)/i){
$bandwidth{$link}=$1;
$maxbytes{$link}=$bandwidth{$link}*1024/8;
}
if(/^\s*KEYPOS\s+(\d+)\s+(\d+)/i){
$keyxpos=$1;
$keyypos=$2;
}
if(/^\s*SCALE\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/i){
$scale_low{"$1:$2"}=$1;
$scale_high{"$1:$2"}=$2;
$scale_red{"$1:$2"}=$3;
$scale_green{"$1:$2"}=$4;
$scale_blue{"$1:$2"}=$5;
$scales++;
}
}
}
sub middle{
return int( $_[0] + ($_[1]-$_[0])/2 )
}
sub dist{
return int( sqrt( $_[0]*$_[0] + $_[1]*$_[1] ) )
}
sub newx{
my($a,$b,$x,$y)=@_;
return int( cos( atan2($y,$x) + atan2($b,$a) ) * sqrt( $x*$x + $y*$y )
;
}
sub newy{
my($a,$b,$x,$y)=@_;
return int( sin( atan2($y,$x) + atan2($b,$a) ) * sqrt( $x*$x + $y*$y )
;
}
sub draw_arrow {
my($x1,$y1,$x2,$y2,$w,$solid,$color)=($_[0],$_[1],$_[2],$_[3],$_[4],$_[
],$_[6]);
my($arrow)=new GD::Polygon;
$arrow->addPt(
$x1 + &newx($x2-$x1, $y2-$y1, 0, $w),
$y1 + &newy($x2-$x1, $y2-$y1, 0, $w)
);
$arrow->addPt(
$x2 + &newx($x2-$x1, $y2-$y1, -4*$w, $w),
$y2 + &newy($x2-$x1, $y2-$y1, -4*$w, $w)
);
$arrow->addPt(
$x2 + &newx($x2-$x1, $y2-$y1, -4*$w, 2*$w),
$y2 + &newy($x2-$x1, $y2-$y1, -4*$w, 2*$w)
);
$arrow->addPt( $x2, $y2);
$arrow->addPt(
$x2 + &newx($x2-$x1, $y2-$y1, -4*$w, -2*$w),
$y2 + &newy($x2-$x1, $y2-$y1, -4*$w, -2*$w)
);
$arrow->addPt(
$x2 + &newx($x2-$x1, $y2-$y1, -4*$w, -$w),
$y2 + &newy($x2-$x1, $y2-$y1, -4*$w, -$w)
);
$arrow->addPt(
$x1 + &newx($x2-$x1, $y2-$y1, 0, -$w),
$y1 + &newy($x2-$x1, $y2-$y1, 0, -$w)
);
if($solid){
$map->filledPolygon($arrow,$color);
}else{
$map->polygon($arrow,$color);
}
}
# rechercher les donn├®es dans le fichier rrd
sub get_data
{
$lower_to=$to;
$lower_to =~ tr/A-Z/a-z/;
$lower_from=$from;
$lower_from =~ tr/A-Z/a-z/;
@output=`$rrdtool fetch $data AVERAGE -s -10min`;
my @return_info;
foreach(@output)
{
if(/^(\d+):\s(\d\.\d{10}e\+\d{2})\s(\d\.\d{10}e\+\d{2})/ )
{ @return_info = ($2,$3); }
}
return @return_info;
}
print "Content-type: text/html\n\n";
print "<html><head>\n";
print "<title></title></head>\n";
print "<BODY BGCOLOR=#FFFFFF LEFTMARGIN=0 TOPMARGIN=0 MARGINWIDTH=0 MARGINHEIGH
=0>";
print "<IMG SRC=\"/weathermap/weathermap.png\" WIDTH=900 HEIGHT=620 BORDER=0>";
print "</body></html>\n";
voila c la source dem on programme
n'etudiez pas le programme an fait c un programme weathermap qui génere un png en focntion de données snmp de traffic sur un réseau