Issues creating custom service from custom check command

This forum was archived to /woltlab and is now in read-only mode.
  • Hello,


    I have been using this forum to solve some issues that I have had in the past using Icinga2, I am quite new with it, and I have an issue now that I am not able to solve, so this is the reason of this message here and requesting you help please.


    I need to create custom services in my environment, so I have created a custom script in order that it would be executed in hosts called hello_world.sh that receives 3 custom parameters:


    After of this, I have created a checkCommand using REST API:

    Code
    1. curl -k -s -u root:password -H 'Accept: application/json' -X PUT 'https://localhost:5665/v1/objects/checkcommands/cmdApi' -d '{ "templates": [ "plugin-check-command" ], "attrs": { "command": [ "/usr/lib/nagios/plugins/check_hello_world.sh"], "arguments": { "-n": "$NAME$", "-h": "$HOSTALIAS$", "-c": "$COUNTRY$" } } }'

    I have checked that the checkCommand, has been created succesfully:


    I need to create a service, adding it to a existing host, using REST API that uses the checkCommand created, but I am not able to find the REST API call that I have to execute to do this. I have been searching information about it, but I haven't find the answer. This is the call that I am executing, and returns a HTTP ERROR 404 (No objects found):

    Code
    1. curl -s -u root:password -H 'Accept: application/json' -X POST -k "https://localhost:5665/v1/objects/services/myHost%21hello_world_service_custom" -d '{ "templates": [ "generic-service" ], "attrs": { "name": "hello_world_service_custom", "check_command" : "cmdApi", "vars.custom_name": "myName", "vars.custom_hostalias": "myHostalias", "vars.custom_country": "myCountry", "host_name": "myHost" } }' | python -m json.tool
    2. {
    3. "error": 404.0,
    4. "status": "No objects found."
    5. }

    One important thing to mention, is that I need to create the services using REST API, because it can be deleted, and if I create the service adding it to services.conf, I could not delete the service later.


    Can anyone tell me what I am doing wrong please? Any help is welcome.


    Thanks in advance!

    The post was edited 2 times, last by drojo ().

  • Thanks for both responses! with those tips I have been able to create the service succesfully using this call:


    Code
    1. curl -s -u root:password -H 'Accept: application/json' -X PUT -k "https://localhost:5665/v1/objects/services/myHost%21hello_world_service_custom" -d '{ "templates": [ "generic-service" ], "attrs": { "display_name": "hello_world_service_custom", "check_command" : "cmdApi", "vars.custom_name": "myName", "vars.custom_hostalias": "myHostalias", "vars.custom_country": "myCountry", "host_name": "myHost" } }' | python -m json.tool

    Now I can see the service in Icinga2 dashboard, but the execution of the command seems that doesn't receive the values for the attributes defined, appearing as output: Hello from Icinga2 - HOST: - Location: instead of Hello myName from Icinga2 - HOST: myHostalias - Location: myCountry


    The thing that I am not able to understand is why if I look at Icinga2 dashboard, I can see that the service has asociated the values that I used in the creation of the service (vars.xxx). You can see them in the attached file service_snapshot.jpg marked in red.


    The checkCommand that I use for the service is this:


    And the .sh file that I execute:


    What am I doing wrong?

  • I did it with config files.

    My script:

    My checkcommand:

    Code
    1. object CheckCommand "cmdApi" {
    2. import "plugin-check-command"
    3. command = [PluginDir + "/check_hello_world.sh" ]
    4. arguments = {
    5. "-c" = "$CUSTOM_COUNTRY$"
    6. "-h" = "$CUSTOM_HOSTALIAS$",
    7. "-n" = "$CUSTOM_NAME$",
    8. }
    9. }

    my service:

    Code
    1. apply Service "hello" {
    2. import "generic-service"
    3. check_command = "cmdApi"
    4. check_interval = 30
    5. retry_interval = 30
    6. assign where host.name == NodeName
    7. }

    my host:

    Code
    1. object Host NodeName {
    2. import "generic-host"
    3. address = "127.0.0.1"
    4. address6 = "::1"
    5. vars.CUSTOM_COUNTRY="mycountry"
    6. vars.CUSTOM_HOSTALIAS="myalias"
    7. vars.CUSTOM_NAME="myname"
    8. }

    Resulting api command:


    Resulting screenshot:


    The eval order

    https://docs.icinga.com/icinga…cs#macro-evaluation-order

    describes where your variables are searched for.

    So, these are neither at the host, service, or checkcommand object and thus are not evaluated in your case.


    Assumption: In your service creation, capitalize the variables as they are in the check_command:

    "vars.custom_name": "myName", "vars.custom_hostalias": "myHostalias", "vars.custom_country": "myCountry"

    to

    "vars.CUSTOM_NAME": "myName", "vars.CUSTOM_HOSTALIAS": "myHostalias", "vars.CUSTOM_COUNTRY": "myCountry"

    The post was edited 1 time, last by sru ().

  • Custom attributes are case sensitive in Icinga 2. I wouldn't go the old way of defining them in uppercase everywhere. Rather add a prefix name of your CheckCommand to your arguments, like "hello_world_country".

  • Thanks for your responses, I have been able to fix the issue and create the service I need successfully.


    I would like to remark your help, your speed on the responses and the quality of them

  • Can you share your final solution with others looking into the same issue please? :)


    Thanks for the kind words. Once you've learned anything you need, you might want to stay here and help others. Our community lives from sharing and giving back :)

  • Yes, of course, after receive so much help is the minimum that I can do. I have used the approach proposed by sru


    From the base, this is my script:


    The checkCommand created using REST API:


    Code
    1. curl -k -s -u root:password -H 'Accept: application/json' -X POST 'https://localhost:5665/v1/objects/checkcommands/cmdApi' -d '{ "templates": [ "plugin-check-command" ], "attrs": { "command": [ "/usr/lib/nagios/plugins/check_hello_world.sh"], "arguments": { "-n": "$CUSTOM_NAME$", "-h": "$CUSTOM_HOSTALIAS$", "-c": "$CUSTOM_COUNTRY$" } } }'

    The service created using REST API:


    Code
    1. curl -s -u root:password -H 'Accept: application/json' -X PUT -k "https://localhost:5665/v1/objects/services/myHost%21hello_world_service_custom" -d '{ "templates": [ "generic-service" ], "attrs": { "display_name": "hello_world_service_custom", "check_command" : "cmdApi", "vars.CUSTOM_NAME": "myName", "vars.CUSTOM_HOSTALIAS": "myHostalias", "vars.CUSTOM_COUNTRY": "myCountry", "host_name": "myHost" } }' | python -m json.tool


    And attached you can find the snapshot of the service working in Icinga2 dashboard. With the script provided by sru, everytime that it is executed, it returns a random value: "OK", "WARNING", "CRITICAL" or "UNKNOWN", so the status of the service changes. A very good point for me, because I am learning how Icinga2 works

  • drojo

    while we are at it:

    The scripts output line is *not* what i proposed.

    It is not allowed to have multiple ":" because that is used to separate the status from the simple text field.

    That is why i proposed

    Code
    1. echo ${tstat[$stat]}: Hello "$CUSTOM_NAME" from Icinga2 - HOST "$CUSTOM_HOSTALIAS" - Location "$CUSTOM_COUNTRY"

    instead.


    Get used to be *very* picky about the plugin output format - for icinga it does not matter that much,

    but with a growing knowledge you will use many addons like grafana, influxdb and the like and these rely on a correct format.

  • Thanks for advice sru (sorry but I don't know how to call you using @format as you did)


    I have only added in the line the ${tstat[$stat]} part, but I will take in account for the future the words you mentioned :)

  • <offtopic>

    drojo

    Just add '@' and start typing the name. It will open up a dropdown to select the correct username. But I guess that works after 3 characters only (anything else will kill the backend obviously).

    </offtopic>