DRY up some server alert logic a bit

This commit is contained in:
Lance Edgar 2016-12-10 23:04:42 -06:00
parent 4f42ad164a
commit de1543018a

View file

@ -107,6 +107,14 @@ class TempmonServerDaemon(Daemon):
self.update_status(probe, self.enum.TEMPMON_PROBE_STATUS_ERROR) self.update_status(probe, self.enum.TEMPMON_PROBE_STATUS_ERROR)
def update_status(self, probe, status, reading=None): def update_status(self, probe, status, reading=None):
data = {
'probe': probe,
'status': self.enum.TEMPMON_PROBE_STATUS[status],
'reading': reading,
'taken': localtime(self.config, reading.taken, from_utc=True) if reading else None,
'now': localtime(self.config),
}
prev_status = probe.status prev_status = probe.status
if probe.status != status: if probe.status != status:
probe.status = status probe.status = status
@ -115,13 +123,7 @@ class TempmonServerDaemon(Daemon):
# send email when things go back to normal, after being bad # send email when things go back to normal, after being bad
if status == self.enum.TEMPMON_PROBE_STATUS_GOOD_TEMP: if status == self.enum.TEMPMON_PROBE_STATUS_GOOD_TEMP:
send_email(self.config, 'tempmon_good_temp', { send_email(self.config, 'tempmon_good_temp', data)
'probe': probe,
'status': self.enum.TEMPMON_PROBE_STATUS[status],
'reading': reading,
'taken': localtime(self.config, reading.taken, from_utc=True) if reading else None,
'now': localtime(self.config),
})
probe.status_alert_sent = self.now probe.status_alert_sent = self.now
# no (more) email if status is good # no (more) email if status is good
@ -146,14 +148,6 @@ class TempmonServerDaemon(Daemon):
self.enum.TEMPMON_PROBE_STATUS_ERROR : 'tempmon_error', self.enum.TEMPMON_PROBE_STATUS_ERROR : 'tempmon_error',
} }
data = {
'probe': probe,
'status': self.enum.TEMPMON_PROBE_STATUS[status],
'reading': reading,
'taken': localtime(self.config, make_utc(reading.taken, tzinfo=True)) if reading else None,
'now': localtime(self.config),
}
send_email(self.config, msgtypes[status], data) send_email(self.config, msgtypes[status], data)
# maybe send more emails if config said so # maybe send more emails if config said so