We have a dynamic environment, and we want to script the import of hosts and hostgroups nightly. The first part of the process is to generate and stage an appropriately formated .cfg file to /usr/local/nagios/etc/cfgprep/orig/. We know how to do this.
We would next run php /usr/local/nagiosxi/tools/xiprepimport.php against the file in /orig. No problem there.
The next steps involve invoking, by script, (not webUI clicks) the import and write configs found in the web UI. I'm referring to documentation - https://answerhub.nagios.com/support/s/ ... I-c854cfdd.
1) The next step in the webUI is to manually execute "Import Config Files". But what php or .sh is called by the "Import Config Files" webUI item?
2) Next is "Write Configs" -- so what php or .sh is called by clicking "Write Configs"?
3) Next, we'll run nagios -v against the file for the "Verify Files" step.
4) If that comes back clean, we cycle the nagios service, and we'll have the nightly hosts and hostgroups in effect.
Thanks much for any insights.
RESOLVED - Automated config importing
Re: What scripts are called from the XI menu items "Import Config Files" and "Config File Management"?
For programmatically preparing configs, you can follow this:
Using-The-Nagios-XI-Config-Import-Prep-Tool
You then can run the aforementioned script
Unfortunately, Nagios XI isn't running an external script to import configs, so you will have to construct your own. You will likely have to construct a PHP script that includes /usr/local/nagiosxi/html/includes/common.inc.php and /usr/local/nagiosxi/html/includes/components/ccm/ccm.inc.php to initialize the CCM, then use the import_configs function from /usr/local/nagiosxi/html/includes/components/ccm/includes/admin_views.inc.php.
This method may not work as there is security around the CCM, but it might be worth a shot.
If this method does work, you should take heed of the following from the Nagios XI import configs page.
You could also try something using the Nagios XI API and that would likely be a more "proper" way to accomplish your goal.
Using-The-Nagios-XI-Config-Import-Prep-Tool
The files should be located in /usr/local/nagios/etc/cfgprep/origThe script can be run from a shell prompt using the following syntax:
php /usr/local/nagiosxi/tools/xiprepimport.php sourceconfigfile
where sourceconfigfile is the path to the source configuration file. Note: the source config file cannot
reside in the current working directory.
Code: Select all
mkdir -p /usr/local/nagios/etc/cfgprep/orig
Code: Select all
for config_file in /usr/local/nagios/etc/cfgprep/orig/*.cfg; do
if [ -f "$config_file" ]; then
php /usr/local/nagiosxi/tools/xiprepimport.php "$config_file"
fi
done
Unfortunately, Nagios XI isn't running an external script to import configs, so you will have to construct your own. You will likely have to construct a PHP script that includes /usr/local/nagiosxi/html/includes/common.inc.php and /usr/local/nagiosxi/html/includes/components/ccm/ccm.inc.php to initialize the CCM, then use the import_configs function from /usr/local/nagiosxi/html/includes/components/ccm/includes/admin_views.inc.php.
This method may not work as there is security around the CCM, but it might be worth a shot.
If this method does work, you should take heed of the following from the Nagios XI import configs page.
To prevent errors or misconfigurations, you should import your configurations in an useful order. We recommend importing in the following order:
Commands -> Time Periods -> Contact Templates -> Contacts -> Contact Groups -> Host Templates -> Hosts -> Host Groups -> Service Templates -> Services -> Service Groups
You could also try something using the Nagios XI API and that would likely be a more "proper" way to accomplish your goal.
Actively advancing awesome answers with ardent alliteration, aptly addressing all ambiguities. Amplify your acumen and avail our amicable assistance. Eagerly awaiting your astute assessments of our advice.
Re: What scripts are called from the XI menu items "Import Config Files" and "Config File Management"?
Thanks very much @bbahn, for the xiprepimport code. Yes, I've been turned on to the API thru another source, as well.
Question on the API - do you know if I must still xiprepimport the files before a PUT with the API, or does the API take care of that?
Fortunately the only things that are highly dynamic to be imported nightly are hosts and hostgroups, so will import only those, in that order.
Question on the API - do you know if I must still xiprepimport the files before a PUT with the API, or does the API take care of that?
Fortunately the only things that are highly dynamic to be imported nightly are hosts and hostgroups, so will import only those, in that order.
Re: What scripts are called from the XI menu items "Import Config Files" and "Config File Management"?
gregbeyer,
I don't believe it is necessary to pre-process the config files. However, as the document states - the goal is to output config files that are more manageable by the XI interface. If I was attempting this, I would try to run the script then operate on the output. This should additionally give you the option of cancelling import if outputs any errors.
Best Regards,
Cory Norell
I don't believe it is necessary to pre-process the config files. However, as the document states - the goal is to output config files that are more manageable by the XI interface. If I was attempting this, I would try to run the script then operate on the output. This should additionally give you the option of cancelling import if
Code: Select all
xiprepimport.phpBest Regards,
Cory Norell
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.
Re: What scripts are called from the XI menu items "Import Config Files" and "Config File Management"?
Closing the loop, I was successful in using the API for import. And I was able to import a monolithic hosts.cfg file, which was created from a query of our hosts database. Note that the content-type, --data-binary, and @<path to file> are key for importing a file containing configs. As opposed to importing host definition directives as shown in the API documentation. Done as follows:
1. Stage the hosts.cfg to /usr/local/nagios/etc/cfgprep/orig/hosts.cfg
2. Prep Import the file to ensure format is good: sudo php /usr/local/nagiosxi/tools/xiprepimport.php /usr/local/nagios/etc/cfgprep/orig/hosts.cfg
3. Validated version of hosts.cfg is generated in /usr/local/nagios/etc/cfgprep/
4. Import the file: sudo curl -XPOST "http://<fqdn>/nagiosxi/api/v1/config/import?apikey=<--obfuscate-->&overwrite=1" -v -H "Content-Type: text/xml" --data-binary "@/usr/local/nagios/etc/cfgprep/hosts.cfg"
5. Pause script if you have a large number of hosts to import. For my 6K import, I allow 6 minutes. But that's a DEV VM -- YMMV.
6. Apply config: sudo curl -XPOST "http://<fqdn>/nagiosxi/api/v1/system/applyconfig?apikey=<--obfuscate-->&pretty=1"
Thanks everyone for your feedback on the correct way to accomplish config import.
1. Stage the hosts.cfg to /usr/local/nagios/etc/cfgprep/orig/hosts.cfg
2. Prep Import the file to ensure format is good: sudo php /usr/local/nagiosxi/tools/xiprepimport.php /usr/local/nagios/etc/cfgprep/orig/hosts.cfg
3. Validated version of hosts.cfg is generated in /usr/local/nagios/etc/cfgprep/
4. Import the file: sudo curl -XPOST "http://<fqdn>/nagiosxi/api/v1/config/import?apikey=<--obfuscate-->&overwrite=1" -v -H "Content-Type: text/xml" --data-binary "@/usr/local/nagios/etc/cfgprep/hosts.cfg"
5. Pause script if you have a large number of hosts to import. For my 6K import, I allow 6 minutes. But that's a DEV VM -- YMMV.
6. Apply config: sudo curl -XPOST "http://<fqdn>/nagiosxi/api/v1/system/applyconfig?apikey=<--obfuscate-->&pretty=1"
Thanks everyone for your feedback on the correct way to accomplish config import.
Re: RESOLVED - Automated config importing
Thanks for the update, @gregbeyer!
I’m glad to hear that you were successful, Your steps will be useful for others.
If you have any more questions or run into issues in the future, feel free to reach out!
I’m glad to hear that you were successful, Your steps will be useful for others.
If you have any more questions or run into issues in the future, feel free to reach out!
Re: RESOLVED - Automated config importing
Hi,
For a long time I use the next procedure to import config files:
Put the cfg file(s) on: /usr/local/nagios/etc/import
Run the script: /usr/local/nagiosxi/scripts/reconfigure_nagios.sh
This will import the config file(s) and do an apply.
For a long time I use the next procedure to import config files:
Put the cfg file(s) on: /usr/local/nagios/etc/import
Run the script: /usr/local/nagiosxi/scripts/reconfigure_nagios.sh
This will import the config file(s) and do an apply.