I wrote a custom plugin script in order to retrieve with API , Moving average values as a line chart each at "start_date" key from json output
and "Alert" when the value is different of 0 at "start_date" as vertical line .
My question is how to modify the graph template in order for nagiosxi to parse my plugin output .
here is the output of the API i called in the plugin script :
Code: Select all
[
{
"date": "25/02/2025",
"time_blocks": [
{
"alert": 4,
"logs": [
{
"positive": false,
"reason": "",
"value": "hi"
},
{
"positive": false,
"reason": "",
"value": "hi"
},
{
"positive": false,
"reason": "",
"value": "hi"
},
{
"positive": false,
"reason": "",
"value": "hi"
},
{
"positive": false,
"reason": "",
"value": "hi"
}
],
"moyenne_mobile": 0,
"retries": 4,
"start_time": "05:49:57"
},
{
"alert": 0,
"logs": [
{
"positive": false,
"reason": "",
"value": "hi"
}
],
"moyenne_mobile": 0,
"retries": 1,
"start_time": "06:39:57"
},
{
"alert": 0,
"logs": [
{
"positive": false,
"reason": "",
"value": "hi"
},
{
"positive": false,
"reason": "",
"value": "hi"
},
{
"positive": false,
"reason": "",
"value": "hi"
}
],
"moyenne_mobile": 0,
"retries": 3,
"start_time": "07:49:57"
},
{
"alert": 0,
"logs": [
{
"positive": false,
"reason": "",
"value": "hi"
}
],
"moyenne_mobile": 0,
"retries": 1,
"start_time": "09:49:57"
},
{
"alert": 0,
"logs": [
{
"positive": false,
"reason": "apple",
"value": "est ce que Apple a sorti des nouvelels distributions"
}
],
"moyenne_mobile": 4.0,
"retries": 1,
"start_time": "10:44:57"
}
],
"validator": "Competitor Check"
},
{
"alert": 4,
"logs": [
{
"positive": false,
"reason": "samsung",
"value": "did samsung partenred with Nvidia"
},
{
"positive": false,
"reason": "samsung",
"value": "did samsung release samsung pro max"
},
{
"positive": false,
"reason": "samsung",
"value": "did samsung release samsung pro max"
}
],
"moyenne_mobile": 0,
"retries": 4,
"start_time": "16:44:57"
}
],
"validator": "Competitor Check"
}
]
Code: Select all
#!/usr/bin/env python3
import requests
import sys
import json
# Nagios status codes
OK = 0
WARNING = 1
CRITICAL = 2
UNKNOWN = 3
API_URL = "http://XXX.XXX.XX.XXX:YYYY/api/comp_check"
def main():
try:
response = requests.get(API_URL, timeout=30)
response.raise_for_status()
data = response.json()
except requests.RequestException as e:
print(f"CRITICAL - API call failed: {e}")
sys.exit(CRITICAL)
except ValueError:
print("CRITICAL - API response is not valid JSON")
sys.exit(CRITICAL)
perf_data_points = []
# Process data
for entry in data:
date = entry.get("date", "")
for tb in entry.get("time_blocks", []):
start_time = tb.get("start_time", "")
ma_value = tb.get("moyenne_mobile", 0)
alert_value = tb.get("alert", 0)
retries = tb.get("retries", 0)
timestamp = f"{date} {start_time}"
perf_data_points.append({
"timestamp": timestamp,
"ma_value": ma_value,
"alert_value": alert_value,
"retries": retries
})
# Determine Nagios status
nagios_status = OK
if any(tb["alert_value"] > 0 for tb in perf_data_points):
nagios_status = WARNING
if nagios_status == OK:
status_text = f"OK - Moving Average: {ma_value}"
else:
status_text = f"WARNING - Alerts detected, Moving Average: {ma_value}"
# Format performance data for graphing
perf_data = " ".join([
f"'{point['timestamp']}'={point['ma_value']};1;2;0;5 'Alert'={point['alert_value']};0;0;0;10"
for point in perf_data_points
])
print(f"Competitor Check - {status_text} | {perf_data}")
sys.exit(nagios_status)
if __name__ == "__main__":
main()
https://drive.google.com/file/d/14Rp9U2 ... sp=sharing
this is what iam expecting to visualize :
orange vertical lines are Alerts when Alert!=0
https://drive.google.com/file/d/1zzS5nv ... sp=sharing
I appreciate ur feedback
