Icinga2 API queries for service failures

This forum was archived to /woltlab and is now in read-only mode.
  • Me again!


    I've written a dashboard for our staff to use on large screens (Unfortunately none of the available ones fitted what we needed exactly), and I'm using the Icinga2 API to make the queries and display then using PHP.


    The display I'm struggling with is this logic:
    Services that are not OK, whose host is in a specific group, whose host is not down, whose host has not been acknowledged.


    So the query I built looked like this:


    Code
    1. /v1/objects/services?joins=host&filter=service.state!=ServiceOK&match('networks',host.groups)&host.last_hard_state!=1&host.last_check!=-1&host.acknowledgement!=0


    Which looks correct for the conditions I'm after, but this seems to be returning services where the parent host is down but has been acknowledged, so the final filter I believe is the one failing me. Can anyone spot the bug in the query I'm trying to make please?


    Thank you.

  • The filter is invalid as it is not a quote string, and it requires && instead of & as logical operator. If I were you I would change the request into POST, send X-HTTP-Method-OverridE: GET as additional header and put the filter string into the request body (also including filter_vars for example). Do you happen to have your code at hand?

  • Thanks again for replying dnsmichi, appreciate your time.


    Ahh, I see what you mean, the && to force the & to be passed as a complete parameter rather than just the url parsing deliminator.


    The code I've written is very basic and uses php curl, it's below:


    So I can simply call it using
    $return=api_connector("/v1/objects/services?joins=host&filter=service.state!=ServiceOK");
    or similar.
    So I'm thinking using the original query string I could use:



    Code
    1. /v1/objects/services?joins=host&filter=service.state!=ServiceOK&&match("networks",host.groups)&&host.last_hard_state!=1&&host.last_check!=-1&&host.acknowledgement!=0

    If I'm understanding correctly? Or like you say change my method to POST?


    I'm looking at the curl examples using POST but can't see how to chain multiple queries along such as I'm doing above, can you point me in the right direction?


    Thank you,
    Andy

  • I guess it is easier to explain what I mean by writing a fresh code. I didn't really like your abstraction, I'm all for objects, classes and functions. Though I am not the ultimate PHP guru. Borrowed a few lines of code from the Icinga Director (hi Tom) and added my own foo. You can find it inside the Icinga 2 API examples repo I keep updating from community examples and my own.


    https://github.com/Icinga/icin…cts/services/problems.php


    Once you look at the bottom where the ApiClient object is created, you can see that I am adding a custom header array which sets the override header and uses "post" as request method. In addition to that, I'm passing "joins" as array and "filter" as string. There are two occasions where "filter_vars" make sense. Both are passed as key value hash. Modify them as your will, I guess it is more readable like this. Bonus: When keeping the filter as static string, you can modify the filter_vars from anywhere then. This is similar to binding variables in SQL queries.


    That'll cost you a beer or a G&T next time we meet ;)

  • Thank you, that is excellent, I'm implementing it now, and for sure I owe you a few drinks!
    (I'm pleased to say the dashboard will be released open source once it's complete so will be available for others)


    That makes a lot more sense as you say, I got the host checks working great, but I'm not sure my filter for hostgroup under service query is working right, I'm using:



    And it always matches 0 results (When there are down services that would match the above statement)
    Using the Icinga Studio browser I've checked that under the host, the groups Array is there and has the "networks" entry in there, and under service the associated host is working correctly, so have I missed something on how to perform the match correctly? (If I remove the match it returns all the services not OK so the query, class, etc are working great)


    Thanks again.
    Andy

  • Update, changing the filter to the following


    Code
    1. match("*networks*",host.groups)

    Worked, so I'd assume it needs to pattern-match or similar in the match filter. That's working now though :-)

  • That's not so easy, as there is no direkt array element match functionality. The default uses the "in" operator, but that is direct string comparison:


    "str" in host.groups


    If you are familiar with static global function definitions, you can add them and use them inside your expressions (config, api, etc.). You can put such into constants.conf or define your own include in icinga2.conf, e.g. include "functions.conf"


    And then use it inside your filter definition after you've reloaded the core.

    Code
    1. 'filter' => 'service.state!=srv_state && array_match("networks*",host.groups)',


    This time - untested.

  • Hello Every One,

    I am new to icinga2 and am using the version icinga2.4.1 in centos7.

    by default i cant find the memory check plugin in it. Could any one please help me to fix this.


    Thanks

    ranjan