How to implement python script to monitor it as a service

Hi, I’m new to icinga and I would like to be able to monitor a small script that I made in python through icingaweb2, indicating its status. One of my biggest questions is how to include or integrate to call this script so that it can be viewed in icingaweb.

1 Like

To use your script aa plugin, there are some basics that you need to ensure are in place.

Exit codes:

0 - OK
1 - WARNING
2 - CRITICAL
3 - UNKNOWN

Python example:
exit(2) when critical

Otherwise, you need some output from the script:

# if everything is ok:
print("OK - Status of this thing is good.")
# if something is degraded -- not often used for state checking
print("WARNING - Something is wrong.")
# if everything has gone to crap
print("CRITICAL - Things are bad, polish your resume.")
# unhandled exceptions/errors
print("UNKNOWN - What happened?")

“Real” example:

try:
    checkFunction = doSomething() # Returns ok, warning, critical for some sort of check
    if checkFunction == "ok":
        print("OK - Status of this thing is good.")
        exit(0)
    elif checkFunction == "warning":
        print("WARNING - Something is wrong.")
        exit(1)
    elif checkFunction == "critical":
        print("CRITICAL - Things are bad, polish your resume.")
        exit(2)
   else:
        print(f"UNKNOWN - value of  checkFunction is {checkFunction}")
        exit(3)
except Exception as e:
    print(f"UNKNOWN - Unhandled exception - {e}")
    exit(3)

After the basics, you need to define a check command, and use the command in a service.
This document should have all of the information needed, including command definitions, that exit code bit I included above, etc…

1 Like

do I need to do something with the python script (file.py) or can I already work with that?

I’ve described it in the thread you’ve been jumped in bevor.

BTW: This is nothing for “Tutorials and Guides”

You either need to make the changes to that script (so that it’s Icinga/Nagios Core friendly using the output and exit codes), or import the functions from that script into another script that will provide exit codes and output as Icinga expects.

After that’s done, you’ll just want to read the docs I posted above to configure your command definition for Icinga, and apply it to a service.

1 Like