Skip to content
Snippets Groups Projects
Verified Commit 26da3f35 authored by Jan Pavlíček's avatar Jan Pavlíček Committed by Jan Pavlíček
Browse files

feat: run_probes outputs in check_mk format with metrics to graphs

parent 9d4fa777
Branches
Tags
1 merge request!53feat: run_probes outputs in check_mk format with metrics to graphs
Pipeline #388384 passed
...@@ -19,16 +19,48 @@ def open_file(filepath): ...@@ -19,16 +19,48 @@ def open_file(filepath):
sys.exit(2) sys.exit(2)
def get_metrics_and_new_output(output):
"""
Parses metrics from output, metrics must be in one of (or combination of)
the following formats:
1) |metric1=val;val;;;|metric2=val (delimiter |)
2) |metric1=val;metric2=val2; (delimiter ;)
3) |metric1=val metric2=val2 (delimiter ' ')
Values must be int or float
"""
metrics_pattern = r"(\s\|\s|\|)(\w+=[\d.;]+(;|\s|$))+"
match = re.search(metrics_pattern, output)
if match:
output = re.sub(metrics_pattern, " ", output)
metrics = re.sub(r"\s", "", match.group())
metrics = re.sub(r"^\|", "", metrics)
metrics = re.sub(r"(\d;?)([a-zA-Z])", r"\1|\2", metrics)
return metrics.strip(), output.strip()
return None, output
def run_probe(probe_name, command): def run_probe(probe_name, command):
"""
Runs nagios monitoring probe and prints output in following format:
return_code metrics output
metrics output format:
metric1=val;|metric2=val2|metric3=val3;val3;;;|metric4=val4
"""
result = subprocess.run( result = subprocess.run(
command, text=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT command, text=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT
) )
output = re.sub("[ \t\n]+", " ", result.stdout) output = re.sub("[ \t\n]+", " ", result.stdout)
search = re.search(r" - .*", output) search = re.search(r" - .*", output)
if search: if search:
print(f"{result.returncode} {probe_name}{search.group()}") output = re.sub(r"^ - ", "", search.group())
metrics, new_output = get_metrics_and_new_output(output)
if metrics:
print(f"{result.returncode} {probe_name} {metrics} {new_output}")
else: else:
print(f"{result.returncode} {probe_name} - {output}") print(f"{result.returncode} {probe_name} {output}")
return result.returncode return result.returncode
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment