Solved

Create powershell ping monitoring

Posted on 2015-02-03
13
1,166 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
 
LVL 4

Expert Comment

by:Joe Prior
ID: 40585777
0
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

 
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 250 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 250 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Copy Sheet to New Workbook based on Cell Value 6 58
Copy-Item -UseTransaction not working 2 37
Calling a function 4 23
Exchange 2013 Message Tracking 3 33
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

734 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