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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
|
#!/usr/bin/python3
import subprocess
import urllib.request
import urllib.error
import thriftpy
from thriftpy.rpc import make_client
from thriftpy.transport import TFramedTransportFactory
import shutil
import datetime
import os
import tftpy
import logging
# Global variables
statusList = []
logEntries = []
newLogIndex = 0
# Class of the status objects
class Status:
def __init__(self, name, host, state, status, service, msg = '', data = []):
# Name of the service.
self.name = name
# Hostname (Domain, ip, ...)
self.host = host
# State of the response. (success, warning, error) <-- This class is responsible for the coloring (green, orange, red)
self.state = state
# Status of the check. (Online, Offline, Temporaily not available, Some services unavailable, ...) <-- This text is shown on the website
self.status = status
# Type of the service. (ping, https, tftp, thrift, ...)
self.service = service
# Message e.g. error message
self.msg = msg
# Data the request return e.g. organisation list from the thrift client.
self.data = data
class LogEntry:
def __init__(self, date, service, state, host, msg = '', data = []):
self.date = date
self.host = host
self.service = service
self.state = state
self.msg = msg
self.data = data
class Organisation:
def __init__(self, id, name):
self.id = id
self.name = name
def __repr__(self):
return str(self.__dict__)
# Check connection functions.
def ping(name, hostname):
# Ping a hostname and tell if the server is up or down.
print('Ping request ' + hostname + ' ... ', end='')
response = subprocess.Popen(
['ping', '-c', '1', hostname],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
response = response.communicate()
error = response[1].decode('utf-8')
# Error happend
if error != '':
print('\033[91m' + 'error' + '\033[0m')
statusList.append(Status(name, hostname, 'error', 'Offline', 'ping', error.rstrip()))
else:
print('\033[92m' + 'success' + '\033[0m')
statusList.append(Status(name, hostname, 'success', 'Online', 'ping'))
logStatus(statusList[-1])
def https(name, url):
print('HTTPS request ' + url + ' ... ', end='')
try:
r = urllib.request.urlopen(url)
if r.getcode() == 200:
statusList.append(Status(name, url, 'success', 'Online', 'https'))
print('\033[92m' + 'success' + '\033[0m')
else:
statusList.append(Status(name, url, 'error', 'Offline', 'https'))
print('\033[91m' + 'error' + '\033[0m')
except urllib.error.URLError as e:
statusList.append(Status(name, url, 'error', 'Offline', 'https', msg=str(e)))
print('\033[91m' + 'error' + '\033[0m')
except ValueError:
statusList.append(Status(name, url, 'error', 'Offline', 'https', msg="Unknown url type"))
print('\033[91m' + 'error' + '\033[0m')
finally:
logStatus(statusList[-1])
def tftp(name, host, port, filename):
print('TFTP request ' + host + ' ...', end='')
hostname = host + ':' + str(port) + ':' + filename
try:
client = tftpy.TftpClient(host, port)
client.download(filename, 'tmp_tftp_file')
print('\033[92m' + 'success' + '\033[0m')
# Delete tmp tftp file
os.remove('tmp_tftp_file')
statusList.append(Status(name, hostname, 'success', 'Online', 'tftp', msg='File ' + filename + ' downloaded successfully.'))
except Exception as e:
print('\033[91m' + 'error' + '\033[0m')
statusList.append(Status(name, hostname, 'error', 'Offline', 'tftp', msg=str(e)))
finally:
logStatus(statusList[-1])
# Creates the thrift client and prwlp-pxe.ruf.uni-freiburg.de .ceeds the get Organisations call. Server can either be SAT or MASTER
def thrift(name, ip, port, server):
host = ip + ':' + str(port)
print('THRIFT request ' + host + ' ...', end='')
bwlp_thrift = thriftpy.load('bwlp.thrift', module_name='bwlp_thrift')
organisations = []
# Different clients for SAT / Master is needed.
try:
if server == 'SAT':
satserver = make_client(bwlp_thrift.SatelliteServer, ip, port, trans_factory=TFramedTransportFactory())
organisations = satserver.getAllOrganizations()
elif server == 'MASTER':
masterserver = make_client(bwlp_thrift.MasterServer, ip, port, trans_factory=TFramedTransportFactory())
organisations = masterserver.getOrganizations()
organisationList = []
for org in organisations:
organisationList.append(Organisation(org.organizationId, org.displayName))
if len(organisationList) == 0:
statusList.append(Status(name, host, 'warning', 'Online (' + str(len(organisationList)) + ')', 'thrift', msg=str(len(organisationList)) + " organizations found",data=organisationList))
else:
statusList.append(Status(name, host, 'success', 'Online (' + str(len(organisationList)) + ')', 'thrift', msg=str(len(organisationList)) + " organizations found",data=organisationList))
print('\033[92m' + 'success' + '\033[0m')
except ConnectionResetError:
statusList.append(Status(name, host, 'error', 'Offline', 'thrift', msg="ConnectionResetError: [Errno 104] Connection reset by peer"))
print('\033[91m' + 'error' + '\033[0m')
finally:
logStatus(statusList[-1])
# Parses the log from the logfile. Fills the logEntries array.
def parseLog():
global logEntries
if not (os.path.exists('history.txt')):
return
with open('history.txt', 'r') as log:
for line in log:
line = line.strip()
entry = line.split('\t')
if len(entry) < 4: continue
logEntries.append(LogEntry(entry[0], entry[1], entry[2], entry[3], '' if len(entry) < 5 else entry[4], [] if len(entry) < 6 else entry[5]))
global newLogIndex
newLogIndex = len(logEntries)
# Writes updated log in the logfile.
def writeLog():
with open('history.txt', 'a') as log:
for entry in logEntries[newLogIndex:]:
log_string = entry.date + '\t' + entry.service + '\t' + entry.state + '\t' + entry.host
log_string += '\t' + str(entry.msg)
log_string += '\t' + str(entry.data)
log.write(log_string + '\n')
# Returns the most recent log object to a given status. None if there is none.
def getLogEntry(status):
return next((x for x in reversed(logEntries) if (x.host == status.host) and (x.service == status.service)), None)
# Checks weather the status has to be logged or not. (Does the status has changes from the last time?)
def logStatus(status):
obj = getLogEntry(status)
if (obj is None) or (status.state != obj.state):
date = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
logEntries.append(LogEntry(date, status.service, status.state, status.host, status.msg, status.data))
# Reads the config and executes the calls.
def readConfig():
if not (os.path.exists('bwlp.config')):
return
with open('bwlp.config', 'r') as config:
for line in config:
line = line.strip()
entry = line.split('\t')
if entry[0] == '#': continue
check(entry)
# Calls the check method.
def check(entry):
if entry[0] == 'ping':
if (len(entry) < 3): return
ping(entry[1], entry[2])
elif entry[0] == 'https':
if (len(entry) < 3): return
https(entry[1], entry[2])
elif entry[0] == 'thrift':
if (len(entry) < 5): return
thrift(entry[1], entry[2], int(entry[3]), entry[4])
elif entry[0] == 'tftp':
if (len(entry) < 5): return
tftp(entry[1], entry[2], int(entry[3]), entry[4])
# Parse the logfile.
parseLog()
# Call the checks.
readConfig()
# Write the new logfile.
writeLog()
# HTML Processing to generate the website.
code = '\r\n'
for status in statusList:
# Prepare and calculate the time since the server is online / offline
obj = getLogEntry(status)
now = datetime.datetime.now()
date = datetime.datetime.strptime(obj.date, '%Y-%m-%d %H:%M:%S')
time = now - date
days = time.days
hours = time.seconds // 3600
minutes = (time.seconds // 60) % 60
timeString = ''
if days > 0:
timeString += str(days) + ' days '
if hours > 0:
timeString += str(hours) + ' hours '
if minutes > 0:
timeString += str(minutes) + ' minutes'
code += ('<div class="content_item"><div class="content_item_part"><div class="content_item_part_title">'
+ status.name + '</div><div class="content_item_part_subtitle">'
+ status.host + '</div></div><div class="content_item_part '
+ status.state + '"><div class="content_item_part_title">'
+ status.status + '</div><div class="content_item_part_subtitle">'
+ timeString + '</div></div></div>\r\n')
# Generate the html code for the log.
log = '\r\n'
# Cut the log to the last n entries
n = 500
cut_amount = 0
if len(logEntries) >= n:
cut_amount = len(logEntries) - n
for entry in reversed(logEntries[cut_amount:]):
log += ('<div class="log_item"><div class="log_item_part time"><div class="content_item_part_title">['
+ entry.date + ']</div></div><div class="log_item_part service"><div class="content_item_part_title">['
+ entry.service + ']</div></div><div class="log_item_part '
+ entry.state + '"><div class="content_item_part_title">['
+ entry.state + ']</div></div><div class="log_item_part"><div class="content_item_part_title">['
+ entry.host +']</div></div><div class="log_item_part"><div class="content_item_part_title log_item_part_msg">'
+ entry.msg + '</div></div></div>')
# Replace the %CONTENT% in the template with the actual html code.
html = open('template.html')
html2 = open('status.html', 'w')
for line in html:
html2.write(line.replace('%CONTENT%', code).replace('%LOG%', log))
html.close()
html2.close()
|