package Monitor::mrtg;

# Author:   Thomas Aeby
#
# Author:   mrwizard@psu.edu (Joseph Gooch)
#
# Authors of the original bb-mrtg.sh:
# 	    Robert-Andre Croteau
# 	    Henrik Olsen
#

require 5.002;
require Exporter;

use strict;
use vars qw(@ISA @EXPORT $VERSION
            $def_timeout $def_proto $max_datasize);

use Carp;

@ISA = qw(Exporter Monitor::Monitor);
$VERSION = 2.02;

sub new
{
    my ($this
        ) = @_;
    my  $class = ref($this) || $this;
    my  $self = {};
    my ($cnt,               # Count through data bytes
        $min_datasize       # Minimum data bytes required
        );

    bless($self, $class);

    print $BigSister::common::prog,", " if ($BigSister::common::dl);
    print $BigSister::common::resources{"mrtgweb"},", " if ($BigSister::common::dl);
    print $BigSister::common::resources{"mrtglog"},"\n" if ($BigSister::common::dl);
    croak("Must specify MRTG's web url") unless $BigSister::common::resources{"mrtgweb"};
    croak("Must specify MRTG's log directory") unless $BigSister::common::resources{"mrtglog"};
    $BigSister::common::resources{"mrtgloghtml"} = 1 unless $BigSister::common::resources{"mrtgloghtml"};

    $self->{"mrtgweb"} = $BigSister::common::resources{"mrtgweb"};
    $self->{"mrtglog"} = $BigSister::common::resources{"mrtglog"};
    $self->{"loghtml"} = $BigSister::common::resources{"mrtgloghtml"};

    my( @bla)  = ();
    $self->{"htable"} = \@bla;

    return($self);
}


sub add_graph {
    my( $self , $host, $item, $mrtgprefix , $column , $warnlev, $paniclev, 
	$maxlev, $units, $in, $out, $bits, $scale) = @_;
    my( $htable ) = $self->{"htable"};

    ($column = "mrtg") unless ($column);
    ($item = "$host\.$column") unless ($item);
    ($in = "In") unless ($in);
    ($out = "Out") unless ($out);
    if ($bits) {
	($units = "b/s") unless ($units);
	($maxlev = 10485760) unless ($maxlev);
	($scale = 8) unless ($scale);
    } else {
	($units = "Bytes/s") unless ($units);
	($maxlev = 1310720) unless ($maxlev);
	($scale = 1) unless ($scale);
    }
    if ($warnlev && $warnlev=~/^(\d*)%$/) {
	$warnlev = $maxlev * $1 / 100;
    }
    if ($paniclev && $paniclev=~/^(\d*)%$/) {
	$paniclev = $maxlev * $1 / 100;
    }
    ($warnlev = $maxlev / 2) unless ($warnlev);
    ($paniclev = $maxlev / 4 * 3) unless ($paniclev);
    push( @$htable, [ $host, $item, $mrtgprefix, $warnlev, $paniclev, 
		      $maxlev, $units, $in, $out, $bits, $scale ] ); 
}

sub check
{
    my ($self) = @_;
    my ( $row );
    my( $htable ) = $self->{"htable"};

    foreach $row (@$htable) {
	my ( $mrtgfile, $mrtgdirfile, $mrtgimgfile );
	my ( $color, $data, $currin, $currout, $percin, $percout, $line, $html) ;

	$color=$BigSister::common::status_codes{"green"};

	$mrtgfile = $$row[2].".html";
	$mrtgdirfile = $self->{"mrtglog"}."/".$$row[2].".log";
	$mrtgimgfile = $$row[2]."-day.png";

	unless (open(MRTGLOG, "$mrtgdirfile")) {
	    $self->report($$row[1], $BigSister::common::status_codes{"red"}, "Could not open MRTG file $mrtgdirfile");
	    next;
	}
	<MRTGLOG>;
	chomp($data=<MRTGLOG>);
	close(MRTGLOG);
	($currin, $currout) = ($data=~/^[0-9]*\s*([0-9]*)\s*([0-9]*)/);
	$currin = $currin * $$row[10];
	$currout = $currout * $$row[10];
	if ($currin >= $$row[4] || $currout >= $$row[4]) {
	    $color=$BigSister::common::status_codes{"red"};
	} elsif ($currin >= $$row[3] || $currout >= $$row[3]) {
	    $color=$BigSister::common::status_codes{"yellow"};
	}
	$percin = sprintf("%1.2f", 100 * $currin / $$row[5]);
	$percout = sprintf("%1.2f", 100 * $currout / $$row[5]);
	$html="&html\n<html><body><center>";
	$html.="Current <font color=lime>".$$row[7]."</font>: ".fmi($currin, $$row[6])." (<font color=lime>".$percin."%</font>)";
	$html.=" Current <font color=cyan>".$$row[8]."</font>: ".fmi($currout, $$row[6])." (<font color=cyan>".$percout."%</font>)\n\n";
	$html.='<a href="http://'.$self->{"mrtgweb"}.'/'.$mrtgfile.'"><IMG SRC="http://'.$self->{"mrtgweb"}.'/'.$mrtgimgfile.'" ALT="Last 24 Hours - Click for more" BORDER=0>';
	$html.="<BR>MRTG Details</a></center></body></html>";
	$line="\nCurrent $$row[7]: ".fmi($currin,$$row[6])." ($percin\%) Current $$row[8]: ".fmi($currout,$$row[6])." ($percout\%)";
	#print $line if ($BigSister::common::dl);
	$self->report($$row[1], $color, $line);
	if ($self->{"loghtml"} == 1) {
	    $self->report($$row[1], 101, $html);
	}
    }
}

# This code is slightly modified from MRTG
sub fmi {
  my($number) = $_[0];
  my(@short);
  @short = ("","k","M","G");
  my $digits=length("".$number);
  my $divm=0;
  while ($digits-$divm*3 > 4) { $divm++; }
  my $divnum = $number/10**($divm*3);
  return sprintf("%1.2f %s%s",$divnum,$short[$divm], $_[1]);
}

1;
