Question

Centreon plugin centreon_nsclient_restapi.pl reports malformed UTF-8 character

  • 1 December 2023
  • 1 reply
  • 79 views

Badge +4

Hi,

We use the standard centreon Plugin ‘centreon_nsclient_restapi.pl ‘ (From centreon-plugin-Operatingsystems-Windows-Restapi-20230912-083720.el8.noarch)… to run multiple scripts on our remote servers.

One of the script (powershell) reads and reports some log file content …. It is executed on a Windows 2019 serveur (The standard encoding on the powershell is UTF-8).

 

The log file is a standard .txt file: 

If the logs files contains a line like :

“ven. 2023-12-01 10:50:04    ERROR    Export_LyncCtrlDeviations    Nombre d'erreurs à vérifier”

The result is :

 

UNKNOWN: Cannot decode json response: malformed UTF-8 character in JSON string, at character offset 191 (before "\x{fffd}0080\x{fffd}...") at /usr/lib/centreon/plugins/centreon_nsclient_restapi.pl line 162.
 

The debug mode shows that It receives the string : ‘à vérifier’, like :      ▒\u0080▒ v▒\u0080\u009Arifier

 

If I just remove the accented characters fron the file…. the plugins works well.

“ven. 2023-12-01 10:50:04    ERROR    Export_LyncCtrlDeviations    Nombre d'erreurs a verifier”

 

The plugin command line is :

 /usr/lib/centreon/plugins/centreon_nsclient_restapi.pl --plugin=apps::nsclient::restapi::plugin --mode=query --ssl-opt="SSL_verify_mode => SSL_VERIFY_NONE" --hostname=myserver --port='5666' --proto='https' --legacy-password='$MYPASSWD' --timeout=15 --command=check_scheduledjoblog --arg="c:/\Log/\Lync/\Export"

What I tried is : 

1- To add this option to the plugin :

--source-encoding=UTF-8 

OR:

--source-encoding=iso-8859-1

… but the error is always the same 

 

2- Transform the output from the executed script itself (powershell) : 

CODE:

$iso = [System.Text.Encoding]::GetEncoding("ISO-8859-1")
$utf8 = [System.Text.Encoding]::UTF8
$bytes = $utf8.GetBytes($today_output)
$today_output2 = $iso.GetString($bytes)

write-host $today_output2

 

…. But is doesn’t help ….

 

Any Idea ??

Thanks a lot

 


1 reply

Userlevel 5
Badge +13

I have the same issue  with all the custom script I run on a windows

The --source-encoding option is working for pre-made check command (like checking windows services) but not for custom powershell returing unicode output

 

I found that ensuring you don’t return accentuated text is working, but yould need to code that in your script and return “a verifier” instead of “à vérifier”, but that would mean you don’t return things from a log parser

 

if you don’t have the control on what you will output, there is a simple powershell command to do that , I use  it in some case

I found it here : PowerShell - Remove Diacritics (Accents) from a string - LazyWinAdmin, and I use the 2nd method which work fine for a simple string

just add the function in your script :

function Remove-StringLatinCharacters
{
PARAM ([string]$String)
[Text.Encoding]::ASCII.GetString([Text.Encoding]::GetEncoding("Cyrillic").GetBytes($String))
}

then before output the result

$output = Remove-StringLatinCharacters -String $mystringwithaccent

(you need to test it, but it works fine with most french caracters)

Reply