MonitorWP

Today, I made a promise: http://forums.iis.net/post/2042216.aspx. So now, I have to deliver, what ever it takes!

Ok, here we are:

<#
.SYNOPSIS
    MonitorWP.ps1 - Gets average CPU percent usage for a specific worker process
.DESCRIPTION
    MonitorWP.ps1 - Gets average CPU percent usage for a specific worker process and send an email alert if a defined threshold is exceeded.
    Writes in a log file MonitorWP.log with the result (threshold exceeded or not).
.PARAMETER AppPool
    Defines the name of the AppPool to monitor
    Default is DefaultAppPool.
.PARAMETER MeasuresTime
    Defines how long the loop to get CPU percent usage measures will run
    Default is 30 seconds.
.PARAMETER Interval
    Defines how long the script sleeps between each measure during the loop
    Default is 5 seconds.
.PARAMETER Threshold
    Defines the threshold. If the average value of the measures during the loop exceeded the threshold, an email alert is sent.
    Default is 80%.
.NOTES
    File Name   : MonitorWP.ps1
    Author      : Fabrice ZERROUKI - fabricezerrouki@hotmail.com
.EXAMPLE
    PS D:\> .\MonitorWP.ps1 -AppPool MyAppPool -Threshold 50
    Performs a measure of CPU percent usage for ApplicationPool MyAppPool each 5s, during 30s and compare the average with 50. If above, an email alert is sent.
.EXAMPLE
    PS D:\> .\MonitorWP.ps1 -MeasuresTime 60 -Interval 1
    Performs a measure of CPU percent usage for ApplicationPool DefaultAppPool each second, during 60s and compare the average with 80. If above, an email alert is sent.
#>
Param(
$AppPool="DefaultAppPool",
$MeasuresTime=30,
$Interval=5,
$Threshold=80
)

$MailTo="recipient@mail.com"
$MailSubject="$env:computername - Daily reboot failed!"
$MailFrom="sender@mail.com"
$SmtpServer="mailserver.example.com"

$ScriptName=($MyInvocation.MyCommand).Name
$ScriptName=$ScriptName -replace ".ps1",""
$Logfile=".\${ScriptName}.log"
function LogWrite
{
   Param ([string]$logstring)
   Add-Content $Logfile -Value $logstring
}

While (1 -eq 1) {
$CountersArray=@()
$ProcessId=cmd /c "c:\Windows\System32\inetsrv\appcmd.exe list wp /apppool.name:$AppPool"
$ProcessId=$ProcessId.Replace("WP `"", "")
$ProcessId=$ProcessId.Replace("`" (applicationPool:$AppPool)", "")

$TimeOut=New-TimeSpan -Seconds $MeasuresTime
$Sw=[Diagnostics.Stopwatch]::StartNew()
While ($Sw.Elapsed -lt $TimeOut)
{
    Try
    {
        $CpuPercent=Get-WmiObject -Class Win32_PerfFormattedData_PerfProc_Process -ComputerName $env:computername | ? { $_.idprocess -eq $ProcessId } | Select-Object -ExpandProperty PercentProcessorTime
        $CountersArray+="$CpuPercent"
        Start-Sleep $Interval
    }
    Catch [system.exception]
    {
        # Writing the exception in the console
        Write-Host $_.Exception
        # Writing the exception in the logfile
        $ErrorTime=Get-Date -format "MM-dd-yyyy %H:mm:ss"
        LogWrite ("$ErrorTime - [ERROR] $_")
        LogWrite ("`r`n")
        Exit;
    }
} #EndOf While ($Sw.Elapsed -lt $TimeOut)

$result=[System.Math]::Round(($CountersArray | Measure-Object -Average).average,2)
$InfoTime=Get-Date -format "MM-dd-yyyy %H:mm:ss"

if ($result -gt $Threshold) {
LogWrite ("$InfoTime - [ALERT] - AppPool $AppPool - ${result}%")
$MailBody=@"
<html><span style='font-family: Tahoma; font-size: 12px;' >Hi,<br />
<br />
$InfoTime<br />
Unfortunatly, the worker process (PID: $ProcessId) for the ApplicationPool "$AppPool" has exceeded the defined CPU usage threshold ($Threshold) in the last ${MeasuresTime}s.<br />
<br />
----------------------------------------------------------------------------</span><br />
<span style='font-family: Tahoma; font-size: 10px;' >This is an automatically generated email, please do not reply.<br />&nbsp;<br /></span></html>
"@
Send-MailMessage -To $MailTo -Subject $MailSubject -From $MailFrom -SmtpServer $SmtpServer -Priority High -BodyAsHtml $MailBody
}
else {LogWrite ("$InfoTime - [INFO] - AppPool $AppPool - ${result}%")}
} #EndOf While (1 -eq 1)

Leave a Reply

Scroll to Top