Re: Event Handlers with Powershell
Posted: Wed Oct 23, 2013 11:17 am
Great! We will keep it open, let us know.
Support for Nagios products and services
https://support.nagios.com/forum/
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
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 -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
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
}Is this used to convert a nsclient 3.9.x ini file to 4.x? Could you share it?I also created a similar script to update the nsclient.ini fiel to the latest version.
Thanks for sharing in advance!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...