logo_mini_comments

Sans partage, la connaissance n'est rien !

Fil de navigation

Si vous ne disposez pas d'une solution de monitoring tel que Nagios ou Zabbix et que vous devez vous assurer qu'un certain nombre de serveurs sont joignables. Voici un script créé par mes soins qui permet de tester la disponibilité de vos serveurs (stockés dans un fichier texte).

 

Fonctionnalités :

  • Test la disponibilité de vos serveurs et/ou de vos équipements réseau (stockés dans un fichier texte)
  • Création de 2 fichiers de logs horodatés et séparés (Ping-Server_Success & Ping-Server_Warning)
  • Exporter les données dans un fichier .csv
  • Exporter les données dans un rapport HTML

 

Utilisation :

  • Compléter le fichier Servers.txt avec les hostnames et / ou les adresses IP des serveurs à contacter

 

Exemple du contenu du fichier Servers.txt avec les hostnames et / ou les adresses IP des serveurs :

SRVDC01
google.fr
192.168.100.222
127.0.0.1
localhost

 

Screenshot :

 

Code du script :

<#
.SYNOPSIS
    Ping multiple servers
.DESCRIPTION
    Check if servers are reachable with event logs creation and export results
.NOTES
    File name : Ping-Server.ps1
    Author : Pierre JACQUOT
    Date : 27/10/2015
    Version : 1.0
.LINK
    Website : https://www.pierrejacquot.yo.fr
    Reference : https://www.pierrejacquot.yo.fr/index.php/scripts/22-script-ping-server
#>

Clear-Host

Function Write-Log([string]$Output, [string]$Message) {
    Write-Verbose $Message
    ((Get-Date -UFormat "[%d/%m/%Y %H:%M:%S] ") + $Message) | Out-File -FilePath $Output -Append -Force
}

$StartTime = Get-Date -Format "dd/MM/yyyy HH:mm:ss"
[string]$Hostname = [Environment]::MachineName
[string]$Login = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
[string]$Workfolder = Split-Path $MyInvocation.MyCommand.Path
[string]$Date = Get-Date -UFormat "%Y-%m-%d"
[string]$TXTFile = $Workfolder + "\Servers.txt"
[string]$CSVFile = $Workfolder + "\$Date-Servers-Export.csv"
[string]$ReportFile = $Workfolder + "\$Date-Servers-Report.html"
[string]$LogFileOK = $Workfolder + "\$Date-Ping-Server_Success.log"
[string]$LogFileKO = $Workfolder + "\$Date-Ping-Server_Warning.log"
[array]$Servers = Get-Content -Path ".\Servers.txt" -ErrorAction SilentlyContinue
[int]$LineNumbers = $Servers.Count
[System.Collections.ArrayList]$ServerList = @()
[string]$Activity = "Trying to ping [$LineNumbers] server(s)"
[int]$Step = 1
[string]$Title = "[$Date] - Ping server(s) report on : $Hostname"

Write-Host "Ping-Server :" -ForegroundColor Black -BackgroundColor Yellow
If ((Test-Path ".\Servers.txt") -eq $False) {
    Write-Warning "TXT file [Servers.txt] does not exist."
    Write-Log -Output $LogFileKO -Message "TXT file [Servers.txt] does not exist."
}
ElseIf ($LineNumbers -eq 0) {
    Write-Warning "TXT file [Servers.txt] is empty."
    Write-Log -Output $LogFileKO -Message "TXT file [Servers.txt] is empty."
}
Else {
    Write-Host "Launching the ping command on [$LineNumbers] server(s)." -ForegroundColor Cyan
    Write-Host "`r"
    ForEach ($Server in $Servers) {
        [string]$Status = "Processing [$Step] of [$LineNumbers] - $(([math]::Round((($Step)/$LineNumbers*100),0)))% completed"
        [string]$CurrentOperation = "Ping : $Server"
        Write-Progress -Activity $Activity -Status $Status -CurrentOperation $CurrentOperation -PercentComplete ($Step/$LineNumbers*100)
        $Step++
        If (Test-Connection -ComputerName $Server -Count 2 -Quiet) {
            [string]$ServerStatus = "OK"
            Write-Host "$Server is alive and pinging." -ForegroundColor Green
            Write-Log -Output $LogFileOK -Message "$Server is alive and pinging."
        }
        Else {
            [string]$ServerStatus = "KO"
            Write-Warning "$Server seems dead not pinging."
            Write-Log -Output $LogFileKO -Message "$Server seems dead not pinging."
        }
        $ServerObject = [PSCustomObject]@{
            "Hostname / IP" = $Server
            Status = $ServerStatus
        }
        $ServerList.Add($ServerObject) | Out-Null
    }
}

$ServerList | Export-Csv -Path $CSVFile -NoTypeInformation -Delimiter ";" -Encoding UTF8
$EndTime = Get-Date -Format "dd/MM/yyyy HH:mm:ss"
[decimal]$Duration = [math]::Round((New-TimeSpan -Start $StartTime -End $EndTime).TotalSeconds,2)
[string]$PreContent = "<h1>$Title</h1>
<h2>Number of server(s) : <span class='PostContentBlue'>$LineNumbers</span></h2>"
[string]$SuccessLogFile = "Success log file : <span class='PostContentBlue'>$(Split-Path $LogFileOK -Leaf)</span><br/>"
[string]$WarningLogFile = "Warning log file : <span class='PostContentBlue'>$(Split-Path $LogFileKO -Leaf)</span><br/>"
[string]$PostContent = "<p id='PostContent'>Script launched from : <span class='PostContentBlue'>$Hostname</span><br/>
By : <span class='PostContentBlue'>$Login</span><br/>
Path : <span class='PostContentBlue'>$Workfolder</span><br/>
TXT file : <span class='PostContentBlue'>$(Split-Path $TXTFile -Leaf)</span><br/>
CSV file : <span class='PostContentBlue'>$(Split-Path $CSVFile -Leaf)</span><br/>
Report file : <span class='PostContentBlue'>$(Split-Path $ReportFile -Leaf)</span><br/>
$(If ((Test-Path $LogFileOK) -eq $True) { 
    $SuccessLogFile
})
$(If ((Test-Path $LogFileKO) -eq $True) { 
    $WarningLogFile
})
Start time : <span class='PostContentBlue'>$StartTime</span><br/>
End time : <span class='PostContentBlue'>$EndTime</span><br/>
Duration : <span class='PostContentBlue'>$Duration</span> second(s)</p>"
[string]$Report = $ServerList | ConvertTo-Html -As Table -CssUri ".\Style.css" -Title $Title -PreContent $PreContent -PostContent $PostContent
$Report = $Report -replace '<td>OK</td>','<td class="SuccessStatus">OK</td>'
$Report = $Report -replace '<td>KO</td>','<td class="CriticalStatus">KO</td>'
$Report | Out-File -FilePath $ReportFile -Encoding utf8

Write-Host "`r"
Write-Host "Script launched from : " -NoNewline; Write-Host $Hostname -ForegroundColor Red
Write-Host "By : " -NoNewline; Write-Host $Login -ForegroundColor Red
Write-Host "Path : " -NoNewline; Write-Host $Workfolder -ForegroundColor Red
Write-Host "TXT file : " -NoNewline; Write-Host (Split-Path $TXTFile -Leaf) -ForegroundColor Red
Write-Host "CSV file : " -NoNewline; Write-Host (Split-Path $CSVFile -Leaf) -ForegroundColor Red
Write-Host "Report file : " -NoNewline; Write-Host (Split-Path $ReportFile -Leaf) -ForegroundColor Red
If ((Test-Path $LogFileOK) -eq $True) {
    Write-Host "Success log file : " -NoNewline; Write-Host (Split-Path $LogFileOK -Leaf) -ForegroundColor Red
}
If ((Test-Path $LogFileKO) -eq $True) {
    Write-Host "Warning log file : " -NoNewline; Write-Host (Split-Path $LogFileKO -Leaf) -ForegroundColor Red
}
Write-Host "Start time : " -NoNewline; Write-Host $StartTime -ForegroundColor Red
Write-Host "End time : " -NoNewline; Write-Host $EndTime -ForegroundColor Red
Write-Host "Duration : " -NoNewline; Write-Host $Duration -ForegroundColor Red -nonewline; Write-Host " second(s)"
Write-Host "`r"

 

Exemple du fichier Ping-Server_Success.log créé automatiquement avec les machines qui sont joignables :

[09/05/2020 14:29:56] SRVDC01 is alive and pinging.
[09/05/2020 14:30:16] 127.0.0.1 is alive and pinging.
[09/05/2020 14:30:17] localhost is alive and pinging.

 

Exemple du fichier Ping-Server_Warning.log créé automatiquement avec les machines qui ne sont pas joignables :

[09/05/2020 14:30:10] google.fr seems dead not pinging.
[09/05/2020 14:30:15] 192.168.100.222 seems dead not pinging.

 

Cliquer ici pour visualiser un exemple du rapport Servers-Report.html créé automatiquement depuis mon poste.

 

Cliquer ici pour télécharger le fichier de style css.

 

Cliquer ici pour télécharger le script.