Apr 30 2009 9:15PM GMT
Posted by: Colin Smith
Database,
Powershell,
SQL Server,
Monitoring,
Ping,
System Administration,
Database Administration
Earlier this month I posted about SQL Ping Servers Script that I was working on to notify different groups if a server was down or just SQL Server was down or just the agent was down. Well here is the next part. This is the Ping_Interface Function of the script. This goes out and does a normal dos ping and looks at the return value. Sends only 1 packet and checks for 0% loss to be in the return value. Anything but that and it fails. If it fails it checks the ping_failure variable. If that is 0 then it knows to send the notification out and it puts the touch file on the filesystem so that on the next run it knows that it sent the notification out on the last run. Windows team did not want to be overtly notified that a server was down. Not my idea. So here is the function and if you have any questions head to http://sysadminsmith.com and click the ‘Submit a Question’ link.
##Ping_Interface
function
Ping_Interface
{
echo
“nt users are $ntusers”
$pingresult
=
ping
$machine
-n 1
if ($pingresult
-like
“*(0% loss*”)
{
## Server Interface is responsive
## Remove Ping Failure file from monitoring folder if ping is successful and file exists.
$pingfail
= 0
if (test-path
“i:\$folder\ping_Failure.txt”)
{
del
“i:\$folder\ping_Failure.txt”
}
echo
“Ping Interface is succseful for $machine” >> “i:\OUT\ping_with_service_check.txt”
Check_Services
## Check to see if the SQL Server and the SQLAgent are running via WMI
}
else
{
## Server Interface is non-responsive
$pingfail
= 1
if (!(test-path
“i:\$folder\ping_Failure.txt”))
{
New-Item
-force
-itemType
file
-path
“i:\$folder\ping_Failure.txt”
}
## Check if this is first time and if it has been more than 6 hours since notification was sent.
if (($pingfail
-eq 1) -and ($pingnoemail
-eq 0))
{
$users
=
get-content
-path
“i:\$folder\email”
|
Sort-Object
|
Get-Unique
$users
=
$users
+
$NTusers
$Subject
=
“Unable to Ping $machine”
$body
=
“$machine is not responding to Ping. $pingresult.”
echo
“Ping interface failed for $machine, Sending notification to Systems Team and DBA Team” >> “i:\OUT\ping_with_service_check.txt”
Notify
}
else
{
echo
“Ping interface failed for $machine, notification has already been sent.” >> “i:\OUT\ping_with_service_check.txt”
}
}
}