#!/usr/bin/env python3 # -*- coding: utf-8 -*- ''' Criticality: High Check that emails can be sent. ''' import os import subprocess import sys import random import time import imp GREEN = '\033[92m' RED = '\033[91m' DEF = '\033[0m' def print_color(txt, col): print('%s%s%s' % (col, txt, DEF)) def print_red(txt): print_color(txt, RED) def print_green(txt): print_color(txt, GREEN) all_ok = True def get_configured_relay(): status, out = subprocess.getstatusoutput('grep relayhost /etc/postfix/main.cf') if status == 0: return out.split('relayhost = ')[1] def check_relay(): global all_ok configured_relay = get_configured_relay() print('Checking if SMTP relay conforms to conf') if os.path.isfile('../utils.py'): es_utils = imp.load_source('es_utils', '../utils.py') conf = es_utils.load_conf() conf_relay = conf.get('EMAIL_SMTP_SERVER') if conf_relay != configured_relay: print_red('Configured STMP relay (%s) does not match the expected value (%s)' % (configured_relay, conf_relay)) all_ok = False def send_test_email(): global all_ok email = "noreply+%s@ubicast.eu" % random.randint(0, 1000) cmd = 'echo "This is a test email" | mail -s "Test email from `cat /etc/hostname`" %s' % email status, out = subprocess.getstatusoutput(cmd) time.sleep(5) status, out = subprocess.getstatusoutput('grep %s /var/log/mail.log | grep bounced' % email) if status == 0: print_red('Sending mail failed') all_ok = False else: print_green('Email sent') if not os.path.exists('/etc/postfix'): print_red('Postfix dir does not exists, please install postfix.') all_ok = False else: # check that postfix listens the port 25 correctly status, out = subprocess.getstatusoutput('netstat -pant | grep master | grep 127.0.0.1:25') if status != 0: print_red('The port 25 is not listened by any process.') all_ok = False else: print_green('Postfix listening port: OK.') check_relay() send_test_email() sys.exit(int(not all_ok))