From 2b9050dd40a2e95d54cb1bb40e969b38c807ec4b Mon Sep 17 00:00:00 2001 From: js420 Date: Thu, 10 Jan 2019 13:16:48 +0100 Subject: Errorhandling and Logfix Fixed logging. Fixed the log size when the msg was too large. Added better errorhandling (try/excepts). Added Thrift Masterserver calls. Added README.txt. --- README.txt | 7 +++ bwLpStatus.py | 126 ++++++++++++++++++++++++++++++---------------- bwlpMonitor_template.html | 7 +-- style.css | 17 ++++--- 4 files changed, 106 insertions(+), 51 deletions(-) create mode 100644 README.txt mode change 100644 => 100755 bwLpStatus.py diff --git a/README.txt b/README.txt new file mode 100644 index 0000000..9b7489f --- /dev/null +++ b/README.txt @@ -0,0 +1,7 @@ +Python modules needed: +pip3 install thriftpy + +bwlp.thrift needed: +wget http://git.openslx.org/bwlp/master-sync-shared.git/plain/src/main/thrift/bwlp.thrift + + diff --git a/bwLpStatus.py b/bwLpStatus.py old mode 100644 new mode 100755 index a0642a8..fbe5e09 --- a/bwLpStatus.py +++ b/bwLpStatus.py @@ -1,7 +1,8 @@ +#!/usr/bin/python3 import subprocess import urllib.request import urllib.error -#import mysql.connector +# import mysql.connector import thriftpy from thriftpy.rpc import make_client from thriftpy.transport import TFramedTransportFactory @@ -39,7 +40,7 @@ class Status: self.data = data class LogEntry: - def __init__(self, date, host, service, state, msg = '', data = []): + def __init__(self, date, service, state, host, msg = '', data = []): self.date = date self.host = host self.service = service @@ -58,69 +59,93 @@ class Organisation: # Check connection functions. def ping(name, hostname): # Ping a hostname and tell if the server is up or down. - print('Ping request ... ', end='') + print('Ping request ' + hostname + ' ... ', end='') response = subprocess.Popen( ['ping', '-c', '1', hostname], stdout=subprocess.PIPE, stderr=subprocess.PIPE ) response = response.communicate() - output = response[0].decode('utf-8') + # output = response[0].decode('utf-8') error = response[1].decode('utf-8') # Error happend if error != '': - print('error: ', end='') - print(error) + print('\033[91m' + 'error' + '\033[0m') statusList.append(Status(name, hostname, 'error', 'Offline', 'ping', error.rstrip())) else: - print('success') + print('\033[92m' + 'success' + '\033[0m') statusList.append(Status(name, hostname, 'success', 'Online', 'ping')) logStatus(statusList[-1]) def https(name, url): - print('HTTPS request') + 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')) - - logStatus(statusList[-1]) + 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 mysql(self, host, user, passwd, db): - print('MYSQL request start ...') - #db = mysql.connector.connect( - # host='localhost', - # user='openslx', - # passwd='geheim', - # database='openslx' - #) - #cursor = db.cursor() - #cursor.execute('SELECT * FROM user') - #result = cursor.fetchall() - #for user in result: - # print(user[1]) - -def thrift(name, ip, port): - print('THRIFT request start ...') + pass + # TODO: Backend to check the mysql status needs to be implemented. + # print('MYSQL request start ...') + # db = mysql.connector.connect( + # host='localhost', + # user='openslx', + # passwd='geheim', + # database='openslx' + # ) + # cursor = db.cursor() + # cursor.execute('SELECT * FROM user') + # result = cursor.fetchall() + # for user in result: + # print(user[1]) + +# Creates the thrift client and proceeds 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') - # masterserver = make_client(bwlp_thrift.MasterServer, '132.230.4.16', 9090, trans_factory=TFramedTransportFactory()) - # TODO: TRY CATCH - satserver = make_client(bwlp_thrift.SatelliteServer, ip, port, trans_factory=TFramedTransportFactory()) - organisations = satserver.getAllOrganizations() - organisationList = [] - for org in organisations: - organisationList.append(Organisation(org.organizationId, org.displayName)) - host = ip + ':' + str(port) - statusList.append(Status(name, host, 'success', 'Online (' + str(len(organisationList)) + ')', 'thrift', data=organisationList)) - logStatus(statusList[-1]) + organisations = [] + # Different clients for SAT / Master is needed. + # TODO: TRY CATCH + try: + if server is 'SAT': + satserver = make_client(bwlp_thrift.SatelliteServer, ip, port, trans_factory=TFramedTransportFactory()) + organisations = satserver.getAllOrganizations() + + elif server is '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)) + + statusList.append(Status(name, host, 'success', 'Online (' + str(len(organisationList)) + ')', 'thrift', msg=str(len(organisationList)) + " organizations",data=organisationList)) + print('\033[92m' + 'success' + '\033[0m') + #logStatus(statusList[-1]) + except ConnectionResetError: + statusList.append(Status(name, host, 'error', 'Offline', 'thrift', msg="ConnectionResetError: [Errno 104] Connection reset by peer")) + print('\033[91m' + 'error' + '\033[0m') + # statusList.append(Status(name, host, 'error', 'Offline', 'thrift', msg=str(e))) + finally: + logStatus(statusList[-1]) # Parses the log from the logfile. Fills the logEntries array. def parseLog(): @@ -140,8 +165,8 @@ def writeLog(): with open('bwLpStatusLog.txt', 'a') as log: for entry in logEntries[newLogIndex:]: log_string = entry.date + '\t' + entry.service + '\t' + entry.state + '\t' + entry.host - if entry.msg != '': log_string += '\t' + entry.msg - if entry.data != []: log_string += '\t' + str(entry.data) + 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. @@ -153,19 +178,25 @@ def getLogEntry(status): # 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) + # print(status.host + " " + status.service + " " + status.state) 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.host, status.service, status.state, status.msg, status.data)) + logEntries.append(LogEntry(date, status.service, status.state, status.host, status.msg, status.data)) # Parse the logfile. parseLog() # Call the checks. https('Masterserver HTTPS Service', 'https://bwlp-masterserver.ruf.uni-freiburg.de') +https('Fail Test', '5121236') +https('Fail Test', 'https://www.amazony.de/') ping('Masterserver IP Ping', '132.230.4.16') ping('Fileserver Ping', 'files.bwlp.ks.uni-freiburg.de') ping('Backup fileserver Ping', 'bwlp-backup.ruf.uni-freiburg.de') -thrift('Thrift SAT', '132.230.8.192', 9090) +ping('Ping Fail', '0.0.0.1') +thrift('Thrift SAT', '132.230.8.192', 9090, 'SAT') +thrift('Thrift MASTER', '132.230.4.16', 9090, 'MASTER') +thrift('Thrift MASTER', '132.230.4.16', 9091, 'MASTER') ping('BAS Ping', 'bas.intra.uni-freiburg.de') ping('yc', '127.0.0.2') ping('yx', '127.0.0.3') @@ -192,12 +223,23 @@ for status in statusList: if minutes > 0: timeString += str(minutes) + ' minutes' - code += '
' + status.name + '
' + status.host + '
' + status.status + '
' + timeString + '
\r\n' + code += ('
' + + status.name + '
' + + status.host + '
' + + status.status + '
' + + timeString + '
\r\n') # Generate the html code for the log. log = '\r\n' for entry in reversed(logEntries): - log += '
[' + entry.date + ']
[' + entry.service + ']
[' + entry.state + ']
[' + entry.host +']
' + entry.msg + '
' + log += ('
[' + + entry.date + ']
[' + + entry.service + ']
[' + + entry.state + ']
[' + + entry.host +']
' + + entry.msg + '
') # Copy the .html file and replace the %CONTENT% to generate the final html file. shutil.copyfile('bwlpMonitor_template.html', 'bwlpMonitor.html') diff --git a/bwlpMonitor_template.html b/bwlpMonitor_template.html index e90b251..6a41d66 100644 --- a/bwlpMonitor_template.html +++ b/bwlpMonitor_template.html @@ -28,10 +28,11 @@
%CONTENT%
- -
+
+
%LOG% -
+
+
diff --git a/style.css b/style.css index eaba8a2..3150bc3 100644 --- a/style.css +++ b/style.css @@ -20,7 +20,7 @@ a { a:hover { color: #014c8c; - !text-decoration: underline; + /*text-decoration: underline;*/ } #header { @@ -137,7 +137,7 @@ a:hover { } .success { - color: #58dc42; !#15e005; + color: #58dc42; /*#15e005;*/ } .warning { @@ -151,15 +151,20 @@ a:hover { #log { display: flex; flex-direction: column; - box-shadow: 0 2px 4px 0 rgba(0,0,0,0.16),0 2px 10px 0 rgba(0,0,0,0.12) !important; background-color: #f5f5f5; + white-space: nowrap; + height: 100%; +} + +#log_wrapper { + display: flex; min-height: 150px; max-height: 600px; - white-space: nowrap; - overflow: auto; margin-top: 60px; - flex-shrink: 5; margin-bottom: 90px; + box-shadow: 0 2px 4px 0 rgba(0,0,0,0.16),0 2px 10px 0 rgba(0,0,0,0.12) !important; + overflow: auto; + max-width: 80vw; } .log_item { -- cgit v1.2.3-55-g7522