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

Stéphane Diemer
committed
Criticality: Low
Checks that the server is synchronized with the configured NTP server.
import os
import sys
import subprocess
import imp
# Check that ntpd is synced
cmd = 'LANG=C ntpq -pd'
expected = 'remote'
ntpconf = '/etc/ntp.conf'
ntpconf_expected = 'server '
ntpconf = '/etc/systemd/timesyncd.conf'
ntpconf_expected = 'NTP='
status = subprocess.getoutput(cmd)
if expected not in status:
print('NTP not working: %s' % status)
sys.exit(1)
else:
print('System is NTP synchronized')
print('Checking NTP server conforms to conf')
if os.path.isfile('../utils.py'):
es_utils = imp.load_source('es_utils', '../utils.py')
conf = es_utils.load_conf()
expected_servers = (conf.get('NTP_SERVER') or 'ntp.ubuntu.com').split(',')
d = f.read()
servers = list()
for l in d.split('\n'):
servers.append(l[len(ntpconf_expected):])
for expected_server in expected_servers:
if expected_server not in servers:
print('Expected NTP server %s not found in %s, found %s instead.' % (expected_server, ntpconf, servers))
sys.exit(1)
else:
print('Expected NTP server %s found in configuration (total servers: %s).' % (expected_server, len(servers)))
print('NTP OK')
print('Could not find envsetup conf file or not running from expected location.')