Check  website names and application pools from local configuration file instead of arguments on the Nagios server

dfke
dfke used Ask the Experts™
on
Hi,

I found this powershell script from Juan Granados.

<#
.SYNOPSIS
	Check IIS Website and AppPool status.
.DESCRIPTION
	Check IIS Website and AppPool status and try to start stopped ones.
.PARAMETER Websites
	Websites to check status.
	Default All.
.PARAMETER AppPools
	AppPools to check status.
	Default All.
.OUTPUTS
    OK: All Websites and AppPools Running.
    WARNING: Websites or AppPools stopped, but started manually.
    CRITICAL: Websites or AppPools stopped and could not started.
.EXAMPLE
    Check a list of Websites and AppPools 
	.\check_iis.ps1 -Websites 'site 1','site 2','site 3' -AppPools 'apppool 1','app2','test 1'
    
    Check all Websites and AppPools 
    .\check_iis.ps1
.NOTES 
	Author:	Juan Granados 
	Date:	December 2017
#>
Param(	
    [Parameter(Mandatory=$false,Position=0)] 
	[ValidateNotNullOrEmpty()]
	$Websites="All",
    [Parameter(Mandatory=$false,Position=1)] 
	[ValidateNotNullOrEmpty()]
	$AppPools="All"
)

$ExitCode = "0"
$Output = ""
$WebsitesRunning = (Get-WebsiteState | Where-Object { ($_.value -eq "Started") }).Count
$AppPoolsRunning = (Get-WebAppPoolState | Where-Object { ($_.value -eq "Started") }).Count
$WebsitesTotal = (Get-WebsiteState).Count
$AppPoolTotal = (Get-WebAppPoolState).Count
$StoppedWebsites = $null
$StoppedAppPools = $null

Function Get-Status ($Names,$List,$Type,[ref]$ExitCode){
    $ReturnOutput=""
    ForEach ($Item in $List){
        if (($Names -eq "All") -or ($Names -contains $Item.Name)){
            If ($Type -eq "Websites"){
                Start-Website -Name $Item.Name -ErrorAction SilentlyContinue
                $Result = (Get-WebsiteState -Name $Item.Name).value
            }
            Else{
                Start-WebAppPool -Name $Item.Name -ErrorAction SilentlyContinue
                $Result = (Get-WebAppPoolState -Name $Item.Name).value
            }
            if($Result -eq "Started"){
                if ($ExitCode.Value -eq 0){
                    $ExitCode.Value = "1"
                }
                If ($ReturnOutput -eq ""){
                   $ReturnOutput += "$($Type) not started: $($Item.Name)(Started manually). " 
                }
                Else{
                    $ReturnOutput +="$($Item.Name)(Started manually). "
                }
            }
            else{
                $ExitCode.Value = "2"
                If ($ReturnOutput -eq ""){
                   $ReturnOutput += "$($Type) not started: $($Item.Name)(Stopped). " 
                }
                Else{
                    $ReturnOutput +="$($Item.Name)(Stopped). "
                }
            }
        }
    }
    Return $ReturnOutput
}
$StoppedWebsites = Get-WebsiteState | Where-Object { ($_.value -ne "Started") } | % { return @{($_.itemxpath -split ("'"))[1]="$($_.value)" } } | % getEnumerator
If ($StoppedWebsites){
    
    $Output += Get-Status $Websites $StoppedWebsites "Websites" ([ref]$ExitCode)
}
$StoppedAppPools = Get-WebAppPoolState | Where-Object { ($_.value -ne "Started") } | % { return @{($_.itemxpath -split ("'"))[1]="$($_.value)" } } | % getEnumerator
If ($StoppedAppPools){
    
    $Output += Get-Status $AppPools $StoppedAppPools "AppPools" ([ref]$ExitCode)
}

$Output += "| WebsitesRunning=$($WebsitesRunning);0;0;0;$($WebsitesTotal) AppPoolsRunning=$($AppPoolsRunning);0;0;0;$($AppPoolTotal)"

If ($ExitCode -eq 2){
    Write-Output "CRITICAL: $($Output)"
    $Host.SetShouldExit(2)
}
ElseIf ($ExitCode -eq 1){
    Write-Output "WARNING: $($Output)"
    $Host.SetShouldExit(1)
}
else{
    Write-Output "OK: All Websites and AppPools Running. $($Output)"
    $Host.SetShouldExit(0)
}

Open in new window


Actually It works fine as is but It depends on given website and applicationpool names as Nagios input variables (Nagios server side).

Instead I would have the script use a local configuration file on the particular webserver containing  the names of the websites and application pools that need to be checked. That way there is no need to alter the Nagios server side configuration when adding a website or application pool. I just can't figure out how to do this in powershell and keep the same functionality. If anyone can help me out with this it would be much appreciated.

Cheers
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Mauro CazabonnetSenior Windows Automation Developer
Top Expert 2015

Commented:
Hi,
Just to clarify you would like to parse the website name and application pool from a flat file?

Based on the sample code you provided

.\check_iis.ps1 -Websites 'site 1','site 2','site 3' -AppPools 'apppool 1','app2','test 1'
This wont work?

M

Author

Commented:
Hi Mauro,

that does work but I indeed would like to parse it from a flat file instead.

Cheers
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
Put the file on the web server as C:\Temp\nagios.ini or whatever.ini, in this format:
Websites = site 1, site 2, site 3
AppPools = apppool 1, app 2, test 1

Open in new window

Then you can call the script like this:
$config = Get-Content -Path "C:\temp\nagios.ini" -Raw | ConvertFrom-StringData
$splat = @{}
If ($config.Websites) {
	$splat['Websites'] = $config.Websites -split ',' | ForEach-Object {$_.Trim()}
}
If ($config.AppPools) {
	$splat['AppPools'] = $config.AppPools -split ',' | ForEach-Object {$_.Trim()}
}
& "C:\Temp\check_iis.ps1" @splat

Open in new window

Author

Commented:
Hi,

thanks for the quick response, this works for me!

Cheers

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial