Newer
Older

Stéphane Diemer
committed
Criticality: High
import random
import time
import imp
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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')
print_red('Postfix dir does not exists, please install postfix.')
all_ok = False
# check that postfix listens the port 25 correctly
status, out = subprocess.getstatusoutput('netstat -pant | grep master | grep 127.0.0.1:25')
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))