#!/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 wanpipe e1 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","wanpipe", "Enter a wanpipe #", required=None)

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

	## Check for particular wanpipe
	if np['wanpipe']:
		wanpipe = "wanpipe" + np['wanpipe']
	else:
		wanpipe = "wanpipe1"

	## lets check for wanpipe proc file
	wanpipe_file = "/proc/net/wanrouter/status"
	if not os.path.isfile(wanpipe_file):
		np.nagios_exit("UNKNOWN", "Missing wanpipe file %s" % wanpipe_file)

	## Get the check value
	for line in open(wanpipe_file):
		if wanpipe in line:
			if "Disconnected" in line:
				np.nagios_exit("CRITICAL", "%s is Disconnected" % wanpipe)
			elif "Connecting" in line:
				np.nagios_exit("WARNING", "%s is Connecting" % wanpipe)
			elif "Connected" in line:
				np.nagios_exit("OK", "%s is Connected" % wanpipe)
			else:
				np.nagios_exit("UNKNOWN", line)	

	## crap something else matched
	np.nagios_exit("UNKNOWN", "no data available")

if __name__ == '__main__' : 
	getstat()

