From a953d5a79b8fec629ff6a0add901892013f83f23 Mon Sep 17 00:00:00 2001 From: Sage Weil Date: Mon, 7 Oct 2019 09:05:43 -0500 Subject: [PATCH] mgr/alerts: raise health alert if smtplib has a problem Signed-off-by: Sage Weil (cherry picked from commit 675ed5f26b42a37fc0863a7266eba00e0c3c0182) --- src/pybind/mgr/alerts/module.py | 34 ++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/src/pybind/mgr/alerts/module.py b/src/pybind/mgr/alerts/module.py index c063235683c1..5962e3018500 100644 --- a/src/pybind/mgr/alerts/module.py +++ b/src/pybind/mgr/alerts/module.py @@ -149,8 +149,13 @@ class Alerts(MgrModule): return d def _send_alert(self, status, diff): + checks = {} if self.smtp_host: - self._send_alert_smtp(status, diff) + r = self._send_alert_smtp(status, diff) + if r: + for code, alert in r.items(): + checks[code] = alert + self.set_health_checks(checks) def serve(self): """ @@ -229,12 +234,23 @@ class Alerts(MgrModule): self.log.debug('message: %s' % message) # send - if self.smtp_ssl: - server = smtplib.SMTP_SSL(self.smtp_host, self.smtp_port) - else: - server = smtplib.SMTP(self.smtp_host, self.smtp_port) - if self.smtp_password: - server.login(self.smtp_user, self.smtp_password) - server.sendmail(self.smtp_sender, self.smtp_destination, message) - server.quit() + try: + if self.smtp_ssl: + server = smtplib.SMTP_SSL(self.smtp_host, self.smtp_port) + else: + server = smtplib.SMTP(self.smtp_host, self.smtp_port) + if self.smtp_password: + server.login(self.smtp_user, self.smtp_password) + server.sendmail(self.smtp_sender, self.smtp_destination, message) + server.quit() + except Exception as e: + return { + 'ALERTS_SMTP_ERROR': { + 'severity': 'warning', + 'summary': 'unable to send alert email', + 'count': 1, + 'detail': [ str(e) ] + } + } self.log.debug('Sent email to %s' % self.smtp_destination) + return None -- 2.47.3