Page 1 of 2
Delete Service API
Posted: Wed Aug 15, 2018 12:00 pm
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"
Re: Delete Service API
Posted: Wed Aug 15, 2018 3:39 pm
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.
Re: Delete Service API
Posted: Mon Sep 03, 2018 10:35 am
by optionstechnology
Can you also include the ability to create services and attach them to hostgroups instead of hosts?
Re: Delete Service API
Posted: Tue Sep 04, 2018 8:53 am
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¬ification_interval=5¬ification_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.
Re: Delete Service API
Posted: Mon Sep 24, 2018 1:27 pm
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.
Re: Delete Service API
Posted: Mon Sep 24, 2018 3:50 pm
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¬ification_interval=5¬ification_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¬ification_interval=60¬ification_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
}
Re: Delete Service API
Posted: Tue Sep 25, 2018 10:01 am
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
Re: Delete Service API
Posted: Tue Sep 25, 2018 2:31 pm
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.
Re: Delete Service API
Posted: Tue Sep 25, 2018 2:40 pm
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.

Re: Delete Service API
Posted: Tue Sep 25, 2018 2:52 pm
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.