How monitoring python program output values in nagios?

Support forum for Nagios Core, Nagios Plugins, NCPA, NRPE, NSCA, NDOUtils and more. Engage with the community of users including those using the open source solutions.
Locked
dnte69
Posts: 3
Joined: Wed Nov 11, 2015 12:12 pm

How monitoring python program output values in nagios?

Post by dnte69 »

Dear colleagues and Regards from Brazil

I created a device that monitors temperature and humidity using a Raspberry and DHT22 sensor output values (humidity and temperature) are shown every 5 seconds in a python program, that is the code (not mine):

Code: Select all

# Autor : FILIPEFLOP

# load libs
import Adafruit_DHT
import RPi.GPIO as GPIO
import time

# type of sensor
#sensor = Adafruit_DHT.DHT11
sensor = Adafruit_DHT.DHT22

GPIO.setmode(GPIO.BOARD)


pino_sensor = 25

# Informacoes iniciais
print ("*** read U and T");

while(1):

umid, temp = Adafruit_DHT.read_retry(sensor, pino_sensor);
# Caso leitura esteja ok, mostra os valores na tela
if umid is not None and temp is not None:
     print ("Temp = {0:0.1f} U = {1:0.1f}\n").format(temp, umid);
     print ("hold 5 secs...\n");
     time.sleep(5)
else:
# error mgs
     print("fail in DHT22 !!!")
Would you like to learn how to configure nrpe.cfg (in my head that's file but correct me) so it know if the temperature and humidity are outside of a preset value established and the nagios issue a warning.

tks
Last edited by jdalrymple on Wed Nov 11, 2015 2:14 pm, edited 2 times in total.
Reason: Please use [code][/code] tags around code - added indentations for python readability/executability
tmcdonald
Posts: 9117
Joined: Mon Sep 23, 2013 8:40 am

Re: How monitoring python program output values in nagios?

Post by tmcdonald »

You can either have NRPE run the program and capture the output directly, or you can have the program log the output to a file and have NRPE check that file. Either way you will need to make some slight modifications to the script. We can help with the NRPE side of things, but the script modification is up to you.

Also, I am moving this out of "Nagios Ideas" since it is not a proposed enhancement.
Former Nagios employee
dnte69
Posts: 3
Joined: Wed Nov 11, 2015 12:12 pm

Re: How monitoring python program output values in nagios?

Post by dnte69 »

tmcdonald wrote:You can either have NRPE run the program and capture the output directly, or you can have the program log the output to a file and have NRPE check that file. Either way you will need to make some slight modifications to the script. We can help with the NRPE side of things, but the script modification is up to you.

Also, I am moving this out of "Nagios Ideas" since it is not a proposed enhancement.
Thanks, if you help me with nrpe i have a friend that could help me with the code.
jdalrymple
Skynet Drone
Posts: 2620
Joined: Wed Feb 11, 2015 1:56 pm

Re: How monitoring python program output values in nagios?

Post by jdalrymple »

Since this script only exits on error it won't work being called by NRPE.

Your options are as follows.

1) Rewrite the script so that it sends passive results after every sleep state.

2) Rewrite the script to exit upon every invocation using sys.exit(0) for OK, sys.exit(1) for WARNING, sys.exit(2) for CRITICAL or sys.exit(4) for UNKNOWN. Print appropriate status output as desired using print().

3) Rewrite the script to output to a file each time the while loop is run. You can either grok that file or use something like pickle to store an object that another simple python script can read in. Either way, this method sucks as it requires a whole new script for NRPE.

I advise you to use option 2. It is how Nagios is intended to work. The only shortcoming is that your status will be updated every 60 seconds at most, not every 5. If you need that level of precision go with option 1.

Get rid of your semicolons terminating statements in your python - they're unnecessary and make python difficult to read since regular colons are significant.
dnte69
Posts: 3
Joined: Wed Nov 11, 2015 12:12 pm

Re: How monitoring python program output values in nagios?

Post by dnte69 »

jdalrymple wrote:Since this script only exits on error it won't work being called by NRPE.

Your options are as follows.

1) Rewrite the script so that it sends passive results after every sleep state.

2) Rewrite the script to exit upon every invocation using sys.exit(0) for OK, sys.exit(1) for WARNING, sys.exit(2) for CRITICAL or sys.exit(4) for UNKNOWN. Print appropriate status output as desired using print().

3) Rewrite the script to output to a file each time the while loop is run. You can either grok that file or use something like pickle to store an object that another simple python script can read in. Either way, this method sucks as it requires a whole new script for NRPE.

I advise you to use option 2. It is how Nagios is intended to work. The only shortcoming is that your status will be updated every 60 seconds at most, not every 5. If you need that level of precision go with option 1.

Get rid of your semicolons terminating statements in your python - they're unnecessary and make python difficult to read since regular colons are significant.
tks man, but I know nothing about python in fact i hate programming (i am linux administrator), zero chance to rewrite anything, but thanks anyway.
jdalrymple
Skynet Drone
Posts: 2620
Joined: Wed Feb 11, 2015 1:56 pm

Re: How monitoring python program output values in nagios?

Post by jdalrymple »

jdalrymple wrote:Since this script only exits on error it won't work being called by NRPE.
This is factual - the way your script is written is incompatible with active nagios checks. It's imperative that a plugin exits for nagios to function.
https://nagios-plugins.org/doc/guidelines.html#AEN78
Incidentally, writing it to exit would be trivial for your python programmer.
dnte69 wrote:i have a friend that could help me with the code
Otherwise, we can't offer any solution. Exit codes are absolutely vital to how nagios functions. Without an exit - there can be no exit code.
Locked