File: //usr/lib/python2.7/site-packages/lap/functional_monitoring.py
import socket
import sys
import os
import time
import json
import urlparse
def __run__(params):
try:
import requests
except ImportError:
return [2, "CRITICAL - Error: Please install python-requests"]
try:
url = params.get("url", "http://localhost/monitoring?functional=true")
alarm = params.get("alarm")
cache_time = int(params.get("cache_time", "60"))
cache_file = '/var/cache/locaweb-plugins/functional_%s' % urlparse.urlparse(url).netloc
except Exception, e:
return [2, "CRITICAL - Error: %s" % repr(e)]
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).json()
with open(cache_file, 'w') as f:
f.write(json.dumps(data))
except:
return [2, "CRITICAL - Error: the url cannot be opened"]
infos = {}
try:
[ infos.update({key: value}) for key, value in
[ line.items()[0] for line in data ] if alarm == key ]
for key, value in infos.iteritems():
if not 'ok' in value['status']:
return [2, "CRITICAL - %s" % value['message'][0]['message']]
else:
return [0, 'OK']
except Exception, e:
return [2, "CRITICAL - Error: %s" % repr(e)]