Script for adding or removing hosts in a hostgroup
Script for adding or removing hosts in a hostgroup
Hello,
In time I'll need a way to dynamically update hostgroups of hosts. Is there anything existing at this time that can do this or something similar. The plan is for example run a script every week that queries certain properties of a host, for example WIndows os and put existing hosts in for example the hostgroup all_srv_windows_2008. So I would need to write a Bash script that egts the Nagios host config file, check the Windows os and then update the hostgroup line.
I know the bulk modification tool can add hosts to a hostgroup, but I don't think i can call this programmatically?
Grtz
Willem
In time I'll need a way to dynamically update hostgroups of hosts. Is there anything existing at this time that can do this or something similar. The plan is for example run a script every week that queries certain properties of a host, for example WIndows os and put existing hosts in for example the hostgroup all_srv_windows_2008. So I would need to write a Bash script that egts the Nagios host config file, check the Windows os and then update the hostgroup line.
I know the bulk modification tool can add hosts to a hostgroup, but I don't think i can call this programmatically?
Grtz
Willem
Last edited by WillemDH on Thu Jan 15, 2015 11:19 am, edited 1 time in total.
Nagios XI 5.8.1
https://outsideit.net
https://outsideit.net
Re: Script or adding or removing hosts in a hostgroup
There's nothing built-in that I know of; this would need to be done via some SQL commands. Updating the config files is not recommended since they would be overwritten on an apply config. You could probably get away with using static configs in the .../nagios/etc/static directory, and just define the hostgroups there, but this would prevent the hostgroups from being listed in the CCM so you would need to be careful.
Former Nagios employee
Re: Script for adding or removing hosts in a hostgroup
Ah,
I was thinking to do it like this:
1) let's take example host server1.cfg
2) Clone the host cfg file for server1 to some remote location
3) Edit this file so all hostgroups reflect the current situation
4) Put the edited cfg in /usr/local/nagios/etc/import/
5) Execute /usr/local/nagiosxi/scripts/reconfigure_nagios.sh
Do you think this is possible and doable? As we already rewrote the switch wizard to be able to import switches command line, I would suspect the above could be done too? I'm quite sure it would work for new hosts, but I'm not sure what it would do with existing hosts..?
If there is a better / easier alternative by making use of SQL command to update the hostgroups, I'd be happy to learn more about that. But I did not found any documentation about SQL commands to update Nagios configuration.
Grtz
Willem
I was thinking to do it like this:
1) let's take example host server1.cfg
2) Clone the host cfg file for server1 to some remote location
3) Edit this file so all hostgroups reflect the current situation
4) Put the edited cfg in /usr/local/nagios/etc/import/
5) Execute /usr/local/nagiosxi/scripts/reconfigure_nagios.sh
Do you think this is possible and doable? As we already rewrote the switch wizard to be able to import switches command line, I would suspect the above could be done too? I'm quite sure it would work for new hosts, but I'm not sure what it would do with existing hosts..?
If there is a better / easier alternative by making use of SQL command to update the hostgroups, I'd be happy to learn more about that. But I did not found any documentation about SQL commands to update Nagios configuration.
Grtz
Willem
Nagios XI 5.8.1
https://outsideit.net
https://outsideit.net
Re: Script for adding or removing hosts in a hostgroup
I don't see why your procedure would not work. I had only one member in the "mac-servers" hostgroup.
I moved the "/usr/local/nagios/etc/hostgroups.cfg" file to "/usr/local/nagios/etc/import/" directory and modified it as such:
Then I ran the reconfigure script:
The additional member we added just fine:
Note: It didn't make any sense to add these members there... it's just an example.
Code: Select all
define hostgroup {
hostgroup_name mac-servers
alias MAC OSX Servers
members MacOSX
}Code: Select all
define hostgroup {
hostgroup_name mac-servers
alias MAC OSX Servers
members CentOS6-NRPE,MacOSX,Win2008-NSClient,Windows7
}Code: Select all
cd /usr/local/nagiosxi/scripts
./reconfigure_nagios.shNote: It didn't make any sense to add these members there... it's just an example.
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!
Re: Script for adding or removing hosts in a hostgroup
Thanks for confirming Ludmill. It will probably take some time to complete it, but I think it will definitely be worth it. Manually updating hostgroups is such a pain. I will work the other way around though and update the host cfg file, not the hostgroup cfg file. Please leave this thread open for a while. I'll update the thread when I get some progress in it and I might need some advice for it.
Thanks.
Willem
Thanks.
Willem
Nagios XI 5.8.1
https://outsideit.net
https://outsideit.net
Re: Script for adding or removing hosts in a hostgroup
No problem. I will keep this thread open. Keep us updated.
Be sure to check out our Knowledgebase for helpful articles and solutions!
Re: Script for adding or removing hosts in a hostgroup
That's the method I use. We have a database with hosts and service categories. I wrote a script that pulls the list of hosts and service categories from our database, and uses a lookup table to match the service categories (nasty names with spaces, punctuation, etc...) to nagios hostgroups. Then I read the hostgroups.cfg file and rebuild it with any changes from the database and store the result in the import directory and trigger a reconfig.
Note: I only change the hostgroups.cfg file, so any hostgroups configured in individual host config files are preserved.
Removing a host from the external database results in it's hostgroups.cfg entries being removed, which automatically removes the nagios checks based on hostgroup membership. This allows users to make changes in the external database and trigger a change in nagios without having to configure them as nagios users and giving them access to the CCM. Since the script does a reconfig, I only run it a couple of times a day via cron, but so far it's saved me a lot of work.
Note: I only change the hostgroups.cfg file, so any hostgroups configured in individual host config files are preserved.
Removing a host from the external database results in it's hostgroups.cfg entries being removed, which automatically removes the nagios checks based on hostgroup membership. This allows users to make changes in the external database and trigger a change in nagios without having to configure them as nagios users and giving them access to the CCM. Since the script does a reconfig, I only run it a couple of times a day via cron, but so far it's saved me a lot of work.
Re: Script for adding or removing hosts in a hostgroup
Thanks for the info, jwelch! Do you mind sharing the script with other users on the forum?
Be sure to check out our Knowledgebase for helpful articles and solutions!
Re: Script for adding or removing hosts in a hostgroup
I'll see what I can salvage from the script and clean it up. The external DB code is worthless
since it's accessing our DB using an internal API, but the rest might be usable.
It was written using the 'get it done....get it done now...is it done yet?' method of coding
so it's pretty ugly, but I'll see what i can do.
since it's accessing our DB using an internal API, but the rest might be usable.
It was written using the 'get it done....get it done now...is it done yet?' method of coding
so it's pretty ugly, but I'll see what i can do.
-
scottwilkerson
- DevOps Engineer
- Posts: 19396
- Joined: Tue Nov 15, 2011 3:11 pm
- Location: Nagios Enterprises
- Contact:
Re: Script for adding or removing hosts in a hostgroup
Thanks again!