# Monitoring d'une ferme de serveurs IIS
# Version 1.1 - light
#
# * Ping
# * Etat du port HTTP
# * Nombre de connections actives
# * cpu%
#
$Servers = (Get-Content .\servers.txt) -notmatch '^#'
$ErrorActionPreference = "SilentlyContinue"
[int]$Refresh = 10
$exit = $False
function CountDown($wait)
{
$startTime = get-date
$endTime = $startTime.addSeconds($wait)
$timeSpan = new-timespan $startTime $endTime
while ($timeSpan -gt 0)
{
$timeSpan = new-timespan $(get-date) $endTime
write-host "`r Refresh in :" $timeSpan.seconds "sec (CTRL-C To Exit) " -nonewline -f yellow
sleep 1
}
write-host ""
}
function CheckPort
{
param([string]$srv, [string]$port)
$Socket = New-Object Net.Sockets.TcpClient
$Socket.Connect($srv, $port)
if ($Socket.Connected) { $result = $true } else { $result = $false }
$Socket.Dispose()
$Socket = $null
return $result
}
while ($true) {
Clear-Host
write-host " "
write-host " Web farm Health Check ------------------------------------" -f Yellow
write-host " "
write-host " Server Ping Http Conn Cpu " -f Yellow
foreach ($server in $servers)
{
#Ping
$max = $server.length
if ($max -gt 30) { $max = 30 }
$Name = $Server.substring(0,$max)
$Name = $Name.PadRight(30)
write-host " " $Name " " -nonewline
$ping = (Test-Connection $server -Quiet -Count 1 -ErrorAction SilentlyContinue)
if ($ping -eq $true) { write-host " OK " -nonewline -f white } else { write-host " XX " -nonewline -f Red }
write-host " " -nonewline
#Http
$http = (CheckPort $server "80")
if ($http -eq $true) { write-host " OK " -nonewline -f white } else { write-host " XX " -nonewline -f Red }
write-host " " -nonewline
#Connections
$Connections = 0
if ($http -eq $true) { $Connections = (Get-Counter -ComputerName $server -Counter "\Web Service(_Total)\Current Connections").CounterSamples[0].CookedValue }
write-host $Connections.ToString().PadLeft(4) -NoNewline
write-host " " -nonewline
#Cpu
$cpu = "00"
if ($http -eq $true) { $cpu = "{0:N0}" -f (Get-Counter -ComputerName $server -Counter "\Processor(_Total)\% Processor Time").CounterSamples[0].CookedValue }
write-host ($cpu+"%").ToString().PadLeft(3) -NoNewline
write-host " "
}
write-host " ---------------------------------------------------------" -f Yellow
write-host " "
#sleep 10
Countdown $Refresh
}