#!/usr/bin/php
<?php
/*
 * @author: oliskibbe (at) gmail.com
 * OIDs: http://www.oidview.com/mibs/3530/BLACKBERRYSERVERMIB-SMIV2.html
*/

function help () {
	echo "This Plugin was developed by Oliver Skibbe - oliskibbe (at) gmail.com
	
	Usage: ".$_SERVER['argv'][0]." ip community command
	
	ip			- ip of bes server
	
	community		- snmp community of bes server
	
	command:
	srp_connection	- checks if connection to rim is ok
	version		- always ok. prints version of bes
	messages [warn] [crit]	- checks pending messages. sends perfdata
	";
}

if(!extension_loaded("snmp")) {
	if (!dl("snmp.so") {
		echo "PHP SNMP extension could not be loaded...please check if it's installed!";
		help();
        exit ( 3 );
	}
}

snmp_set_quick_print ( true );
snmp_set_valueretrieval(SNMP_VALUE_PLAIN);



if ( $_SERVER['argc'] < 4 ) {
	help();
	exit ( 3 );
}

$ip = $_SERVER['argv'][1];
$community = $_SERVER['argv'][2];
$command = $_SERVER['argv'][3];

switch ( $command ) {
	case "messages":	
		$warn = $_SERVER['argv'][4]?$_SERVER['argv'][4]:10;
		$crit = $_SERVER['argv'][5]?$_SERVER['argv'][5]:50;
	
		$pending = @snmp2_getnext ( $ip, $community, '.1.3.6.1.4.1.3530.5.25.1.202' );
		$total_send = @snmp2_getnext ( $ip, $community, '.1.3.6.1.4.1.3530.5.25.1.203' );
		$total_recv = @snmp2_getnext ( $ip, $community, '.1.3.6.1.4.1.3530.5.25.1.204' );
		$total_expired = @snmp2_getnext ( $ip, $community, '.1.3.6.1.4.1.3530.5.25.1.205' );
		$total_filtered = @snmp2_getnext ( $ip, $community, '.1.3.6.1.4.1.3530.5.25.1.206' );
		if ( $pending === FALSE || $total_send === FALSE || $total_recv === FALSE ||
			$total_expired === FALSE || $total_filtered === FALSE ) {
			echo "Could not read data!\n";
			exit(2);
		}

		if ( $pending > $crit ) {
			echo "CRITICAL - ";
			$ret = 2;
		} else if ( $pending > $warn ) {
			echo "WARNING - ";
			$ret = 1;
		} else {
			echo "OK - ";
			$ret = 0;
		}
		echo $pending." mails are pending. Send: ".$total_send." Recv: ".$total_recv." Expired: ".$total_expired." Filtered: ".$total_filtered." | send=".$total_send." recv=".$total_recv." expired=".$total_expired." filtered=".$total_filtered." pending=".$pending."\n";
		exit ( $ret );
	break;
	case "srp_connection":
		$state = @snmp2_getnext ( $ip, $community, '.1.3.6.1.4.1.3530.5.25.1.10' );
		$last_connect = @snmp2_getnext ( $ip, $community, '.1.3.6.1.4.1.3530.5.25.1.11' );

		if ( $state === FALSE || $last_connect === FALSE ) {
			echo "Could not read data!\n";
			exit(2);
		}


		if ( $state <= 0 ) {
			echo "CRITICAL - No connection to SRP Router\n";
			exit ( 2 );
		}
		$last_connect = date('H:i Y-m-d',$last_connect);	
		echo "OK - Connection to SRP Router exists. Last connect at: ".$last_connect."\n";
		exit ( 0 );
	break;
	case "version":		
		$version = @snmp2_getnext ( $ip, $community, '.1.3.6.1.4.1.3530.5.20.1.10');
		$licences = @snmp2_getnext ( $ip, $community, '.1.3.6.1.4.1.3530.5.20.1.21' );
		if ( $version === FALSE || $licences === FALSE ) {
			echo "Could not read data!\n";
			exit(2);
		}

		echo $version." (Licences used: ".$licences.")\n";
		exit ( 0 );
	break;
}




?>
