Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Create powershell ping monitoring

Posted on 2015-02-03
13
Medium Priority
?
1,659 Views
Last Modified: 2015-03-18
Dear Experts,

I need help with PowerShell script to perform ping monitoring with email notification. I have list of servers stored in ServerList.txt

ServerList.txt
Server1
Server2
Server3
.
.
.

This PowerShell script will be scheduled to run every 5mins to ping list of servers stored in ServerList.txt and if any of them ping fail, email notification will be sent to Administrators.

The issue with our current ping script is that notification will send every 5mins (as the server is still down), which is very annoying.
Thus, would like to have PowerShell script with the following logic:-
1) Once server is detected ping down, only one ping down email notification to be sent.
3) Once server is up and during next schedule ping check, another ping up email notification to be sent to indicate server is back to normal.

Thanks.

Regards,
Kung Hui
0
Comment
Question by:kunghui80
  • 6
  • 3
  • 2
  • +1
13 Comments
 
LVL 4

Expert Comment

by:Joe Prior
ID: 40585692
Hi..

You can download the required script from technet

https://gallery.technet.microsoft.com/scriptcenter/2d537e5c-b5d4-42ca-a23e-2cbce636f58d/

Thanks
Joe
0
 
LVL 2

Author Comment

by:kunghui80
ID: 40585751
Hi Joe,
I have go through the link given.  This powershell script basically monitor list of servers in single email.  

I would prefer every single host down, one email will be send just for that particular host.  If 10 servers are down, 10 email notification will be sent, and after recovery, 10 recovery email will be received to indicate server is up.

The reason is that each server ping down and every single email generated will link to our remedy system for auto ticket creation to reflect total hosts down/incident occurred.  Thanks for input.

Regards,
Kung Hui
0
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
LVL 2

Author Comment

by:kunghui80
ID: 40585790
Hi Joe,
The link provided does not meet my requirement in question.
1) Email notification.
2) Single email alert per single host down and single recovery email when host is up.

Thanks.

Regards,
Kung Hui
0
 
LVL 29

Assisted Solution

by:becraig
becraig earned 750 total points
ID: 40588140
Something like this should work for what you need:


gc c:\serverlist.txt | % {
$Server = $_
if (test-Connection -ComputerName $Server -Count 2 -Quiet ) {
write-Host "$Server is alive and Pinging " -ForegroundColor Green
}
else
	{
	$date = Get-Date -Format MM-dd-yyy-HH::mm
	$smtpServer = "smtp.domain.com"
    $msg = new-object Net.Mail.MailMessage
    $smtp = new-object Net.Mail.SmtpClient($smtpServer)
    $msg.From = "email@domain.com"
    $msg.ReplyTo = "noreply@domain.com"
    $msg.To.Add("recipient@domain.com")
    $msg.Subject = "$server Ping Monitor failure - $date "
    $msg.body = "$Server is not responding to ping."
    $smtp.Send($msg)
	}       
 
}

Open in new window

   

Replace c:\serverlist.txt  with your actual list of servers to test.
0
 
LVL 2

Author Comment

by:kunghui80
ID: 40588175
Hi Becraig,
Thanks for the feedback, by looking at content of the script, probably the following 2 scenario will surface.
1) The script will be set to run every 5mins, thus I might receive tons of alerts (Ping Down) till the server is up.
2) There isn't any recovery alert (i.e. Ping Up) sent when the host is recovered.

Thanks much!
0
 
LVL 2

Author Comment

by:kunghui80
ID: 40588180
Dear Experts,
I need some kind of logic as follow:-

The script will be scheduled to run every 5mins to detect server ping down in our network.
1) When a host is down, only one notification alert (Ping Down) per host will be sent (Not continuous flooding alerts).
2) When a host is up, only one notification alert (Ping Up) per host will be sent.

I would need one alert per host.  If hundreds host down, we will expect to receive hundreds alert too. When all are recovered, will receive hundreds of recovery email too.

Thanks all!
0
 
LVL 29

Expert Comment

by:becraig
ID: 40588194
Ok so an easy solution would be to create a text file such and badhosts.txt  

When a server does not respond to ping then add the servername to the file.

If a server is not responding to ping  and it is present in that file then ignore.
If it is not present then add it.
If it is present and it is responding to ping then remove it.
0
 
LVL 2

Author Comment

by:kunghui80
ID: 40588207
Hi Becraig,
You've got it right, and not forgetting about email alerting portion.
Just I have no idea how to achieve that using Powershell with such logic.

Thanks!
0
 
LVL 29

Expert Comment

by:becraig
ID: 40601683
Sorry I have had a few busy days, you want me to script in the email portion for you ?
0
 
LVL 2

Author Comment

by:kunghui80
ID: 40602142
Hi becraig, yes, would appreciate that to include email portion too :)
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 750 total points
ID: 40636965
Hi, I've come up with a modification to becraig's solution, that should satisfy your requirements, although I haven't tested it yet.

See how this goes.

Regards,

Rob.

$DownServerFile = "C:\DownServers.txt"
$FullServerFile = "C:\ServerList.txt"
# Create empty arrays for offline server lists
$PreviousDownServers = @()
$CurrentDownServers = @()
If (Test-Path -Path $DownServerFile) { $PreviousDownServers = Get-Content C:\DownServers.txt }
Get-Content -Path $FullServerFile | ForEach {
    $Server = $_
    if (test-Connection -ComputerName $Server -Count 2 -Quiet ) {
        write-Host "$Server is alive and Pinging " -ForegroundColor Green
        #Send email if this server was down previously, but is now up
        if ($PreviousDownServers -notcontains $Server) {
	        $date = Get-Date -Format MM-dd-yyy-HH::mm
	        $smtpServer = "smtp.domain.com"
            $msg = new-object Net.Mail.MailMessage
            $smtp = new-object Net.Mail.SmtpClient($smtpServer)
            $msg.From = "email@domain.com"
            $msg.ReplyTo = "noreply@domain.com"
            $msg.To.Add("recipient@domain.com")
            $msg.Subject = "$server Ping Monitor success - $date "
            $msg.body = "$Server is now responding to ping."
            $smtp.Send($msg)
        }
    }
    else
   	{
        $CurrentDownServers += $Server
        #Send email only if this is the first time the server is down
        if ($PreviousDownServers -notcontains $Server) {
	        $date = Get-Date -Format MM-dd-yyy-HH::mm
	        $smtpServer = "smtp.domain.com"
            $msg = new-object Net.Mail.MailMessage
            $smtp = new-object Net.Mail.SmtpClient($smtpServer)
            $msg.From = "email@domain.com"
            $msg.ReplyTo = "noreply@domain.com"
            $msg.To.Add("recipient@domain.com")
            $msg.Subject = "$server Ping Monitor failure - $date "
            $msg.body = "$Server is not responding to ping."
            $smtp.Send($msg)
        }
	}       
 
}
$CurrentDownServers | Out-File $DownServerFile

Open in new window

1

Featured Post

WatchGuard Case Study: NCR

With business operations for thousands of customers largely depending on the internal systems they support, NCR can’t afford to waste time or money on security products that are anything less than exceptional. That’s why they chose WatchGuard.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Screencast - Getting to Know the Pipeline
Suggested Courses

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question