-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.py
127 lines (116 loc) · 4.56 KB
/
status.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
import socket
import json
import threading
import time
import logging
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
serviceconfigversion = None
lastsuccessfulflush = None
lastfailedflush = None
flushids = {'Edgecast': None, 'Cloudfront': None, 'Level3': None}
flushfailcount = 0
flushSuccess = {}
serviceconfigfailcount = 0
configfailcount = 0
class StatusHandler(BaseHTTPRequestHandler):
def __init__(self, hostname, version, instanceid, *args):
self.hostname = hostname
self.version = version
self.instanceid = instanceid
BaseHTTPRequestHandler.__init__(self, *args)
# Handler for the GET requests
def do_GET(self):
if serviceconfigfailcount > 0:
status = 'Critical'
message = 'Could not get the service config version!'
elif configfailcount > 0:
status = 'Warning'
message = 'LPConfig request failed or version doesnt match c3ServiceConfig.xml.'
elif flushfailcount > 0:
status = 'Warning'
message = 'Flush request failed.'
elif not all(flushSuccess.values()):
status = 'Warning'
message = 'Flush request in progress.'
else:
status = 'Ok'
message = 'Ok'
basicjsonresponse = {
"basic":
{
"status": status,
"hostname": self.hostname,
"componentName": "config-flusher",
"instanceId": self.instanceid,
"version": self.version,
"message": message
},
}
detailsjsonresponse = {
"details":
{
"lastSuccesfulFlush":
{
"flushIds":
{
"Edgecast": flushids['Edgecast'],
"Cloudfront": flushids['Cloudfront'],
"Level3": flushids['Level3'],
},
"time": lastsuccessfulflush,
},
"lastFailedFlush":
{
"time": lastfailedflush,
},
"lastFlushSuccessStatus": flushSuccess,
"c3ConfigVersion": serviceconfigversion,
}
}
statisticsjsonresponse = {
"failCounts":
{
"serviceConfig": serviceconfigfailcount,
"Config": configfailcount,
"Flush": flushfailcount,
}
}
if self.path == "/1/status":
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(json.dumps({"status": status}))
self.wfile.write("\n")
elif self.path == "/1/status/basic":
jsonresponse = basicjsonresponse
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(json.dumps(jsonresponse, indent=4, separators=(',', ': ')))
self.wfile.write("\n")
elif self.path == "/1/status/details":
jsonresponse = basicjsonresponse
jsonresponse.update(detailsjsonresponse)
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(json.dumps(jsonresponse, indent=4, separators=(',', ': ')))
self.wfile.write("\n")
elif self.path == "/1/status/statistics":
jsonresponse = statisticsjsonresponse
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(json.dumps(jsonresponse, indent=4, separators=(',', ': ')))
self.wfile.write("\n")
def start_status_server(port, version, instanceid):
# status server
hostname = socket.gethostname()
# a wrapper, so that we can pass some parameters to BaseHTTPRequestHandler
def status_handler_wrapper(*arguments):
StatusHandler(hostname, version, instanceid, *arguments)
server = HTTPServer(('', port), status_handler_wrapper)
thread = threading.Thread(target=server.serve_forever, name='status_server')
thread.daemon = True
thread.start()
logging.info("Started httpserver on port %d" % port)