Delete Service API

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
optionstechnology
Posts: 234
Joined: Thu Nov 17, 2016 11:26 am

Delete Service API

Post by optionstechnology »

I am trying to delete services via the API but all my services are linked to hostgroups not hosts

The command seems to specifically ask for a hostname and wont run without it-

Code: Select all

curl -XDELETE "https://SERVER/nagiosxi/api/v1/config/service?apikey=longasskey&pretty=1&host_name=testapihost&service_description=PING&applyconfig=1"
User avatar
lmiltchev
Bugs find me
Posts: 13589
Joined: Mon May 23, 2011 12:15 pm

Re: Delete Service API

Post by lmiltchev »

Currently, this is not possible as the REST API verifies if the service is a "part of a host". Accepting more directives in the future, such as hostgroup_name is on our todo list.
Be sure to check out our Knowledgebase for helpful articles and solutions!
optionstechnology
Posts: 234
Joined: Thu Nov 17, 2016 11:26 am

Re: Delete Service API

Post by optionstechnology »

Can you also include the ability to create services and attach them to hostgroups instead of hosts?
User avatar
lmiltchev
Bugs find me
Posts: 13589
Joined: Mon May 23, 2011 12:15 pm

Re: Delete Service API

Post by lmiltchev »

You can already do that in the REST API.

Example:

Code: Select all

# curl -XPOST "http://x.x.x.x/nagiosxi/api/v1/config/service?apikey=LTltbjobR0X3V5ViDIitYaI8hjsjoFBaOcWYukamF7oAsD8lhJRvSPWq8I3PjTf7&pretty=1" -d "hostgroup_name=printers&service_description=PING&check_command=check_ping\!3000,80%\!5000,100%&check_interval=5&retry_interval=5&max_check_attempts=2&check_period=24x7&contacts=nagiosadmin&notification_interval=5&notification_period=24x7&applyconfig=1&force=1"
{
    "success": "Successfully added  :: PING to the system. Config applied, Nagios Core was restarted."
}
The config that was generated:

Code: Select all

define service {
    service_description      PING
    hostgroup_name           printers
    check_command            check_ping!3000,80%!5000,100%
    max_check_attempts       2
    check_interval           5
    retry_interval           5
    check_period             24x7
    notification_interval    5
    notification_period      24x7
    contacts                 nagiosadmin
    register                 1
}
Important: Notice the "&force=1" part. If you don't force the command, than it would fail with:

Code: Select all

{
    "error": "Missing required variables",
    "missing": [
        "host_name"
    ]
}
when you try to add a service to a hostgroup only.

Hope this helps.
Be sure to check out our Knowledgebase for helpful articles and solutions!
User avatar
eloyd
Cool Title Here
Posts: 2190
Joined: Thu Sep 27, 2012 9:14 am
Location: Rochester, NY
Contact:

Re: Delete Service API

Post by eloyd »

lmiltchev wrote:Currently, this is not possible as the REST API verifies if the service is a "part of a host". Accepting more directives in the future, such as hostgroup_name is on our todo list.
Need to know when this will be finished. Since there is no "update service" directive, then this is the only way to delete-then-add via the API. And it fails if you don't have a host_name but rely on hostgroup.

Optionally, need to know when adding a service with the same "config_name" and "service_description" as an existing service will update the service with the information specified. That would be a real problem solver.
Image
Eric Loyd • http://everwatch.global • 844.240.EVER • @EricLoyd
I'm a Nagios Fanatic! • Join our public Nagios Discord Server!
User avatar
lmiltchev
Bugs find me
Posts: 13589
Joined: Mon May 23, 2011 12:15 pm

Re: Delete Service API

Post by lmiltchev »

Need to know when this will be finished.
Unfortunately, I don't have an ETA on that. Our developers will get to it as soon as they can, depending on the length or their TODO list.
Optionally, need to know when adding a service with the same "config_name" and "service_description" as an existing service will update the service with the information specified. That would be a real problem solver.
You can update a service with the same config_name and service_description (at least in XI 5.5.4) by re-adding it.

Example of adding a "new" service:

Code: Select all

curl -XPOST "http://x.x.x.x/nagiosxi/api/v1/config/service?apikey=LTltbjobR0X3V5ViDIitYaI8hjsjoFBaOcWYukamF7oAsD8lhJRvSPWq8I3PjTf7&pretty=1" -d "host_name=localhost&config_name=localhostconfig&service_description=TEST&check_command=check_ping\!3000,80%\!5000,100%&check_interval=5&retry_interval=5&max_check_attempts=2&check_period=24x7&contacts=nagiosadmin&notification_interval=5&notification_period=24x7&applyconfig=1"
{
    "success": "Successfully added localhost :: TEST to the system. Config applied, Nagios Core was restarted."
}
example01.PNG

Code: Select all

define service {
    host_name                localhost
    service_description      TEST
    check_command            check_ping!3000,80%!5000,100%
    max_check_attempts       2
    check_interval           5
    retry_interval           5
    check_period             24x7
    notification_interval    5
    notification_period      24x7
    contacts                 nagiosadmin
    register                 1
}
Example of "updating" the service:

Code: Select all

curl -XPOST "http://x.x.x.x/nagiosxi/api/v1/config/service?apikey=LTltbjobR0X3V5ViDIitYaI8hjsjoFBaOcWYukamF7oAsD8lhJRvSPWq8I3PjTf7&pretty=1" -d "host_name=localhost&config_name=localhostconfig&service_description=TEST&check_command=check_dummy\!0\!'Dummy check'&check_interval=10&retry_interval=3&max_check_attempts=1&check_period=24x7&contacts=nagiosadmin&notification_interval=60&notification_period=24x7&applyconfig=1"
{
    "success": "Successfully added localhost :: TEST to the system. Config applied, Nagios Core was restarted."
}

Code: Select all

define service {
    host_name                localhost
    service_description      TEST
    check_command            check_dummy!0!'Dummy check'
    max_check_attempts       1
    check_interval           10
    retry_interval           3
    check_period             24x7
    notification_interval    60
    notification_period      24x7
    contacts                 nagiosadmin
    register                 1
}
You do not have the required permissions to view the files attached to this post.
Be sure to check out our Knowledgebase for helpful articles and solutions!
User avatar
eloyd
Cool Title Here
Posts: 2190
Joined: Thu Sep 27, 2012 9:14 am
Location: Rochester, NY
Contact:

Re: Delete Service API

Post by eloyd »

Hm. We did:

Code: Select all

curl -k -XPOST "https://127.0.0.1/nagiosxi/api/v1/config/service?apikey=ILIKEPONIES&pretty=1" -d "service_description=Eric&use=generic-service&display_name=&force=1&config_name=VoIP Peers"
Twice in a row and got two "Eric" services in the "Eric Configs" config group. If we do it a third time but change the display_name to something other than blank, we get yet a third service. Maybe it has to do with the minimalist approach we take to using templates for everything?

Nagios XI 5.5.2
Image
Eric Loyd • http://everwatch.global • 844.240.EVER • @EricLoyd
I'm a Nagios Fanatic! • Join our public Nagios Discord Server!
User avatar
lmiltchev
Bugs find me
Posts: 13589
Joined: Mon May 23, 2011 12:15 pm

Re: Delete Service API

Post by lmiltchev »

I was able to recreate the issue in Nagios XI 5.5.4... kind of. When try to "update" a service via the REST API (trying the change the display_name only), my service gets "duplicated" when it's added to a hostgroup.

Code: Select all

curl -XPOST "http://x.x.x.x/nagiosxi/api/v1/config/service?apikey=LTltbjobR0X3V5ViDIitYaI8hjsjoFBaOcWYukamF7oAsD8lhJRvSPWq8I3PjTf7&pretty=1" -d "hostgroup_name=linux-servers&service_description=Eric&config_name=MyConfigName1&use=generic-service&check_command=check_ping\!3000,80%\!5000,100%&display_name=MyDisplayName1&applyconfig=1&force=1"

Code: Select all

curl -XPOST "http://x.x.x.x/nagiosxi/api/v1/config/service?apikey=LTltbjobR0X3V5ViDIitYaI8hjsjoFBaOcWYukamF7oAsD8lhJRvSPWq8I3PjTf7&pretty=1" -d "hostgroup_name=linux-servers&service_description=Eric&config_name=MyConfigName1&use=generic-service&check_command=check_ping\!3000,80%\!5000,100%&display_name=MyDisplayName2&applyconfig=1&force=1"
example01.PNG

Code: Select all

[root@main-nagios-xi services]# pwd
/usr/local/nagios/etc/services
[root@main-nagios-xi services]# ll MyConfig*
-rw-rw-r-- 1 apache nagios 1308 Sep 25 14:19 MyConfigName1.cfg

Code: Select all

define service {
    service_description    Eric
    use                    generic-service
    hostgroup_name         linux-servers
    display_name           MyDisplayName1
    check_command          check_ping!3000,80%!5000,100%
    register               1
}

define service {
    service_description    Eric
    use                    generic-service
    hostgroup_name         linux-servers
    display_name           MyDisplayName2
    check_command          check_ping!3000,80%!5000,100%
    register               1
}
However, when I added the service to a host (not hostgroup), it did not get duplicated. To test it, I ran:

Code: Select all

curl -XPOST "http://x.x.x.x/nagiosxi/api/v1/config/service?apikey=LTltbjobR0X3V5ViDIitYaI8hjsjoFBaOcWYukamF7oAsD8lhJRvSPWq8I3PjTf7&pretty=1" -d "host_name=localhost&service_description=Eric&config_name=MyConfigName1&use=generic-service&check_command=check_ping\!3000,80%\!5000,100%&display_name=MyDisplayName1&applyconfig=1&force=1"

Code: Select all

curl -XPOST "http://x.x.x.x/nagiosxi/api/v1/config/service?apikey=LTltbjobR0X3V5ViDIitYaI8hjsjoFBaOcWYukamF7oAsD8lhJRvSPWq8I3PjTf7&pretty=1" -d "host_name=localhost&service_description=Eric&config_name=MyConfigName1&use=generic-service&check_command=check_ping\!3000,80%\!5000,100%&display_name=MyDisplayName2&applyconfig=1&force=1"
It seems like we need to do some more digging into this. The issue is caused by the way objects are imported in the CCM. The config_name is not an actual config directive (in Nagios Core), which creates issues with the import process.
You do not have the required permissions to view the files attached to this post.
Be sure to check out our Knowledgebase for helpful articles and solutions!
User avatar
eloyd
Cool Title Here
Posts: 2190
Joined: Thu Sep 27, 2012 9:14 am
Location: Rochester, NY
Contact:

Re: Delete Service API

Post by eloyd »

Feel free to delete the "config_name" entirely from Nagios XI as it usually only serves to confuse our customers. :-) And if adding without a host casuses the behavior, then we're screwed, since most of our services are added to hostgroups. :-)
Image
Eric Loyd • http://everwatch.global • 844.240.EVER • @EricLoyd
I'm a Nagios Fanatic! • Join our public Nagios Discord Server!
User avatar
lmiltchev
Bugs find me
Posts: 13589
Joined: Mon May 23, 2011 12:15 pm

Re: Delete Service API

Post by lmiltchev »

The issue with updating services that are added to hostgroups via the REST API should be fixed in XI 5.6. We have an internal FR for adding this functionality, that is pending approval. Please keep in mind that the decision to implement the enhancement is at the sole discretion of our development team.
Be sure to check out our Knowledgebase for helpful articles and solutions!
Locked