#!/usr/bin/perl

use MIME::Base64();
use REST::Client;
use Data::Dumper;
use JSON;
use Getopt::Std;
getopts("h:w:c:u:p:f:");

$warning = $opt_w;
$critical = $opt_c;
$username = $opt_u;
$password = $opt_p;
$fqdn = $opt_f;

$STATE_OK = 0;
$STATE_WARNING = 1;
$STATE_CRITICAL = 2;
$STATE_UNKNOWN = 3;

if (!defined($warning) || !defined($critical) || !defined($username) || !defined($password) || !defined($fqdn)){
    print "Define some options:
    -w WARNING
    -c CRITICAL
    -u username
    -p password
    -f FQDN
    This script checks the overall capacity of an Isilon System.
    Please provide the warning and critical used percentage level.  No need to use the percent sign.
    The username and password.
    The fully qualified domain name of the system to be checked.\n\n";
    exit $STATE_UNKOWN;
    }

$TB = 1000*1000*1000*1000;
chomp $username;
chomp $password;
$encoded = MIME::Base64::encode($username . ":" . $password);
$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0;

$client = REST::Client->new();

$headers = { Accept => 'application/json', Authorization => 'Basic ' . $encoded};

$client->setHost("https://$fqdn:8080");
$client->GET('/platform/1/statistics/current?key=ifs.bytes.total&key=ifs.bytes.free&key=ifs.bytes.avail&key=ifs.bytes.used&devid=all', $headers);
$response = from_json($client->responseContent);
$size = $#{$response->{'stats'}};

for ($i = 0; $i <= $size; $i++){
  if ($response->{'stats'}->[$i]->{'key'} eq "ifs.bytes.avail"){
      $ifs_avail = $response->{'stats'}->[$i]->{'value'};
        }
  elsif ($response->{'stats'}->[$i]->{'key'} eq "ifs.bytes.free"){
         $ifs_free = $response->{'stats'}->[$i]->{'value'};
        }
  elsif ($response->{'stats'}->[$i]->{'key'} eq "ifs.bytes.used"){
         $ifs_used = $response->{'stats'}->[$i]->{'value'};
        }
  elsif ($response->{'stats'}->[$i]->{'key'} eq "ifs.bytes.total"){
         $ifs_total = $response->{'stats'}->[$i]->{'value'};
        }
  else {print "Something is not right with the world, check your connection!\n";
        exit $STATE_UNKNOWN;}
}

$bytes_freepct = sprintf("%.2f", $ifs_free*100/$ifs_total );
$bytes_availpct = sprintf("%.2f", $ifs_avail*100/$ifs_total);
$bytes_usedpct = sprintf("%.2f", $ifs_used*100/$ifs_total);
$perfdata = "bytes_avail=$ifs_avail bytes_free=$ifs_free bytes_used=$ifs_used bytes_total=$ifs_total";

if ($bytes_usedpct < $warning){
    print "Isilon Storage System OK - Used = ",sprintf("%.2f",$ifs_used/$TB), "TB, Free = ",sprintf("%.2f",$ifs_free/$TB), "TB, Available = ",sprintf("%.2f",$ifs_avail/$TB), "TB, Total = ",sprintf("%.2f",$ifs_total/$TB), "TB | $perfdata\n";
    exit $STATE_OK;
    }

elsif ($warning <= $bytes_usedpct && $bytes_usedpct < $critical){
    print "Isilon Storage System WARNING - Used = ",sprintf("%.2f",$ifs_used/$TB), "TB, Free = ",sprintf("%.2f",$ifs_free/$TB), "TB, Available = ",sprintf("%.2f",$ifs_avail/$TB), "TB, Total = ",sprintf("%.2f",$ifs_total/$TB), "TB | $perfdata\n";;
    exit $STATE_WARNING;
    }

elsif ($critical <= $bytes_usedpct ){
    print "Isilon Storage System CRITICAL - Used = ",sprintf("%.2f",$ifs_used/$TB), "TB, Free = ",sprintf("%.2f",$ifs_free/$TB), "TB, Available = ",sprintf("%.2f",$ifs_avail/$TB), "TB, Total = ",sprintf("%.2f",$ifs_total/$TB), "TB | $perfdata\n";;
    exit $STATE_CRITICAL;
    }

else {print "Something is not working properly, check yourself and come back later\n";
      exit $STATE_UNKNOWN;}
