Page 1 of 1

Using Negate with check_ncpa.py error after 5.7 Upgrade

Posted: Tue Jan 05, 2021 11:10 am
by shoreypu
I upgraded Nagios XI to version 5.7.5 this morning and I'm running a negate check that is now reporting the following error:
File "/usr/local/nagios/libexec/check_ncpa.py", line 353, in <module>
print(stdout.encode('utf-8', 'replace').decode('utf-8'))
UnicodeEncodeError: 'ascii' codec can't encode character u'\\u2019' in position 220: ordinal not in range(128)

Here is the command:
$USER1$/negate -u OK -o WARNING -s $USER1$/check_ncpa.py -H SELENIUM_HOST -t "$_HOSTNCPA_TOKEN$" -P $_HOSTNCPA_PORT$ -M 'plugins/p3ui' -a "fed 'config/iam-cas.json' --starturl 'https://$HOSTNAME$' --logname $USER13$ --password '$USER12$'"

To summarize, Nagios XI is calling a Selenium check (via NCPA) that is running from our Selenium server and reporting the results back to Nagios XI. In this case, the check is attempting to run against a failover system, so by default, the result would be unknown, so we change that result using negate so that the Unknown reports as OK.

The upgrade was installed via command line as root using the manual download instructions in https://assets.nagios.com/downloads/nag ... ctions.pdf

Also, from /var/log/yum.log
Jan 05 06:24:06 Installed: perl-TermReadKey-2.30-20.el7.x86_64
Jan 05 06:24:06 Installed: perl-Git-1.8.3.1-23.el7_8.noarch
Jan 05 06:24:07 Installed: git-1.8.3.1-23.el7_8.x86_64
Jan 05 06:24:07 Updated: nagiosxi-deps-el7-5.7.5-1.noarch

Any assistance is greatly appreciated.

Re: Using Negate with check_ncpa.py error after 5.7 Upgrade

Posted: Wed Jan 06, 2021 12:25 pm
by vtrac
Hi shoreypu,
Sorry, but I need to ask you a few questions so that I can understand the situation better ... :-)
Did you upgrade your OS and python?
What is your OS and Python version now?
Did you move to a new server with a new name?
Were this check_ncpa.py command works before the upgrade?

Line 353 in check_ncpa.py is:

Code: Select all

print(stdout.encode('utf-8', 'replace').decode('utf-8'))
Based on the error provide, it can't encode character u'\\u2019' in position 220.
The "\u2019" is the "RIGHT SINGLE QUOTATION MARK" in the "stdout" returned from the "main()" function.

Here is the "main()" function called by line 353 (below):

Code: Select all

    312 def main():
    313     options = parse_args()
    314
    315     # We need to ensure that we will only execute for a certain amount of
    316     # seconds.
    317     signal.signal(signal.SIGALRM, timeout_handler(options.timeout))
    318     signal.alarm(options.timeout)
    319
    320     try:
    321         if options.version:
    322             stdout = 'The version of this plugin is %s' % __VERSION__
    323             return stdout, 0
    324
    325         info_json = get_json(options)
    326
    327         if options.list:
    328             return show_list(info_json)
    329         else:
    330             stdout, returncode = run_check(info_json)
    331
    332             if options.performance and stdout.find("|") == -1:
    333                 stdout = "{0} | 'status'={1};1;2;;".format(stdout, returncode)
    334             return stdout, returncode
    335     except (HTTPError, URLError) as e:
    336         if options.debug:
    337             return 'The stack trace:\n' + traceback.format_exc(), 3
    338         elif options.verbose:
    339             return 'An error occurred:\n' + str(e.error_message), 3
    340         else:
    341             return e.error_message, 3
    342     except Exception as e:
    343         if options.debug:
    344             return 'The stack trace:\n' + traceback.format_exc(), 3
    345         elif options.verbose:
    346             return 'An error occurred:\n' + str(e), 3
    347         else:
    348             return 'UNKNOWN: Error occurred while running the plugin. Use the verbose flag for more details.', 3
    349
    350
    351 if __name__ == "__main__":
    352     stdout, returncode = main()
    353     print(stdout.encode('utf-8', 'replace').decode('utf-8'))
    354     sys.exit(returncode)
Can you please add the below line above line 353 in the check_ncpa.py script?
I would like to know what is returning from the "main()" function and the position 220.

Code: Select all

print(stdout)
Once you have added the print statement above, please manually run the below command on your Nagios XI command line (xterm) and update the results:

Code: Select all

cd /usr/local/nagios/libexec
./check_ncpa.py -H SELENIUM_HOST -t "$_HOSTNCPA_TOKEN$" -P $_HOSTNCPA_PORT$ -M 'plugins/p3ui' -a "fed 'config/iam-cas.json' --starturl 'https://$HOSTNAME$' --logname $USER13$ --password '$USER12$'
Regards,
Vinh

Re: Using Negate with check_ncpa.py error after 5.7 Upgrade

Posted: Thu Jan 07, 2021 8:04 am
by shoreypu
Vinh,

Did you upgrade your OS and python?
- No, we only upgraded Nagios XI
What is your OS and Python version now?
- Python Version is 2.7.5. OS is Oracle Linux Server release 7.9
Did you move to a new server with a new name?
- No
Were this check_ncpa.py command works before the upgrade?
- Yes

Here is the output after modifying (a copy of) check_ncpa.py:
Error output from command:
Traceback (most recent call last):
File "/usr/local/nagios/libexec/check_ncpa_test.py", line 353, in <module>
print(stdout)
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 220: ordinal not in range(128)

Re: Using Negate with check_ncpa.py error after 5.7 Upgrade

Posted: Thu Jan 07, 2021 4:31 pm
by vtrac
Hi shoreypu,
Could you please try commented out the original line (353), then add the below line:

Code: Select all

print(stdout.encode('ascii', 'replace').decode('utf-8'))
Here's an example:

Code: Select all

if __name__ == "__main__":
    stdout, returncode = main()
    print(stdout.encode('ascii', 'replace').decode('utf-8'))
    #print(stdout.encode('utf-8', 'replace').decode('utf-8'))
    sys.exit(returncode)
Regards,
Vinh

Re: Using Negate with check_ncpa.py error after 5.7 Upgrade

Posted: Thu Jan 07, 2021 5:03 pm
by vtrac
Hi shoreypu,
Just got a message from one of my teammate that said our developers will update the fix as follows:

Code: Select all

    if sys.version_info[0] < 3:
        print(unicode(stdout).encode('utf-8'))
    else:
        print(stdout.encode().decode('utf-8'))
So, could you please update your "check_ncpa.py" as below:

Code: Select all

if __name__ == "__main__":
    stdout, returncode = main()
    if sys.version_info[0] < 3:
        print(unicode(stdout).encode('utf-8'))
    else:
        print(stdout.encode().decode('utf-8'))
    sys.exit(returncode)
Best Regards,
Vinh

Re: Using Negate with check_ncpa.py error after 5.7 Upgrade

Posted: Wed Jan 13, 2021 8:13 am
by shoreypu
Updating the check_ncpa.py with the code you provided resolved our issue.

This topic can be closed. Thanks.

Re: Using Negate with check_ncpa.py error after 5.7 Upgrade

Posted: Wed Jan 13, 2021 9:21 am
by scottwilkerson
shoreypu wrote:Updating the check_ncpa.py with the code you provided resolved our issue.

This topic can be closed. Thanks.
Locking thread