NagiosEnterprises / ncpa

Nagios Cross-Platform Agent
Other
177 stars 95 forks source link

Update check_ncpa.py to Decode latin-1 #970

Open maxwbot opened 1 year ago

maxwbot commented 1 year ago

Some error with ptbr entry. Solved by adding correct Decoding Latin-1.

sawolf commented 1 year ago

Hi @maxwbot, thanks for the PR!

Can you let us know 1) what endpoint you were trying to use, 2) what environment NCPA is installed in, and 3) what error you were seeing? I'm not very experienced with the NCPA project, but I would be surprised if NCPA was intentionally serving anything other than UTF-8. What would be a lot less surprising is if some endpoint was taking data (especially on Windows, but possibly on other devices) and failing to properly re-encode it. If this is happening, we'd probably want to fix the endpoint, instead of having check_ncpa.py force everything to be re-interpreted as Latin-1 encoding.

maxwbot commented 11 months ago
[root@VM]# /usr/local/nagios/libexec/check_ncpa_pt-br.py -H 192.0.0.X -t 'mytoken' -P 5693 -M 'services' -q 'service=Servi.*o Integra.*o,status=running,match=regex'
OK: Serviço Integração is running
[root@VM]# /usr/local/nagios/libexec/check_ncpa.py -H 192.0.0.X -t 'mytoken' -P 5693 -M 'services' -q 'service=Servi.*o Integra.*o,status=running,match=regex'
UNKNOWN: Error occurred while running the plugin. Use the verbose flag for more details.

[root@VM]# /usr/local/nagios/libexec/check_ncpa.py -H 192.0.0.X -t 'mytoken' -P 5693 -M 'services' -q 'service=Servi.*o Integra.*o,status=running,match=regex' -vvv
Connecting to: https://192.0.0.X:5693/api/services/?token=XXXX&check=1&service=Servi.%2Ao+Integra.%2Ao&status=running&match=regex
An error occurred:
'utf-8' codec can't decode byte 0xe7 in position 48: invalid continuation byte

Tested in NCPA 3.0.0 and 2.4.1;

maxwbot commented 11 months ago

Error log in ncpa with windows_service_name: "testémção":

2023-09-15 10:46:31,205 listener.server ERROR Exception on /api/services [GET]
Traceback (most recent call last):
  File "psutil\_pswindows.py", line 543, in _wrap_exceptions
  File "psutil\_pswindows.py", line 532, in _query_status
OSError: [WinError 1060] O serviço especificado não existe como serviço instalado: '(originated from OpenService)'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "flask\app.py", line 2190, in wsgi_app
  File "flask\app.py", line 1486, in full_dispatch_request
  File "flask\app.py", line 1484, in full_dispatch_request
  File "flask\app.py", line 1469, in dispatch_request
  File "listener\server.py", line 289, in token_auth_decoration
  File "listener\server.py", line 1093, in api
  File "listener\services.py", line 307, in walk
  File "listener\services.py", line 18, in wrapper
  File "listener\services.py", line 112, in get_services_via_psutil
  File "psutil\_pswindows.py", line 599, in status
  File "psutil\_pswindows.py", line 531, in _query_status
  File "contextlib.py", line 155, in __exit__
  File "psutil\_pswindows.py", line 552, in _wrap_exceptions
psutil.NoSuchProcess: service 'testémção' does not exist) (name='testémção')
maxwbot commented 11 months ago

Hi @sawolf , Do you have the ncpa 3 beta repository? (NCPA_2 uses the obsolete version of python 2.7.. the new NCPA_3 uses python 3?)

The problem lies in the coding: for service in psutil.win_service_iter(): name = service.name()