File: //usr/lib/python2.7/site-packages/lap/check_vpn_fortigate100e.py
import json
from FgSNMP import FortiGet, SimpleSNMP
def bytes_to_human(value, suffix='B'):
for unit in ['','Ki','Mi','Gi','Ti','Pi','Ei','Zi']:
if abs(value) < 1024.0:
return "%3.1f%s%s" % (value, unit, suffix)
value /= 1024.0
return "%.1f%s%s" % (value, 'Yi', suffix)
def __run__(params):
vdom = params.get("vdom")
vpn = params.get("vpn")
user = params.get("snmp_user")
host = params.get("fortigate")
password = params.get("snmp_password")
fortigate = FortiGet()
mysnmp = SimpleSNMP()
failed = []
if not all([vdom, vdom, user, host, host, host]):
return([2, "Check your YAML file"])
try:
with open("/tmp/vpns100e.json") as temp:
history = json.load(temp)
for phase, infos in history.get(vdom).get(vpn).iteritems():
vpnid = infos.get("id")
outdata = infos.get("outdata")
indata = infos.get("indata")
status = fortigate.get_vpn_status(vpnid, mysnmp, user, password, host)
if not "up" in status and phase:
failed.append(str(phase))
if failed:
return([2, ', '.join(failed)])
else:
return([0, "Input Bytes: %s, Output Bytes: %s" % (bytes_to_human(int(indata)), bytes_to_human(int(outdata)))])
except Exception, err:
raise
return([2, err])