#!/usr/bin/perl -w
# $Header$
# # ============================================================================
# # Script for my private use on my server by StEfEk
# #
# #       This program is free software; you can redistribute it and/or modify it
# #       under the terms of the GNU General Public License as published by the
# #       Free Software Foundation; either version 2, or (at your option) any
# #       later version.
# #
# #       This program is distributed in the hope that it will be useful,
# #       but WITHOUT ANY WARRANTY; without even the implied warranty of
# #       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# #       GNU General Public License for more details.
# #
# #       You should have received a copy of the GNU General Public License
# #       along with this program; if not, write to the Free Software
# #       Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
# #
# # ============================================================================
# #
# #       Description:

=head1 NAME

check_Forti_cluster1.pl : Check en snmp un cluster de Fortigates

=cut

=head2 Example

 ./check_Forti_Cluster1.pl ip community

=cut

#
## ============================================================================
#

use strict;

my $UNKNOW = -1;
my $OK = 0;
my $WARNING = 1;
my $CRITICAL = 2;
my $state = $UNKNOW;
my @serials; 
my ($host, $community) = @ARGV;
my $path="/usr/lib/nagios/plugins/FortiSerial";
my $filename = $path."/".$host;


if (-e $filename){
	open (SERIALNUMBER,"$filename") or die "Error opening the ip file\n";
	close (SERIALNUMBER);
}
else{
	print "This ip doesn't exist yet, creating the file with Fortigates serial numbers:\n";
	$serials[0] = `/usr/lib/nagios/plugins/check_snmp -H $host -C $community -o "1.3.6.1.4.1.12356.1.100.6.1.2.1"`;
	$serials[1] = `/usr/lib/nagios/plugins/check_snmp -H $host -C $community -o "1.3.6.1.4.1.12356.1.100.6.1.2.2"`;
	print "$filename\n\n";
	open (SERIALNUMBER,"+>$filename") || die "Error while creating the ip file";
	print (SERIALNUMBER @serials);
	close (SERIALNUMBER);
}
open (SERIALNUMBER,"$filename") or die "Error while reading the ip file";
while (<SERIALNUMBER>) {
push(@serials,$_);
#print $serials[0]."\n";
#print $serials[1]."\n";
}
close (SERIALNUMBER);
#print $serials[0];
#print "\n";
#print $serials[1];
#print "\n";
   

if (defined($host) && defined ($serials[0]) && defined ($serials[1])) {
    open(STDERR,">/dev/null");
    my $result_index11 = `/usr/lib/nagios/plugins/check_snmp -H $host -C $community -o "1.3.6.1.4.1.12356.1.100.6.1.1.1" -l "Index11"`;
    my $result_index22 = `/usr/lib/nagios/plugins/check_snmp -H $host -C $community -o "1.3.6.1.4.1.12356.1.100.6.1.1.2" -l "Index22"`;
    my $result_serial11 = `/usr/lib/nagios/plugins/check_snmp -H $host -C $community -o "1.3.6.1.4.1.12356.1.100.6.1.2.1"`;
    my $result_serial22 = `/usr/lib/nagios/plugins/check_snmp -H $host -C $community -o "1.3.6.1.4.1.12356.1.100.6.1.2.2"`;
    chomp($result_index11);
    chomp($result_index22);
    chomp($serials[0]);
    chomp($serials[1]);
    my $serial1="";
    my $serial2="";

    if ($result_serial11 =~ / "(.+)" /){
    	$serial1=$1;
    	#print $serial1."\n";
    }	
    else{
    	print "Serial Number 1 not found\n";
    }
    if ($result_serial22 =~ / "(.+)" /){
        $serial2=$1;
	#print $serial2."\n";
    }	
    else{
        #print "Serial Number 2 not found\n";
    }
    #print $serials[0];	    
    #print "\n";
    #print $serials[1];
    #print "\n";
    my $flag1= index($serials[0], $serial1);
    my $flag2= index($serials[0], $serial2);


	if ($flag1 >=0 && $result_index11 =~ /Index11 OK \- 1/){
            $state = $OK;
	    print "Primary Member ok: HA1 ok 1\n";
            exit $state;
        }
        elsif($flag2 >=0){
            $state= $CRITICAL;
            print "Primary Member is down";
        }
        else {
            print "Primary Member: unknown state\n";
            exit $CRITICAL;
        }

}    
else {
    print "Wrong arguments\n";
    exit $UNKNOW;
}

# ============================================================================
 #
=head1 AUTHORS

by R3dl!GhT 

=cut


=head1 COPYRIGHT

 Copyright (C) R3Dl!GhT 2006.

This module is free software; you can redistribute it and/or
modify it under the terms of the GNU Public License.

=cut
# ============================================================================
#
# __END__
#

