Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

graph templates does not match my plugin script #1023

Open
sirine707 opened this issue Feb 28, 2025 · 0 comments
Open

graph templates does not match my plugin script #1023

sirine707 opened this issue Feb 28, 2025 · 0 comments

Comments

@sirine707
Copy link

Hello ,

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 :

[
    {
        "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"
    }
]

here is my plugin script logic that i want to visualize :

#!/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()

this is the grap output :

Image

this is what iam expecting to visualize :
orange vertical lines are Alerts when Alert!=0

Image

I Highly appreciate your insights and feedback

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant