1
0
Fork 0
mirror of https://github.com/munin-monitoring/contrib.git synced 2025-07-22 02:51:03 +00:00

knot: cache results, needed for first run after server reboot

Sometimes after reboot munin-async + munin-node runs before knot is
ready. This will result missing knot stats, as there is no static
config in plugin. Cache results and use them instead if knot's output
is empty.
This commit is contained in:
Kim B. Heino 2021-04-01 15:28:16 +03:00 committed by Lars Kruse
parent 9f1d967cbc
commit 9759634977

View file

@ -38,6 +38,7 @@ GPLv2
import os
import subprocess
import sys
import time
from collections import defaultdict
@ -112,14 +113,26 @@ def get_stats():
"""Get statistics."""
# Get status output
try:
pipe = subprocess.Popen(
['/usr/sbin/knotc', '--force', 'stats'],
output = subprocess.run(['knotc', '--force', 'stats'],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
output = pipe.communicate()[0].decode('utf-8', 'ignore')
except OSError:
stderr=subprocess.PIPE, check=False,
encoding='utf-8', errors='ignore').stdout
except FileNotFoundError:
return {}
# After server reboot output can be almost empty. Use cached results
# instead, needed for plugin config when using munin-async.
cachename = os.getenv('MUNIN_PLUGSTATE') + '/knot.state'
if len(output) > 2048:
with open(cachename, 'wt') as cache:
cache.write(output)
elif (
os.path.exists(cachename) and
os.stat(cachename).st_mtime > time.time() - 900
):
with open(cachename, 'rt') as cache:
output = cache.read()
# Parse output. Keep graph labels in knotc-order.
values = defaultdict(dict)
for line in output.splitlines():