#!/usr/bin/env perl
use strict;
use warnings;
use diagnostics;

use lib "../../lib";
use HotSaNICparser;
use HotSaNICdiagram;
use RRDs;

# read global settings
#
my $MODNAME=HotSaNICparser::get_module_name();
my %CONFIG=HotSaNICparser::get_config("../..");

my $OUTDIR=$CONFIG{"WEBDIR"}."/".lc $MODNAME;
if ( ! -e $OUTDIR ) { mkdir $OUTDIR,0755; }

my $IMGFMT=lc $CONFIG{"IMAGEFORMAT"};
my $TEMPNAME="$OUTDIR/temp.$IMGFMT";

my $FORCE=@ARGV;
my @ITEMS;
my $STYLE="bytes";
my $MULT=1;

# read module-specific settings
#
foreach (HotSaNICparser::read_settings(".")) {
  my ($var,$value)=HotSaNICparser::parse_line($_);
  if ($var eq "STYLE") {
    $STYLE=lc $value;
    if ($STYLE eq "bits") { $MULT=8; }
    }
  if (index($var,"DEV") >= 0 ) {
    $var = lc $var;
    $var =~ s/dev//g;
    push @ITEMS,"$var$value";
    }
  }

my $LEGEND="$STYLE/s";

# generate diagrams
#
foreach (@ITEMS) {
  my ($dev,$devname)=split /,/;
  (my $name=$dev)=~ s/\//_/g;

# if database exists, create images...
  if ( -e "./rrd/$name.rrd") {
    print "creating images for $dev ($devname) ...\n";

    foreach my $range ("1h","6h","1day","1week","1month","1year") {
      my ($descr,$file,$build,$fullrange,$DATESTRING)=HotSaNICdiagram::get_diagram_properties($range);

      my $FILENAME="$OUTDIR/$name-$file.$IMGFMT";

      my $make=$FORCE;
      if ($range eq "1h") { $make++; }
      elsif (! -e "$FILENAME") { $make++; }
      else  {
        my (undef,undef,undef,undef,undef,undef,undef,undef,undef,$mtime,undef,undef,undef) = stat( "$FILENAME" );
        if (time > ($mtime+$build)) { $make++; }
        }

      if ($make>0) {
	
        my ($prints,$xs,$ys);

        ($prints,$xs,$ys)=RRDs::graph $TEMPNAME, "-i", "-b","1024", "-v", $LEGEND,
          "-s","-$range", "-w",$CONFIG{"WIDTH"}, "-h",$CONFIG{"HEIGHT"}, "-a", uc($IMGFMT),
          "-u 1000", "-l -1000", "--title", "traffic to $dev - last $descr ($DATESTRING)",
          "DEF:dbtcpin=rrd/$name.rrd:tcpin:AVERAGE",
          "DEF:dbudpin=rrd/$name.rrd:udpin:AVERAGE",
          "DEF:dbicmpin=rrd/$name.rrd:icmpin:AVERAGE",
          "DEF:dbtcpout=rrd/$name.rrd:tcpout:AVERAGE",
          "DEF:dbudpout=rrd/$name.rrd:udpout:AVERAGE",
          "DEF:dbicmpout=rrd/$name.rrd:icmpout:AVERAGE",
          "CDEF:tcpin=dbtcpin,$MULT,*",
          "CDEF:udpin=dbudpin,$MULT,*",
          "CDEF:icmpin=dbicmpin,$MULT,*",
          "CDEF:allin=tcpin,udpin,icmpin,+,+",
          "CDEF:spanin=allin,$fullrange,*",
          "CDEF:tcpout=dbtcpout,$MULT,*",
          "CDEF:udpout=dbudpout,$MULT,*",
          "CDEF:icmpout=dbicmpout,$MULT,*",
          "CDEF:tcpout_inv=0,tcpout,-",
          "CDEF:udpout_inv=0,udpout,-",
          "CDEF:icmpout_inv=0,icmpout,-",
          "CDEF:allout=tcpout,udpout,icmpout,+,+",
          "CDEF:spanout=allout,$fullrange,*",
          "AREA:tcpin#00ff00:tcp  in",
          "GPRINT:tcpin:MIN:(min\\: %6.2lf %s",
          "GPRINT:tcpin:AVERAGE:avg\\: %6.2lf %s",
          "GPRINT:tcpin:MAX:max\\: %6.2lf %s $LEGEND)\\n",
          "STACK:udpin#00bb00:udp  in",
          "GPRINT:udpin:MIN:(min\\: %6.2lf %s",
          "GPRINT:udpin:AVERAGE:avg\\: %6.2lf %s",
          "GPRINT:udpin:MAX:max\\: %6.2lf %s $LEGEND)\\n",
          "STACK:icmpin#007700:other in",
          "GPRINT:icmpin:MIN:(min\\: %6.2lf %s",
          "GPRINT:icmpin:AVERAGE:avg\\: %6.2lf %s",
          "GPRINT:icmpin:MAX:max\\: %6.2lf %s $LEGEND)\\n",
          "GPRINT:allin:AVERAGE:   total average\\: %6.2lf %s $LEGEND",
          "GPRINT:spanin:AVERAGE: - %6.2lf %s $STYLE/$descr\\n",
          "AREA:tcpout_inv#ff0000:tcp  out",
          "GPRINT:tcpout:MIN:(min\\: %6.2lf %s",
          "GPRINT:tcpout:AVERAGE:avg\\: %6.2lf %s",
          "GPRINT:tcpout:MAX:max\\: %6.2lf %s $LEGEND)\\n",
          "STACK:udpout_inv#bb0000:udp  out",
          "GPRINT:udpout:MIN:(min\\: %6.2lf %s",
          "GPRINT:udpout:AVERAGE:avg\\: %6.2lf %s",
          "GPRINT:udpout:MAX:max\\: %6.2lf %s $LEGEND)\\n",
          "STACK:icmpout_inv#770000:other out",
          "GPRINT:icmpout:MIN:(min\\: %6.2lf %s",
          "GPRINT:icmpout:AVERAGE:avg\\: %6.2lf %s",
          "GPRINT:icmpout:MAX:max\\: %6.2lf %s $LEGEND)\\n",
          "GPRINT:allout:AVERAGE:   total average\\: %6.2lf %s $LEGEND",
          "GPRINT:spanout:AVERAGE: - %6.2lf %s $STYLE/$descr\\n",
          "HRULE:0#000000";
            
        rename $TEMPNAME,$FILENAME;
        printf "  %-7s %d"."x%d %s\n",$descr,$xs,$ys,$FILENAME;
        }
      }
    }
  else { print "  No database found\n  Please check if the daemon is running.\n";next; }
  print "\n";
  }
exit 0;


