-
Notifications
You must be signed in to change notification settings - Fork 4
/
sample_service2.py
executable file
·69 lines (57 loc) · 1.8 KB
/
sample_service2.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
#!/usr/bin/env python
import json
import sys
import os
from bottle import route, run, get
import time
import httplib
server = "127.0.0.1"
statport = "18082"
host = "%s:18001" % server
staturl = "http://%s:%s/status" % (server,statport)
blob = {"id": "foo", "url": staturl}
data = json.dumps(blob)
connection = httplib.HTTPConnection(host)
connection.request('POST', '/checks', data)
result = connection.getresponse()
print "RESULT: %s - %s" % (result.status, result.reason)
def usage():
print "%s [status: OK,Unknown,Warning,Critical]" % (sys.argv[0])
msgs = {
"OK": "Everything is groovy!",
"Unknown": "Unknown error!",
"Warning": "Houston, I think we have a problem.",
"Critical": "Danger Will Rogers! Danger!"
}
t = len(sys.argv)
if t < 2:
usage()
sys.exit(1)
else:
statusm = sys.argv[1]
t = time.localtime()
ts = time.strftime('%Y-%m-%dT%H:%M:%S%Z', t)
rootdir = "./"
# Change working directory so relative paths (and template lookup) work again
root = os.path.join(os.path.dirname(__file__))
sys.path.insert(0, root)
# generate nested python dictionaries, copied from here:
# http://stackoverflow.com/questions/635483/what-is-the-best-way-to-implement-nested-dictionaries-in-python
class AutoVivification(dict):
"""Implementation of perl's autovivification feature."""
def __getitem__(self, item):
try:
return dict.__getitem__(self, item)
except KeyError:
value = self[item] = type(self)()
return value
@get('/status')
def status():
data = AutoVivification()
data['id'] = "bar"
data['status'] = statusm
data['date'] = ts
data['message'] = msgs[statusm]
data['version'] = "1.0.0"
return data
run(host='localhost', port=statport, debug=True)