From c31ddef8b6ffe5c366cb89377b0da2c5f85b0ce0 Mon Sep 17 00:00:00 2001 From: Adrien Beudin Date: Fri, 10 Apr 2015 14:29:55 +0000 Subject: [PATCH] [enh] Add MX check + Refactoring --- data/actionsmap/yunohost.yml | 6 ++++++ lib/yunohost/monitor.py | 29 ++++++++++++++++++++++++++--- locales/en.json | 1 + 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/data/actionsmap/yunohost.yml b/data/actionsmap/yunohost.yml index 0ce254119..97592259c 100644 --- a/data/actionsmap/yunohost.yml +++ b/data/actionsmap/yunohost.yml @@ -679,6 +679,12 @@ monitor: action: append_const const: infos dest: units + -c: + full: --check + help: Check network configuration + action: append_const + const: check + dest: units -H: full: --human-readable help: Print sizes in human readable format diff --git a/lib/yunohost/monitor.py b/lib/yunohost/monitor.py index 53f15346b..70966fd5b 100644 --- a/lib/yunohost/monitor.py +++ b/lib/yunohost/monitor.py @@ -33,6 +33,7 @@ import xmlrpclib import os.path import errno import os +import dns.resolver import cPickle as pickle from urllib import urlopen from datetime import datetime, timedelta @@ -160,7 +161,7 @@ def monitor_network(units=None, human_readable=False): smtp_check = m18n.n('network_check_smtp_ko') if units is None: - units = ['usage', 'infos'] + units = ['check', 'usage', 'infos'] # Get network devices and their addresses devices = {} @@ -173,7 +174,30 @@ def monitor_network(units=None, human_readable=False): # Retrieve monitoring for unit(s) for u in units: - if u == 'usage': + if u == 'check': + result[u] = {} + with open('/etc/yunohost/current_host', 'r') as f: + domain = f.readline().rstrip() + cmd_check_smtp = os.system('/bin/nc -z -w1 yunohost.org 25') + if cmd_check_smtp == 0: + smtp_check = m18n.n('network_check_smtp_ok') + else: + smtp_check = m18n.n('network_check_smtp_ko') + + try: + answers = dns.resolver.query(domain,'MX') + mx_check = {} + i = 0 + for server in answers: + mx_check[i] = server + i = i + 1 + except: + mx_check = m18n.n('network_check_mx_ko') + result[u] = { + 'smtp_check': smtp_check, + 'mx_check': mx_check + } + elif u == 'usage': result[u] = {} for i in json.loads(glances.getNetwork()): iname = i['interface_name'] @@ -210,7 +234,6 @@ def monitor_network(units=None, human_readable=False): 'public_ip': p_ip, 'local_ip': l_ip, 'gateway': gateway, - 'smtp_check' : smtp_check } else: raise MoulinetteError(errno.EINVAL, m18n.n('unit_unknown', u)) diff --git a/locales/en.json b/locales/en.json index ce2cd74eb..c0d89a7b0 100644 --- a/locales/en.json +++ b/locales/en.json @@ -118,6 +118,7 @@ "network_check_smtp_ok" : "Outbound mail (SMTP port 25) is not blocked", "network_check_smtp_ko" : "Outbound mail (SMTP port 25) seems to be blocked by your network", + "network_check_mx_ko" : "DNS MX record is not set", "ldap_initialized" : "LDAP successfully initialized", "admin_password_change_failed" : "Unable to change password",