Export VMware data directly into Nagios

I ran into an issue the yesterday.  In our environment there is NO budget for deploying an enterprise class monitoring solution.  In this case the team had chosen to deploy Nagios.  Nagios is a pretty robust tool, but limited in what you can do on the web interface.  In addtion we had 200+ VM's that had yet to be added.  There are some handy web UI tools to manage the configuration scripts, but who has time for that.  I whipped up this handy PowerCLI script to pull all the live VM data from VCenter and export that data into a .CFG file ready for Nagios Core to use.  Feel free to download and share.

You can copy and paste this right into your powerCLI interface once you connect to your VI Server.

#Export VM Info into Nagios Ready Configuration Files

$VMInfo = Get-Cluster "BladeServers" | get-vm | Select Name, @{N="IP Address";E={@($_.guest.IPAddress[0])}},PowerState
New-Item c:\temp\windowsVms.cfg -ItemType file
foreach ($vm in $VMInfo){
$os = get-vm $vm.name | get-view -property @("Guest.GuestFullName") | select -property {$_.Guest.GuestFullName}
$os = [string]$os
$os = $os.TrimEnd("}"," ")
$os = $os.substring(25)
$servername = [string]$vm.name
$servername = $servername -replace '[\W]', ''
$serverIP = [string]$vm."IP Address"

if ($vm."IP Address" -ne $null){
if ($vm.Powerstate -eq "PoweredOn"){
if ($os -like "*Windows*"){
write-host $vm.name $vm."IP Address" $vm.PowerState $os
Add-Content -Encoding Ascii c:\temp\windowsVms.cfg "define host{"
Add-Content -Encoding Ascii c:\temp\windowsVms.cfg "use windows-server"
Add-Content -Encoding Ascii c:\temp\windowsVms.cfg "host_name       $servername"
Add-Content -Encoding Ascii c:\temp\windowsVms.cfg "alias           $servername"
Add-Content -Encoding Ascii c:\temp\windowsVms.cfg "address   $serverIP"
Add-Content -Encoding Ascii c:\temp\windowsVms.cfg "}"
}
}
}
}
Add-Content -Encoding Ascii c:\temp\windowsVms.cfg "# END OF FILE"

New-Item c:\temp\LinuxVms.cfg -ItemType file
foreach ($vm in $VMInfo){
$os = get-vm $vm.name | get-view -property @("Guest.GuestFullName") | select -property {$_.Guest.GuestFullName}
$os = [string]$os
$os = $os.TrimEnd("}"," ")
$os = $os.substring(25)
$servername = [string]$vm.name
$servername = $servername -replace '[\W]', ''
$serverIP = [string]$vm."IP Address"

if ($vm."IP Address" -ne $null){
if ($vm.Powerstate -eq "PoweredOn"){
if ($os -like "*Linux*"){
write-host $vm.name $vm."IP Address" $vm.PowerState $os
Add-Content -Encoding Ascii c:\temp\LinuxVms.cfg "define host{"
Add-Content -Encoding Ascii c:\temp\LinuxVms.cfg "use windows-server"
Add-Content -Encoding Ascii c:\temp\LinuxVms.cfg "host_name       $servername"
Add-Content -Encoding Ascii c:\temp\LinuxVms.cfg "alias           $servername"
Add-Content -Encoding Ascii c:\temp\LinuxVms.cfg "address   $serverIP"
Add-Content -Encoding Ascii c:\temp\LinuxVms.cfg "}"
}
}
}
}
Add-Content -Encoding Ascii c:\temp\LinuxVms.cfg "# END OF FILE"


Once the commands have ran, there will be two Nagios configuration files:  LinuxVms.cfg and windowsVms.cfg.  Add these to your /usr/local/nagios/etc/objects folder, and define them in the nagios.cfg file, then restart nagios.

Also you will need to adjust your template name to match, in our case our template.cfg has a configuration item for "windows-servers", which specify how often to check for outages and what commands to run.

Comments

Popular posts from this blog

Integrate Choco with SCCM

Windows 11 22H2 production setup!

Automate Server Patching with Puppet (Part 2)