added check hostname

This commit is contained in:
Andrew McLeod 2016-10-01 01:14:21 +02:00
parent 0179852eb2
commit 851d66aeba
3 changed files with 37 additions and 14 deletions

View File

@ -1,4 +1,8 @@
options:
check_hostname:
default: true
description: Check if local hostname is resolvable
type: boolean
dns_server:
default: ''
description: Use unit default DNS server

View File

@ -17,7 +17,29 @@ def ping(input, ping_time, ping_tries):
return 1
def check_local_hostname():
local_hostname = subprocess.check_output('hostname', shell=True)
lookup_cmd = "getent hosts {}".format(local_hostname)
hookenv.log('Looking up Hostname: {}'.format(local_hostname))
try:
result = subprocess.check_output(lookup_cmd, shell=True)\
.decode('utf-8').rstrip()
except subprocess.CalledProcessError as exc:
result = "Local hostname lookup failed: {}".format(str(exc.output))
stderr = exc.returncode
return result, stderr
def check_nodes(nodes):
cfg = hookenv.config()
check_hostname = cfg.get('check_hostname')
if check_hostname:
no_hostname = check_local_hostname()
if not no_hostname:
no_hostname = ', hostname ok'
else:
no_hostname = ', hostname failed: ' + str(no_hostname)
no_ping = check_ping(nodes)
no_dns = check_dns(nodes)
hookenv.log("Units with DNS problems: " + str(no_dns))
@ -40,9 +62,7 @@ def check_nodes(nodes):
if no_match != []:
dns_status = ', match dns failed: ' + str(no_match)
else:
if not no_rev:
no_rev = ', rev dns ok'
else:
if no_rev:
no_rev = ', rev dns failed: ' + str(no_rev)
if no_fwd != []:
no_fwd = ', fwd dns failed: ' + str(no_fwd)
@ -55,7 +75,10 @@ def check_nodes(nodes):
dns_status = '{}{}{}'\
.format(dns_status, str(no_rev), str(no_fwd))
check_status = '{}{}'.format(no_ping, str(dns_status))
if check_hostname:
check_status = '{}{}{}'.format(no_ping, str(no_hostname), str(dns_status))
else:
check_status = '{}{}'.format(no_ping, str(dns_status))
if 'failed' in check_status:
workload = 'blocked'
@ -182,7 +205,7 @@ def reverse_dns(input, dns_server, tries, timeout):
.decode('utf-8').rstrip()
stderr = 0
except subprocess.CalledProcessError as exc:
result = "Reverse DNS lookup error: " + str(exc.returncode)
result = "Reverse DNS lookup error: " + str(exc.output)
stderr = exc.returncode
if result == '':
result = 'No reverse response'
@ -201,7 +224,7 @@ def forward_dns(input, dns_server, tries, timeout):
.decode('utf-8').rstrip()
stderr = 0
except subprocess.CalledProcessError as exc:
result = "Forward DNS lookup error: " + str(exc.returncode)
result = "Forward DNS lookup error: " + str(exc.output)
stderr = exc.returncode
if result == '':
result = 'No forward response'

View File

@ -23,15 +23,11 @@ class TestDeploy(unittest.TestCase):
def test_deploy(self):
self.d.sentry.wait_for_messages({'magpie': re.compile('ok|failed')}, timeout=60)
def test_wait_for_settle(self):
"""
Make it wait for the units to stop doing stuff
"""
# self.d.sentry.wait()
def test_break_dns_single(self):
print ('Test break dns single...')
"""Break DNS on one unit, make sure DNS check fails, fix DNS, toggle back"""
self.d.sentry.wait_for_messages({'magpie': 'icmp ok, dns ok'}, timeout=60)
self.magpie_0.run("sudo mv /etc/resolv.conf /etc/resolv.conf.bak")
self.magpie_0.run("hooks/update-status")
self.d.sentry.wait_for_messages({'magpie': {re.compile('.*dns failed.*')}}, timeout=60)
@ -54,9 +50,9 @@ class TestDeploy(unittest.TestCase):
def test_break_ping_single(self):
print ('Test break ping single')
"""Take primary interface down and make sure ICMP fails."""
interface, retcode = self.magpie_1.run("ip route get 255.255.255.255")
interface = interface.split(" ")[3]
self.magpie_1.run("(sudo ifconfig {} down ; sleep 60 ; sudo ifconfig {} up)&".format(interface, interface))
#interface, retcode = self.magpie_1.run("ip route get 255.255.255.255")
#interface = interface.split(" ")[3]
self.magpie_1.run("(sudo service networking stop; sleep 60 ; sudo service networking start)&")
self.magpie_1.run("hooks/update-status")
self.d.sentry.wait_for_messages({'magpie': {re.compile('icmp failed.*')}}, timeout=60)
self.magpie_1.run("hooks/update-status")