File: //usr/lib/python2.7/site-packages/lap/app_monitoring.py
import socket
import sys
import os
import time
import json
def __run__(params):
try:
import requests
except ImportError:
return [2, "CRITICAL - Error: Please install python-requests"]
try:
os.environ['no_proxy'] = '127.0.0.1,localhost'
use_cache = params.get("use_cache", 0)
cache_time = params.get("cache_time", 180)
timeout = int(params.get("timeout", 10))
cache_file = '/var/cache/locaweb-plugins/app_monitoring_cache'
url = params.get("url", "http://localhost/monitoring")
except Exception, e:
return [2, "CRITICAL - Error: %s" % repr(e)]
if use_cache:
if os.path.isfile(cache_file) and \
(time.time() - os.stat(cache_file).st_mtime) < cache_time and \
os.stat(cache_file).st_size > 0:
data = json.loads(open(cache_file).read())
else:
try:
socket.setdefaulttimeout(10)
data = requests.get(url, verify=False, timeout=timeout).json()
with open(cache_file, 'w') as f:
f.write(json.dumps(data))
except Exception, e:
return [2, "CRITICAL - Error: %s" % repr(e)]
else:
try:
socket.setdefaulttimeout(10)
data = requests.get(url, verify=False, timeout=timeout).json()
except:
return [2, "CRITICAL - Error: the url cannot be opened"]
infos = {}
try:
try:
[ infos.update({key: value['message'][0]}) for key, value in data.iteritems() if not 'ok' in value['status']]
except:
for line in data:
for key, value in line.items():
if type(value) is dict and value['status'] != 'ok':
infos.update({key: value['message'][0]})
else:
[ infos.update({key: value['message'][0]}) for key, value in [ line[1].iteritems() for line in data.items() if isinstance(line[1], dict) ] if not 'ok' in value['status'] ]
finally:
errors = []
if infos:
for key, value in infos.iteritems():
if type(infos) is dict:
errors.append("%s: %s," % (key, value['message']))
return [2, "CRITICAL - Error: %s" % ' '.join(errors).strip(',')]
elif type(infos) is str:
errors.append("%s: %s," % (key, value['message'][0]))
return [2, "CRITICAL - Error: %s" % ' '.join(errors).strip(',')]
else:
return [0, "OK"]
except Exception, e:
return [2, "CRITICAL - Error: %s" % repr(e)]