centreon / centreon-plugins

Collection of standard plugins to discover and gather cloud-to-edge metrics and status across your whole IT infrastructure.
https://www.centreon.com
Apache License 2.0
311 stars 274 forks source link

enh(protocols::http): mode collection add function boolean2integer #5218

Open garnier-quentin opened 1 month ago

garnier-quentin commented 1 month ago

Community contributors

Description

On legacy centreon system with JSON::XS version < 4.0 (rhel 7 and 8 ), JSON boolean values are of the type ‘JSON::PP::Boolean’. It’s an issue because we can’t manage objects in a Safe Perl object.

CTOR-957

Type of change

How this pull request can be tested ?

The command to test it:

$ perl centreon_plugins.pl --plugin=apps/protocols/http/plugin.pm --mode=collection --config=/root/test-boolean.config --constant='hostname=127.0.0.1'  --constant='port=80' --constant='critical=1' --verbose
CRITICAL: interruptor '2' switch enabled: 1 | '1#interruptor.enabled.count'=0;;;; '2#interruptor.enabled.count'=1;;;;
interruptor '1' switch enabled: 0
interruptor '2' switch enabled: 1

JSON file provided by the http server:

{ "interruptor": [
        {
            "name": "1",
            "enabled" : false 
        },
        {
            "name": "2",
            "enabled": true
        }
    ]   
}

The test-boolean.config file:

{
    "mapping": {},
    "constants": {
        "method": "http",
        "port": "80"
    },
    "http": {
        "requests": [
            {
                "name": "interruptor",
                "hostname": "%(constants.hostname)",
                "proto": "%(constants.method)",
                "port": "%(constants.port)",
                "endpoint": "/boolean",
                "headers": ["Accept:application/json", "Content-Type:application/json"],
                "insecure": 1,
                "timeout": 30,
                "backend": "curl",
                "rtype": "json",
                "parse": [
                    {
                        "name": "entry",
                        "path": "..interruptor[*]",
                        "entries": [
                            { "id": "name" },
                            { "id": "enabled" }
                        ]
                    }
                ]
            }
        ]
    },
    "selection_loop": [
        {
            "name": "interruptorValues",
            "source": "%(http.tables.interruptorEntry)",
            "expand_table": {
                "interruptor": "%(http.tables.interruptorEntry.[%(interruptorEntry.instance)])"
            },
            "functions": [
                { "type": "boolean2integer", "src": "%(interruptor.enabled)", "save": "%(interruptor.enabled)" }
            ],
            "critical":  "%(interruptor.enabled) =~ /%(constants.critical)/i",
            "perfdatas": [
                { "nlabel": "interruptor.enabled.count", "value": "%(interruptor.enabled)", "instances": ["%(interruptor.name)"] }
            ],
            "formatting": {
                "printf_msg":"interruptor '%s' switch enabled: %s",
                "printf_var":[
                    "%(interruptor.name)",
                    "%(interruptor.enabled)"
                ]
            }
        }
    ],
    "formatting":{
        "custom_message_global": "interruptor switches are ok",
        "separator": "-"
    }
}

Checklist