From b30a714393b464777bd24cfd95fe247552c5333a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Florent=20Thi=C3=A9ry?= <florent.thiery@ubicast.eu>
Date: Wed, 22 Feb 2017 18:32:48 +0100
Subject: [PATCH] add dns record tester, fixes #20581

---
 tests/test_dns_records.py | 65 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 65 insertions(+)
 create mode 100755 tests/test_dns_records.py

diff --git a/tests/test_dns_records.py b/tests/test_dns_records.py
new file mode 100755
index 00000000..06d4552a
--- /dev/null
+++ b/tests/test_dns_records.py
@@ -0,0 +1,65 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+# Copyright 2017, Florent Thiery
+'''
+Checks that DNS records are provided by the customer servers
+'''
+import subprocess
+import os
+import sys
+import imp
+
+GREEN = '\033[92m'
+RED = '\033[91m'
+DEF = '\033[0m'
+
+def get_dns_servers():
+    servers = list()
+    with open('/etc/resolv.conf', 'r') as f:
+        d = f.read().strip()
+        for l in d.split('\n'):
+            if l.startswith('nameserver '):
+                servers.append(l.split('nameserver ')[1])
+    return servers
+
+resolvers = get_dns_servers()
+
+def get_result(output):
+    for line in output.split('\n'):
+        if "has address " in line:
+            return line.split("has address ")[1]
+
+def check_dns(hostname):
+    all_ok = True
+    for resolver in resolvers:
+        status, output = subprocess.getstatusoutput("host %s %s" % (hostname, resolver))
+        success = (status == 0)
+        if success:
+            color = GREEN
+            address = get_result(output)
+        else:
+            color = RED
+            all_ok = False
+            address = "FAIL"
+        print('%sDNS resolution of %s on server %s returned %s%s' % (color, hostname, resolver, address, DEF))
+    return all_ok
+
+if os.path.isfile('../utils.py'):
+    all_ok = True
+    es_utils = imp.load_source('es_utils', '../utils.py')
+    conf = es_utils.load_conf()
+    servers = (
+        'MS_SERVER_NAME',
+        'MONITOR_SERVER_NAME',
+        'CM_SERVER_NAME',
+    )
+    for s in servers:
+        hostname = conf.get('MS_SERVER_NAME')
+        ok = check_dns(hostname)
+        if not ok:
+            all_ok = False
+
+if not all_ok:
+    sys.exit(1)
+else:
+    sys.exit(0)
-- 
GitLab