File: //lib/python2.7/site-packages/lap/stfc_naps_available.py
#
import subprocess
import os
import re
import sys,linecache, types, time
_tries=3
_sleep=5
def __run__(params):
try:
nap = params.get("nap")
norm = int(params.get("norm"))
warn = int(params.get("warn"))
crit = int(params.get("crit"))
except Exception, e:
return [2, "CRITICAL - Error getting params: %s" % repr(e)]
try:
count = 0
if os.path.isfile("/tmp/kmedias_stats.txt"):
_ct=0
while _ct < _tries:
kmedias_stats=open("/tmp/kmedias_stats.txt")
data = kmedias_stats.read()
kmedias_stats.close()
r = re.search(re.escape(nap)+' available_cnt=([0-9]+)', data)
if not isinstance(r, types.NoneType):
count = int(r.group(1))
break
else:
_ct+=1
time.sleep(_sleep)
if _ct == 3:
return [2, "CRITICAL - regexp failed(3x5s): %s" % nap+' available_cnt=([0-9]+)']
else:
return [2, "CRITICAL - kmedias stats file does not exist: %s" % repr(e)]
if count == norm:
return [0, "OK - %s - Channels availables: %s" % (nap,count)]
if count >= warn and count < norm:
return [1, "WARN - %s -Channels availables: %s" % (nap,count)]
else:
return [2, "CRIT - %s - Channels availables: %s" % (nap,count)]
except Exception, e:
exc_type, exc_obj, tb = sys.exc_info()
f = tb.tb_frame
lineno = tb.tb_lineno
filename = f.f_code.co_filename
linecache.checkcache(filename)
line = linecache.getline(filename, lineno, f.f_globals)
return [2, 'Exception in ({}, line {} "{}"): {}'.format(filename, lineno, line.strip(), exc_obj)]
#if __name__ == '__main__':
#
# ''' test only '''
# params = {
# "nap": 'NAP_TLF_LD_8198',
# "norm": 92,
# "warn": 62,
# "crit": 31
# }
#
# print __run__(params)