#!/usr/bin/perl
use strict;
use warnings;
use WWW::Mechanize;
use HTML::TreeBuilder;
use Getopt::Long;
no warnings 'utf8';

my $username="YOUR ADMIN USERNAME";
my $password="YOUR ADMIN PASSWORD";


# Nagios specific
use lib "/usr/lib/nagios/plugins";
use utils qw(%ERRORS $TIMEOUT); #my %ERRORS=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3,'DEPENDENT'=>4);
my %CONF;
GetOptions( \%CONF, "type=s" , "host=s") or die << 'USAGE';
Options disponibles :
  --type <raid|disk|quota>
  --host <hostname or ip> 
USAGE

my $hostname=$CONF{host};


my $m = WWW::Mechanize->new;
    $m->agent_alias('Linux Mozilla'); # fait passer notre robot pour Mozilla, grâce à l'en-tête User-Agent
    $m->add_header( Cookie => 'BrowserDetect=passed' );
    
$m->get("https://$hostname/cserv/user_login.csp"); # GET du formulaire de login
die $m->res->status_line unless $m->success;

# remplissage et validation form
$m->set_fields(
    event => "login",
    thispage => "",
    userName => $username,
    password => $password,
    langddl => 'fr_FR',
    dateclient => time()."000",
);
$m->click;
die $m->res->status_line unless $m->success;

# vérification que l'identification est réussie 
{
  my $ok = 0;
  $m->cookie_jar->scan( sub { $ok++ if $_[1] eq 'lang' } );
  die "Echec d'identification : mauvais Pseudo ou mot de passe ?\n"
  unless $ok;
}

my $exitCode = $ERRORS{'OK'};
my $outputCritical;

if ( $CONF{type} eq "quota"){
	# Quotas from last Logs
	$m->get("https://$hostname/cserv/systemStatusMessagesContent.csp");
	my @line= map { $_->as_HTML }
	  HTML::TreeBuilder
	  ->new_from_content( $m->content )
	  ->look_down( _tag => 'tr' );
	my $date;
	foreach (@line) {
		s/<tr class="\w+"><td>\d+<\/td><td>([^<]+)//;
		$date=$1;
		s/<\/td><td>//; 
		s/<\/td><\/tr>//; 
		s/$/<br>/;
		next if /Upstream connection failure/;
		chomp;
		if(/has reached.*quota/){
			$outputCritical .= $_;
      $exitCode = $ERRORS{'WARNING'} if($exitCode != $ERRORS{'CRITICAL'}) ;		
		}
	}
	$outputCritical = "quota OK" if($exitCode == $ERRORS{'OK'});
	$outputCritical =~ s/<br>$//;
	printf $outputCritical;
}

if ( ($CONF{type} eq "raid") ||($CONF{type} eq "disk")) {
	# Etat du Raid
	$m->get("https://$hostname/cserv/raidxml.csp");
	my @line= split(/\n/,$m->content);
	foreach (@line) {
		next if /status="NOT-PRESENT"/;
		chomp;
		if( (/port="(\d+)" status="(.+)" size=/) && ( $CONF{type} eq "disk")){
			if($2 ne "NORMAL"){
				$outputCritical .= "CRITICAL - Disque $1 status : $2<br>";
      	$exitCode = $ERRORS{'CRITICAL'};
      }else{
				$outputCritical .= "OK - Disque $1 status : $2<br>";
      	$exitCode = $ERRORS{'OK'} if($exitCode != $ERRORS{'CRITICAL'}) ;	
      }
		}		
		if( (/array="(\d+)" status="(.+)" size=/) && ($CONF{type} eq "raid")){
			if( $2 eq "BROKEN"){
			  $outputCritical .= "CRITICAL - RAID5 array $1 status : $2<br>";
        $exitCode = $ERRORS{'CRITICAL'};
      }elsif($2 eq "NORMAL"){
				$outputCritical .= "OK - RAID5 array $1 status : $2<br>";
      	$exitCode = $ERRORS{'OK'} if($exitCode != $ERRORS{'CRITICAL'}) ;	
      }else{
			  $outputCritical .= "UNKNOWN - RAID5 array $1 status : $2<br>";
        $exitCode = $ERRORS{'UNKNOWN'} if($exitCode != $ERRORS{'CRITICAL'}) ;
      }
		}
	}
	$outputCritical =~ s/<br>$//;
	printf $outputCritical;
}

exit $exitCode;