#!/usr/bin/python
import os,sys

## This is for the custom nagios module
#sys.path.insert(1, '../')
from pynag.Plugins import simple as Plugin

"""
simple pynag based nagios plugin to check chan_ss7 cic status
(c)mmii convergence. all rights are God's alone. http://convergence.pk
please report all bugs and idiocies to info@convergence.pk
"""

def getstat() :
	## Create the plugin option
	np = Plugin()

	## Add a command line argument
	np.add_arg("g","linkset", "Enter a linkset ", required=None)

	## This starts the actual plugin activation
	np.activate()

	## Check for particular linkset
	if np['linkset'] :
		linkset = np['linkset']
	else :
		linkset = "ptcl"

	## where does our suid asterisk 
	cmd = "/usr/sbin/asterisk"
	if not os.path.isfile(cmd):
		np.nagios_exit("UNKNOWN", "Missing asterisk bin %s" % cmd)

	## Get the check value
	lines = os.popen("%s -rnx 'ss7 status'" % cmd).readlines()
	for line in lines :
		if linkset in line :
			values = line.split()
			values = [int(i) for i in values[1:]]
			if values[3] > 0:
				np.nagios_exit("CRITICAL", "%s rst:%s" % (linkset, values[3]))
			#idle busy initiating resetting total incoming total outgoing
			#28    0          2         0              6             11
			elif values[0] == 0 and values[3] == 0 and (values[1] >= 1 or values[2] >= 1) :
				np.nagios_exit("WARNING", "%s idl:%s bsy:%s iam:%s" % (linkset, values[0], values[1], values[2]))
			elif values[0] >= 1 and values[2] == 0 :
				np.nagios_exit("OK", "%s idl:%s bsy:%s iam:%s inc:%s out:%s" % (linkset, values[0], values[1], values[2], values[4], values[5]))
			else:
				np.nagios_exit("UNKNOWN", values)

	np.nagios_exit("UNKNOWN", "no data available")

if __name__ == '__main__' : 
	getstat()

