How To: Duplicating A Service Check To Multiple Hosts

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
Locked
User avatar
Box293
Too Basu
Posts: 5126
Joined: Sun Feb 07, 2010 10:55 pm
Location: Deniliquin, Australia
Contact:

How To: Duplicating A Service Check To Multiple Hosts

Post by Box293 »

Overview
This is a guide on how to add the same service to multiple hosts. Let’s say you’ve decided you want to monitor the Windows Page File Usage on all of your existing windows hosts.
  • You have already configured one of your existing hosts with this service check
  • This existing service is how you want all of your other hosts configured
There are two different ways to accomplish this:
1) Using Host Groups
2) Using the XI Config Import Prep Tool

In both examples the following will apply:
  • Existing “Template” Host: testhost
  • Existing “Template” Service: Page File Usage
  • Existing Destination Host #1: server01.mydomain.local
  • Existing Destination Host #2: server02.mydomain.local
  • Existing Destination Host #3: server03.mydomain.local
  • Existing Destination Host #4: server04.mydomain.local
Using Host Groups
Summary
Nagios XI allows you to create a service that "runs" on a host group instead of a host. When used this way, all the members of that group have this service check applied.

Pros
  • Super Quick
  • All hosts can have this service configuration changed and it would be applied to all hosts in one step
  • Great for applying a standard
Cons
  • ANY change to this service and it would be applied to all hosts in one step
  • If you wanted to customise this service for any host you would need to a)Remove the Host from the host group, b)Duplicate the service and apply it directly to the host
Create The Host Group
This group will contain all the members that you want the service check to run on.

Open Nagios XI and login as nagiosadmin
  • Click the Configure link on the blue bar
  • Click the Core Configuration Manager (CCM) link on the left
  • Login to CCM as nagiosadmin
  • Under the Monitoring menu click Host Groups
  • Click the Add New button
  • Host group name: host_template_services
  • Description: Hosts that use a template service
  • Click the Members list Modify icon
  • Add the hosts server01.mydomain.local, server02.mydomain.local, server03.mydomain.local and server04.mydomain.local to the Selected pane
  • Click the Save button
  • Click the Apply Configuration button
  • Wait while the changes are applied
  • If all goes OK you will receive a Success message
Create The Service
I already have the service Page File Usage defined on testhost. We'll make a copy of this to use as our service for the host group.
  • Under the Monitoring menu click Services
  • Use the Config name filter to select testhost
  • Click the Copy icon for the Page File Usage service
  • Use the Config name filter to select testhost (1)
  • Click the Modify icon for the Page File Usage service
  • Config Name: standard_page_file_usage
  • Click the Hosts list Modify icon
  • Move the host testhost from the Selected pane to the Available pane
  • Click the Save button
  • Click the Host Groups list Modify icon
  • Move the group host_template_services from the Available pane to the Selected pane
  • Click the Save button
  • Tick the Active box
  • Click the Save button
  • Click the Apply Configuration button
  • Wait while the changes are applied
  • If all goes OK you will receive a Success message
  • Click the Home link
  • Click the Service Detail link
In a couple of minutes a Page File Usage service should start appearing Service Detail window for each host that we selected earlier.
Note: You can speed this screen update by restarting the Monitoring Engine

You will also notice that when you view each hosts services there will NOT be a Page File Usage service.

That completes this section Using Host Groups to add the same service to multiple hosts.


Using the XI Config Import Prep Tool
Summary
If you wanted to specifically have a service created for each host then this could become a tedious task with lots of hosts. This is a guide on how to help you automate this process.

Pros
  • Each host has a service created for Page File Usage
  • Great when your service checks require customisations that are specific to each host
  • Takes about 15 minutes to complete the task for xxx amount of hosts
Cons
  • Takes about 15 minutes to complete the task (not that long when you consider the manual option)
  • Not the best way to apply a standard
Requirements
This guide has been created using a Windows 7 PC and hence some additional windows utilities may need to be downloaded. When you need to edit config files listed in the steps below, Notepad will suffice for this.

This guide is based on a running virtual machine of Nagios XI downloaded from the Nagios Enterprise website. If you have your own Nagios XI installation on another Linux distribution and I’m sure if you follow these steps you’ll work out how to adapt it to your installation.

WinSCP is used to transfer the service config file we create into your Nagios XI server http://winscp.net/eng/download.php.

Putty is used to remotely access your Nagios XI server http://www.chiark.greenend.org.uk/~sgtatham/putty/.

WinSCP and Putty
The first time you connect to the Nagios XI server it will present a warning dialog box about the Nagios XI host not being in the local cache, click Yes and you will not be prompted again.
All the steps I use with WinSCP are done using the Commander view. To enable this do the following:
  • Click Options > Preferences
  • Click Interface on the left menu
  • Select the Commander radio button
  • Click OK
Tip: A lot of the steps in this guide can be copied and pasted into Putty / WinSCP, this prevents typing mistakes.

Obtain Original Config File
Using WinSCP we will retrieve the existing service definition Page File Usage for testhost
Open WinSCP
  • Hostname: IP address of your Nagios XI server
  • Username: root
  • Password: Your root Password (nagiosxi by default)
  • Click the Login button
  • Click in the right pane
  • Click the Open directory/bookmark icon [Remote > Goto > Open directory/bookmark]
  • In the remote directory type in /usr/local/nagios/etc/services
  • Click OK
  • In the left pane browse to C:\Temp (create this directory if it does not exist)
  • Drag the testhost.cfg file from the right pane to the left pane
  • Click the Copy button
  • Wait for the file to copy
  • You can minimize WinSCP for the time being, we’ll use it again shortly
Create Import Config File
The following step can be automated using PowerShell and is explained in detail later on. Following the steps below will help you understand the process before you try to upscale it.
  • Open Windows Explorer to C:\Temp
  • Rename testhost.cfg to servicesimport.cfg
  • Edit servicesimport.cfg
  • If the original host testhost had multiple service definitions they will all appear in this file. You need to delete everything except the Page File Usage service. This is all that you should be left with (mine may differ to yours):

Code: Select all

define service {
host_name		testhost
	service_description		Page File Usage
	use		alert_service_24x7,check_service_24x7,xiwizard_windowsserver_nsclient_service
	check_command			check_xi_service_nsclient!!COUNTER!-l "\\Paging File(_Total)\\% Usage","Paging File usage is %.2f %%" -w 70 -c 90!!!!!
	_xiwizard		windowsserver
	register			1
	}	
NOTE: the check_command line and the line that follows it is all one continual line, _xiwizard is a new line
Make the following change
  • host_name server01.mydomain.local
  • Highlight all of the text and copy it
  • Paste it three times after the original service definition
Make the following changes on the second, third and fourth instance
  • host_name server02.mydomain.local
  • host_name server03.mydomain.local
  • host_name server04.mydomain.local
Your file should now look like this:

Code: Select all

define service {
host_name		server01.mydomain.local
	service_description		Page File Usage
	use		alert_service_24x7,check_service_24x7,xiwizard_windowsserver_nsclient_service
	check_command			check_xi_service_nsclient!!COUNTER!-l "\\Paging File(_Total)\\% Usage","Paging File usage is %.2f %%" -w 70 -c 90!!!!!
	_xiwizard		windowsserver
	register			1
	}	

define service {
host_name		server02.mydomain.local
	service_description		Page File Usage
	use		alert_service_24x7,check_service_24x7,xiwizard_windowsserver_nsclient_service
	check_command			check_xi_service_nsclient!!COUNTER!-l "\\Paging File(_Total)\\% Usage","Paging File usage is %.2f %%" -w 70 -c 90!!!!!
	_xiwizard		windowsserver
	register			1
	}	

define service {
host_name		server03.mydomain.local
	service_description		Page File Usage
	use		alert_service_24x7,check_service_24x7,xiwizard_windowsserver_nsclient_service
	check_command			check_xi_service_nsclient!!COUNTER!-l "\\Paging File(_Total)\\% Usage","Paging File usage is %.2f %%" -w 70 -c 90!!!!!
	_xiwizard		windowsserver
	register			1
	}	

define service {
host_name		server04.mydomain.local
	service_description		Page File Usage
	use		alert_service_24x7,check_service_24x7,xiwizard_windowsserver_nsclient_service
	check_command			check_xi_service_nsclient!!COUNTER!-l "\\Paging File(_Total)\\% Usage","Paging File usage is %.2f %%" -w 70 -c 90!!!!!
	_xiwizard		windowsserver
	register			1
	}	
Save and close the file servicesimport.cfg

Prepare The Configuration Files
Return to your WinSCP session
  • Click the Open directory/bookmark icon [Remote > Goto > Open directory/bookmark]
  • In the remote directory type in /usr/local/nagios/etc
  • Click OK
  • Press F7
  • Type cfgprep
  • Click OK
  • Double click cfgprep
  • Press F7
  • Type orig
  • Click OK
  • Double click orig
  • In the left pane browse to C:\Temp
  • Drag the servicesimport.cfg file from the left pane to the right pane
  • Click the Copy button
  • Wait for the file to copy
You can close WinSCP

Start a Putty session that connects to your Nagios XI server
  • Username: root
  • Password: Your root Password (nagiosxi by default)
  • Type cd /usr/local/nagios/etc/cfgprep/ and press Enter
  • Type php /usr/local/nagiosxi/tools/xiprepimport.php orig/servicesimport.cfg and press Enter
  • Type ls -al and press Enter

    You should see results like this:
  • -rw-r--r-- 1 root root 347 Apr 25 22:10 server01.mydomain.local.cfg
  • -rw-r--r-- 1 root root 347 Apr 25 22:10 server02.mydomain.local.cfg
  • -rw-r--r-- 1 root root 347 Apr 25 22:10 server03.mydomain.local.cfg
  • -rw-r--r-- 1 root root 347 Apr 25 22:10 server04.mydomain.local.cfg
    These are the configuration files that have been created to import into Nagios XI
Terminate your putty session

Import The Configuration Files
Open Nagios XI and login as nagiosadmin
  • Click the Configure link on the blue bar
  • Click the Core Configuration Manager (CCM) link on the left
  • Login to CCM as nagiosadmin
  • Click the menu Tools and then Import Config Files

    You should now see your new files listed in the import file list
  • Hold down CTRL and click:
    /usr/local/nagios/etc/cfgprep/server01.mydomain.local.cfg
    /usr/local/nagios/etc/cfgprep/server02.mydomain.local.cfg
    /usr/local/nagios/etc/cfgprep/server03.mydomain.local.cfg
    /usr/local/nagios/etc/cfgprep/server04.mydomain.local.cfg
  • Click the Import button
    If all goes well it will display the message underneath the Import button

    Code: Select all

    Entry config_name::server01.mydomain.local inside tbl_service successfully inserted
    Entry config_name::server02.mydomain.local inside tbl_service successfully inserted
    Entry config_name::server03.mydomain.local inside tbl_service successfully inserted
    Entry config_name::server04.mydomain.local inside tbl_service successfully inserted
  • In the Tools menu on the left click Write Config Files
  • Click the Go button for Write monitoring data
    Wait while it performs the action, if all is OK it will report success
  • Click the Go button for Write additional data
    Wait while it performs the action, if all is OK it will report success
  • Click the Go button for Check configuration files
    Wait while it performs the action
    Depending on your existing config, there may be lots of warnings, this is OK
    Total Errors should be 0
    Example:

    Code: Select all

    Total Warnings: 207 
    Total Errors: 0 
  • In the Quick Tools menu on the left click Apply Configuration
  • Click the Apply Configuration button
  • Wait while the changes are applied
  • If all goes OK you will receive a Success message
This completes the import process.

Check Service Definitions
  • In the Monitoring menu on the left click Services
  • Use the Config name filter to select server01.mydomain.local
  • Here you should see the new Page File Usage service
  • Check the other hosts to confirm they all exist
  • After a couple of minutes they should start appearing in Service Detail window (access this from the Home link)
Delete Temporary Files
Some files were created during the import process and are no longer required. In this example, the following files were created and should be deleted:
  • /usr/local/nagios/etc/cfgprep/orig/servicesimport.cfg
  • /usr/local/nagios/etc/cfgprep/servicesimport.cfg
  • /usr/local/nagios/etc/cfgprep/server01.mydomain.local.cfg
  • /usr/local/nagios/etc/cfgprep/server02.mydomain.local.cfg
  • /usr/local/nagios/etc/cfgprep/server03.mydomain.local.cfg
  • /usr/local/nagios/etc/cfgprep/server04.mydomain.local.cfg
Use PowerShell To Automate The Creation Of servicesimport.cfg
This section will explain how to use PowerShell to speed up the creation of the servicesimport.cfg file. This will save lots of time if you have a gazillion hosts :twisted:

Create A List Of Hosts
Start a Putty session that connects to your Nagios XI server
  • Username: root
  • Password: Your root Password (nagiosxi by default)
  • Type cd /tmp and press Enter
  • Type ls /usr/local/nagios/etc/hosts -1 >hostlist.csv and press Enter
You can minimize Putty for the time being, we’ll use it again shortly

Open WinSCP
  • Hostname: IP address of your Nagios XI server
  • Username: root
  • Password: Your root Password (nagiosxi by default)
  • Click the Login button
  • Click in the right pane
  • Click the Open directory/bookmark icon [Remote > Goto > Open directory/bookmark]
  • In the remote directory type in /tmp
  • Click OK
  • In the left pane browse to C:\Temp
  • Drag the hostlist.csv file from the right pane to the left pane
  • Click the Copy button
  • Wait for the file to copy
You can minimize WinSCP for the time being, we’ll use it again shortly

Edit The Host List
  • Browse to C:\Temp
  • Double click hostlist.csv to open it Notepad
  • Press Ctrl + H
  • Find what: .cfg
  • Replace with: Leave this field empty
  • Click the Replace All button
  • Click the Cancel button
  • Remove any hosts that you DO NOT want the new service to be applied to (like localhost)
  • Go to the very beginning of the file
  • Add this line so it is the first line of the file
  • hostname
  • Save the file and Close Notepad
Create The PowerShell Script
  • Open Notepad
  • Paste the following into Notepad

Code: Select all

$ConfirmPreference = "None"
$list = Import-Csv "C:\Temp\hostlist.txt"
$ServicesImportFile = "C:\Temp\servicesimport.cfg"
Remove-Item	$ServicesImportFile
foreach($entry in $list)
{
Write-Output "define service {" | Out-File $ServicesImportFile -Append -Encoding ASCII
Write-Output "	host_name			$($entry.hostname)" | Out-File $ServicesImportFile -Append -Encoding ASCII
Write-Output "	service_description		Page File Usage" | Out-File $ServicesImportFile -Append -Encoding ASCII
Write-Output "	use				alert_service_24x7,check_service_24x7,xiwizard_windowsserver_nsclient_service" | Out-File $ServicesImportFile -Append -Encoding ASCII
Write-Output "	check_command			check_xi_service_nsclient!!COUNTER!-l `"\\Paging File(_Total)\\% Usage`",`"Paging File usage is %.2f %%`" -w 70 -c 90!!!!!" | Out-File $ServicesImportFile -Append -Encoding ASCII
Write-Output "	_xiwizard			windowsserver" | Out-File $ServicesImportFile -Append -Encoding ASCII
Write-Output "	register			1" | Out-File $ServicesImportFile -Append -Encoding ASCII
Write-Output "	}" | Out-File $ServicesImportFile -Append -Encoding ASCII
Write-Output "" | Out-File $ServicesImportFile -Append -Encoding ASCII
}
NOTE 1: The above service definition was taken from when we obtained the original configuration
NOTE 2: Some additional characters have been added
  • Whenever a quotation mark (") is used [inside of quotation marks] an escape character (`) will precede it
  • What this does it prevents PowerShell from mistaking the quotation mark as the end of the output
  • Specifically:
    Write-Output " check_command check_xi_service_nsclient!!COUNTER!-l `"\\Paging File(_Total)\\% Usage`",`"Paging File usage is %.2f %%`" -w 70 -c 90!!!!!"
  • The quotation marks at the begining and end DO NOT get an escape character (`)
  • Save this file as C:\Temp\CreateImportConfig.ps1
  • Close Notepad
CreateImportConfig.ps1.zip
Use PowerShell To Create The Import Config File
Run PowerShell as Administrator
  • Type cd \temp and press Enter
  • Type Set-ExecutionPolicy -ExecutionPolicy Bypass and press Enter
  • Press Enter to accept the default answer of Y
    Note: If you are not running as Administrator you will get an access denied error message
  • Type .\CreateImportConfig.ps1 and press Enter
  • The script will run relatively quickly and you will end up with a new file called C:\Temp\servicesimport.cfg
  • Type notepad C:\Temp\servicesimport.cfg and press Enter
    Here you will see the contents of the services that you are going to create in Nagios XI.
  • Close Notepad
Close PowerShell

Continue on following the steps in the section Prepare The Configuration Files to complete the import process.

How Can I Use The CentOS ShellTo Automate The Creation Of servicesimport.cfg
Unfortunately my linux experience is minimal; I would appreciate someone telling me how so I could include it in this guide.

Other Notes
The flexibility of Nagios XI is what makes applying configurations in bulk a less time consuming task. This process can easily be applied to create hosts, groups, etc.

The key thing to know is explained on the Import Config Files screen:
  • To prevent errors or misconfigurations, you should import your configurations in an useful order. We recommend to do it like this:
    commands -> timeperiods -> contacttemplates -> contacts -> contactgroups -> hosttemplates -> hosts -> hostgroups -> servicetemplates -> services -> servicegroups
If you have any problems following these steps or have any feedback please let me know.
You do not have the required permissions to view the files attached to this post.
Last edited by Box293 on Tue May 25, 2010 8:08 pm, edited 2 times in total.
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.
mmestnik
Posts: 972
Joined: Mon Feb 15, 2010 2:23 pm

Re: How To: Duplicating A Service Check To Multiple Hosts

Post by mmestnik »

I'm a bit confused by this.

It would be nice to have explained why this is better(perhaps also list the cons) to using a host group. I didn't fully read the instructions, I stopped when I read far enough that it had previously become more difficult then doing it this way:

1. Create a host group and add all the hosts into it... If you have several hundred or thousands then I'd bet you have a list somewhere, in that case you just make one file and no need to export anything with a rough draft of a hostgroup and import it. For the first import I wouldn't even worry too much about being %100, if you leave out 10 or 20 hosts you can add them later. For import a hostgroup the part you want is the hard part, the name and other options can be set later in the config manager.
2. Create a service for this host group and don't add any hosts to it... unless you want to and you think you know what you are doing.
3. Done.
User avatar
Box293
Too Basu
Posts: 5126
Joined: Sun Feb 07, 2010 10:55 pm
Location: Deniliquin, Australia
Contact:

Re: How To: Duplicating A Service Check To Multiple Hosts

Post by Box293 »

Wow, I had no idea it worked like this. I keep discovering ways in which this program works that fascinate me.

Clearly at times I have no idea how to use this product. Can you point me to the Nagios XI User Manual that explains these three simple steps / concepts. I am very keen to learn how to use the product effectively and how I could have learnt what you have explained.

I will update the post later so it reflects your simpler procedure.
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.
User avatar
Box293
Too Basu
Posts: 5126
Joined: Sun Feb 07, 2010 10:55 pm
Location: Deniliquin, Australia
Contact:

Re: How To: Duplicating A Service Check To Multiple Hosts

Post by Box293 »

mmestnik,
I've updated the content with your suggestions, it makes for a great guide. :D

Let me know if there is anything else that could be improved.

Thanks
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.
mmestnik
Posts: 972
Joined: Mon Feb 15, 2010 2:23 pm

Re: How To: Duplicating A Service Check To Multiple Hosts

Post by mmestnik »

Nagios Core Version 3.x Documentation

http://support.nagios.com/knowledgebase/officialdocs
Specifically the part about service object definitions. You should give the whole Object Definitions page a read, then Standard Macros in Nagios. After that the rest should seam boring, but if you get stuck on something that's what you should read.
Documentation Author wrote:Directive Descriptions:
host_name: This directive is used to specify the short name(s) of the host(s) that the service "runs" on or is associated with. Multiple hosts should be separated by commas.
hostgroup_name: This directive is used to specify the short name(s) of the hostgroup(s) that the service "runs" on or is associated with. Multiple hostgroups should be separated by commas. The hostgroup_name may be used instead of, or in addition to, the host_name directive.
Locked