[IMP] maintenancer_server_monitoring :

* simplify log management in launch_test function
* set ping True when ping very slow
* bugfix on ssh
This commit is contained in:
clementthomas
2024-04-12 09:13:32 +02:00
parent fa21e7a2ac
commit ef3e32071d
3 changed files with 12 additions and 18 deletions

View File

@@ -110,22 +110,22 @@ class MaintenanceEquipment(models.Model):
"""
self.search([("enable_monitoring","=",True)]).monitoring_test()
def launch_test(self, attribute, log, *test_function_args):
def launch_test(self, field_name, *test_function_args):
"""run test function with name = test_[attribute]
associate result of test to equipment
write logs of test
Args:
attribute (string): attribute of MaintenanceEquipment we want to test
attribute_name (string): attribute of MaintenanceEquipment we want to test
*test_function_args = optionnal args to pass to function (unused for the moment)
Returns:
MonitoringTest: returned by test function
"""
test_function = getattr(self,"test_"+attribute)
test_function = getattr(self,"test_"+field_name)
test = test_function(*test_function_args)
setattr(self, attribute, test.result)
log.write(test.log)
setattr(self, field_name, test.result)
return test
def get_tests(self):
@@ -139,25 +139,20 @@ class MaintenanceEquipment(models.Model):
def monitoring_test(self):
for equipment in self:
# we use StingIO instead of string to use mutable object
log = StringIO()
# array of all tests
tests_results = []
# run all tests referenced in get_tests and save result
for test in self.get_tests():
tests_results.append(equipment.launch_test(test, log))
tests_results.append(equipment.launch_test(test))
# set test date
equipment.last_monitoring_test_date = fields.Datetime.now()
# write logs
log.seek(0) #log is a StringIO so seek to beginning before read
new_log = f'📣 {fields.Datetime.now()}\n{log.read()}\n'
new_log = new_log.replace("\n","<br />") # log field is HTML, so format lines
#new logs are current datetime + join of test results logs
new_log = f'📣 {fields.Datetime.now()}\n{"".join([tests_result.log for tests_result in tests_results])}\n'.replace("\n","<br />")
#add new logs to the beginning of equipment log
equipment.log = f'{new_log}<br />{equipment.log}'[:LOG_LIMIT] #limit logs
#Create maintenance request only if monitoring is enabled

View File

@@ -64,7 +64,7 @@ class MaintenanceEquipment(models.Model):
return test.test_warning(True, f"PING OK in {ping_ms}ms (> {MAX_PING_MS_WARNING})")
else:
# ping result higher than ERROR => ERROR
return test.test_error(False, f"PING OK in {ping_ms}ms (> {MAX_PING_MS_ERROR})")
return test.test_error(True, f"PING OK in {ping_ms}ms (> {MAX_PING_MS_ERROR})")
else:
return test.test_error(False, "PING FAILED")

View File

@@ -22,7 +22,6 @@ class MaintenanceEquipment(models.Model):
* log file
"""
test = self.MonitoringTest("SSH OK")
self.get_ssh_connection()
try:
# SSH connection ok : set ssh connection in result, converted in boolean (True) when set in ssh_ok field
return test.test_ok(self.get_ssh_connection(), "SSH Connection OK") #ssh connection given by maintenance_server_ssh module