Skip to main content

Hi, I have written a skeleton for python script for centreon. The problem is that the script works fine until I import the pyrfc library, which is the key for this script.
Without this library centreon returns me a green OK message which is fine.
Once the library is there, it returns No output returned from plugin. If I run the script through the OS under the user centreon-engine it works fine even with the library. I have the latest version of pyrfc and it works fine because I use it in other scripts, it just doesn't work in the centreon UI. My command is : /usr/bin/env python3 /usr/lib/nagios/plugins/SCRIPTS/test.py (working fine from OS)

With library, without library it’s green and returning desired output.

What I've tried so far:
set chmod 777 on the script
change the owner to centreon-engine
run the script on the OS under the user centreon-engine - works
reinstall pyrfc library
checked Python versions
 

My Code:

#!python

__author__ = 'xxxx'
__version__ = 0.1

import os
import time
import pyrfc  # If I comment remove this line, it works.
from optparse import OptionParser, OptionGroup
import logging as log
import argparse
import subprocess


os.environ."NWRFCSDK_INCLUDE"] = "/usr/local/sap/nwrfcsdk/include"
os.environ."NWRFCSDK_LIBS"] = "/usr/local/sap/nwrfcsdk/lib"
os.environ."SAPNWRFC_HOME"] = "/usr/local/sap/nwrfcsdk"

# Initialize parser
parser = argparse.ArgumentParser()
parser.add_argument("-d", "--destination", help="Define RFC destination.")

# Read arguments from command line
args = parser.parse_args()


def main():
    """ Main plugin logic """

    with open('/usr/lib/nagios/plugins/SCRIPTS/output_rfc.txt') as f:
        lines = f.readlines()
        integer = int("".join(str(x) for x in lines))
    if integer == 0:
        gtfo(0, "OK - destination {} reached.".format(args.destination))
    elif integer == 2:
        gtfo(2, "CRITICAL - destination {} does not exist.".format(args.destination))
    elif integer == 3:
        gtfo(1, "WARNING - {} is illegal destination type 'G'.".format(args.destination))
    else:
        gtfo(2, "CRITICAL - destination {} unreachable.".format(args.destination))


def gtfo(exitcode, message=''):
    """ Exit gracefully with exitcode and (optional) message """

    if message:
        print(message)
    exit(exitcode)


if __name__ == '__main__':
    main()

 

Thanks in advance for the help.

Ciao!

I’m not able to install pyrfc, so I can not fully reproduce your test, but here some suggestion:

  • What are the paths of macro $CENTREONPLUGINS$ and $USER1$?
    I suggest to put custom script under a subfolder of $USER1$, Eg. in my case will be /usr/lib64/nagios/plugins/custom/
  • Better write all the starting instruction on the shebang row. Can you try and test if still work on cmd line with the following starting row?
    !/usr/bin/env python3
  • Sometime the scripts works only in a shell environment, can you also check the flag ‘Enable shell’ in the command?

Hello,

thank you for your reply. The path $CENTREONPLUGINS$ is /usr/lib/centreon/plugins/ - I tried to put it there too but it didn't help.

If I add #!/usr/bin/env python3 to the beginning of the file that doesn't help either. I also tried “Enable shell”, but it didn't help either :/


What happen when you execute the script manually under centreon-engine user ?

You have an Traceback  Error telling you the problem.

 

Il also consider that changing shbang is the righ way

 


@sdouce 

When I run it under the centreon-engine user it works, as I mentioned above.

Changing shbang didn't help and has nothing to do with the problem because the script works without importing that library, if shbang was the problem, the script wouldn't work without the library either.


Try to play with variable  LD_LIBRARY_PATH

Maybe to add in /etc/profile

export LD_LIBRARY_PATH= /usr/local/sap/nwrfcsdk/lib

 

 


Try to play with variable  LD_LIBRARY_PATH

Maybe to add in /etc/profile

export LD_LIBRARY_PATH= /usr/local/sap/nwrfcsdk/lib

 

 


No luck with Python 😪 Anyway it’s working with shell.
Thank you a lot 🙂. I created a wrapper shell script with export at the beginning and it’s working like a charm !

#!/bin/bash

export LD_LIBRARY_PATH=/usr/sap/nwrfcsdk/lib/

check_destination() {

  output=$(python /usr/lib/centreon/plugins/check_rfc_destination.py -a $1 -s $2 -c $3 -u $4 -p $5 -d $6) # Wrapper needed because of PyRfc library.

  if r $output -eq 0 ]; then

    echo "OK - destination $6 reached."

    exit 0

  elif 0 $output -eq 2 ]; then

    echo "CRITICAL - destination $6 not reached."

    exit 2

  elif e $output -eq 3 ]; then

    echo "WARNING - destination $6 does not exist."

    exit 1

  elif $output -eq 4 ]; then

    echo "WARNING - illegal destination type (only destination type 3 supported)."

    exit 1

  fi

}


Reply