Newer
Older
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright 2017, Florent Thiery
'''

Florent Thiery
committed
Checks that DNS records are provided by the customer servers are correctly set
'''
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]

Florent Thiery
committed
def check_dns(hostname, expected_ip):
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)

Florent Thiery
committed
if address != expected_ip:
print('Expected ip was: %s, got %s' % (expected_ip, address))

Florent Thiery
committed
color = RED
all_ok = False
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()

Florent Thiery
committed
ip = conf.get('NETWORK_IP_NAT')
if not ip or ip == '0':
ip = conf.get('NETWORK_IP')

Florent Thiery
committed
conf_resolvers_keys = (
'NETWORK_DNS1',
'NETWORK_DNS2',
)
for conf_resolver_key in conf_resolvers_keys:
conf_resolver = conf.get(conf_resolver_key)
if conf_resolver and conf_resolver != '0' and conf_resolver not in resolvers:

Florent Thiery
committed
print('Resolver %s not configured on the system' % conf_resolver)
all_ok = False
conf_servers = (
'MS_SERVER_NAME',
'MONITOR_SERVER_NAME',
'CM_SERVER_NAME',
)

Florent Thiery
committed
for s in conf_servers:
hostname = conf.get(s)
ok = check_dns(hostname, ip)

Florent Thiery
committed
else:
print('conf.sh not found')
sys.exit(1)
if not all_ok:
sys.exit(1)
else:
sys.exit(0)