Friday, 10 October 2014

vbscript tell me when my machine / website / anything

Here is a simple script that will tell you when a ip / name / web address comes up:

Dim strServerName, oFSO, WSHShell, strServerFile, PINGFlag, i, ServerPingFlag
Dim strLogFileName, strLogFolderName, strLogPath, objLogFolderName
Dim objLogFileName, objLogTextFile, strMyDate
Const ForAppending = 8
Public strMailTo, strSMTP, strSubject, strBody, strSMTPUserName, strSMTPPassword, strSMTPPort

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set WSHShell = CreateObject("WScript.Shell")

'Keep looping back through the script so that it stays running
i = 0
Do While i = 0
serverList = ARRAY("aubdc00-jap01p","aubdc00-jap02p")

for each strServerName in serverList
If ServerPing(strServerName) Then
ServerPingFlag = "Online"
EmailAdmins strServerName
'Ping was successful
Else
'Ping was not successful
ServerPingFlag = "***Offline***"
End If
next
'This is in milliseconds, 5000 is 5 seconds
WScript.Sleep 60000
Loop

'********************************************************************************
'ServerPing Function
'Ping the server and if available return true, otherwise false
'********************************************************************************
Function ServerPing(strServerName)
Set WSHShell = CreateObject("WScript.Shell")
PINGFlag = Not CBool(WSHShell.Run("ping -n 1 " & strServerName, 0, True))
If PINGFlag = True Then
'Ping was successful
ServerPing = True
Else
'Ping not successful
ServerPing = False
End If
End Function

'********************************************************************************
'EmailAdmins Sub-Routine
'Send email to the admins
'********************************************************************************
Sub EmailAdmins (strServerName)
on error resume next
Dim iMsg
Dim iConf
Dim Flds
Const cdoSendUsingPort = 2
FromEmailAddress="jde@myriad-it.com"
Const strSmartHost = "smtpServerName.myriad-it.com"
Company="Myriad"
RecipientNames = ARRAY("smoir@myriad-it.com")


for each name in RecipientNames
'Create the message object
Set iMsg = CreateObject("CDO.Message")
'Create the configuration object
Set iConf = iMsg.Configuration
'Set the fields of the configuration object to send using SMTP via port 25.
With iConf.Fields
.item("http://schemas.microsoft.com/cdo/configuration/sendusing") = cdoSendUsingPort
.item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = strSmartHost
.Update
End With

'Set the message to,from,subject,body properties.
if strlen(attachment) > 1 then
With iMsg
.AddAttachment attachment
.To = name
.From = FromEmailAddress
.Subject = "Server is up" & " " & now() & strServerName
.TextBody = chr(1) & " " & strServerName
.Send
End With
else
With iMsg
.To = name
.From = FromEmailAddress
.Subject = "Server is up" & " " & now() & strServerName
.TextBody = chr(1) & " " & strServerName
.Send
End With
end if
set iMsg = Nothing
next

End Sub

This can easily be modified to do all sorts of emailing, when machines are up or down.  This was conceived because we were waiting for the AS/400 to come back up after a backup, so instead of hanging around – we just awaited the email… (perhaps from the pub – perhaps not…)

No comments:

Extending JDE to generative AI