#!/usr/bin/env python3 # -*- coding: utf-8 -*- # Copyright 2017, Florent Thiery ''' 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 if os.path.isfile('/usr/bin/ntpq'): cmd = "LANG=C ntpq -pd" expected = "remote" else: cmd = "LANG=C timedatectl" expected = 'NTP synchronized' print("Running %s" % cmd) status = subprocess.getoutput(cmd) if expected not in status: print('NTP not working: %s' % status) sys.exit(1) else: print('System is NTP synchronized') os.chdir(os.path.dirname(__file__)) 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() NTP_SERVER = conf.get('NTP_SERVER') or 'ntp.ubuntu.com' with open('/etc/ntp.conf', 'r') as f: d = f.read() servers = list() for l in d.split('\n'): if l.startswith('server '): servers.append(l.split('server ')[1]) if not 'server %s' % NTP_SERVER in d: print('Expected NTP server %s not found in /etc/ntp.conf, found %s instead' % (NTP_SERVER, servers)) sys.exit(1) else: print('Expected NTP server %s found in configuration (total servers: %s)' % (NTP_SERVER, len(servers))) else: print('Could not find envsetup conf file or not running from expected location') sys.exit(1)