#!/usr/bin/perl
$TMP = "/tmp/tc_snmp" ;
$RRDS = "/docum.org/project/tc-snmp/example/data" ;

##### No editing needed below this line ##############
#
use CGI qw (:standard) ;
use RRDs ;

$webcgi = url ; # Get our URL
$handle = param ('handle') or die "parameter handle missing" ;

$show      = param('show') ;
$interface = param('interface') ;
$graph     = param('graph') ;
$childs    = param('childs') ;

@color = ("#00FF00","#0000FF","#FF0000","#FFFF00") ;
$date = localtime ;


# Check an/or create the needed directories
if (not -d $TMP) {
	mkdir $TMP,0755 ;
}
if (not -d "$TMP/images") {
	mkdir "$TMP/images",0755 ;
}
open (DEBUG,">>$TMP/debug2") ;

# 4 type of graphs :
#   1. Last 48 hours
#   2. Last 12 days
#   3. Last 48 days
#   4. Last 2 years
#
@graphs = (1,2,3,4) ;
$start{1} = "e-48h";
$start{2} = "e-12d";
$start{3} = "e-48d";
$start{4} = "e-576d";

# If we have to draw a graph, make one :
if($graph) {

  print header (-type=>'image/png',-expires=>'+5m');
  # We store the image in a temp-file
  $graph_png = $TMP . "/images/" . $interface . "." . $handle . $childs . "." . $show . "." . $graph . ".png" ;

  # Create a png image
  @option = ("-s","$start{$graph}","-a","PNG","--lazy") ;

  $color=0;

  if ( $show =~ /sum/ ) {
    if ( $show =~ /bytes/ ) {
      $show = "bytes" ;
    } elsif ( $show =~ /packets/ ) {
      $show = "packets" ;
    }
    @childs = sort(split "_", $childs) ;
    foreach $child (@childs) {
      $temp = $child ;
      $temp =~ s/:/_/g ;
      $child =~ s/:/\\:/g ;
      $rrd = $RRDS . "/" . $interface . "." . $temp . "." . $show . ".rrd" ;
print DEBUG "$rrd\n" ;
      push (@option,"DEF:X$temp=$rrd:$show:AVERAGE") ;
      if ( $color == "0" ) {
        push (@option, "AREA:X$temp$color[$color]:$child:") ;
      } else {
        push (@option,"STACK:X$temp$color[$color]:$child:") ;
      }
      push (@option,"GPRINT:X$temp:LAST:Current \\: %5.1lf $type/s\\n") ;
      $color ++ ;
    }
  } else {
    $rrd = $RRDS . "/" . $interface . "." . $handle . "." . $show . ".rrd" ;
    $rrd =~ s/:/_/g ;

    # Show the current value
    $handle =~ s/:/\\:/g ;
    push (@option,"DEF:handle=$rrd:$show:AVERAGE") ;
    push (@option,"AREA:handle$color[$color]:$handle:") ;
    push (@option,"COMMENT:\\n") ;
    push (@option,"GPRINT:handle:LAST:Current \\: %5.1lf $show/s\\n") ;
    push (@option,"GPRINT:handle:MIN:Min     \\: %5.1lf $show/s\\n") ;
    push (@option,"GPRINT:handle:AVERAGE:Average \\: %5.1lf $show/s\\n") ;
    push (@option,"GPRINT:handle:MAX:Max     \\: %5.1lf $show/s\\n") ;
    #$color ++ ;
    #push (@option,"DEF:handle_min=$rrd:$show:MIN") ;
    #push (@option,"LINE2:handle_min$color[$color]:Minimum:") ;
    #push (@option,"COMMENT:\\n") ;
    #$color ++ ;
    #push (@option,"DEF:handle_max=$rrd:$show:MAX") ;
    #push (@option,"LINE2:handle_max$color[$color]:Maximum:") ;
    #push (@option,"COMMENT:\\n") ;
    # Show the maximum value
    #	push (@option,"DEF:handle_max=$rrd:$show:MAX") ;
    #	push (@option,"LINE1:handle_max$color[$color]:$handle:") ;
    #	push (@option,"GPRINT:handle_max:MAX:Max     \\: %5.1lf $show/s\\n") ;
    #	$color ++ ;
    # Show the minimum value
    #	push (@option,"DEF:handle_min=$rrd:$show:MIN") ;
    #	push (@option,"LINE1:handle_min$color[$color]:$handle:") ;
    #	push (@option,"GPRINT:handle_min:MIN:Min     \\: %5.1lf $show/s\\n") ;
  }

  push (@option,"COMMENT:$date") ;
  RRDs::graph($graph_png, @option ) ;
  my ($err) = RRDs::error;
  die $err if $err;

  open ( GRAPH, "<$graph_png" ) ;
  binmode(GRAPH);
  while ($len = read(GRAPH,$bits,8192)) {
    $pic .= $bits;
  }
  print $pic;

  # If we have a show, show the 4 possible graphs :
} elsif ($show) {
	print header () ;
	print start_html (-style=>{'code'=>"$CSS"}) ;
#print qq{<html><head>
#<META HTTP-EQUIV="REFRESH" CONTENT="300">
#</head><body>
#} ;

#<META HTTP-EQUIV="EXPIRES" CONTENT="Sat, 01 Jan 2001 00:00:00 GMT">
	print "<H1>Handle $handle on interface $interface</H1>\n" ; 
	foreach my $graph (@graphs) {
		if ( defined $childs ) {
			print "<P><IMG SRC=$webcgi?handle=$handle&show=$show&interface=$interface&childs=$childs&graph=$graph BORDER=0>\n" ;
		} else {
			print "<P><IMG SRC=$webcgi?handle=$handle&show=$show&interface=$interface&graph=$graph BORDER=0>\n" ;
		}
	}
	print qq{</BODY></HTML>} ;

# Show all the possible types of graph we can draw :
} else {
	print header ;
	print qq{<html><head>
<META HTTP-EQUIV="REFRESH" CONTENT="300">
<link rel="stylesheet" type="text/css" href="/qos/qos.css" />
</head><body>
} ;
#<META HTTP-EQUIV="EXPIRES" CONTENT="Sat, 01 Jan 2001 00:00:00 GMT">
	if ( @childs ) {
		print "<H1>Handle $handle on interface $interface with childs @childs</H1>\n" ;
	} else {
		print "<H1>Handle $handle on interface $interface</H1>\n" ;
	}

if ( defined $childs ) {
	print qq{
<P><A HREF=$webcgi?handle=$handle&show=bytes&interface=$interface>
  <IMG SRC=$webcgi?handle=$handle&show=bytes&interface=$interface&graph=1 BORDER=0></A>
<P><A HREF=$webcgi?handle=$handle&show=packets&interface=$interface>
  <IMG SRC=$webcgi?handle=$handle&show=packets&interface=$interface&graph=1 BORDER=0></A>
</BODY></HTML>} ;
} else {
	print qq{
<P><A HREF=$webcgi?handle=$handle&show=bytes&interface=$interface&childs=$childs>
  <IMG SRC=$webcgi?handle=$handle&show=bytes&interface=$interface&childs=$childs&graph=1 BORDER=0></A>
<P><A HREF=$webcgi?handle=$handle&show=packets&interface=$interface&childs=$childs>
  <IMG SRC=$webcgi?handle=$handle&show=packets&interface=$interface&childs=$childs&graph=1 BORDER=0></A>
</BODY></HTML>} ;
}
}
