#!/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 $IMGFMT=lc $CONFIG{"IMAGEFORMAT"};

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

my $FORCE=@ARGV;
my $TYPE;
undef my @ITEMS;
undef my @OPTIONS;

# read module-specific settings
#
my $LOWBORDER=0;
my $UPPERBORDER=10;
foreach (HotSaNICparser::read_settings(".")) {
  my ($var,$value)=HotSaNICparser::parse_line($_);
  if ($var eq "HOST") { push @ITEMS,$value; }
  if ($var eq "SCALE") { if($value ne "lin") { push @OPTIONS,"-o"; $LOWBORDER=0.0001; } }
  if ($var eq "TYPE") { $TYPE=$value; }
  if ($var eq "UPPER") { $UPPERBORDER=$value; }
  }
 
$UPPERBORDER/=1000;

my $LEGEND="sec.";

# generate diagrams
#
foreach (@ITEMS) {
  my ($dev,$description)=split /,/;

  my $name=$dev;

# if database exists, create images...
  if ( -e "./rrd/$name.rrd") {
    print "creating images for $dev ($description) ...\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);

	if ($range eq "1h") {
          ($prints,$xs,$ys)=RRDs::graph $TEMPNAME, "-i", "-b","1000", "-v",$LEGEND,
            "-s","-$fullrange", "-w",$CONFIG{WIDTH}, "-h",$CONFIG{HEIGHT}, @OPTIONS, "-a", uc($IMGFMT),
            "-u $UPPERBORDER", "-l $LOWBORDER", "--title", "ping-times to $dev - last $descr ($DATESTRING)",
            "DEF:min=rrd/$dev.rrd:min:AVERAGE",
            "DEF:avg=rrd/$dev.rrd:avg:AVERAGE",
            "DEF:max=rrd/$dev.rrd:max:AVERAGE",
            "CDEF:wrong=max,0,EQ,INF,UNKN,IF",
            "AREA:max#a0a0ff:min/max",
            "GPRINT:min:MIN:  (%6.2lf%s /\\g",
            "GPRINT:max:MAX: %6.2lf%s $LEGEND)\\n",
            "LINE1:max#c00000:max",
            "GPRINT:max:MIN:  (min\\:%6.2lf%s /\\g",
            "GPRINT:max:AVERAGE: avg\\:%6.2lf%s /\\g",
            "GPRINT:max:MAX: max\\:%6.2lf%s $LEGEND)\\n",
            "AREA:min#ffffff:",
            "LINE1:avg#000000:avg",
            "GPRINT:avg:MIN:  (min\\:%6.2lf%s /\\g",
            "GPRINT:avg:AVERAGE: avg\\:%6.2lf%s /\\g",
            "GPRINT:avg:MAX: max\\:%6.2lf%s $LEGEND)\\n",
            "LINE1:min#00c000:min",
            "GPRINT:min:MIN:  (min\\:%6.2lf%s /\\g",
            "GPRINT:min:AVERAGE: avg\\:%6.2lf%s /\\g",
            "GPRINT:min:MAX: max\\:%6.2lf%s $LEGEND)\\n",
            "AREA:wrong#ffffa0:unreachable",
            "HRULE:0#000000";
          }
        else {
	  if ($TYPE eq "simple") {
            ($prints,$xs,$ys)=RRDs::graph $TEMPNAME, "-i", "-b","1000", "-v",$LEGEND,
              "-s","-$fullrange", "-w",$CONFIG{WIDTH}, "-h",$CONFIG{HEIGHT}, @OPTIONS, "-a", uc($IMGFMT),
              "-u $UPPERBORDER", "-l $LOWBORDER", "--title", "ping-times to $dev - last $descr ($DATESTRING)",
              "DEF:mini=rrd/$dev.rrd:min:MIN",
              "DEF:mina=rrd/$dev.rrd:min:AVERAGE",
              "DEF:minx=rrd/$dev.rrd:min:MAX",
              "DEF:avgi=rrd/$dev.rrd:avg:MIN",
              "DEF:avga=rrd/$dev.rrd:avg:AVERAGE",
              "DEF:avgx=rrd/$dev.rrd:avg:MAX",
              "DEF:maxi=rrd/$dev.rrd:max:MIN",
              "DEF:maxa=rrd/$dev.rrd:max:AVERAGE",
              "DEF:maxx=rrd/$dev.rrd:max:MAX",
              "CDEF:wrong=maxx,0,EQ,INF,UNKN,IF",
              "AREA:maxx#c0c0ff:",
              "AREA:mini#ffffff:",
              "LINE1:maxx#c00000:max",
              "GPRINT:maxi:MIN:  (min\\:%6.2lf%s /\\g",
              "GPRINT:maxa:AVERAGE: avg\\:%6.2lf%s /\\g",
              "GPRINT:maxx:MAX: max\\:%6.2lf%s $LEGEND)\\n",
              "LINE1:avga#000000:avg",
              "GPRINT:avgi:MIN:  (min\\:%6.2lf%s /\\g",
              "GPRINT:avga:AVERAGE: avg\\:%6.2lf%s /\\g",
              "GPRINT:avgx:MAX: max\\:%6.2lf%s $LEGEND)\\n",
              "LINE1:mini#00c000:min",
              "GPRINT:mini:MIN:  (min\\:%6.2lf%s /\\g",
              "GPRINT:mina:AVERAGE: avg\\:%6.2lf%s /\\g",
              "GPRINT:minx:MAX: max\\:%6.2lf%s $LEGEND)\\n",
              "AREA:wrong#ffffa0:unreachable",
              "HRULE:0#000000";
            }
          else {
            ($prints,$xs,$ys)=RRDs::graph $TEMPNAME, "-i", "-b","1000", "-v",$LEGEND,
              "-s","-$fullrange", "-w",$CONFIG{WIDTH}, "-h",$CONFIG{HEIGHT}, @OPTIONS, "-a", uc($IMGFMT),
              "-u $UPPERBORDER", "-l $LOWBORDER", "--title", "ping-times to $dev - last $descr ($DATESTRING)",
              "DEF:mini=rrd/$dev.rrd:min:MIN",
              "DEF:mina=rrd/$dev.rrd:min:AVERAGE",
              "DEF:minx=rrd/$dev.rrd:min:MAX",
              "DEF:avgi=rrd/$dev.rrd:avg:MIN",
              "DEF:avga=rrd/$dev.rrd:avg:AVERAGE",
              "DEF:avgx=rrd/$dev.rrd:avg:MAX",
              "DEF:maxi=rrd/$dev.rrd:max:MIN",
              "DEF:maxa=rrd/$dev.rrd:max:AVERAGE",
              "DEF:maxx=rrd/$dev.rrd:max:MAX",
              "CDEF:wrong=maxx,0,EQ,INF,UNKN,IF",
              "AREA:maxx#ffc0c0:max",
              "GPRINT:maxi:MIN:  (min\\:%6.2lf%s /\\g",
              "GPRINT:maxa:AVERAGE: avg\\:%6.2lf%s /\\g",
              "GPRINT:maxx:MAX: max\\:%6.2lf%s $LEGEND)\\n",
              "AREA:maxi#ffffff:",
              "AREA:avgx#c0c0ff:avg",
              "GPRINT:avgi:MIN:  (min\\:%6.2lf%s /\\g",
              "GPRINT:avga:AVERAGE: avg\\:%6.2lf%s /\\g",
              "GPRINT:avgx:MAX: max\\:%6.2lf%s $LEGEND)\\n",
              "AREA:avgi#ffffff:",
              "AREA:minx#c0ffc0:min",
              "GPRINT:mini:MIN:  (min\\:%6.2lf%s /\\g",
              "GPRINT:mina:AVERAGE: avg\\:%6.2lf%s /\\g",
              "GPRINT:minx:MAX: max\\:%6.2lf%s $LEGEND)\\n",
              "AREA:mini#ffffff:",
              "LINE1:maxa#c00000:",
              "LINE1:mina#00c000:",
              "LINE1:avga#000000:",
              "AREA:wrong#ffffa0:unreachable",
              "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;

