File: //usr/lib/python2.7/site-packages/lap/influxdata.py
import requests
import getpass
import socket
import numpy
def format_default(data):
return str(data)
def format_seconds(data):
return str(data) + " seconds"
def __run__(params):
socket.setdefaulttimeout(10)
query = {
"db": params.get("database", getpass.getuser()),
"q": params["query"],
}
try:
response = requests.get("%s/query" % params.get("endpoint", "http://influxdb-ha.locaweb.com.br"), params=query).json()
except Exception, e:
return [2, "CRITICAL - Error: %s" % repr(e)]
data = []
for result in response["results"]:
for serie in result["series"]:
for timestamp, value in serie["values"]:
try:
data.append(float(value))
except:
continue
data = numpy.percentile(data, params.get("percentile", 50))
prio = 6
alert = False
for priority, threshold in params.get('thresholds', {}).iteritems():
if priority <= prio and data > threshold:
prio = priority
alert = True
format_function = eval(params.get("format_function", "format_default"))
if alert:
return [2, "CRIT,Priority:%s - Current value is %s" % (prio, format_function(data))]
else:
return [0, "OK - Current value is %s" % format_function(data)]
if __name__ == '__main__':
print __run__({
"query": """SELECT percentile("delivery_delay_percentil", 95) FROM "isrcd" WHERE "cluster" = 'simpsons0035' AND time > now() - 10m GROUP BY time(1m) fill(null)""",
"database": "ft_email",
"percentile": 95,
"thresholds": {
2: 30,
3: 20
},
"format_function": "format_seconds",
})