#!/usr/bin/perl

use vars qw/$home_dir/;

BEGIN {
  $home_dir = "/home/grapher2";
  open(FL,"/etc/hgs2.conf");
  ($t1,$t2,undef)=split(/:/,<FL>);
  close(FL);
  if ( $t1 ne ""){ $home_dir=$t1;}
};

 ($dmn,undef)=@ARGV;
  if ( $dmn eq "" ){ $dmn="ALL";}
  if ( $dmn ne "ALL" ){
    $search="DOMAIN=$dmn";
  }else{
    $search="DOMAIN";
  }


print "Content-type: text/html\n\n";

print '
<head>
<META HTTP-EQUIV="REFRESH"
CONTENT="30;URL=cover?',$dmn,'">
</head>
<body bgcolor=#FFE6C5><p>
Warnings:<p>';


$pt="$home_dir/data";

opendir(DIR, $pt) || die "can't opendir $some_dir: $!";
@dots = grep { ! /^\./  } readdir(DIR);
closedir DIR;
$now=time();

foreach $dot (@dots){
  (@temp2)=`grep $search $pt/$dot/info2`;
  if ( $#temp2 > -1 ){ 
    ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
    $atime,$mtime,$ctime,$blksize,$blocks)
    = stat("$pt/$dot/alerter");
    $a=localtime($mtime);
    if ( $now-(60*10) > $mtime ){
      print "<h4><font color=darkred>CRITICAL : host $dot down since $a !</font></h4>\n";
    }else{
      open(FL,"$pt/$dot/alerter");
      while($tmp=<FL>){
        ($plugin,$type,$value)=split(/:\:/,$tmp);
        if ( $type =~ /WARNING/ ){
           print "<h4><font color=darkorange>WARNING  : host $dot $PLUGIN  ( $value ) !</font></h4>\n";
        }else{
           print "<h4><font color=darkred>CRITICAL : host $dot $PLUGIN  ( $value ) !</font></h4>\n";
        }
      }
      close(FL);
    }
  }
}




print '
</body>

';
