centreon-plugins icon indicating copy to clipboard operation
centreon-plugins copied to clipboard

enh(http/collection): move json parsing outside http call

Open cgagnaire opened this issue 2 years ago • 3 comments

This PR moves JSON (and XML) parsing outside the call_http function so we can actually use the scenario_stopped directive if we don't do recursive requests (only one request, which must be most use cases).

It was not possible before because the parsing bit was stopping the scenario for JSON decoding problem before HTTP metrics were used (through check_filter2 function).

Now, for example we can look at HTTP returned code before parsing a content, so we can exit the scenario in case of non-2xx.

This example adds a "test" selection that shows the returned code and message that can handle, in addition to the "scenario_stopped" directive, the cases where the API does not respond as expected. That way, we don't have "Cannot decode ..." unknown output when the API actually doesn't work.

{
    "http": {
        "requests": [
            {
                "name": "ping",
                ...
                "endpoint": "/toto",
                "method": "GET",
                ...
                "insecure": 0,
                "timeout": 30,
                ...
                "backend": "curl",
                "scenario_stopped": "%(builtin.httpCode.ping) != 200",
                "rtype": "xml",
                "parse": [
                    {
                        "name": "result",
                        "path": "$..PingResponse.PingResult",
                        "entries": [
                            {
                                "id": "ElapsedMilliseconds"
                            }
                        ]
                    }
                ]
            }
        ]
    },
    "selection": [
        {
            "name": "test",
            "critical": "%(builtin.httpCode.ping) != 200",
            "exit": "%(builtin.httpCode.ping) != 200",
            "formatting": {
                "printf_msg":"API returned HTTP code '%s' with message '%s'",
                "printf_var":[
                    "%(builtin.httpCode.ping)",
                    "%(builtin.httpMessage.ping)"
                ],
                "display_ok": true
            }
        },
        {
            "name": "status",
            "formatting": {
                "printf_msg": "Worker ping returned in %s ms",
                "printf_var": [
                    "%(http.tables.pingResult.[0].ElapsedMilliseconds)"
                ],
                "display_ok": true
            },
            "perfdatas": [
                {
                    "nlabel": "worker.ping.milliseconds",
                    "value": "%(http.tables.pingResult.[0].ElapsedMilliseconds)",
                    "min": 0,
                    "unit": "ms"
                }
            ]
        }
    ],
    "formatting": {
        "custom_message_global": "Saas platform returned calls successfully",
        "separator": "-"
    }
}

cgagnaire avatar Jan 19 '24 08:01 cgagnaire

Hi @garnier-quentin and @cgagnaire, First thanks to both of you for your contributions. Do you think there is a way to combine both your fixes/enhancements? If not, we'll first merge Quentin's PR to fix this infinite loop issue, that has proven useful to at least one of our customers. Regards

omercier avatar Apr 12 '24 13:04 omercier

I'll check @cgagnaire last comment: https://github.com/centreon/centreon-plugins/pull/4915

garnier-quentin avatar Apr 12 '24 13:04 garnier-quentin

I have updated the patch @omercier

garnier-quentin avatar Apr 12 '24 14:04 garnier-quentin

Closing the PR (see #4915 and #5044). Thanks again @cgagnaire !

omercier avatar Jun 04 '24 08:06 omercier