Solved

Create powershell ping monitoring

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

Expert Comment

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

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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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 28

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 28

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

0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
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…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now