#!/usr/bin/perl -w

use lib "../../lib";
use HotSaNIC;
use RRDs;
#use DBI;

# get cmdline arguments (i.e. options passed by the daemon)
# and other global parameters
#
%ARGS=arg2opt(@ARGV);

dupe_control("start",$ARGS{"MODNAME"},"");

# read module-specific settings
#
open (FILE,"settings");
while (<FILE>) {
  ($var,$value)=parse_line($_);

  if ($var eq "ALL") {$ALL=$value;}
  if ($var eq "ROUTERS") {$ROUTERS=$value;}
  if ($var eq "CLOUD") {$CLOUD=$value;}
  if ($var eq "DEFAULT") {$DEFAULT=$value;}
  if ($var eq "IFACE") {
     ($iface)=split(/,/,$value);
     push @IFACES,$iface;
     next;
     }
  }
close(FILE);

foreach $iface (@IFACES) {
  if ( ! -e "rrd/$iface.rrd" ) { system("./makerrd","$iface", "U") }

  system("$ALL > $iface.all");
  system("$ROUTERS > $iface.routers");
  system("$CLOUD > $iface.cloud");
  system("$DEFAULT > $iface.default");
  system("wget -q http://localhost/cgi-bin/ospfd_database.cgi -O - | grep -c \"Advertising Router\" > $iface.ra");
  system("wget -q http://localhost/cgi-bin/ospfd_database.cgi -O - | grep -c \"Advertising Router: 10.33\" > $iface.rb");
  system("wget -q http://localhost/cgi-bin/ospfd_database.cgi -O - | grep -c \"Advertising Router: 10.32\" > $iface.rc");
  system("wget -q http://localhost/cgi-bin/ospfd_database.cgi -O - | grep -c \"Advertising Router: 10.43\" > $iface.rd");
  system("/sbin/ip ro ls | /bin/grep -c ^10.32 > $iface.re");
  system("/sbin/ip ro ls | /bin/grep -c ^10.43 > $iface.rf");
  system("/sbin/ip ro ls | /bin/grep -c \/16 > $iface.rg");

  open (FILE,"$iface.all");
  while(<FILE>) {
    $all=$_*1;
  }
  close(FILE);
  open (FILE,"$iface.routers");
  while(<FILE>) {
    $routers=$_*1;
  }
  close(FILE);
  open (FILE,"$iface.cloud");
  while(<FILE>) {
    $cloud=$_*1;
  }
  close(FILE);
  open (FILE,"$iface.default");
  while(<FILE>) {
    $default=$_*1;
  }
  close(FILE);
  
# Routing ra
  open (FILE,"$iface.ra");
  while(<FILE>) {
    $ra=$_*1;
  }
  close(FILE);
  
# Routing rb
  open (FILE,"$iface.rb");
  while(<FILE>) {
    $rb=$_*1;
  }
  close(FILE);

# Routing rc
  open (FILE,"$iface.rc");
  while(<FILE>) {
    $rc=$_*1;
  }
  close(FILE);

# Routing rd
  open (FILE,"$iface.rd");
  while(<FILE>) {
    $rd=$_*1;
  }
  close(FILE);
# Routing re
  open (FILE,"$iface.re");
  while(<FILE>) {
    $re=$_*1;
  }
  close(FILE);
# Routing rf
  open (FILE,"$iface.rf");
  while(<FILE>) {
    $rf=$_*1;
  }
  close(FILE);
# Routing rg
  open (FILE,"$iface.rg");
  while(<FILE>) {
    $rg=$_*1;
  }
  close(FILE);
  
  $default=$default/100;
  $all=$all/10;
#  RRDs::update "rrd/$iface.rrd",time.":".$all.":".$routers.":".$cloud.":".$default.":".$ra.":".$rb.":".$rc.":".$rd.":".$re.":".$rf.":".$rg;
  RRDs::update "rrd/$iface.rrd",time.":".$all.":".$routers.":".$cloud.":".$default.":".$ra.":".$rb.":".$rc.":".$rd.":".$re.":".$rf.":".$rg;
  if ($ERROR = RRDs::error) { dupe_control("warn",$ARGS{"MODNAME"},"unable to update `$iface.rrd': $ERROR"); }
}  

