Question Monitoring Service

Plus d'informations
il y a 13 ans 1 mois #9021 par maycry
Monitoring Service a été créé par maycry
Salut,
Dans le même chemin que la news

je poste mon script
effectuant un check à intervalle régulier par défaut 10min


[code:1]#'******************************************************************************
#' Surveillance d'un processus; celui-ci doit avoir au maximum
#' 2 instances du mme nom, si l'un d'eux crash
#' le script tue l'emsemble des processus et relance ceux-ci 1 la fois
#'==============================================================================
$Host.UI.RawUI.WindowTitle = \"Monitoring-SERV-U on filer\"

function send-mail
{
param ([string]$Subject,[string]$Body)
$SmtpClient = new-object system.net.mail.smtpClient

$MailMessage = New-Object system.net.mail.mailmessage

$SmtpClient.Host = \"SMTPHOST

$mailmessage.from = (\"WindowsServerEvents@domain.com\"«»)

$mailmessage.To.add(\"intalerte@domain.com\"«»)

$mailmessage.Subject = $Subject

$mailmessage.Body = $Body

$smtpclient.Send($mailmessage)
}


#################################################################################################################################################|
# BOUCLAGE a INTERVALLE DE 10 Minutes |
#################################################################################################################################################|
#****************************#
# DEBUT DE BOUCLE #
#****************************#
[System.Console]::TreatControlCAsInput = $TRUE
while ($TRUE)
{
Write-host -ForegroundColor Yellow -BackgroundColor Black \" ==> Verification du service SERV-U <=https://documents.domain.com=>\"
$ServerName = \"servu.domain.com\"
$ServiceName = \"SerV-U\"
$QueryWMI = (Get-WmiObject -Computername $ServerName -Query \"select * from win32_service where name='$ServiceName'\"«»)
$StartService = $QueryWMI.startservice()
$WshShell = New-Object -comobject \"Wscript.shell\"
$WhatStatus = $QueryWMI.state
$WhatStatus
if ($WhatStatus -ne \"Running\"«»)
{

#*************************************************#
# DEBUT DE BOUCLE tant que le service n'est pas OK#
#*************************************************#

while ($WhatStatus -ne \"Running\"«»)
{
Write-Host -ForegroundColor red -BackgroundColor white \"Le service $ServiceName est actuellement en status: ==> $WhatStatus\"

(Get-WmiObject -Computername $ServerName -Query \"select * from win32_service where name='$ServiceName'\"«») | out-file $env:«»SystemDrive\PowerShell_SCRIPTS\Temp\Servicedown.log
$ReadQueryWMI = Get-Content $env:«»SystemDrive\PowerShell_SCRIPTS\Temp\Servicedown.log
Remove-Item -Confirm:$false $env:«»SystemDrive\PowerShell_SCRIPTS\Temp\Servicedown.log

Send-Mail -Subject \"$ServerName !$ServiceName is $WhatStatus \" -body \"$ServiceName est actuellement en status $WhatStatus `n Relancement du service , `n Description du service detaille `n `n` `n $ReadQueryWMI \"

Write-Host -ForegroundColor red -BackgroundColor white \"Relancement du service\"


$StartService
Start-Sleep 10

$ServiceCheck = (Get-WmiObject -Computername $ServerName -Query \"select * from win32_service where name='$ServiceName'\"«»).state
Write-Host \"status du relancement $ServiceCheck\"


if ($ServiceCheck -eq \"Running\"«»)
{
$WhatStatus = $ServiceCheck
}
}
#***************************************************#
# Verification si le service est up sinan on relance#
#***************************************************#

Write-Host -ForegroundColor Cyan -BackgroundColor Black \" ==>Relancement OK $ServiceName is on $Servicecheck \"
Send-Mail -Subject \"$ServerName REcovery !$ServiceName is $Servicecheck \" -body \" ==>Le service est up \"
}
#*****************************#
# FIN DE BOUCLE SI SERVICE UP#
#*****************************#






#
#
# PARAMETRE DE BOUCLAGE #
#
#
$DateTime = Get-Date
Write-Host \"
\"
Write-Host -ForegroundColor Cyan -BackgroundColor Black \" ==>Ok! $ServiceName à pour status $WhatStatus\"
Write-Host \"
\"
Write-Host \"\"
Write-Host \"
\"
Write-Host -foregroundcolor Black -backgroundcolor Green \"Heure de lancement du processus $DateTime `n Prochaine exécution dans 10 Minutes\"
Write-Host \"
\"
Write-Host \"\"

$EndTime = Start-Sleep 600
while (($DateTime) -le $EndTime)
{
if ([System.Console]::KeyAvailable)
{
$key = [System.Console]::ReadKey($TRUE)

# intercepter CTRL+C :
if (($key.Modifiers -band [System.ConsoleModifiers]\"control\"«») -and ($key.Key -eq \"C\"«»))
{
# interrompre le script :
exit
} # fin du if (($key.Modifiers ...

# si autre touche, abr?ger l'attente :
else
{
break
}}}}

#****************************#
# FIN DE BOUCLE #
#****************************#
[/code:1]

Connexion ou Créer un compte pour participer à la conversation.

Temps de génération de la page : 0.067 secondes
Propulsé par Kunena