Python script to query NTP status and return it as JSON

Setting up and automatically keeping an eagle eye on NTP from software can be a bit of a (difficult) black art. A common way to query NTP status is to run:

ntpq -p

This returns some NTP status information. It’s output can be a bit difficult to work with from your software, so I have included a small python 3 script which runs runs “ntpq -p” parses, and prints the output in Json format. So instead of shelling out to ntpq you can shell out to this script and deal with the resulting json string instead… This can be handy if you don’t have access to a Python NTP library.

#!/usr/bin/python

#   Copyright 2018 Kevin Godden
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

import subprocess
import json
import re
import sys

# Shell out to 'ntpq -p'
proc = subprocess.Popen(['ntpq', '-p'], stdout=subprocess.PIPE)

# Get the output
stdout_value = proc.communicate()[0].decode("utf-8")

#remove the header lines
start = stdout_value.find("===\n")

if start == -1:
    # We may be running on windows (\r\n), try \r...
    start = stdout_value.find("===\r")

    if start == -1:
        # No, go, exit with error
        result = {'query_result': 'failed', 'data': {}}
        print(json.dumps(result))
        sys.exit(1)

# Get the data part of the string
#pay_dirt = stdout_value[start+4:]
pay_dirt = stdout_value[start:]

# search for NTP line starting with * (primary server)
exp = ("\*((?P\S+)\s+)"
       "((?P\S+)\s+)"
       "((?P\S+)\s+)"
       "((?P\S+)\s+)"
       "((?P\S+)\s+)"
       "((?P\S+)\s+)"
       "((?P\S+)\s+)"
       "((?P\S+)\s+)"
       "((?P\S+)\s+)"
       "((?P\S+)\s+)")

regex = re.compile(exp, re.MULTILINE)
r = regex.search(pay_dirt)

# Did we get anything?
if not r:
    # No, try again without the * at the beginning, get
    # the first entry instead
    exp = (" ((?P\S+)\s+)"
           "((?P\S+)\s+)"
           "((?P\S+)\s+)"
           "((?P\S+)\s+)"
           "((?P\S+)\s+)"
           "((?P\S+)\s+)"
           "((?P\S+)\s+)"
           "((?P\S+)\s+)"
           "((?P\S+)\s+)"
           "((?P\S+)\s+)")

    regex = re.compile(exp, re.MULTILINE)
    r = regex.search(pay_dirt)

data = {}

if r:
    data = r.groupdict()

# Output Result
result = {'query_result': 'ok' if r else 'failed', 'data': data}

print(json.dumps(result))

GitHub repo is here.