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 os
import subprocess import subprocess
import sys import sys
import time
from collections import defaultdict from collections import defaultdict
@ -112,14 +113,26 @@ def get_stats():
"""Get statistics.""" """Get statistics."""
# Get status output # Get status output
try: try:
pipe = subprocess.Popen( output = subprocess.run(['knotc', '--force', 'stats'],
['/usr/sbin/knotc', '--force', 'stats'], stdout=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False,
stderr=subprocess.STDOUT) encoding='utf-8', errors='ignore').stdout
output = pipe.communicate()[0].decode('utf-8', 'ignore') except FileNotFoundError:
except OSError:
return {} 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. # Parse output. Keep graph labels in knotc-order.
values = defaultdict(dict) values = defaultdict(dict)
for line in output.splitlines(): for line in output.splitlines():