Restarting the Jaguar Service with VBScript

Written By: Kevin Jordan

- 23 Mar 2006 -
















Description: This is a tool that will test to see if there are any current connections to an EAServer and then restart the jaguar service. Extra functionality was included such as a time limit on the vbscript and the ability to send email notifications.

  1. Ensure There Are No Connections
  2. Performing the Script Actions

Copying the Log File

After the vbscript determines there have been 0 active connections 3 times in a row then the script performs 3 actions: archiving the jaguar.log file, restarting the jaguar service, and sending an email notification.

' Copy EA logs
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFileCopy = objFSO.GetFile("C:\Program Files\Sybase\EAServer\bin\Jaguar.log")
objFileCopy.Copy ("C:\ArchivedLogs\Sybase\JaguarLog\Jaguar" & Dateyyyymmdd() & "Restartservice.log")

Using built in file handling functions, the following lines are used to copy the jaguar.log file from C:\Program Files\Sybase\EAServer\bin to C:\ArchivedLogs\Sybase\JaguarLog. The file is also given a timestamp with Dateyyyymmdd and "Restartservice" is added to the end to distinguish it from other jaguar logs.

' // * Returns a 6 character date
' in the form yyyymmdd * //
Function Dateyyyymmdd()
        Dim strMonth, strDay
 
        If Month(now) < 10 Then
                strMonth = "0" & Month(now)
        Else 
                strMonth = Month(now)
        End If
 
        If Day(now) < 10 Then
                strDay = "0" & Day(now)
        Else 
                strDay = Day(now)
        End If
 
        Dateyyyymmdd = Year(now) & strMonth & strDay
End Function ' Dateyyyymmdd 

I use this function all the time when I want to add a timestamp to things. I would bet there’s something built into the vbscript date system, but writing this was actually easier than finding out. The kicker is that I had to have those leading 0s for single digit months and days. Otherwise the filenames will not sort correctly.

Restarting the Service

' Restart Jaguar service and quit
Set WShell = CreateObject("WScript.Shell")
WShell.Run "net stop jaguar",0,true
WShell.Run "net start jaguar",0,true

To restart the service net stop and net start are used just as we would from a command line.

Sending an Email Notification

Because the script needed to email in more than one situation, I created the SendMessage subroutine to clean up the code. To call SendMessage, include the address you’re sending the message to and the message.

' Send message that the service has been restarted
Call SendMessage("",  & _
"connEAReset.vbs has restarted the jaguar" & _ 
"service at " & Now & " on server " & GetIP())
Exit Sub

This message includes the time, provided by the Now function, and the IP address of the server from GetIP.

' // * Provide only the recipient 
' and the message for email * //
Function SendMessage(messageTo, strMessage)
        Dim objMessage, strSMTPServer, intSMTPPort
 
        Set objMessage = CreateObject("CDO.Message")
 
        ' Set the SMTP server information
        strSMTPServer = "192.168.0.100"
        intSMTPPort = 25
 
        objMessage.Configuration.Fields.Item( _
        "http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
        objMessage.Configuration.Fields.Item( _
        "http://schemas.microsoft.com/cdo/configuration/smtpserver") = strSMTPServer
        objMessage.Configuration.Fields.Item( _
        "http://schemas.microsoft.com/cdo/configuration/smtpserverport") = intSMTPPort
        objMessage.Configuration.Fields.Update
 
        objMessage.Subject = "EA Restart Script"
        objMessage.From = ""
        objMessage.TextBody = strMessage
        objMessage.Send
 
        Set objMessage = NOTHING
End Function ' Send Message 

SendMessage cleans up the code a little, by sent the majority of our parameters within the subroutine. While this affects the portability of the function, it allows emails to be sent without having to provide the SMTP server, exchange server properties, etc. Most of these lines are pretty self explanatory by looking at the variable name. Additional functionality is built into the CDO. The following lines may also be necessary:

' Add an attachment
objMessage.AddAttachment("filename.txt")
 
' The username for authenticating to an SMTP
' server using basic (clear-text) authentication 
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusername") = ""
 
' The password used to authenticate 
' to an SMTP server using authentication
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "Password"
 
'Use SSL for the connection (False or True)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False
 
' Set the number of seconds to wait for a valid
' socket to be established with the SMTP 
' service before timing out.
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60

In addition to setting these, be sure to also update the strSMTPPort, strSMTPServer, and ObjMessage.From.

' // * Uses ipconfig to parse
' the servers IP address * //
Function GetIP()
        Dim ws
        Set ws = CreateObject("WScript.Shell")
        Dim fso
        Set fso = CreateObject("Scripting.FileSystemObject")
        Dim TmpFile
        TmpFile = fso.GetSpecialFolder(2) & "/ip.txt"
        Dim ThisLine, IP
 
        If ws.Environment("SYSTEM")("OS") = "" Then
                ws.run "winipcfg /batch " & TmpFile, 0, True
        Else
                ws.run "%comspec% /c ipconfig > " TmpFile, 0, True
        End If
 
        With fso.GetFile(TmpFile).OpenAsTextStream
                Do While NOT .AtEndOfStream
                        ThisLine = .ReadLine
 
                        If InStr(ThisLine, "Address") <> 0 Then 
                                IP = Mid(ThisLine, InStr(ThisLine, ":") + 2)
                End If
                Loop
 
                .Close
        End With
 
        ' WinXP (NT? 2K?) leaves a carriage
        ' return at the end of line
        If IP <> "" Then
                If Asc(Right(IP, 1)) = 13 Then
                        IP = Left(IP, Len(IP) - 1)
                End If
        End If
 
        GetIP = IP
 
        fso.GetFile(TmpFile).Delete  
        Set fso = Nothing
        Set ws = Nothing
End Function ' GetIP 

I found this function a long time ago. It works similar to the way GetConnCache does. From the command prompt, ipconfig is executed and saved to a temporary file. Knowing exactly what location to parse out, the IP address is retrieved. I may re-write this pretty soon and post the updated version in the forum. While it works just fine, the code just isn’t very organized.

Setting a Time Limit

Rather than have the program run forever if there is constantly active connections, the script has a two hour limit. If the counter doesn’t reach 3 within the two hour time, the program exits.

' We want the program to end after two hours of trying
progEndTime = DateAdd("h", 2, Now)
 
' If the program has been running for 2 hours 
'quit, retest in 15 seconds
If Now() >= progEndTime Then
        ' Send message that the service will not restart
        Call SendMessage("", _
                "connEAReset.vbs has exceed its time " & _
                "limitation at " & Now & " on server " & _
                GetIP())
        Exit Sub
Else
        Wait(15)
End If

By using DateAdd(), we set the time limit by adding two hours to when the program first runs. After every a test iteration is completed, a condition statement checks to see if the current time (Now) is greater than or equal to the progEndTime. If it is then our SendMessage is called again, this time sending notification that the jaguar service has not been reset. Otherwise the program will wait its standard 15 seconds.

Conclusion

I was actually able to create this program pretty quickly because many of the functions and subroutines were reused from some of my older vbscripts. After writing getConnCach() and Wait() everything just fell into place. Be sure to write your code for portability (ignoring my SendMessage routine), and the more you code the quicker your future vbscript will come together.

<< Previous