# Microsoft ActiveDirectory - Domain Controller Services Check # # This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # # Revision History: # 2012-08-23 Bastian W. [Bastian@gmx-ist-cool.de] 1.0 (initial version) # 2012-09-04 Bastian W. [Bastian@gmx-ist-cool.de] 1.1 code adjusted: # * Fixed a problem where some services couldnīt be detected correctly (thanks Steve H.) # 2016-03-05 Bastian W. [Bastian@gmx-ist-cool.de] 1.2 code adjusted: # * updated script for Windows 2012R2 compatibility # # Info: # This powershell plugin for the NSClient++ can be used to monitor the services used by a Microsoft ActiveDirectory Domain Controller based on Windows 2008 # # # FAQ: # How To solve "cannot be loaded because the execution of scripts is disabled on this system. Please see "get-help about_signing" for more details." # See: http://technet.microsoft.com/en-us/library/ee176949.aspx [a possible solution would be 'Set-ExecutionPolicy RemoteSigned'] # # Installation: # # 1.) # Create a folder on C:\ called "NagiosMonitoring" if it didnīt exist, and put this script into that folder. # # Note: If you use another Foldername, you need to change this script completly! # # 2.) # Now start a Powershell and run the following comands (to test if the script is working for you) # "C:" # "cd NagiosMonitoring" # ".\NagiosMonitoring_AD_DomainControllerServicesCheck.ps1" # # 3.) # Add this script to the NSClient++ configuration (file: NSC.ini) # # [External Scripts] # DomainControllerServicesCheck=cmd /c echo C:\NagiosMonitoring\NagiosMonitoring_AD_DomainControllerServicesCheck.ps1 | PowerShell.exe -Command - # # On the check_nrpe command include the -t 30, since it takes some time to load the Exchange cmdlet's. # # 4.) # uncommend CheckExternalScripts.dll in the NSClient++ configuration (file: NSC.ini) # # 5.) # You need to edit the config below # # Configuration: # ==================== # Create a subset of the previous array for services you want to monitor # Syntax: $ServicesToCheckArray=@("Service1","Service2","Service3","Service4","Service5"); $ServicesToCheckArray=@("kdc","DNS","NTDS","ADWS","Netlogon") # # 6.) # restart the NSClient++ Service # ========================================================================================================================= # # ------------- Do not change anything behind that line ! ---------------------------- # # initialize vars/reset vars to zero $intNagiosStatus = "0" $strNagiosDescription = "" $strServiceNotFound = "" $bDebug = 0 #Create an array of all services running $GetService = get-service -ComputerName $env:computername #Find any iWFM service that is stopped foreach ($Service in $GetService) { foreach ($strServiceToCheck in $ServicesToCheckArray) { if ($Service.Name -eq $strServiceToCheck) { if ($bDebug -eq 1){ Write-Host " DEBUG: Service:"$Service.Name"eq"$strServiceToCheck" we will check it!" -foregroundcolor yellow} if ($Service.Status -eq "StopPending") { #check if a service is hung #automatically restart the service. #$servicePID = (gwmi win32_Service | where { $_.Name -eq $strServiceToCheck}).ProcessID #Stop-Process $ServicePID #Start-Service -InputObject (get-Service -ComputerName $env:computername -Name $strServiceToCheck) if ($strNagiosDescription -eq "") { # $strNagiosDescription = $strServiceToCheck + " (pending)" $strNagiosDescription = $Service.DisplayName + " (pending)" } else { #$strNagiosDescription = $strNagiosDescription + ", " + $strServiceToCheck + " (pending)" $strNagiosDescription = $strNagiosDescription + ", " + $Service.DisplayName + " (pending)" } if ($intNagiosStatus -ne "2") { # Don't lower the status level if we already have a critical state, otherwiese set it to critical $intNagiosStatus = "2" } } elseif ($Service.Status -eq "Stopped") { # check if a service is stopped #automatically restart the service. #Start-Service -InputObject (get-Service -ComputerName $env:computername -Name $strServiceToCheck) if ($strNagiosDescription -eq "") { #$strNagiosDescription = $strServiceToCheck + " (stopped)" $strNagiosDescription = $Service.DisplayName + " (stopped)" } else { #$strNagiosDescription = $strNagiosDescription + ", " + $strServiceToCheck + " (stopped)" $strNagiosDescription = $strNagiosDescription + ", " + $Service.DisplayName + " (stopped)" } if ($intNagiosStatus -ne "2") { # Don't lower the status level if we already have a critical state, otherwiese set it to warning $intNagiosStatus = "1" } } # the service we will check here can be removed from the to be checked array now! $ServicesToCheckArray = @($ServicesToCheckArray | Where-Object {$_ -ne $strServiceToCheck}) if ($bDebug -eq 1){ Write-Host " DEBUG: New to be checked array:"$ServicesToCheckArray -foregroundcolor yellow} } } } foreach ($strServiceNotFound in $ServicesToCheckArray) { # seamed the service we should control here is missing if ($strNagiosDescription -eq "") { $strNagiosDescription = $strServiceNotFound + " (not found)" } else { $strNagiosDescription = $strNagiosDescription + ", " + $strServiceNotFound + " (not found)" } if ($intNagiosStatus -ne "2") { # Don't lower the status level if we already have a critical state, otherwiese set it to critical $intNagiosStatus = "2" } } # Output, when should we push out a notification to nagios? if ($intNagiosStatus -eq "2") { Write-Host "CRITICAL:" $strNagiosDescription } elseif ($intNagiosStatus -eq "1") { Write-Host "WARNING:" $strNagiosDescription } else { Write-Host "OK: All services are up and running" } exit $intNagiosStatus