How to call Python script within Nagios check

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.
jankogaga
Posts: 37
Joined: Thu Apr 19, 2018 8:16 am

How to call Python script within Nagios check

Post by jankogaga »

Hi,

I would like to call Python script within Nagios check.
Python script has the arguments. E.g.:
/usr/local/nagios/libexec/check_ssl.py -H google.com -c E+,E,E-,F+,F,F-,T,M,C+,C-,C,D+,D,D- -d 15
and it works when I run in command line for nagios user.

What I have tried and it didn't work for me:

in commands.cfg:

Code: Select all

define command{
        command_name check_ssl
        command_line /usr/local/nagios/libexec/check_ssl.py -H $ARG1$ -c $ARG2$ -d $ARG3$
}
monitor.cfg:

Code: Select all

define service{
        use                             local-service         ; Name of service template to use
        host_name                          xxx
        service_description             SSLLABS Web service
        check_command                 check_ssl!google.com!E+,E,E-,F+,F,F-,T,M,C+,C-,C,D+,D,D-!15
        notifications_enabled           1
        }
Thanks,
Dragan
User avatar
mbellerue
Posts: 1403
Joined: Fri Jul 12, 2019 11:10 am

Re: How to call Python script within Nagios check

Post by mbellerue »

The only thing I'm seeing right away is the service definition is missing some information.

https://assets.nagios.com/downloads/nag ... ml#service
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.

Be sure to check out our Knowledgebase for helpful articles and solutions!
jankogaga
Posts: 37
Joined: Thu Apr 19, 2018 8:16 am

Re: How to call Python script within Nagios check

Post by jankogaga »

Hi,

Thank you for your reply.
We run Nagios Core 4.4.3.
In the same .cfg file for the host we have already working service definition like this:

Code: Select all

 define service{
        use                                    local-service         ; Name of service template to use
        host_name                         xxx
        service_description            Swap Usage
        check_command                check_local_swap!20!10
        }
Thanks,
Dragan
User avatar
mbellerue
Posts: 1403
Joined: Fri Jul 12, 2019 11:10 am

Re: How to call Python script within Nagios check

Post by mbellerue »

What does the Service page look like in Nagios? Does it show an error, or just give an Unknown status?

What if you altered your command to call Python specifically?

Code: Select all

command_line /usr/bin/env python2 /usr/local/nagios/libexec/check_ssl.py -H $ARG1$ -c $ARG2$ -d $ARG3$
Or /usr/bin/env python3 if it's a Python 3 script, of course.
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.

Be sure to check out our Knowledgebase for helpful articles and solutions!
jankogaga
Posts: 37
Joined: Thu Apr 19, 2018 8:16 am

Re: How to call Python script within Nagios check

Post by jankogaga »

I have got the following message in Nagios WebGUI:

Code: Select all

	(Return code of 127 is out of bounds. Check if plugin exists)
When running a python script from the command line as nagios user, I have got:

Code: Select all

bash-4.2$ /usr/bin/env python3  /usr/local/nagios/libexec/check_ssl.py -H google.com -c E+,E,E-,F+,F,F-,T,M,C+,C-,C,D+,D,D- -d 15
SSLLABS grade for google.com is A, ssl certificate expires in 55 days
User avatar
mbellerue
Posts: 1403
Joined: Fri Jul 12, 2019 11:10 am

Re: How to call Python script within Nagios check

Post by mbellerue »

Can you check /etc/group and make sure that the Apache user is in the Nagios group?

e.g.

Code: Select all

nagios:x:500:nagios,apache
Also what is the output of the following command?

Code: Select all

ls -l /usr/local/nagios/libexec/check_ssl*
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.

Be sure to check out our Knowledgebase for helpful articles and solutions!
jankogaga
Posts: 37
Joined: Thu Apr 19, 2018 8:16 am

Re: How to call Python script within Nagios check

Post by jankogaga »

/etc/group has the following records:

Code: Select all

nagios:x:992:nrpe
nagcmd:x:1000:nagios,apache
Please note that a bunch of services already work with that configuration.

The output of ls -l /usr/local/nagios/libexec/check_ssl* is

Code: Select all

-rwxr-xr-x 1 nagios nagios 8549 Aug  7 13:46 /usr/local/nagios/libexec/check_ssl.py
Thanks,
Dragan
User avatar
mbellerue
Posts: 1403
Joined: Fri Jul 12, 2019 11:10 am

Re: How to call Python script within Nagios check

Post by mbellerue »

The permissions do look good. Let's check the shebang on the script itself.

Code: Select all

head -2 /usr/local/nagios/libexec/check_ssl.py
Also, what command are you using when you switch over to the nagios user to run the check commands?
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.

Be sure to check out our Knowledgebase for helpful articles and solutions!
jankogaga
Posts: 37
Joined: Thu Apr 19, 2018 8:16 am

Re: How to call Python script within Nagios check

Post by jankogaga »

Here you are:

head -2 /usr/local/nagios/libexec/check_ssl.py

Code: Select all

#! /usr/bin/env python3
# -*- coding: utf-8 -*-
for switching to nagios user:

Code: Select all

su nagios
User avatar
mbellerue
Posts: 1403
Joined: Fri Jul 12, 2019 11:10 am

Re: How to call Python script within Nagios check

Post by mbellerue »

Try it with a dash.

Code: Select all

su - nagios
That should drop your user's environment, and give you just the nagios user's environment. Then try to run the check command.
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.

Be sure to check out our Knowledgebase for helpful articles and solutions!
Locked