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:
parent
9f1d967cbc
commit
9759634977
1 changed files with 19 additions and 6 deletions
|
@ -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():
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue