Jump to content

System Heartbeat???


Graywalker
 Share

Recommended Posts

I am having serious trouble with my AutoIT3 programs locking up.

I am trying to create a Remote Heartbeat Monitor - seems simple enough. Get computer name, click the one button - it pings or checks the system for a response, turns the button green or red. Click the one button, it stops and turns button gray.

It works - once. Press the button again - Ping won't respond right.. or FileExists doesn't work or whatever.

I've tried so many frikkin ways... on using $runping = Run(@ComSpec & " /c " & 'ping -t ' & $ComputerName, "", @SW_SHOWDEFAULT, $STDOUT_CHILD + $STDIN_CHILD) - but can't send a Ctrl+c to the STDIN and get it to close... processclose won't work either.

The current GUI

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Constants.au3>

Global $status = "off", $ComputerName, $runping = ""
Global $line, $change = "no", $state = "no"

$Form1 = GUICreate("Monitor", 226, 74, 272, 140)
$ComputerName = GUICtrlCreateInput("computername", 96, 8, 121, 21)
$Label1 = GUICtrlCreateLabel("Computer Name", 8, 8, 80, 17)
$Button1 = GUICtrlCreateButton("Status", 8, 32, 211, 33, 0)
GUISetState(@SW_SHOW)

ProgressOn("Testing", "Running?", "", 500, 5, 16)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            If $runping <> "" Then
                StdinWrite($runping,00000011)
            EndIf
            ;ProcessClose($runping)
            MsgBox(0, "Variables", $status & @CRLF & $line & @CRLF & $runping & @CRLF & $change & @CRLF)
            Exit
        Case $Button1
            If $runping <> "" Then
                StdinWrite($runping,00000011)
            EndIf
            Buttonclick()
            ;Case Else
    EndSwitch
WEnd

Exit

Func _ping()
    $change = "no"
    $state = "no"
    $runping = Run(@ComSpec & " /c " & 'ping -t ' & $ComputerName, "", @SW_SHOWDEFAULT, $STDOUT_CHILD + $STDIN_CHILD)
    ProgressSet(30, "In Ping")
    While 1
        $line = StdoutRead($runping)
        ProgressSet(20, "Read Line")
        If @error Then ExitLoop
        Select
            Case StringInStr($line, "Reply from")
                $change = "up"
            Case StringInStr($line, "timed out")
                $change = "down"
        EndSelect

        If $change <> $state Then
            ProgressSet(50, "Changed")
            Select
                Case $change = "up"
                    GUICtrlSetBkColor($Button1, 0x00cc00) ; Green
                    $state = "up"
                Case $change = "down"
                    GUICtrlSetBkColor($Button1, 0xFF3300) ; Red
                    $state = "down"
            EndSelect

        EndIf
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                $writein = StdinWrite($runping,Binary("0x03"))
                ProcessClose($runping)
                MsgBox(0, "Variables", $status & @CRLF & $line & @CRLF & $runping & @CRLF & $change & @CRLF & $writein)
                Exit
            Case $Button1
                $writein = StdinWrite($runping,Binary("0x03"))
                ProcessClose($runping)
                MsgBox(0, "Variables", $status & @CRLF & $line & @CRLF & $runping & @CRLF & $change & @CRLF & $writein)
                Buttonclick()
                ;Case Else
        EndSwitch
    WEnd

EndFunc   ;==>_ping

(Oh yeah, instead of $writein = StdinWrite($runping,Binary("0x03")), I've tried StdinWrite($runping,00000011), BinarytoString, etc, etc. Not working - sends ^c apparently, but does nothing to stop the program.)

First attempt :

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

Global $status = "off", $ComputerName, $ts, $td, $pc = 0, $i = 0

#region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Monitor", 226, 74, 272, 140)
$ComputerName = GUICtrlCreateInput("computername", 96, 8, 121, 21)
$Label1 = GUICtrlCreateLabel("Computer Name", 8, 8, 80, 17)
$Button1 = GUICtrlCreateButton("Status", 8, 32, 211, 33, 0)
GUISetState(@SW_SHOW)
#endregion ### END Koda GUI section ###

ProgressOn("Testing","Running?","",5,5,16)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            MsgBox(0, "Variables", $status & @CRLF & $td & @CRLF & $ts & @CRLF & $pc & @CRLF)
            Exit
        Case $Button1
            Buttonclick()
        Case Else
            $i = $i + 1
            If $i > 100 Then $i = 1
            ProgressSet($i,$td)
            If $ComputerName <> "computername" Then
                If $status = "on" Then
                    $td = TimerDiff($ts)
                    If $td > 10000 Then
                        _ping()
                    EndIf
                EndIf
            EndIf
    EndSwitch
WEnd

Exit

Func Buttonclick()

    If $status = "on" Then
        $status = "off"
        GUICtrlSetBkColor($Button1, 0x999999) ; medium gray
        GUICtrlSetData($Label1, "Computer Name")
        $ts = 0
        $pc = 0
        Return $status
    Else
        $status = "on"
        $ComputerName = GUICtrlRead($ComputerName)
        GUICtrlSetBkColor($Button1, 0xffff99) ; light yellow
        GUICtrlSetData($Label1, "Computer Name")
        $ts = 0
        $pc = 0
        Return $status
    EndIf

EndFunc   ;==>Buttonclick

Func _ping()
    Dim $pr = 1

    $pc = $pc + 1

    $ts = TimerInit()
    $pr = Ping($ComputerName)

    If $pr = 0 Then
        GUICtrlSetBkColor($Button1, 0xFF3300) ; Red
    Else
        GUICtrlSetBkColor($Button1, 0x00cc00) ; Green
    EndIf

    GUICtrlSetData($Label1, " " & $pc & " Pings")
    $ts = TimerInit()
    Return $ts

EndFunc   ;==>_ping

Second attempt was to use If Fileexists("\\" & $ComputerName & "\c$) instead of a ping - still locked up.

(I am using Windows 7, 64bit OS)

Any ideas on how to make this work?? Repeatedly? Click the little button on or off as much as you want and it keep working???

Edited by Graywalker
Link to comment
Share on other sites

I fixed the issue by removing $computerName from the ping. I removed it cause it's an input field, I understand it worked a little bit (the first time). I think you have to use $name = GUICtrlGetState($computerName) if you want to use it the way you had it setup before. However I did not get it to work yet and I have to get to a dentist appointment.

The code I have below here is an example of how it does work each time you press the button. I may have changed unnecessary things as well, but I was in a hurry. Please try my code, hopefully it will help you figure out how to make your code do what you want it to do.

Catch yah later, nice avatar by the way. : v)

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

Global $status = "off", $ComputerName, $ts, $td, $pc = 0, $i = 0
$count=0
#region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Monitor", 226, 74, 272, 140)
$ComputerName = GUICtrlCreateInput("lblw-pc", 96, 8, 121, 21)
$Label1 = GUICtrlCreateLabel("Computer Name", 8, 8, 80, 17)
$Button1 = GUICtrlCreateButton("Status", 8, 32, 211, 33, 0)
GUISetState(@SW_SHOW)
#endregion ### END Koda GUI section ###

ProgressOn("Testing","Running?","",5,5,16)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            MsgBox(0, "Variables", $status & @CRLF & $td & @CRLF & $ts & @CRLF & $pc & @CRLF)
            Exit
        Case $Button1
            Buttonclick()
    EndSwitch
    $i = $i + 1
    If $i > 100 Then $i = 1
    ProgressSet($i,$td)
    ;If $ComputerName <> "computername" Then
        If $status == "on" Then
            ;$td = TimerDiff($ts)
            ;If $td > 1000 Then
                _ping()
            ;EndIf
        EndIf
    ;EndIf      
WEnd

Exit

Func Buttonclick()
    If $status = "on" Then
        $status = "off"
        GUICtrlSetBkColor($Button1, 0x999999) ; medium gray
        GUICtrlSetData($Label1, "Computer Name")
        $ts = TimerInit()
        $pc = 0
        Return $status
    Else
        $status = "on"
        $ComputerName = GUICtrlRead($ComputerName)
        GUICtrlSetBkColor($Button1, 0xffff99) ; light yellow
        GUICtrlSetData($Label1, "Computer Name")
        $ts = TimerInit()
        $pc = 0
        Return $status
    EndIf
EndFunc   ;==>Buttonclick

Func _ping()
    Dim $pr = 1
    $pc = $pc + 1
    $ts = TimerInit()
    $name="A_Computer_On_My_Network_Replace_With_Your_Networked_Computer_Name_Just_To_See_It_All_Work"
    $pr = Ping($name)
    If $pr == 0 Then
        GUICtrlSetBkColor($Button1, 0xFF3300) ; Red
    Else
        GUICtrlSetBkColor($Button1, 0x00cc00) ; Green
    EndIf
    ;GUICtrlSetData($Label1, " " & $pc & " Pings")
    $ts = TimerInit()
    Return $ts
EndFunc   ;==>_ping
Link to comment
Share on other sites

I fixed the issue by removing $computerName from the ping. I removed it cause it's an input field, I understand it worked a little bit (the first time). I think you have to use $name = GUICtrlGetState($computerName) if you want to use it the way you had it setup before.

Yep. I feel like the bird that flew into the window.

DOAH!! Anyway - thank you very much. Below code works nicely. May have unnecessary includes.

If you are a systems admin and sometimes remote to user's computers - now you don't have to keep asking "is it back to the log-on screen yet?" after having them reboot. (as long as its on your network) You can use the IP address as well.

; Heartbeat Monitor for Remote Systems
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

Global $status = "off", $ComputerName, $ts, $td, $pc = 0, $i = 0, $PCName

#region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Monitor", 226, 74, 272, 140)
$ComputerName = GUICtrlCreateInput("computername", 96, 8, 121, 21)
$Label1 = GUICtrlCreateLabel("Computer Name", 8, 8, 80, 17)
$Button1 = GUICtrlCreateButton("Status", 8, 32, 211, 33, 0)
GUISetState(@SW_SHOW)
#endregion ### END Koda GUI section ###

;ProgressOn("Testing","Running?","",5,5,16)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            ;MsgBox(0, "Variables", $status & @CRLF & $td & @CRLF & $ts & @CRLF & $pc & @CRLF)
            Exit
        Case $Button1
            Buttonclick()
        Case Else
            ;$i = $i + 1
            ;If $i > 100 Then $i = 1
            ;ProgressSet($i,$td)
            $PCName = GUICtrlRead($ComputerName)
            If $PCName <> "computername" Then
                If $status = "on" Then
                    $td = TimerDiff($ts)
                    If $td > 10000 Then
                        _ping()
                    EndIf
                EndIf
            EndIf
    EndSwitch
WEnd

Exit

Func Buttonclick()

    If $status = "on" Then
        $status = "off"
        GUICtrlSetBkColor($Button1, 0x999999) ; medium gray
        GUICtrlSetData($Label1, "Computer Name")
        GUICtrlSetData($Button1,"Status")
        $ts = 0
        $pc = 0
        Return $status
    Else
        $status = "on"
        $PCName = GUICtrlRead($ComputerName)
        GUICtrlSetBkColor($Button1, 0xffff99) ; light yellow
        $ts = 0
        $pc = 0
        Return $status
    EndIf

EndFunc   ;==>Buttonclick

Func _ping()
    Dim $pr = 1
    $PCName = GUICtrlRead($ComputerName)
    $pc = $pc + 1

    $ts = TimerInit()
    $pr = Ping($PCName)

    If $pc = 1 Then
        Sleep(200)
        $pr = Ping($PCName)
    EndIf

    If $pr = 0 Then
        Select
            Case @error = 1
                GUICtrlSetData($Button1,"Offline")
            Case @error = 2
                GUICtrlSetData($Button1,"Unreachable")
            Case @error = 3
                GUICtrlSetData($Button1,"Bad Destination")
            Case @error = 4
                GUICtrlSetData($Button1,"Not Repsonding")
        EndSelect
        GUICtrlSetBkColor($Button1, 0xFF3300) ; Red
    Else
        GUICtrlSetBkColor($Button1, 0x00cc00) ; Green
        GUICtrlSetData($Button1,"Repsonding")
    EndIf

    GUICtrlSetData($Label1, " " & $pc & " Pings")
    $ts = TimerInit()
    Return $ts

EndFunc   ;==>_ping
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...