#!/usr/bin/perl -w

use strict;
use Getopt::Long;
use Net::SSH qw(ssh_cmd);

my $opt_H;
my $host;
my $msg = "";
my $oldlines;
my $newlines;
my $state = 0;
my @lines;
my $cmd = "errpt|grep -v ^IDENT";
my $cil;

GetOptions ("H=s" => \$opt_H);

$host =  $opt_H;

my $errfile_old =  ">/tmp/".$host."errlog_old";
my $errfile_old_inp = "</tmp/".$host."errlog_old";
my $errfile_new =  ">/tmp/".$host."errlog_new";
my $errfile_new_inp = "</tmp/".$host."errlog_new";
my $oldfile = "/tmp/".$host."errlog_old";

if ( ! -e $oldfile) {
        my $ssh2 = ssh_cmd($host, $cmd);
        open OUT, $errfile_old;
        print OUT "";
        close OUT;
	open OUT, $errfile_new;
        print OUT $ssh2;
        close OUT;
        open INP, $errfile_new_inp;
        @lines = <INP>;
        close INP;
        $oldlines = @lines;
        $msg = "Found new ".$oldlines." errors on ".$host;
        $state = 2;
	print $msg."\n";
	exit $state;
}

	open (INP, $errfile_new_inp);
        open (OUT, $errfile_old);
	print OUT <INP>;
	close INP;
	close OUT;
	open INP, $errfile_new_inp;
        @lines = <INP>;
	$oldlines = @lines;
	close INP;
	my $ssh2 = ssh_cmd($host, $cmd);
        open OUT, $errfile_new;
        print OUT $ssh2;
        close OUT;
        open INP, $errfile_new_inp;
        @lines = <INP>;
        $newlines = @lines;
        close INP;
if ( $newlines > $oldlines) {
	$newlines = $newlines - $oldlines;
        $msg = "Found new ".$newlines." errors on ".$host;
        $state = 2;
        }else{
        $msg = "Didn't find any new errors on ".$host;
        $state = 0;
}

print $msg."\n";               
exit $state;
