BashScripts.org
http://bashscripts.org/forum/

Connectivity Monitor
http://bashscripts.org/forum/viewtopic.php?f=8&t=1326
Page 1 of 1

Author:  mikeylikesit [ Tue Mar 01, 2011 8:29 am ]
Post subject:  Connectivity Monitor

So heres the thing... my scripting knowledge is limited at best, and I have always been a windows guy... I found a powershell script that met my basic needs:

E-Mail immediately when a ping is returned notifying that my WebSense website blocking appliance wasnt functioning properly.

E-Mail immediately when functionality has been restored...

Powershell works great but instead of running this on my machine 24 hours it has been requested that I convert to shell script so we can run this off of a linux box... so now I am totally lost since I know absolutely nothing about shell scripting, I do have a book that I am reading on it now but it will likely take me FOREVER to figure it all out...
Side Question: Is there any type of conversion setup that can convert powershell to a unix script?? I assume not but figured I would ask...

Here is the powershell script I have now, it reads a file with an IP address of a blocked website ie. gmail.com, the file also has a true/false statement associated with the IP that was written based on the last ping attempt, if the status changes from what is in the file an e-mail is automatically generated.

# -----------------------------------------------------------------------------
# Script: Invoke-PingStats.ps1
# Author: Chris Brown http://www.flamingkeys.com
# Date: 20/12/2010 10:51:51
# Keywords:
# comments:
#
# Version History
#-+-----+-----------+------+----------------------------------------------------+
# | Ver | Date | Name | Changes |
#-+-----+-----------+------+----------------------------------------------------+
# | 1.0 | 20/12/10 | CJB | Initial Script |
# | 1.1 | 17/01/10 | CJB | Updates for release |
#-+-----+-----------+------+----------------------------------------------------+
#
# TODO: - Allow for 3 pings to be missed before alerting
#
# ♥ ♦ ♣ ♠ ♥ ♦ ♣ ♠ ♥ ♦ ♣ ♠ ♥ ♦ ♣ ♠ ♥ ♦ ♣ ♠ ♥ ♦ ♣ ♠ ♥ ♦ ♣ ♠ ♥ ♦ ♣ ♠ ♥ ♦ ♣ ♠ ♥ ♦ ♣ ♠
# Variables
#
# $configFile stores the location of the config xml file
$configFile = "c:\scripts\hosts.xml"
#
# Email Settings
$mailFrom = "[email protected]"
$mailTo = "[email protected]","[email protected]"
$mailSubjectPrefix = "[PingStats] "
$mailServer = "Mail Server Here"
#
# ♥ ♦ ♣ ♠ ♥ ♦ ♣ ♠ ♥ ♦ ♣ ♠ ♥ ♦ ♣ ♠ ♥ ♦ ♣ ♠ ♥ ♦ ♣ ♠ ♥ ♦ ♣ ♠ ♥ ♦ ♣ ♠ ♥ ♦ ♣ ♠ ♥ ♦ ♣ ♠

function Test-Ping ([string]$pingtarget) {
Get-WmiObject -Class Win32_PingStatus -Filter "Address='$pingtarget'"
}

function Send-Email ([String]$hostname, [switch]$up) {
#Send email, $hostname is $state
if ($up) {
# Websense is Down, send the email
$mailSubject = $mailSubjectPrefix + "WebSense has gone DOWN [$(Get-Date -format "HH:mm")]"
$mailBody = @"
<span style="font-family: sans-serif;color: red"><strong>$($hostname)</strong> is now DOWN!</span>
"@
Send-MailMessage -From $mailFrom -SmtpServer $mailServer -Subject $mailSubject -To $mailTo -Body $mailbody -BodyAsHtml
} else {
# Websense is UP, send the email
$mailSubject = $mailSubjectPrefix + "WebSense is back UP [$(Get-Date -format "HH:mm")]"
$mailBody = @"
<span style="font-family: sans-serif;color: green"><strong>WebSense</strong> is back UP!</span>
"@
Send-MailMessage -From $mailFrom -SmtpServer $mailServer -Subject $mailSubject -To $mailTo -Body $mailbody -BodyAsHtml
}
}


while (1 -eq 1) {
$results = @()
# Get the latest xml data
[xml]$xml = Get-Content $configFile
# For each host
Clear-Host
foreach ($comp in $xml.hosts.host) {
# Test a ping
$ping = Test-Ping $comp.ip
if ($ping.StatusCode -ne 0) {
##The host is offline
if ($comp.lastresult -eq $true) {
# if it was up last ping, send an email and rewrite the xml
Send-Email -hostname $comp.InnerXml
$comp.lastresult = "false"
} else {
# if it was down last ping, do nothing
}
Write-Host $comp.InnerXml "`t("$comp.ip")`t x ms" -ForegroundColor White -BackgroundColor Red

} else {
##The host is online
if ($comp.lastresult -eq $true) {
# if it was up last ping, do nothing
} else {
# if it was down last ping, send an email and rewrite the xml
Send-Email -hostname $comp.InnerXml -up
$comp.lastresult = "true"
}
# Write to screen
Write-Host $comp.InnerXml "`t("$comp.ip")`t" $ping.ResponseTime "ms" -ForegroundColor black -BackgroundColor Green
}


}
# Save the updated xml out
$xml.Save("c:\scripts\hosts.xml")
$results
Start-Sleep -Seconds 5
}

Author:  mikeylikesit [ Tue Mar 01, 2011 8:31 am ]
Post subject:  Re: Connectivity Monitor

- <hosts>
<host ip="64.233.161.83" lastresult="false" site="">Gmail</host>
</hosts>

Thats what my host XML file looks like if it would be helpful...

Page 1 of 1 All times are UTC - 6 hours
© 2000, 2002, 2005, 2007 phpBB Group • http://www.phpbb.com