pythonlinuxmonitoringnagiospyrfc

No output returned from plugin in centreon when using Python PyRfc library


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 the 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: python3 /usr/lib/nagios/plugins/SCRIPTS/test.py (working fine from OS). As far as the code is concerned, it is syntactically fine. Seems like some error in centreon or something.

enter image description here

What I've tried so far:

My Code:

#!python

__author__ = 'xxxx'
__version__ = 0.1

import os
import time
import pyrfc  # If I 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.


Solution

  • For anyone having a similar problem, the answer is the LD_LIBRARY_PATH variable. I created a wrapper in bash where at the beginning I put the export of this variable and call the Python script, then it works fine.

    #!/bin/bash
    
    ### ======================================================================= ###
    ###                               FUNCTIONS                                 ###
    ### ======================================================================= ###
    
    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 [ $output -eq 0 ]; then
        echo "OK - destination $6 reached."
        exit 0
      elif [ $output -eq 2 ]; then
        echo "CRITICAL - destination $6 not reached."
        exit 2
      elif [ $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
    }
    
    ### ======================================================================= ###
    ###                     SCRIPT EXECUTION START HERE                         ###
    ### ======================================================================= ###
    
    if [ $# -lt 6 ]
    then
        echo "Missing parameters! Syntax: $0 host sysnr client username password destination"
        exit 3
    fi
    
    
    check_destination "$1" "$2" "$3" "$4" "$5" "$6"
    
    ### ======================================================================= ###
    ###                         END OF SCRIPT                                   ###
    ### ======================================================================= ###