Commit 8330e313 authored by Olav Morken's avatar Olav Morken

Add script to plot clock offset for server.

parent c8f2474c
#!/usr/bin/env python3
import argparse
import datetime
import os
import statistics
import sys
import matplotlib.pyplot
import requests
def get_auth():
try:
with open(os.path.join(os.path.expanduser('~'), '.zabbixauth'), 'r') as fh:
return fh.read().strip()
except:
return None
def invoke(method, **kwargs):
message = {
"jsonrpc": "2.0",
"method": method,
"params": kwargs,
"id": 1,
"auth": get_auth(),
}
url = 'https://zabbix.uninett.no/zabbix/api_jsonrpc.php'
headers = {
'Content-Type': 'application/json-rpc',
}
r = requests.post(url, headers=headers, json=message)
try:
r.raise_for_status()
except requests.RequestException as e:
print('Error fetching {url}: {exception}'.format(url=url, exception=e), file=sys.stderr)
if e.response.text:
print('HTTP response body: {response}'.format(response=e.response.text), file=sys.stderr)
else:
print('No HTTP response body.', file=sys.stderr)
sys.exit(1)
response = r.json()
if not 'result' in response:
raise Exception('Invalid response from Zabbix: {response}'.format(response=repr(response)))
return response['result']
def parse_args():
parser = argparse.ArgumentParser('Plot clock offsets for server')
parser.add_argument('hostname', nargs=1, help='Hostname of server')
return parser.parse_args()
def find_clock_item(hostname):
hosts = invoke('host.get', filter={'host': hostname}, selectItems=['itemid', 'key_', 'value_type'])
if not hosts:
print('No host with hostname {hostname}'.format(hostname=hostname), file=sys.stderr)
sys.exit(1)
host = hosts[0]
for item in host['items']:
if item['key_'] == 'system.localtime[utc]':
return item
print('Could not find clock item in {hostname}'.format(hostname=hostname), file=sys.stderr)
sys.exit(1)
def get_clock_offsets(hostname):
clock_item = find_clock_item(hostname)
ret_ts = []
ret_offset = []
current_offsets = []
current_hour = None
for value in invoke('history.get', history=int(clock_item['value_type']), itemids=clock_item['itemid'], sortfield='clock', sortorder='ASC'):
ts_unix = int(value['clock']) + int(value['ns']) / 1e12
offset = float(value['value']) - ts_unix
ts_hour = int(ts_unix / 3600)
if current_hour is None:
current_hour = ts_hour
elif ts_hour != current_hour:
ts = datetime.datetime.utcfromtimestamp(ts_hour * 3600)
ret_ts.append(ts)
ret_offset.append(statistics.mean(current_offsets))
current_hour = ts_hour
current_offsets = []
current_offsets.append(offset)
ts = datetime.datetime.utcfromtimestamp(ts_hour * 3600)
ret_ts.append(ts)
ret_offset.append(statistics.mean(current_offsets))
return ret_ts, ret_offset
def main():
args = parse_args()
timestamps, offsets = get_clock_offsets(args.hostname)
matplotlib.pyplot.plot(timestamps, offsets)
matplotlib.pyplot.legend()
matplotlib.pyplot.show()
if __name__ == '__main__':
main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment