json

/nagios/jsonquery.html

#!/usr/bin/php
<?php

# Author: Rob Hassing <rob.hassing@deltics.nl>
# Created: March 2015
#
# $Date: 2015-11-26 $
# $Revision: 1 $
#

//--------------------------------------------------------------------//
// CONFIG
//--------------------------------------------------------------------//

// revision
$strRevision = "0.0.3";

// help
$strHelp = "";

// basedir
$strBaseDir = "";

//--------------------------------------------------------------------//

require_once($strBaseDir.'utils.php');

# Default values for all run-time options
$opt_hostname=$argv[1];
$opt_user=$argv[2];
$opt_passwd=$argv[3];

$credentials="$opt_user:$opt_passwd";

# NAGIOS PLUG-IN EXIT CODES
$NAGIOS_CODE_OK=0;
$NAGIOS_CODE_WARNING=1;
$NAGIOS_CODE_CRITICAL=2;
$NAGIOS_CODE_UNKNOWN=3;

# Variables for internal use
$FULL_COMMAND_LINE="$0 $*";
$THRESHOLDS_WARNING="";
$THRESHOLDS_CRITICAL="";

$option1=$argv[1];

if($option1!="-h"){


$hostexit= check_host($credentials,$opt_hostname);
$serviceexit= check_services($credentials,$opt_hostname);

if($hostexit==2 OR $serviceexit==2) {
 echo "CRITICAL - Something is wrong at this site\n\n";
 exit(2);
 }elseif($hostexit==1 OR $serviceexit==1) {
 echo "WARNING - There is a warning at this site \n\n";
 exit(1);
 }elseif($hostexit==0 OR $serviceexit==0) {
 echo "OK - Looks good! \n\n";
 exit(0);
 }


 
} elseif($option1=="-h") {

echo "
 Required Options:
 hostname 
 user
 Password
 \n
 $argv[0] <hostname> <user> <password>
 \n
 ";
}

function check_host($credentials,$opt_hostname)
{
 $url = 'http://'.$credentials.'@'.$opt_hostname.'/nagios/cgi-bin/statusjson.cgi?query=hostlist&details=true&hoststatus=up+do
wn+unreachable+pending'; 
 $json = file_get_contents($url);
 $arr = json_decode($json, true);
 $data=$arr['data'];
 $hostlist=$data["hostlist"];
 $values=$hostlist;

 function recurse_array($values){
 $pendcount = 0;
 $okcount = 0;
 $warncount = 0;
 $warnnackcount = 0;
 $critcount = 0;
 $critnackcount = 0;
 if( is_array($values) ){
 foreach($values as $key => $host){
 $name=$host["name"];
 $service=$host["description"];
 $status=$host["status"];
 $ack_type=$host["acknowledgement_type"];
 // print "$name: $service: Status: $status\n";
 if($status==1){
 $pendcount++;
 }elseif($status==2){
 $okcount++;
 }elseif($status==4){
 $critcount++;
 if($ack_type > 0){
 $critnackcount++;
 }
 }
 }
 echo "HOSTS:\n";
 echo " UP: $okcount\n";
 echo " DOWN: $critcount / $critnackcount\n";
 echo " UNREACHABLE: $warncount\n";
 echo " PENDING: $pendcount\n\n";

 if($critcount > $critnackcount)
 {
 $total=$critcount-$critnackcount;
 echo "CRITICAL: Not all $critcount Criticals are acknowleged ($total are not acknowledged)\n\n";
 return 2;
 }elseif($warncount > $warnnackcount)
 {
 $total=$warncount-$warnnackcount;
 echo "UNREACHABLE: $warncount Hosts are UNREACHABLE \n\n";
 return 1;
 }else
 {
 echo "Looks good!\n\n";
 return 0;
 }
 }
 }
 return $array_text = recurse_array($values);
} 

function check_services($credentials,$opt_hostname)
{
 $url = 'http://'.$credentials.'@'.$opt_hostname.'/nagios/cgi-bin/statusjson.cgi?query=servicelist&details=true&servicestatus
=ok+warning+critical+unknown+pending'; 
 $json = file_get_contents($url);
 $arr = json_decode($json, true);
 $data=$arr['data'];
 $servicelist=$data["servicelist"];
 $values=$servicelist;

 function Srecurse_array($values){
 $Spendcount = 0;
 $Sokcount = 0;
 $Swarncount = 0;
 $Swarnackcount = 0;
 $Scritcount = 0;
 $Scritackcount = 0;
 $Sunkncount = 0;
 $Sunknackcount = 0;
 if( is_array($values) ){
 foreach($values as $key => $host){
 if( is_array($host) ){
 foreach($host as $key => $host){
 $name=$host["host_name"];
 $service=$host["description"];
 $status=$host["status"];
 $ack_type=$host["acknowledgement_type"];
 $downtime=$host["scheduled_downtime_depth"];
 if($status==1){
 $Spendcount++;
 }elseif($status==2){
 $Sokcount++;
 }elseif($status==8){
 $Sunkncount++;
 if($ack_type > 0){
 $Sunknackcount++;
 }
 }elseif($status==16){
 $Scritcount++;
 if($ack_type > 0 || $downtime > 0){
 $Scritackcount++;
 }
 }elseif($status==4){
 $Swarncount++;
 if($ack_type > 0){
 $Swarnackcount++;
 }
 }
 }
 }
 }
 echo "SERVICES:\n";
 echo " OK: $Sokcount\n";
 echo " WARNING: $Swarncount / $Swarnackcount\n";
 echo " CRITICAL: $Scritcount / $Scritackcount\n";
 echo " UNKNOWN: $Sunkncount / $Sunknackcount\n";
 echo " PENDING: $Spendcount\n\n";


 if($Scritcount > $Scritackcount)
 {
 $Stotal=$Scritcount-$Scritackcount;
 echo "CRITICAL: Not all $Scritcount Criticals are acknowleged ($Stotal are not acknowledged)\n\n";
 return 2;
 }elseif($Swarncount > $Swarnackcount)
 {
 $Stotal=$Swarncount-$Swarnackcount;
 echo "WARNING: Not all $Swarncount Warnings are acknowleged ($Stotal are not acknowledged)\n\n";
 return 1;
 }else
 {
 echo "Looks good!\n\n";
 return 0;
 }
 }

 }
 return $Sarray_text = Srecurse_array($values);
}

?>