#!/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 link 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 = "suic"

	## 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
	line = os.popen("%s -rnx 'ss7 link status'" % cmd).readline()
	if linkset in line :
		if "NOT_ALIGNED" in line :
			np.nagios_exit("CRITICAL", "link %s NOT_ALIGNED" % linkset)
		elif "PROVING" in line :
			np.nagios_exit("WARNING", "link %s PROVING" % linkset)
		elif "INSERVICE" in line :
			np.nagios_exit("OK", "link %s INSERVICE" % linkset)
		else:
			np.nagios_exit("UNKNOWN", linkset)

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

if __name__ == '__main__' : 
	getstat()

