Skip to content

Shootre21/Monitoring-Project

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 

Repository files navigation

Pingservertest

This Python script continuously monitors the status of specified servers and firewalls by pinging their IP addresses. Here's a summary and the purpose of the code:

Summary

  1. IP Address Mapping:

    • The script defines two dictionaries: firewalls and servers, mapping IP addresses to their respective names.
  2. Ping Function:

    • ping_server(ip_address): This function pings the given IP address once and returns True if the ping is successful (i.e., the server/firewall is up), and False otherwise.
  3. Monitoring Loop:

    • print_results(): This function continuously pings all the servers and firewalls, collecting their statuses.
    • The results are displayed in a formatted table using the tabulate library.
    • The status is color-coded: green (OK) if the server/firewall is reachable, and red (DOWN) if it is not.
    • The terminal screen is cleared before each update to provide a real-time monitoring effect.
    • The script waits for 3 seconds before repeating the checks.

Purpose

The primary purpose of this script is to monitor the connectivity status of a list of servers and firewalls. By periodically pinging these devices and displaying their statuses, administrators can quickly see which devices are operational and which are not. This real-time monitoring helps in promptly identifying and addressing network issues.

PING LOG FILE

This PowerShell script is designed to periodically ping a list of IP addresses and log the results to a file. Here's a detailed explanation of the code:

Explanation

Define IP Addresses to Ping

$ipAddresses = @{
    "Google" = "8.8.8.8"
    "ERP" = "XXXXXXXXXXXXX"
    "DC1" = "XXXXXXXXXXX"
    "FILE" = "XXXXXXXXXX"
    "DC2" = "XXXXXXXXXX"
    "TSFS" = "XXXXX"
    "RDS1" = "XXXXXX"
    "RDS2" = "XXXXXXX"
    "RDS3" = "XXXXXX"
}
  • This section defines a hashtable named $ipAddresses where the keys are names (descriptive labels) and the values are the corresponding IP addresses.

Set the Interval and Log File Path

$interval = 3600
$logFile = "ping_log.txt"
  • $interval is set to 3600 seconds (60 minutes). This determines how often the script will ping the IP addresses.
  • $logFile specifies the path to the log file where the ping results will be saved.

Function to Ping an IP Address and Log the Result

function PingAndLog($name, $ip, $count) {
    Write-Host "Test $count In Progress..."
    $result = Test-Connection -ComputerName $ip -Count 60 | Select-Object ResponseTime
    $result | ForEach-Object {
        $log = "{0}`t{1}`t{2} ms" -f (Get-Date), $name, $_.ResponseTime
        Add-Content -Path $logFile -Value $log
    }
    Write-Host "Test $count Completed Log file results sent"
}
  • PingAndLog($name, $ip, $count) is a function that:
    • Takes three parameters: the name (label) of the IP address, the IP address itself, and the count of the current test.
    • Uses Test-Connection to ping the specified IP address 60 times.
    • Selects only the ResponseTime from the ping results.
    • Logs the date, name, and response time to the specified log file (ping_log.txt) with tab-separated values.
    • Displays messages to the console to indicate the progress and completion of each test.

Loop to Ping Each IP Address Every 60 Minutes

$count = 1
while ($true) {
    $ipAddresses.GetEnumerator() | ForEach-Object {
        PingAndLog $_.Key $_.Value $count
    }
    $count++
    Start-Sleep -Seconds $interval
}
  • Initializes a counter $count to 1.
  • Enters an infinite loop (while ($true)).
  • Iterates over each key-value pair in the $ipAddresses hashtable.
    • Calls PingAndLog for each IP address, passing the name, IP address, and current count.
  • Increments the counter $count.
  • Pauses for the specified interval (60 minutes) before repeating the loop.

Prompt the User to Exit

Write-Host "Press any key to exit..."
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')
  • Displays a message prompting the user to press any key to exit the script.
  • Waits for a key press without displaying the key ('NoEcho,IncludeKeyDown').

Purpose

The purpose of this script is to:

  1. Continuously monitor the connectivity and response times of a list of important IP addresses (servers and services).
  2. Log the results of these pings to a file for later review.
  3. Provide a mechanism to monitor network performance and availability over time.
  4. Allow administrators to identify and troubleshoot connectivity issues based on the logged data.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors