Jump to content

Online and Offline servers ping or not to ping!


mbkowns
 Share

Recommended Posts

I have a script that pings servers in a list and sends out an email if one is offline. When it comes back online it sends out another email saying its online.

Problem:

Servers get loaded down and timeout during pings and falsely report being offline.

I have tried settings delays and have it not act until a second failed ping has occured but I am using the default ping on both of 4000ms or 4 seconds.

Solution:

Is there a better way to see if a server is down instead of pinging?

ServerList.txt contains unlimited different servers by line.

server01

server92

server84

Settings.ini

[Email Setup]

SMTPServer=EXCHANGE

EmailFromName=NIS

EmailFromAddress=Monitor@emailaddress.com

EmailToAddress=TEST@emailaddress.com

EmailSubject=Server Alert

EmailOutDelaySeconds=0

#include <file.au3>
#include <INet.au3>
#include <Array.au3>

Opt("TrayAutoPause", 0)

Dim $Pulltxtfilelist, $num, $CurrentOffline, $RunForever

$INIFULLPATH = @ScriptDir & "\Settings.ini"
$TMPDATSTORE = @ScriptDir & "\temp.dat"

;~ Grabs email settings from Settings.ini in script directory.
$s_SmtpServer = IniRead($INIFULLPATH, "Email Setup", "SMTPServer", "Default")
$s_FromName = IniRead($INIFULLPATH, "Email Setup", "EmailFromName", "Default")
$s_FromAddress = IniRead($INIFULLPATH, "Email Setup", "EmailFromAddress", "Default")
$s_ToAddress = IniRead($INIFULLPATH, "Email Setup", "EmailToAddress", "Default")
$s_Subject = IniRead($INIFULLPATH, "Email Setup", "EmailSubject", "Default")

$Delay = IniRead($INIFULLPATH, "Setup", "EmailOutDelaySeconds", "0")

;~ Clears temp.dat if you stop and start the service.
IniDelete($TMPDATSTORE, "Offline")
IniDelete($TMPDATSTORE, "Online")
FileDelete($TMPDATSTORE)

;~ Creates Dat file to temp store online and offline information
IniWriteSection($TMPDATSTORE, "Offline", "")
IniWriteSection($TMPDATSTORE, "Online", "")


;~  Sends out a single ping to ServerList.txt and puts each server into an Offline or Online List.
;~  Also sends out first email if the server is already offline.
If Not _FileReadToArray(@ScriptDir & "\ServerList.txt", $Pulltxtfilelist) Then
    MsgBox(4096, "Error", "ServersList.txt is not found please create and restart service.  error:" & @error)
    Exit
EndIf
For $x = 1 To $Pulltxtfilelist[0]
    $num = $num + 1
    $pingerror = Ping($Pulltxtfilelist[$x])
    If $pingerror Then
        IniWrite($TMPDATSTORE, "Online", $num, $Pulltxtfilelist[$x])
    EndIf
    
    If $pingerror = 0 Then
        IniWrite($TMPDATSTORE, "Offline", $num, $Pulltxtfilelist[$x])
        Dim $as_Body[1]
        $as_Body[0] = $Pulltxtfilelist[$x] & " Server is down..."
        $Response = _INetSmtpMail($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject, $as_Body, "", -1)
    EndIf

Next

Do;Loop checking online and offline servers to see if status changes forever.
    
    Sleep($Delay);Set from Settings.ini seconds delay on how often to ping servers.
    
;~  Sents out a single ping to Offline list to check if a server came back online.
    $OfflineCheck = IniReadSection($TMPDATSTORE, "Offline")
    If @error Then
;~  Removed this error message line if the section offline is missing it will display a error.
    Else
        For $i = 1 To $OfflineCheck[0][0]
            $pingerror = Ping($OfflineCheck[$i][1])
            If $pingerror = 0 Then
            Else
                IniDelete($TMPDATSTORE, "Offline", $OfflineCheck[$i][0])
                IniWrite($TMPDATSTORE, "Online", $OfflineCheck[$i][0], $OfflineCheck[$i][1])
                Dim $as_Body[1];Set to 2 if second body line is added.
                $as_Body[0] = $OfflineCheck[$i][1] & " Server is back online!"
                $Response = _INetSmtpMail($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject, $as_Body, "", -1)
            EndIf
        Next
    EndIf


;~  Adds list of all currently offline servers second line in email.
    $CurrentOffline = ""
    For $l = 1 To $OfflineCheck[0][0]
        $CurrentOffline = $CurrentOffline & @CRLF & $OfflineCheck[$l][1]
    Next

;~  Sends out a single ping to Online list to check if a server went offline.
    $OnlineCheck = IniReadSection($TMPDATSTORE, "Online")
    If @error Then
;~  Removed this error message line if the section online is missing it will display a error.
    Else
        For $i = 1 To $OnlineCheck[0][0]
            $pingerror = Ping($OnlineCheck[$i][1])
            If $pingerror = 0 Then
                Sleep(1000)
                $pingerror2 = Ping($OnlineCheck[$i][1])
                msgbox(4096, "", $pingerror2)
                If $pingerror2 = 0 Then
                    IniDelete($TMPDATSTORE, "Online", $OnlineCheck[$i][0])
                    IniWrite($TMPDATSTORE, "Offline", $OnlineCheck[$i][0], $OnlineCheck[$i][1])
                    Dim $as_Body[3];Set to 2 if second body line is added.
                    $as_Body[0] = $OnlineCheck[$i][1] & " Server is down..."
                    $CurrentOffline = $CurrentOffline & @CRLF & $OnlineCheck[$i][1]
                    $as_Body[1] = @CRLF
                    $as_Body[2] = "Server Outage List:" & @CRLF & $CurrentOffline
                    $Response = _INetSmtpMail($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject, $as_Body, "", -1)
                EndIf
            EndIf
        Next
    EndIf
    
Until $RunForever = 100
Link to comment
Share on other sites

As long as you consider being able to respond to ping as up, this is a very good way. For example, if a database server had the database crash, but not the entire server, it would still ping. The only other thing you might worry about is, if this script is only running on one computer and it crashes, you wouldn't get any reports.

Also you might want to look into using functions to clean up your code. Right now it is sort of hard to follow (but not terrible).

Link to comment
Share on other sites

  • 2 weeks later...

Ya I use functions all the time but I didn't this time. I will be cleaning it up with that. I am using an ini file to store the data right now which is kinda lame but I had trouble using arrays.

Could someone look at my code above and give me some good examples of how I could use arrays to make it cleaner.

THANKS!

Link to comment
Share on other sites

Nice bit of code: The following might be helpful

I use this code to check internet connectivity:

$IsCon = DllCall("WinInet.dll", "int", "InternetGetConnectedState", "int*", 0, "int", 0)

If $IsCon[0] = 0 Then Msgbox(0,"","No Internet Connectivity")

I also use this code to write protect the INI file:

FileSetAttrib($ScriptDirM, "-RS")

IniWrite($ScriptDirM, "s_TestTransport", "value", 0)

FileSetAttrib($ScriptDirM, "+RS")

I use this code to do multiple reads on the same INI File or Registry to create the Global variable. In the example I have only provide two variables you can of course

have as many variables as will be accommodated by AutoIT. I usually put the read INI or Registry in a Func (subroutine) especially where you might want to update the variables

at different times during the running of the script. The following routines can also be modified to write the INI File or Registry.

;//Read Install Data from Registry

Global $LData[2] = ["HKLM\SOFTWARE\Proxloc\sSentry|ExpiryDate", "HKLM\SOFTWARE\MICROSOFT\WINDOWS NT\CURRENTVERSION\|InstallDate"]

For $i = 0 To UBound($LData) - 1

$LICENCEData = StringSplit($LData[$i], "|")

$RegRead = RegRead($LICENCEData[1], $LICENCEData[2])

Assign($LICENCEData[2], $RegRead, 2)

Next

;//Read Install Data from ndasi.log

Global $LData[2] = ["InstallLog_ss|section1|value3|", "LicenceLog_dd|section1|value6|"]

;//Read Licence INI File

For $i = 0 To UBound($LData) - 1

$LICENCEData = _StringSplit($LData[$i], "|")

$INIRead = (_StringEncrypt(0, IniRead($ndasiLOG, $LICENCEData[1], $LICENCEData[2], $LICENCEData[3]), $ECode, 1))

Assign($LICENCEData[0], $INIRead, 2)

Next

Hope those routines are helpful. Ant..

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...