Page 1 of 1

Bulk HostGoup Cloning

Posted: Fri Apr 14, 2017 3:14 pm
by nagwindmon
hello,
I thought I read it somewhere that it is now an option to do so in CCM but cant find it, my Nagios XI ver 5.4.3
Or do I have to build configuration files, then import them back into XI?

Re: Bulk HostGoup Cloning

Posted: Mon Apr 17, 2017 9:01 am
by avandemore
What is it you are trying to do exactly? You can simply copy a host group in the CCM if that is what you are trying to do.

Re: Bulk HostGoup Cloning

Posted: Mon Apr 17, 2017 9:19 am
by nagwindmon
That is pretty much what I'm looking for... so where in CCM I can copy and create 50+ new ones based off existing one?

Re: Bulk HostGoup Cloning

Posted: Mon Apr 17, 2017 10:03 am
by lmiltchev
I don't believe we have a GUI tool that would clone hostgroup multiple times, however you could accomplish this by running some commnads/scripts from the CLI. You could create your configs via some kind a scripting solution in the "/usr/local/nagios/etc/import" directory, and then run:

Code: Select all

cd /usr/local/nagiosxi/scripts
./reconfigure_nagios.sh
Note: You need to make sure that the configs, dropped in the "import" directory are valid (there are no config errors).

Another way (preferred) would be to use the new REST API in Nagios XI. You can run multiple commands ( i.e. via copy/paste or by placing them in a bash script) to add bunch of hostgroups.

Here's an example of adding 3 hostgroups from the command line via the REST API (HG1, HG2, and HG3). All 3 hostgorups have only one member - localhost. In your case, you could have multiple, comma separated members.

Code: Select all

[root@main-nagios-xi libexec]# curl -XPOST "http://x.x.x.x/nagiosxi/api/v1/config/hostgroup?apikey=LTltbjobR0X3V5ViDIitYaI8hjsjoFBaOcWYukamF7oAsD8lhJRvSPWq8I3PjTf7&pretty=1" -d "hostgroup_name=HG1&alias=HG1&members=localhost"
{
    "success": "Successfully added HG1 to the system. Config imported but not yet applied."
}
You have mail in /var/spool/mail/root
[root@main-nagios-xi libexec]# curl -XPOST "http://x.x.x.x/nagiosxi/api/v1/config/hostgroup?apikey=LTltbjobR0X3V5ViDIitYaI8hjsjoFBaOcWYukamF7oAsD8lhJRvSPWq8I3PjTf7&pretty=1" -d "hostgroup_name=HG2&alias=HG2&members=localhost"
{
    "success": "Successfully added HG2 to the system. Config imported but not yet applied."
}
[root@main-nagios-xi libexec]# curl -XPOST "http://x.x.x.x/nagiosxi/api/v1/config/hostgroup?apikey=LTltbjobR0X3V5ViDIitYaI8hjsjoFBaOcWYukamF7oAsD8lhJRvSPWq8I3PjTf7&pretty=1" -d "hostgroup_name=HG3&alias=HG3&members=localhost&applyconfig=1"
{
    "success": "Successfully added HG3 to the system. Config applied, Nagios Core was restarted."
}
example01.PNG
Note: Notice that I used "&applyconfig=1" only in the last command. I would recommend trying this out with just a few hosgroups first to see how it is done (before trying to create 50 hosgroups in bulk). To view the command that you need to use, go to the Help menu in Nagios XI, click on "Config Reference" on the left, then click on "POST config/hostgroup" link on the right.

Hope this helps.

Re: Bulk HostGoup Cloning

Posted: Tue Apr 18, 2017 3:23 pm
by nagwindmon
works great, thanks lmiltchev!
so hostgroups have been added and now what would be a proper syntax for API to add our hosts to each created groups?
I would assume I need at least following properties to fill in:
Name
Alias
Address
Description
HostGroups
...
Can I accomplish this task of adding hosts in bulk utilizing API as well?

Re: Bulk HostGoup Cloning

Posted: Tue Apr 18, 2017 5:04 pm
by avandemore
The API is not suitable for every task at the moment. To find what capabilities exist in the version you are using you can refer to:

XI > Help > REST API Docs

Capabilities vary a bit by version so this is the best place to check.

Re: Bulk HostGoup Cloning

Posted: Tue Apr 18, 2017 5:05 pm
by dwhitfield
While @avandemore is correct, this is the example we give at http://YOURIP/nagiosxi/help/api-config-reference.php
curl -XPOST "http://YOURIP/nagiosxi/api/v1/config/ho ... V&pretty=1" -d "host_name=testapihostapply&address=127.0.0.1&check_command=check_ping\!3000,80%\!5000,100%&max_check_attempts=2&check_period=24x7&contacts=nagiosadmin&notification_interval=5&notification_period=24x7&applyconfig=1"

If you go to http://YOURIP/nagiosxi/help/api-config-reference.php you'll see
** All normal host directives are able to be used like normal core config definition files.
Thus,
Name
Alias
Address
Description
HostGroups

should all work. Basically, you just do directive=value&nextdirective=nextvalue

Please let us know if you run into any issues.

Re: Bulk HostGoup Cloning

Posted: Wed Apr 26, 2017 1:05 pm
by nagwindmon
what command to use in API call when I just need to Apply config to take place?

curl -XPOST "http://x.x.x.x/nagiosxi/api/v1/config/? ... lyconfig=1"

Re: Bulk HostGoup Cloning

Posted: Wed Apr 26, 2017 2:14 pm
by dwhitfield
That's in the next link down on the help menu. The direct link is: http://YOURIP/nagiosxi/help/api-system-reference.php

Obviously, I can't give the exact command because I don't know your API key.