Page 1 of 1

Adding service via API

Posted: Wed May 30, 2018 3:55 am
by rambertnala
Hi friends.
I need to add a new service to more tham 1000 hosts, and via API is more easier than manual, but i have a problem.

This is my commnad to add it

curl -XPOST "http://IP_NAGIOS/nagiosxi/api/v1/config/service?apikey=MY_API_KEY&pretty=1" -d "host_name=HOSTNAME&service_description=SERVICENAME&use=generic-service&check_command=check_nrpe!check_service!-a "filter= name like 'servicename'" "warning= count = 0" "ok=state='running'" "critical=state='stopped'" "perf-config=*(ignored:true)"!!!!!!&max_check_attempts=5&check_interval=15
&retry_interval=5&active_checks_enabled=1&passive_checks_enabled=1
&check_period=xi_timeperiod_24x7&flap_detection_enabled=0
&notification_interval=15&notification_period=xi_timeperiod_24x7
&notifications_enabled=1&contacts=nagiosadmin&_xiwizard=passiveobject&register=1"


Command line has been copied from a manual creation into CCM nagios xi

My nagios xi version is 5.4.13

Well i get this error

error sintactico cerca del elemento inesperado `('

and i think problem is double quotes at command line.

Thanks and regards

Re: Adding service via API

Posted: Wed May 30, 2018 8:20 am
by mcapra
Strictly speaking, this is not a Nagios XI problem.

You have un-escaped quotes (") in your command. In this particular case, Bash requires you to escape quotes like so: \"

You also have un-escaped bangs (!) in your command. In this particular case, Bash requires you to escape bangs like so: \!

Applying some simple search+replace via a text editor, I've produced the following:

Code: Select all

curl -XPOST "http://IP_NAGIOS/nagiosxi/api/v1/config/service?apikey=MY_API_KEY&pretty=1" -d "host_name=HOSTNAME&service_description=SERVICENAME&use=generic-service&check_command=check_nrpe\!check_service\!-a \"filter= name like 'servicename'\" \"warning= count = 0\" \"ok=state='running'\" \"critical=state='stopped'\" \"perf-config=*(ignored:true)\"\!\!\!\!\!\!&max_check_attempts=5&check_interval=15&retry_interval=5&active_checks_enabled=1&passive_checks_enabled=1&check_period=xi_timeperiod_24x7&flap_detection_enabled=0&notification_interval=15&notification_period=xi_timeperiod_24x7&notifications_enabled=1&contacts=nagiosadmin&_xiwizard=passiveobject&register=1"
Depending on which version of curl you're using (curl --version), you may also want to leverage --data-urlencode to handle some of the special characters in this POST body. More info here:
https://stackoverflow.com/questions/100 ... -post-data

Re: Adding service via API

Posted: Wed May 30, 2018 1:34 pm
by kyang
Thanks for the help mcapra!

rambertnala, let us know if you have any more questions.