Event Handlers with Powershell

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
slansing
Posts: 7698
Joined: Mon Apr 23, 2012 4:28 pm
Location: Travelling through time and space...

Re: Event Handlers with Powershell

Post by slansing »

Great! We will keep it open, let us know.
User avatar
WillemDH
Posts: 2320
Joined: Wed Mar 20, 2013 5:49 am
Location: Ghent
Contact:

Re: Event Handlers with Powershell

Post by WillemDH »

In the meantime I got this working. This is really a major feature, as this means we can turn Nagios Xi into some Altiris-like software packet. As promised I'll post my current setup:

/usr/local/nagios/libexec/dig_nscp_install.sh

Code: Select all

#!/bin/bash

HOSTNAME=$1
VERSION=$(/usr/local/nagios/libexec/check_nrpe -H $HOSTNAME -c checkversion)
NOW=$(date '+%Y-%m-%d -- %H:%M')
LOGFILE=/var/log/dig-nscp-install.log

case $VERSION in
	"Connection refused by host"*)
		echo  "$NOW : Powershell script initiated to install version 0.4.1.101 on $HOSTNAME!" >> /var/log/dig-nscp-install.log
		/usr/local/nagios/libexec/check_nrpe -H <Windows gateway server> -t 300 -c install_nscp_0.4.101 -a $HOSTNAME
		;;
	"0,4,1,101 2013-05-18"*)
		echo  "$NOW : Version $VERSION already installed on $HOSTNAME! Installation aborted." >> /var/log/dig-nscp-install.log
		;;
	*)
		echo "$NOW : Some other problem. Installation aborted." >> /var/log/dig-nscp-install.log
		;;
			
esac
In nsclient.ini on the gateway server:

Code: Select all

[/settings/external scripts/scripts]
install_nscp_0.4.101=cmd /c echo scripts\Install_NSCP_0.4.1.101.ps1 $ARG1$; exit $LastExitCode | powershell.exe -command -
The installation script on the gateway server:

Code: Select all

param(
	[Parameter(Mandatory=$true)][string]$hostname
)

$sourcerepo="\\server\share"
$sourceremote="\\$hostname\c$\Nagios"
$loglocal="c:\Nagios\NagiosInstallLog.txt"
$logremote="\\$hostname\c$\Nagios\Nagioslog.txt"

if (!(Test-path -Path \\$hostname\c$\Nagios)) {
	New-Item -ItemType directory -Path \\$hostname\c$\Nagios
	$date = Get-Date -Format "yyyy-MM-dd hh:mm:ss"
	"$date : Directory created on $hostname" | Out-File -filepath $loglocal -Append
	"$date : Directory created on $hostname" | Out-File -filepath $logremote -Append
	}

robocopy $sourcerepo $sourceremote /copy:DAT /log:c:\Nagios\Robo_NSCP_0.4.1.101.log

$date = Get-Date -Format "yyyy-MM-dd hh:mm:ss"
"$date : Robocopy source files copied to $hostname" | Out-File -filepath $loglocal -Append

$InstallScript = "C:\Nagios\Install_NSCP-0.4.1.101.ps1"
$InstallString = "powershell.exe -File `"$InstallScript`""
$process = ([WMICLASS]"\\$hostname\ROOT\CIMV2:Win32_Process").Create($InstallString)


$date = Get-Date -Format "yyyy-MM-dd hh:mm:ss"
"$date : Install script on $hostname called from gateway" | Out-File -filepath $loglocal -Append
"$date : Install script on $hostname called from gateway" | Out-File -filepath $logremote -Append

And the install script copied to the server where the nscp agent needs to be installed:

Code: Select all

$loglocal = "C:\Nagios\Nagioslog.txt"
$os_arch = (Get-ItemProperty "hklm:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment").PROCESSOR_ARCHITECTURE
if ($os_arch.Contains("32") -or $os_arch.Contains("86")){
	$Installer = "NSCP-0.4.1.101-Win32.msi"
}
if ($os_arch.Contains("64")){
	$Installer = "NSCP-0.4.1.101-x64.msi"
}
$date = Get-Date -Format "yyyy-MM-dd hh:mm:ss"
"$date : OS Architecture checked: $installer will be used." | Out-File -filepath $loglocal -Append
$parameters = "/qn /norestart"
$successExitCodes = 0,3010
$scriptpath = split-path -parent $MyInvocation.MyCommand.Definition
$installerFull = "$scriptPath\$installer"
"$date : Installer $installerFull starting..." | Out-File -filepath $loglocal -Append
$process = Start-Process -FilePath msiexec -ArgumentList /i, $installerFull, $parameters -Wait -PassThru
$installerExitcode = $process.exitcode
$date = Get-Date -Format "yyyy-MM-dd hh:mm:ss"
sleep -Seconds 1
if ($successExitCodes -contains $InstallerExitcode){
	"$date : Installation $installer succeeded." | Out-File -filepath $loglocal -Append
	}
else{
	"$date : Installation $installer failed with exit code $installerExitcode." | Out-File -filepath $loglocal -Append
}
I also created a similar script to update the nsclient.ini fiel to the latest version.
Nagios XI 5.8.1
https://outsideit.net
slansing
Posts: 7698
Joined: Mon Apr 23, 2012 4:28 pm
Location: Travelling through time and space...

Re: Event Handlers with Powershell

Post by slansing »

Interesting, while a component addon to nagios xi that allows you to remotely install nsclient/ncpa this is intriguing:
I also created a similar script to update the nsclient.ini fiel to the latest version.
Is this used to convert a nsclient 3.9.x ini file to 4.x? Could you share it?
User avatar
WillemDH
Posts: 2320
Joined: Wed Mar 20, 2013 5:49 am
Location: Ghent
Contact:

Re: Event Handlers with Powershell

Post by WillemDH »

I'm waiting for Michael to release RC1 for 0.4.2. We already are at 0.4.1.101. I never used the 0.3 versions. The update nsclient.ini update script only replaces the current nsclient.ini script with a newer version. As we user real-time eventlog monitoring, the nsclient.ini file is constantly updated with new event id's and other locally running scripts. For 180 servers atm (has to grow to 600+) we only have 3 nsclient.ini file versions. One for QA / DV servers, one for production standalone and one for production cluster nodes. As I'm using a Powershell script that creates special cluster events with more information about recent cluster events, which are captured through NSCA to a passive service of cluster nodes. So we always know when something happened to a MS failover cluster.
I'm at home atm, but I'll share it after next week (got an advanced Powershell course in Brussels next week). Don't expect too much from it... :) it mainly just copies a file and then restarts the service...
Nagios XI 5.8.1
https://outsideit.net
User avatar
lmiltchev
Bugs find me
Posts: 13589
Joined: Mon May 23, 2011 12:15 pm

Re: Event Handlers with Powershell

Post by lmiltchev »

I'm at home atm, but I'll share it after next week (got an advanced Powershell course in Brussels next week). Don't expect too much from it... :) it mainly just copies a file and then restarts the service...
Thanks for sharing in advance! :D
Be sure to check out our Knowledgebase for helpful articles and solutions!
Locked