Add host using a script

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
Post Reply
soran
Posts: 1
Joined: Fri Nov 15, 2024 5:38 am

Add host using a script

Post by soran »

Hello,

I am trying to code bash script which basically add a host to nagios.
It creates a .cfg file into the repertory where all hosts .cfg file are located after asking some input such as the host_name, ip address etc ...

It does work, but not totally. I mean, it does appear on Nagios web interface after a restart however there's some troubleshooting
If I am looking for the host created by my script through the web interface, it's okay.
However if I am looking at it from Core Config Manager, it doesn't appear anymore and cannot be edited from the web interface

I am wondering if I missed anything> My script is quite simple and does something like this (Can not copy paste)

echo "define host {
(tab) host_name $HOST_NAME
...
}" >> /etc/loca/nagios/etc/hosts/${HOST_NAME}.cfg
DoubleDoubleA
Posts: 199
Joined: Thu Feb 09, 2017 5:07 pm

Re: Add host using a script

Post by DoubleDoubleA »

Hi @soran,

With XI, there is more going on than just the .cfg file(s), which is why you don't see anything in the CCM.

I would recommend, if you prefer bash scripting, to cURL the XI API to add hosts programmatically.

Aaron
gregbeyer
Posts: 174
Joined: Fri Sep 11, 2020 2:13 pm

Re: Add host using a script

Post by gregbeyer »

You'll want to leverage the API: I do an import of hosts every night from our CMDB database which can change that often. Hosts are contained in one file, with all of the required parameters that a nagios host needs (nodes.cfg).

sudo curl -XPOST "http://<your-nagios-FQDN>/nagiosxi/api/v1/config/import?apikey=<key_obfuscated>=1" -v -H "Content-Type: text/xml" --data-binary "@/usr/local/nagios/etc/cfgprep/nodes.cfg"

After uploading, you have to apply config. You could do that manually, or as we do, include it in the script:

sudo curl -XPOST "http://<your-nagios-FQDN>/nagiosxi/api/v1/system/applyconfig?apikey=<key_obfuscated>&pretty=1"

You'll need to use an admin equiv apikey, either yours or, better, an admin equiv account just for this purpose.

I have noticed that, unfortunately, the apikey ends up clear text in nagios.log (or is it /var/log/messages?, can't remember). From the above. Not great for XI security, if someone figures out how to leverage it.
kg2857
Posts: 369
Joined: Wed Apr 12, 2023 5:48 pm

Re: Add host using a script

Post by kg2857 »

You can also write the .cfg file to the import dir and then run the reconfigure script, which will define the host in the DB and write it's .cfg file in the hosts dir.
Post Reply