Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Create powershell ping monitoring

Posted on 2015-02-03
13
Medium Priority
?
1,493 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Implementing Azure Infrastructure Exam 70-533

This course is designed to familiarize and instruct students in the content that is covered by Microsoft Exam 70-533, Implementing Microsoft Azure Solutions. It focuses on all the November 2016 objective domain topics.

 
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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

721 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