Commit ef95d4fe authored by Julien Muchembled's avatar Julien Muchembled

admin: new --smtp-tls & --smtp-auth options

parent caabe3c8
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import getpass, os, smtplib import getpass, os
from collections import Counter from collections import Counter
from email.mime.text import MIMEText from email.mime.text import MIMEText
from email.utils import formataddr, formatdate from email.utils import formataddr, formatdate
...@@ -100,6 +100,8 @@ class Backup(Monitor): ...@@ -100,6 +100,8 @@ class Backup(Monitor):
class Application(BaseApplication, Monitor): class Application(BaseApplication, Monitor):
"""The storage node application.""" """The storage node application."""
from smtplib import SMTP
@classmethod @classmethod
def _buildOptionParser(cls): def _buildOptionParser(cls):
_ = cls.option_parser _ = cls.option_parser
...@@ -116,6 +118,10 @@ class Application(BaseApplication, Monitor): ...@@ -116,6 +118,10 @@ class Application(BaseApplication, Monitor):
help='name of backup cluster to monitor' + hint) help='name of backup cluster to monitor' + hint)
_('smtp', metavar='HOST[:PORT]', _('smtp', metavar='HOST[:PORT]',
help='SMTP for email notifications') help='SMTP for email notifications')
_.bool('smtp-tls',
help='use STARTTLS')
_('smtp-auth', metavar='USER:PASS',
help='SMTP credentials')
_.int('i', 'nid', _.int('i', 'nid',
help="specify an NID to use for this process (testing purpose)") help="specify an NID to use for this process (testing purpose)")
...@@ -135,8 +141,13 @@ class Application(BaseApplication, Monitor): ...@@ -135,8 +141,13 @@ class Application(BaseApplication, Monitor):
x.max_lag = max_lag x.max_lag = max_lag
self.email_list = config.get('monitor_email', ()) self.email_list = config.get('monitor_email', ())
if self.email_list: if self.email_list:
self.smtp = smtplib.SMTP()
self.smtp_host = config.get('smtp') or 'localhost' self.smtp_host = config.get('smtp') or 'localhost'
self.smtp_tls = config.get('smtp_tls')
if 'smtp_auth' in config:
user, pwd = config['smtp_auth'].split(':', 1)
self.smtp_login = user, pwd
else:
self.smtp_login = None
email_from = os.getenv('EMAIL') email_from = os.getenv('EMAIL')
if not email_from: if not email_from:
try: try:
...@@ -321,9 +332,13 @@ class Application(BaseApplication, Monitor): ...@@ -321,9 +332,13 @@ class Application(BaseApplication, Monitor):
msg['Subject'] = 'NEO monitoring: ' + x msg['Subject'] = 'NEO monitoring: ' + x
msg['From'] = self.email_from msg['From'] = self.email_from
msg['To'] = ', '.join(email_list) msg['To'] = ', '.join(email_list)
s = self.smtp s = self.SMTP()
try: try:
s.connect(self.smtp_host) s.connect(self.smtp_host)
if self.smtp_tls:
s.starttls()
if self.smtp_login:
s.login(*self.smtp_login)
s.sendmail(None, email_list, msg.as_string()) s.sendmail(None, email_list, msg.as_string())
except Exception: except Exception:
x = format_exc() x = format_exc()
......
...@@ -431,10 +431,12 @@ class ServerNode(Node): ...@@ -431,10 +431,12 @@ class ServerNode(Node):
class AdminApplication(ServerNode, neo.admin.app.Application): class AdminApplication(ServerNode, neo.admin.app.Application):
def __setattr__(self, name, value): def SMTP(self):
if name == 'smtp': return self.smtp
value = FakeSMTP()
super(AdminApplication, self).__setattr__(name, value) @cached_property
def smtp(self):
return FakeSMTP()
class MasterApplication(ServerNode, neo.master.app.Application): class MasterApplication(ServerNode, neo.master.app.Application):
pass pass
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment