#! /usr/bin/perl 
#
# check_bigip_active_status
# This works for bigip LTM version 11.2.0 and later

#This script can be used if you have a F5 pair. 
#Run it against the active F5 
#Alert will go out if failover occurs or status changes to one of the following -  forcedoffline, offline, unknown, standby

#
#
# 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
# of the License, 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, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
# Author Rupinder Singh  rupi10@yahoo.com
#version 1.0
#
# using this as Reference https://support.f5.com/kb/en-us/solutions/public/14000/300/sol14399.html

use strict;
use warnings;
use vars qw($PROGNAME $snmpcmd $f5status);
use Nagios::Plugin;

$PROGNAME = 'check_bigip_active_status';
$snmpcmd = '/usr/bin/snmpget';

$f5status = '.1.3.6.1.4.1.3375.2.1.14.3.1.0';



my $np = Nagios::Plugin->new(
  usage => "Usage: %s -H <hostname> -C <Community> \n",
  plugin  => $PROGNAME,
  shortname => uc($PROGNAME),
  blurb => 'Check the status of the F5',
);

$np->add_arg(
  spec => 'hostname|H=s',
  help => '-H, --hostname=<hostname>',
  required => 1,
);

$np->add_arg(
  spec => 'community|C=s',
  help => '-C, --community=<Community>',
  required => 1,
);


$np->getopts;

# Assign, then check args
my $hostname = $np->opts->hostname;
my $community = $np->opts->community;

$np->nagios_exit('UNKNOWN', 'Hostname contains invalid characters.')
  if ($hostname =~ /\`|\~|\!|\$|\%|\^|\&|\*|\||\'|\"|\<|\>|\?|\,|\(|\)|\=/);

$np->nagios_exit('UNKNOWN', 'Community contains invalid characters.')
  if ($community =~ /\`|\~|\!|\$|\%|\^|\&|\*|\||\'|\"|\<|\>|\?|\,|\(|\)|\=/);


my $cmd;
  $cmd = "$snmpcmd -v2c -c $community $hostname $f5status";
#uncomment to debug
#print "snmpcmd $cmd\n";

my $f5_integer = `$cmd`;
#uncomment to debug
#print "f5_integer $f5_integer\n";
my @test;
@test = split(/ /, $f5_integer);
#uncomment to debug
#print "test values $test[3]\n";
$f5_integer = $test[3];
my $f5_final = 0;
$f5_final = int($f5_integer);
#uncomment to debug
#print "f5 Final $f5_final\n";




# Return the status based on value 
#0 	unknown 	The failover status of the device is unknown
#1 	offline 	The device is offline
#2 	forcedOffline 	The device is forced offline
#3 	standby 	The device is standby
#4 	active 	The device  is active

$np->nagios_exit('OK', "F5 is Active")
   if ($f5_final == 4 );

$np->nagios_exit('WARNING', "F5 is standby")
   if ($f5_final == 3 );

$np->nagios_exit('WARNING', "F5 is forcedoffline")
   if ($f5_final == 2 );

$np->nagios_exit('CRITICAL', "F5 is offline")
   if ($f5_final == 1 );

$np->nagios_exit('UNKNOWN', "F5 status is UNKNOWN")
   if ($f5_final == 0 );