#!/usr/bin/perl

#-------------------------------------------------------------
#use SOAP::Lite + trace => qw(method debug);
use SOAP::Lite;

BEGIN {push (@INC, "..");}
use iControlTypeCast;

#-----------------------------------------------------------

#-----------------------------------------------------------
#Arguments
#----------------------------------------------------------
my $iHost = $ARGV[0];
my $iPort = $ARGV[1];
my $iUser = $ARGV[2];
my $iPass = $ARGV[3];
my $iservice = $ARGV[4];

#makes iControl connection to the bigip

sub SOAP::Transport::HTTP::Client::get_basic_credentials
{
        return "$iUser" => "$iPass";
}

$soap = SOAP::Lite
        -> uri('urn:iControl:System/Services')
        -> proxy("https://$iHost:$iPort/iControl/iControlPortal.cgi");

#get the server status for the service passed on command line.
$soapResponse = $soap->get_service_status
(
  SOAP::Data->name(services => [$iservice])
);
my @service_status = @{$soapResponse->result};
#read the results and outputs to Nagios
foreach $service_status (@service_status)
{
  $status = $service_status->{"status"};
  print "$iservice = $status\n";
}
#check to see if service is up if not exits on 1
 if ($status eq "SERVICE_STATUS_UP") {
    $exit_code = "green";
 #   print "$exit_code";
    exit 0;
 } else {
   $exit_code = "red";
 #  print "$exit_code";
   exit 1;
}
