Sign in to follow this  
Followers 0
DougH

Help with script

4 posts in this topic

I have been asked to create a script that monitors a Service on a Server. Based on their criteria, the script checks the service every 15 minutes. I created the script using WMI and set the script to sleep for 15 minutes.

Now they need not only to monitor the Service, but also shutdown the Service only on Wednesdays at 11:00 p.m. for one hour and then restart the Service. I added some controls so they could change the time when the Service needs to be shutdown. I used the GUICtrlSetOnEvent to run the functions during the sleep process.

What I need to find out how to do, when the specified time occurs, I want the script to run a function that shuts down the Service without any intervention. I added a couple of buttons to be sure the function shuts down the Service and starts the Service when depressed using GUICtrlSetOnEvent. How can this be done automatically without a user pressing the button?

I have added my script, may not be the best formated.

Thanks,

Doug

Opt("GUIOnEventMode", 1)
$PCompName = "10.70.22.2"
$EMailServer = "smtp.sssss.net"
$From = "XXXXXXXXXXXX"
$FromAddress = "XXXXXXXXXXXX@XXXXXXXXXXXX.com"
$Recip = "XXXXXXXXX@XXXXXXXXX.com"
$Subject = "Revenue Runner Services Error"
$ChartCnt = 0
$ServiceName = "Mirth"

$RevenueRunner = GUICreate("Revenue Runner Service Monitor", 380, 210)
$RevRun = GUICtrlCreatelistView("    Revenue Runner Service     |  Status   |     Date    |",15,18,350,85,BitOr($LVS_EX_FULLROWSELECT,$LVS_EX_GRIDLINES))
$Version = GUICtrlCreatelabel("Version 2.0.01",5,157,105,20)
GUICtrlSetState($Version,$GUI_DISABLE)
$StatusBar = GUICtrlCreateLabel("",0,170,280,20,$SS_SUNKEN)
$PingStat = GUICtrlCreateLabel("",282,170,98,20,$SS_SUNKEN)
$Serv1 = GUICtrlCreateLabel("Stop Mirth Service",70,115,120,20)
$Serv2 = GUICtrlCreateLabel("Start Mirth Service", 70,140,120,20)
$StopServ = GUICtrlCreateInput("", 175,115,60,20)
$StartServ = GUICtrlCreateInput("", 175,140,60,20)
$SetSched = GUICtrlCreateButton("Set Time",250,115,50,20)
$SetCancel = GUICtrlCreateButton("Cancel",250,140,50,20)
$ServStart = GUICtrlCreateButton("Start", 310,115,50,20)
$ServStop = GUICtrlCreateButton("Stop", 310,140,50,20)
$Stat = GUICtrlCreatelabel("e-Mail Suspened",33,0,100,20)
$StartMirth = RegRead("HKEY_CURRENT_USER\Software\RevRunner", "StartMirthService")
GUICtrlSetData($StartServ, $StartMirth)
$StopMirth = RegRead("HKEY_CURRENT_USER\Software\RevRunner", "StopMirthService")
GUICtrlSetData($StopServ, $StopMirth)
GUICtrlSetState($Serv1, $GUI_DISABLE)
GUICtrlSetState($Serv2, $GUI_DISABLE)
GUICtrlSetState($StopServ, $GUI_DISABLE)
GUICtrlSetState($StartServ, $GUI_DISABLE)
GUICtrlSetState($SetSched, $GUI_DISABLE)
GUICtrlSetState($SetCancel, $GUI_DISABLE)
    ; Create the menu system
    GUICtrlSetState($Stat,$GUI_HIDE)
    $MainMenu = GUICtrlCreateMenu("&File")
    ;$Config = GUICtrlCreateMenuItem("Configure", $MainMenu)
    $Close = GUICtrlCreateMenuItem("Close", $MainMenu)
        GUICtrlSetState(-1, $GUI_DEFBUTTON)
    $Options = GUICtrlCreateMenu("Options", -1, 1)
        $Susp = GUICtrlCreateMenuItem("Suspend_eMail", $Options)
        $Serv = GUICtrlCreateMenuItem("Service_Scheduler", $Options)
        ;GUICtrlSetState(-1, $GUI_DEFBUTTON)
        GUICtrlSetState($Susp, $GUI_UNCHECKED)
        ;GUICtrlSetState($Serv, $GUI_CHECKED)
GUISetState(@SW_SHOW)
ListProceses($PCompName)

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $CloseServ
            Exit
        Case $msg = $Config
            ConfigService($PCompName)
        Case $msg = $Close
            Exit
        Case $msg = $SetCancel
            CloseWin()
    EndSelect   
Wend        

Func ListProceses($PCompName)
    $Services = 0
    $M_Service = 0
    _GUICtrlListView_DeleteAllItems($RevRun)
    $MirthCnt = 0
    $Time = _Now()
        GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")
        GUICtrlSetOnEvent($Close, "CLOSEClicked")
        GUICtrlSetOnEvent($Susp,"SuspendEMail")
        GUICtrlSetOnEvent($Serv,"SchedServShtdn")
        GUICtrlSetOnEvent($SetSched, "StopStartService")
        GUICtrlSetOnEvent($SetCancel, "SchedServShtdn")
        GUICtrlSetOnEvent($ServStart, "StartService")
        GUICtrlSetOnEvent($ServStop, "StopService")
    $objWMIProcService = ObjGet("winmgmts:\\" & $PCompName & "\root\CIMV2")
        If @error Then
            GUICtrlSetData ($StatusBar,"Error retrieving object, " & @Error)
            $Body = "A problem has been detected with the Revenue Runner Server.  Cannot connect to Server. Please do not reply to this e-mail."
            GUICtrlSetData ($StatusBar,"Error connecting to WMI, " & @Error)
            PauseProcess $Body
        Else
            GUICtrlSetData ($PingStat,"Server Running")
        EndIf
    $Services = $objWMIProcService.ExecQuery ("Select * from Win32_Service")
        If @error Then
            GUICtrlSetData ($StatusBar,"Error executing WMI Query, " & @Error)
        EndIf
        For $SItems In $Services
            $M_Service = StringInStr ( $SItems.Name, "Mirth" , 2)
            If $M_Service > 0 and $SItems.State = "Running" Then
                ;MsgBox(0,"Mirth Service",$M_Service & " - " & $SItems.State & " - " & $SItems.Name)
                GUICtrlSetData ($StatusBar, " Service is running.") 
                GUICtrlCreateListViewItem($SItems.Name & "|" & $SItems.State & "|" & $Time & "|", $RevRun)
                $MirthCnt = 1
            Endif
            
            If $M_Service > 0 and $SItems.State = "Stopped" and $MirthCnt < 1 Then
                GUICtrlCreateListViewItem($SItems.Name & "|" & $SItems.State & "|" & $Time & "|", $RevRun)
            EndIf
        Next
            If $MirthCnt < 0 Then
                $ErrCnt = $ErrCnt + 1
                ;MsgBox(0,"Error Counts", $MirthCnt & " - " & $ErrCnt)
                $Body = "A problem has been detected with the Revenue Runner Service.  Please verify the Mirth service is running."
                GUICtrlSetData ($StatusBar, " A problem has been detected with the Mirth Service.") 
                If $ErrCnt > 1 Then
                    $ErrCnt = 0
                    SendEMail($EMailServer,$From,$FromAddress,$Recip,$Subject,$Body)
                EndIf
            EndIf
        Sleep(900000)
        $Time = ""
        $ErrCnt = 0
        $MirthCnt = 0
    ListProceses($PCompName)
EndFunc

Func ClearList($a,$b,$c,$d)
    _GUICtrlListView_DeleteAllItems($RevRun)
    $ChartCnt = 0
    $Time = _Now()
    ListProceses($PCompName)
EndFunc

Func PauseProcess($eMailBody)
    $ErrCnt = $ErrCnt + 1
    If $ErrCnt > 2 Then
        SendEMail($EMailServer,$From,$FromAddress,$Recip,$Subject,$eMailBody)
    Else
        Sleep(90000)
        ListProceses($PCompName)
    EndIf
EndFunc

Func SendEMail($EMailServer,$From,$FromAddress,$Recip,$Subject,$Body)
    If $SuspEMail = 0 Then  
        $objMessage = ObjCreate("CDO.Message") 
        $objMessage.To = $Recip
        $objMessage.TextBody = $Body

        $objMessage.Subject = $Subject
        $objMessage.From = $FromAddress
        $objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
        $objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = $EMailServer 
        $objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
        $objMessage.Configuration.Fields.Update
        $objMessage.Send
            Sleep(90000)
        ListProceses($PCompName)
    EndIf
EndFunc

Func SuspendEMail()
    If BitAND(GUICtrlRead($Susp), $GUI_CHECKED) = $GUI_CHECKED Then
        GUICtrlSetState($Susp, $GUI_UNCHECKED)
        $SuspEMail = 0
        GUICtrlSetState($Stat, $GUI_HIDE)
    Else
        GUICtrlSetState($Susp, $GUI_CHECKED)
        $SuspEMail = 1
        GUICtrlSetState($Stat, $GUI_SHOW)
    EndIf
EndFunc

Func SchedServShtdn()
    $State = GUICtrlGetState($SetSched)
    If GUICtrlGetState($SetSched) = "144" then
        GUICtrlSetState($Serv1, $GUI_ENABLE)
        GUICtrlSetState($Serv2, $GUI_ENABLE)
        GUICtrlSetState($StopServ, $GUI_ENABLE)
        GUICtrlSetState($StartServ, $GUI_ENABLE)
        GUICtrlSetState($SetSched, $GUI_ENABLE)
        GUICtrlSetState($SetCancel, $GUI_ENABLE)
    Else
        GUICtrlSetState($Serv1, $GUI_DISABLE)
        GUICtrlSetState($Serv2, $GUI_DISABLE)
        GUICtrlSetState($StopServ, $GUI_DISABLE)
        GUICtrlSetState($StartServ, $GUI_DISABLE)
        GUICtrlSetState($SetSched, $GUI_DISABLE)
        GUICtrlSetState($SetCancel, $GUI_DISABLE)
    EndIf
EndFunc

Func StopStartService()
    ;Write the Stop and Stop times to the Registry, then disable the GUI
    $Result = RegWrite("HKEY_CURRENT_USER\Software\RevRunner", "StartMirthService", "REG_SZ", GUICtrlRead($StartServ))
    $Result = RegWrite("HKEY_CURRENT_USER\Software\RevRunner", "StopMirthService", "REG_SZ", GUICtrlRead($StopServ))
        GUICtrlSetState($Serv1, $GUI_DISABLE)
        GUICtrlSetState($Serv2, $GUI_DISABLE)
        GUICtrlSetState($StopServ, $GUI_DISABLE)
        GUICtrlSetState($StartServ, $GUI_DISABLE)
        GUICtrlSetState($SetSched, $GUI_DISABLE)
        GUICtrlSetState($SetCancel, $GUI_DISABLE)
EndFunc

Func StopService()
    ;At the specified time, shutdown the Mirth Service. The Registry entry "ServiceDown" indicates if the Service is Running or not.  1=Service Down, 0=Service Running
    $ServRun = RegRead("HKEY_CURRENT_USER\Software\RevRunner", "ServiceDown")
    If $ServRun = "0" Then
        $MService = $objWMIProcService.ExecQuery ("Select * from Win32_Service")
        For $objService in $MService
            $_MService = StringInStr ( $objService.Name, $ServiceName , 2)
            If $_MService > 0 then
                $objService.StopService()
                $Result = RegWrite("HKEY_CURRENT_USER\Software\RevRunner", "ServiceDown", "REG_SZ", "1")
                _GUICtrlListView_DeleteAllItems($RevRun)
                $MService = $objWMIProcService.ExecQuery ("Select * from Win32_Service")
                    For $objService in $MService
                        $_MService = StringInStr ( $objService.Name, $ServiceName , 2)
                        If $_MService > 0 then
                            GUICtrlCreateListViewItem($objService.Name & "|" & $objService.State & "|" & $Time & "|", $RevRun)
                        EndIf
                    Next
            EndIf   
        Next
    EndIf
    $MService = ""
    $_MService = ""
EndFunc

Func StartService()
    ;At the specified time, shutdown the Mirth Service. The Registry entry "ServiceDown" indicates if the Service is Running or not.  1=Service Down, 0=Service Running
    $ServRun = RegRead("HKEY_CURRENT_USER\Software\RevRunner", "ServiceDown")
    If $ServRun = "1" Then
        $MService = $objWMIProcService.ExecQuery ("Select * from Win32_Service")
        For $objService in $MService
            $_MService = StringInStr ( $objService.Name, $ServiceName , 2)
            If $_MService > 0 then
                $objService.StartService()
                $Result = RegWrite("HKEY_CURRENT_USER\Software\RevRunner", "ServiceDown", "REG_SZ", "0")
                _GUICtrlListView_DeleteAllItems($RevRun)
                $MService = $objWMIProcService.ExecQuery ("Select * from Win32_Service")
                    For $objService in $MService
                        $_MService = StringInStr ( $objService.Name, $ServiceName , 2)
                        If $_MService > 0 then
                            GUICtrlCreateListViewItem($objService.Name & "|" & $objService.State & "|" & $Time & "|", $RevRun)
                        EndIf
                    Next
            EndIf   
        Next
    EndIf
    $MService = ""
    $_MService = ""
EndFunc

Func CLOSEClicked()
  Exit
EndFunc

Share this post


Link to post
Share on other sites



Try something like this in the While/Wend loop

;
While 1
    $msg = GUIGetMsg()
    $iStatus = RegRead("HKEY_CURRENT_USER\Software\RevRunner", "ServiceDown")
    If $iStatus = 0 Then
      If @Hour = 23 AND @WDay = 4 Then StopService()
    ElseIf $iStatus = 1 Then
      If @Hour = 0 Then StartService()
    EndIf
    Select
        Case $msg = $CloseServ
            Exit
        Case $msg = $Config
            ConfigService($PCompName)
        Case $msg = $Close
            Exit
        Case $msg = $SetCancel
            CloseWin()
    EndSelect   
Wend
;

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

GEOSoft,

Thanks for the fast response. I tried what you suggested and it did not work, but I did come up with a solution using your code. Instead of using the While loop, I used a Do Until loop. Since I check the status of the Service every 15 minutes, I divided the Sleep time by 3 and then each time I check if the service should stopped or started. I use $DelayCnt to loop 3 times equaling 15 minutes

This Do is in the ListProceses Function.

Do

$iStatus = RegRead("HKEY_CURRENT_USER\Software\RevRunner", "ServiceDown")

If $iStatus = 0 AND @WDay = 4 Then

If _NowTime(4) >= $StopMirth Then StopService()

ElseIf $iStatus = 1 AND @WDay = 4 Then

If _NowTime(4) >= $StartMirth Then StartService()

EndIf

$DelayCnt = $DelayCnt + 1

Sleep(300000)

Until $DelayCnt = 2

$Time = ""

$ErrCnt = 0

$MirthCnt = 0

ListProceses($PCompName)

Share this post


Link to post
Share on other sites

It's good that you have it working and using the Do/Until is a good way of doing it. To check every 15 seconds all you really need is

;
If @Min = 0 OR Mod(@Min, 15) = 0 Then
;

I always throw a check for seconds in there as well but in your case it may not be needed.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
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
Sign in to follow this  
Followers 0