-
Notifications
You must be signed in to change notification settings - Fork 3
/
geckoboard.py
141 lines (113 loc) · 4.5 KB
/
geckoboard.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
'''
This class is responsible for pushing data to Geckoboard
'''
import json
import requests
from helpers import *
import configs
class Geckoboard:
def __init__(self):
self.widgetdata = {}
self.widgetdata['api_key'] = configs.geckoboardapikey
self.headers = {'content-type': 'application/json'}
def line(self, zhist, widgetdefinitions):
mintrigger = widgetdefinitions['mintrigger']
maxtrigger = widgetdefinitions['maxtrigger']
colortrigger = widgetdefinitions['colortrigger']
unit = widgetdefinitions['unit']
points = []
data = {}
settings = {}
settings['colour'] = '78ab49' # Default color line: light green
for i in zhist:
points.append(i['value'])
#print("{0}: {1}".format(datetime.
# fromtimestamp(int(point['clock'])).
# strftime("%x %X"), point['value']))
print widgetdefinitions['name']
if unit == 'bps' or unit == 'int':
points = map(int,points)
if unit == '%':
points = map(float,points)
print points
try:
widgetdefinitions['percenttrigger']
except KeyError:
if (max(points) > maxtrigger) or (min(points) < mintrigger) or (min(points) == max(points)):
settings['colour'] = colortrigger
else:
if unit != '%':
percentdiff = 100.0 - (float((min(points))/float(max(points))) * 100)
else:
percentdiff = max(points) - min(points)
if float(widgetdefinitions['percenttrigger'] <= percentdiff) or (float(min(points)) == float(max(points))):
settings['colour'] = colortrigger
settings['axisy'] = [formatunit(float(min(points)),unit),
formatunit(float(max(points)),unit)]
data['item'] = points
data['settings'] = settings
self.widgetdata['data'] = data
print self.widgetdata
def monitoring(self, zhist, widgetdefinitions):
data = {}
httpcode = zhist['httpcode']
httptimeresponsems = sec_to_ms(float(zhist['httptimeresponse']))
httplasterrorresponse = calculate_age(zhist['httplasterrorresponse'])
try:
widgetdefinitions['httpexpectedcode']
except KeyError:
if int(httpcode) == 200:
data['status'] = 'Up'
else:
data['status'] = 'Down'
else:
if int(httpcode) == widgetdefinitions['httpexpectedcode']:
data['status'] = 'Up'
else:
data['status'] = 'Down'
data['downTime'] = str(httplasterrorresponse)
data['responseTime'] = str(httptimeresponsems)
self.widgetdata['data'] = data
print widgetdefinitions['name']
print self.widgetdata
def text(self, values):
data = {}
data['item'] = values
self.widgetdata['data'] = data
print self.widgetdata
def triggerlist(self, ztriggers, widgetdefinitions):
data = []
if len(ztriggers):
for trigger in ztriggers:
title = {}
label = {}
listitem = {}
title['text'] = trigger['host'] + ' - ' + trigger['description']
title['highlight'] = 'True'
severity = priority_to_humanreadable(trigger['priority'])
label['name'] = severity['txt']
label['color'] = severity['color']
description = "Age: " + str(calculate_age(trigger['lastchange']))
#print title
#print label
#print description
listitem['label'] = label
listitem['title'] = title
listitem['description'] = description
data.append(listitem)
else:
title = {}
data = []
listitem = {}
title['text'] = configs.notriggersmessage
title['highlight'] = 'True'
listitem['title'] = title
data.append(listitem)
self.widgetdata['data'] = data
#print self.widgetdata
def push(self, widgetdefinitions):
wk = widgetdefinitions['widget_key']
response = requests.post('https://push.geckoboard.com/v1/send/' + wk,
data=json.dumps(self.widgetdata),
headers=self.headers)
print response.text