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

apps::protocols::http::plugin json source with collection mode

Open Faruk0 opened this issue 2 years ago • 2 comments

Hello,

I have a question about the collection mode with a json source.

My json file contains multiple nodes in a "nodes" array and in each node there's a "services" array containing informations about services running on this node.

I would like to get the "status" of every nodes and the "status" and "activity" of every services in every nodes. My goal is to create a check command and create a service using this command on my host in centreon to monitor these datas.

I saw that the collection mode could help me with that but i can't make it work. I'm quite new to centreon and this plugin so I'm certainly missing something (probably with the selection_loop in config), the parsing is done correctly but I have no perfdata and only the status of vm2 is displayed.

My config file:

{
    "mapping": {},
    "http": {
        "cache": {
            "enable": false,
            "reload": 5
        },
        "requests": [
            { 
                "name": "centreon",
                "hostname": "X.X.X.X",
                "proto": "http",
                "port": "80",
                "endpoint": "/centreon-collection.json",
                "get_params": ["actual=1"],
                "headers": ["Accept: application/json"],
                "insecure": 1,
                "timeout": 30,
                "backend": "curl",
                "rtype": "json",
                "parse": [
                    {
                        "name": "test",
                        "path": "$.nodes[*]",
                        "entries": [
                            { "id": "name" },
                            { "id": "status" },
                            { "id": "services" }
                        ]
                    },
                    {
                        "name": "test2",
                        "path": "$.nodes[*].services[*]",
                        "entries": [
                            { "id": "id" },
                            { "id": "status" },
                            { "id": "activity" }
                        ]
                    }
                ]
            }
        ]
    },
    "selection_loop": [
        {
            "name": "nodes",
            "source": "%(http.tables.centreonTest)",
            "expand_table": {
                "nodestable": "%(http.tables.centreonTest.[%(centreonTest.instance)])"
            },
            "critical": "%(nodestable.status) != OK",
            "perfdatas": [
                { "nlabel": "node.status", "instance": ["%(nodestable.name)"], "value": "%(nodestable.status)" }
            ],
            "formatting": {
                "printf_msg":"name '%s' status is '%s'",
                "printf_var":[
                    "%(nodestable.name)",
                    "%(nodestable.status)"
                ]
            }
        },
        {
            "name": "services",
            "source": "%(http.tables.centreonTest2)",
            "expand_table": {
                "servicestable": "%(http.tables.centreonTest2.[%(centreonTest2.instance)])"
            },
            "critical": "%(servicestable.status) != running",
            "critical": "%(servicestable.activity) != active",
            "perfdatas": [
                { "nlabel": "service.status", "instances": ["%(servicestable.id)"], "value": "%(servicestable.status)" },
                { "nlabel": "service.activity", "instances": ["%(servicestable.id)"], "value": "%(servicestable.activity)" }
            ],
            "formatting": {
                "printf_msg":"service id '%s' status is '%s' activity is '%s'",
                "printf_var":[
                    "%(servicestable.id)",
                    "%(servicestable.status)",
                    "%(servicestable.activity)"
                ]
            }
        }
    ],
    "formatting":{
        "custom_message_global": "All selections are OK",
        "separator": "-"
    }
}

The debug output when using the command (with the source json):

>perl centreon_plugins.pl --plugin=apps::protocols::http::plugin --mode=collection --config config-test.json --debug
OK: name 'vm2' status is 'OK' 

======> request send
GET http://X.X.X.X:80/centreon-collection.json?actual=1
Accept:  application/json
User-Agent: centreon::plugins::backend::http::useragent

======> response done
HTTP/1.1 200 OK
Connection: keep-alive
Date: Thu, 11 Aug 2022 07:48:19 GMT
Accept-Ranges: bytes
ETag: "62f4b428-544"
Server: nginx/1.20.1
Content-Length: 1348
Content-Type: application/json
Last-Modified: Thu, 11 Aug 2022 07:47:52 GMT
Client-Date: Thu, 11 Aug 2022 07:48:19 GMT
Client-Peer: X.X.X.X:80
Client-Response-Num: 1

{
  "environment": "test",
  "timestamp": 1660056238,
  "nodes": [
    {
      "name": "vm1",
      "status": "OK",
      "services": [
        {
          "id": "service1",
          "status": "running",
          "activity": "active"
        },
        {
          "id": "service2",
          "status": "running",
          "activity": "active"
        },
        {
          "id": "service3",
          "status": "running",
          "activity": "active"
        },
        {
          "id": "service4",
          "status": "running",
          "activity": "active"
        },
        {
          "id": "service7",
          "status": "running",
          "activity": "active"
        },
        {
          "id": "service5",
          "status": "running",
          "activity": "active"
        },
        {
          "id": "service6",
          "status": "running",
          "activity": "active"
        },
        {
          "id": "service8",
          "status": "running",
          "activity": "active"
        },
        {
          "id": "service9",
          "status": "running",
          "activity": "active"
        }
      ]
    },
    {
      "name": "vm2",
      "status": "OK",
      "services": [
        {
          "id": "service1",
          "status": "running",
          "activity": "active"
        }
      ]
    }
  ]
}
======> variables
    http.tables.centreonTest2.[6].status = running
    http.tables.centreonTest2.[6].id = service6
    http.tables.centreonTest2.[6].activity = active
    http.tables.centreonTest2.[3].status = running
    http.tables.centreonTest2.[3].id = service4
    http.tables.centreonTest2.[3].activity = active
    http.tables.centreonTest2.[7].status = running
    http.tables.centreonTest2.[7].id = service8
    http.tables.centreonTest2.[7].activity = active
    http.tables.centreonTest2.[9].status = running
    http.tables.centreonTest2.[9].id = service1
    http.tables.centreonTest2.[9].activity = active
    http.tables.centreonTest2.[2].status = running
    http.tables.centreonTest2.[2].id = service3
    http.tables.centreonTest2.[2].activity = active
    http.tables.centreonTest2.[8].status = running
    http.tables.centreonTest2.[8].id = service9
    http.tables.centreonTest2.[8].activity = active
    http.tables.centreonTest2.[1].status = running
    http.tables.centreonTest2.[1].id = service2
    http.tables.centreonTest2.[1].activity = active
    http.tables.centreonTest2.[4].status = running
    http.tables.centreonTest2.[4].id = service7
    http.tables.centreonTest2.[4].activity = active
    http.tables.centreonTest2.[0].status = running
    http.tables.centreonTest2.[0].id = service1
    http.tables.centreonTest2.[0].activity = active
    http.tables.centreonTest2.[5].status = running
    http.tables.centreonTest2.[5].id = service5
    http.tables.centreonTest2.[5].activity = active
    http.tables.centreonTest.[1].services = ARRAY(0x3346be8)
    http.tables.centreonTest.[1].status = OK
    http.tables.centreonTest.[1].name = vm2
    http.tables.centreonTest.[0].services = ARRAY(0x3346858)
    http.tables.centreonTest.[0].status = OK
    http.tables.centreonTest.[0].name = vm1
name 'vm2' status is 'OK'

Can you help me with that ?

Thanks for your help.

Faruk0 avatar Aug 11 '22 14:08 Faruk0

I don't think we manage that kind of json structure with JSON Path (with that json structure): $.nodes[*].services[*] (get also the node name)

garnier-quentin avatar Aug 12 '22 14:08 garnier-quentin

Thanks for your answer.

If I keep 1 file but with the arrays in the same "level" in the json path will this be compliant with how the plugin manage json files ?

If possible can you explain what kind of structure will work with the plugin or if the following file could work ?

file.json

{
  "nodes": [
    {
      "name": "vm1",
      "status": "OK",
    },
    {
      "name": "vm2",
      "status": "OK",
    } ],
    "services" : [
        {
          "vm1svc1_status": "running",
          "vm1svc1_activity": "active"
        },
        {
          "vm1svc2_status": "running",
          "vm1svc2_activity": "active"
        },
        {
          "vm2svc1_status": "running",
          "vm2svc1_activity": "active"
        } ]
}

Faruk0 avatar Aug 12 '22 15:08 Faruk0

It won't work also. The node name should be in each services entry.

garnier-quentin avatar Aug 16 '22 11:08 garnier-quentin

So if I understand correctly, this one should work :

...
"services" : [
        {
          "node": "vm1",
          "id": "service1",
          "status": "running",
          "activity": "active"
        },
       {
          "node": "vm1",
          "id": "service2",
          "status": "running",
          "activity": "active"
        },
        {
          "node": "vm2",
          "id": "service1",
          "status": "running",
          "activity": "active"
        },
        ... ]

Or is there anything else I'm missing?

Faruk0 avatar Aug 16 '22 12:08 Faruk0

You can keep the node section for the status. You only need to add the node name.

garnier-quentin avatar Aug 16 '22 12:08 garnier-quentin

Thanks for your help, I'll keep the node section untouched and change the service section.

For the config file, I'll need to change the parse section and for the selection loop, how can I make it work with my new file ?

Faruk0 avatar Aug 16 '22 12:08 Faruk0

There was a bug. Please use last git version: https://github.com/centreon/centreon-plugins/pull/3842

I have done the test with the following files:

{
    "nodes": [
        {
            "name": "vm1",
            "status": "OK"
        },
        {
            "name": "vm2",
            "status": "OK"
        }
    ],
    "services": [
        {
            "node": "vm1",
            "service": "service1",
            "status": "running",
            "activity": "active"
        },
        {
            "node": "vm1",
            "service": "service2",
            "status": "running",
            "activity": "active"
        },
        {
            "node": "vm2",
            "service": "service1",
            "status": "running",
            "activity": "active"
        }
    ]
}
{
    "mapping": {},
    "http": {
        "cache": {
            "enable": false,
            "reload": 5
        },
        "requests": [
            { 
                "name": "centreon",
                "hostname": "10.25.7.145",
                "proto": "http",
                "port": "80",
                "endpoint": "/centreon-collection.json",
                "get_params": ["actual=1"],
                "headers": ["Accept: application/json"],
                "insecure": 1,
                "timeout": 30,
                "backend": "curl",
                "rtype": "json",
                "parse": [
                    {
                        "name": "nodes",
                        "path": "$.nodes[*]",
                        "entries": [
                            { "id": "name" },
                            { "id": "status" }
                        ]
                    },
                    {
                        "name": "services",
                        "path": "$.services[*]",
                        "entries": [
                            { "id": "node" },
                            { "id": "service" },
                            { "id": "status" },
                            { "id": "activity" }
                        ]
                    }
                ]
            }
        ]
    },
    "selection_loop": [
        {
            "name": "nodes",
            "source": "%(http.tables.centreonNodes)",
            "expand_table": {
                "node": "%(http.tables.centreonNodes.[%(centreonNodes.instance)])"
            },
            "critical": "%(node.status) ne 'OK'",
            "formatting": {
                "printf_msg":"node '%s' status is '%s'",
                "printf_var":[
                    "%(node.name)",
                    "%(node.status)"
                ]
            }
        },
        {
            "name": "services",
            "source": "%(http.tables.centreonServices)",
            "expand_table": {
                "service": "%(http.tables.centreonServices.[%(centreonServices.instance)])"
            },
            "critical": "%(service.status) ne 'running' or %(service.activity) ne 'active'",
            "formatting": {
                "printf_msg":"node '%s' service '%s' status is '%s' [activity: '%s']",
                "printf_var":[
                    "%(service.node)",
                    "%(service.service)",
                    "%(service.status)",
                    "%(service.activity)"
                ]
            }
        }
    ],
    "formatting":{
        "custom_message_global": "All selections are OK",
        "separator": "-"
    }
}

garnier-quentin avatar Aug 17 '22 08:08 garnier-quentin