Page 1 of 2

Plugin return report NULL

Posted: Mon Dec 15, 2014 12:54 pm
by filipe.avelino1
Good afternoon, I developed a plugin in Python to read a value of a presence sensor connected through the serial port with arduino. I used several techniques, the latest version used the pynagios library. I realized that by removing the code that connects the serial port the plugin works normally, but to keep the code snippet in webinterface and returns status WARNING and status NULL. Someone can help me ? Please

Code: Select all

#!/usr/bin/python
import pynagios
import serial
from pynagios import Plugin, Response
valor = 1
class MyCheck(Plugin):
    def check(self):
      #ser = serial.Serial('/dev/ttyACM0',9600,timeout=0)
      #if str(ser.read()) == '1':
      if valor == 1:
	return Response(pynagios.CRITICAL, "Intrusos Detectados")
      else:
	return Response(pynagios.OK, "Sem Ameacas")

if __name__ == "__main__":
    # Instantiate the plugin, check it, and then exit
    MyCheck().check().exit()

Re: Plugin return report NULL

Posted: Mon Dec 15, 2014 1:08 pm
by tmcdonald
I believe that has to do with your indentation:

http://stackoverflow.com/questions/1226 ... -in-python

Specifically, I think you need to move your "return" statements over in the if and else sections. I am not a python expert, but from what I have read this seems to be the case.

Also, you might like the presentation I did at this year's Nagios World Conference. I did something very similar to what you are doing with Arduinos:

https://www.youtube.com/watch?v=Bt9yUpLrC5w

Re: Plugin return report NULL

Posted: Mon Dec 15, 2014 1:16 pm
by filipe.avelino1
I think not, because I run manually and works normally. In this example that I said the passage that communicates with the serial port and I configured the manual value

Re: Plugin return report NULL

Posted: Mon Dec 15, 2014 1:24 pm
by sreinhardt
Can you give us some examples of this working and not working from the cli, along with the associated exit codes after each run?

Re: Plugin return report NULL

Posted: Mon Dec 15, 2014 2:39 pm
by filipe.avelino1
Sure,
Not working

Code: Select all

#!/usr/bin/python
import pynagios
import serial
from pynagios import Plugin, Response

class MyCheck(Plugin):
    def check(self):
      ser = serial.Serial('/dev/ttyACM0',9600,timeout=0)
      if str(ser.read()) == '1':
     	return Response(pynagios.CRITICAL, "Intrusos Detectados")
      else:
	return Response(pynagios.OK, "Sem Ameacas")

if __name__ == "__main__":
    # Instantiate the plugin, check it, and then exit
    MyCheck().check().exit()

Code: Select all

[root@tux-mateus plugins]# python tcc2_final.py 
CRIT: Intrusos Detectados
Image attachment

Re: Plugin return report NULL

Posted: Mon Dec 15, 2014 2:50 pm
by filipe.avelino1
Working, but i set this values manually
See, I commented the code that instantiates the serial port

cli:

Code: Select all

[root@tux-mateus plugins]# python tcc2.py 
OK: Sem Ameacas

Code: Select all

#!/usr/bin/python
import pynagios
import serial
from pynagios import Plugin, Response
valor = 0
class MyCheck(Plugin):
    def check(self):
      #ser = serial.Serial('/dev/ttyACM0',9600,timeout=0)
      #if str(ser.read()) == '1':
      if valor == 1:
	return Response(pynagios.CRITICAL, "Intrusos Detectados")
      else:
	return Response(pynagios.OK, "Sem Ameacas")

if __name__ == "__main__":
    # Instantiate the plugin, check it, and then exit
    MyCheck().check().exit()

Re: Plugin return report NULL

Posted: Mon Dec 15, 2014 3:02 pm
by tmcdonald
What are the permissions on the plugin? It seems like it might not have permissions as the nagios user to open the serial pipe.

Can you try to "su nagios" and run it as the nagios user? Then run "echo $?" to see the exit code. I'll be it is 1.

Re: Plugin return report NULL

Posted: Mon Dec 15, 2014 3:07 pm
by filipe.avelino1
My permissions
I could not use the su command nagios returns the account is not available at the time. I execute echo $? , return 1

Re: Plugin return report NULL

Posted: Mon Dec 15, 2014 5:10 pm
by tmcdonald
You might not have the nagios user, but that should show other problems as well. Try running the following:

Code: Select all

grep "nagios" /etc/passwd
chage -l nagios

Re: Plugin return report NULL

Posted: Mon Dec 15, 2014 5:22 pm
by filipe.avelino1
Return:

Atachament