Server Monitor (GUI Ping Tool with RDP and TRACERT)

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Server Monitor (GUI Ping Tool with RDP and TRACERT)

10 Oct 2016, 01:06

Here is a flushdns function: FlushDNS.ahk
So there is no need to call cmd
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
Grendahl
Posts: 170
Joined: 30 Sep 2013, 08:21

Re: Server Monitor (GUI Ping Tool with RDP and TRACERT)

10 Oct 2016, 07:41

Thinking about the colored blocks, and Nextron's request to have the graphs next to the colored blocks.

What would you think about using the graphs as the background to the host name, with the colored border around it all? Perhaps with an option to turn the graph backgrounds on/off?
Elesar
Posts: 70
Joined: 31 Oct 2013, 07:56

Re: Server Monitor (GUI Ping Tool with RDP and TRACERT)

10 Oct 2016, 09:08

jNizM: Thanks, I'll work on getting that changed out. I did change over to the Ping4 function yesterday in my test branch, but am having some issues with IP -> Name resolution. It isn't really used in this script, but I have another script that uses the same PingMsg.ahk file that relies heavily on it. It is quite a bit faster, just gotta figure out the name res and it'll be good.

Grendahl: I had thought about that as well, but haven't looked into it yet.
Elesar
Posts: 70
Joined: 31 Oct 2013, 07:56

Re: Server Monitor (GUI Ping Tool with RDP and TRACERT)

10 Oct 2016, 11:35

repo updated:
Finished implementing Ping4() as well as put in FlushDNS()
Removed reload requirement for a couple of settings
Added screenshots to the README.md
Elesar
Posts: 70
Joined: 31 Oct 2013, 07:56

Re: Server Monitor (GUI Ping Tool with RDP and TRACERT)

10 Oct 2016, 16:50

A couple questions, as I can't decide on how I want to do a few things:
  • Should logging be per session, per host, or per host & per session? Only warnings & errors will be logged for the normal window.
    On the graph window, if logging is enabled, should it be a full log, or only warnings/errors? I'm thinking full, as it is a more intensive monitoring...pinging much more often than the main window (at least by default).
    Should we change to prompting for a host list at startup, so you could have several different text files of system names depending on what you want to monitor? (I'm going yes here, but just barely. I could see how it would be inconvenient if someone didn't need that feature.)
User avatar
hoppfrosch
Posts: 443
Joined: 07 Oct 2013, 04:05
Location: Rhine-Maine-Area, Hesse, Germany
Contact:

Re: Server Monitor (GUI Ping Tool with RDP and TRACERT)

11 Oct 2016, 00:00

@Elesar:
There seems to be a problem with yout latest github-version [at least here with me):

I put some machines from our local network into the hosts file. Starting HostMonitor.ahk displays some of those machines with a red border and "Last Seen: Never".

But: These machines are reachable for sure - opening a command shell and doing a "ping -t ...." on these machines, shows that those machines are reachable and healthy.

Running the scripts for several minutes makes changing the state of almost all machines from red to green or from green to red - but all machines are healthy at any time

Former versions of your script worked fine (as far as I tested).

-----
Edit: My added machines all live in our domain <computername>.<ourdomain>.de. I added the full qualified machinename (<computername>.<ourdomain>.de) into hosts-file.
Last edited by hoppfrosch on 11 Oct 2016, 07:42, edited 1 time in total.
User avatar
Grendahl
Posts: 170
Joined: 30 Sep 2013, 08:21

Re: Server Monitor (GUI Ping Tool with RDP and TRACERT)

11 Oct 2016, 07:26

Elesar wrote:A couple questions, as I can't decide on how I want to do a few things:
  • Should logging be per session, per host, or per host & per session? Only warnings & errors will be logged for the normal window.On the graph window, if logging is enabled, should it be a full log, or only warnings/errors? I'm thinking full, as it is a more intensive monitoring...pinging much more often than the main window (at least by default).
I'd think that per session logging of only warnings and errors is enough. There are enough log parsing options out there that folks can use if they want finer detail.
  • Should we change to prompting for a host list at startup, so you could have several different text files of system names depending on what you want to monitor? (I'm going yes here, but just barely. I could see how it would be inconvenient if someone didn't need that feature.)
Maybe we don't have to choose... Make it a settings option to prompt for host file on each load? That way, for those of us who don't want that feature, we won't get bugged on each launch, and for those that do, they can enable it.
User avatar
Grendahl
Posts: 170
Joined: 30 Sep 2013, 08:21

Re: Server Monitor (GUI Ping Tool with RDP and TRACERT)

11 Oct 2016, 07:35

hoppfrosch wrote:@Elesar:
There seems to be a problem with yout latest github-version [at least here with me):

I put some machines from our local network into the hosts file. Starting HostMonitor.ahk displays some of those machines with a red border and "Last Seen: Never".

But: These machines are reachable for sure - opening a command shell and doing a "ping -t ...." on these machines, shows that those machines are reachable and healthy.

Running the scripts for several minutes makes changing the state of almost all machines from red to green or from green to red - but all machines are healthy at any time

Former versions of your script worked fine (as far as I tested).
I'm seeing the same issues. Some hosts work normally (like http://www.msn.com), but others (like http://www.google.com) never return, and stay yellow.
www.google.com never returns (stays yellow), but google.com does. msn.com returns red, but www.msn.com returns green. Other hosts I've tested don't seem to return at all, they just stay yellow)
Elesar
Posts: 70
Joined: 31 Oct 2013, 07:56

Re: Server Monitor (GUI Ping Tool with RDP and TRACERT)

11 Oct 2016, 08:02

Yes, I've noticed the same name resolution issues since changing to the Ping4 function. I'm trying to find how to improve it's reliability, as it is quite a bit faster.

If anybody know anything about Winsock and cares to take a look, please do. I'll post in the thread for that function as well to see if they can figure out what is going on.
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Server Monitor (GUI Ping Tool with RDP and TRACERT)

11 Oct 2016, 08:32

If tested it with cmd console ->

amazon.com [54.239.25.192] -> no ping
www amazon.com [54.239.25.200] -> no ping

So it looks like they just disabled ICMP to prevent ping requests. Has nothing to do with the Ping4 function.
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
Elesar
Posts: 70
Joined: 31 Oct 2013, 07:56

Re: Server Monitor (GUI Ping Tool with RDP and TRACERT)

11 Oct 2016, 08:50

jNizM, the issue is with more than just external websites. Computers on my LAN that I CAN ping in console will randomly not resolve their names, or not return. I have tested this quite extensively now and am confident that the Ping4 function is where the issue is, now its just a matter of trying to resolve the issue.
User avatar
Grendahl
Posts: 170
Joined: 30 Sep 2013, 08:21

Re: Server Monitor (GUI Ping Tool with RDP and TRACERT)

11 Oct 2016, 14:20

Elesar - I fixed the <1 ms response on my test version.

Change PingMsg.ahk Line 98 to:

Code: Select all

Result.RTTime := ((NumGet(Reply, 8, "UInt")<1) ? 1 : Numget(Reply, 8, "UInt"))
User avatar
Grendahl
Posts: 170
Joined: 30 Sep 2013, 08:21

Re: Server Monitor (GUI Ping Tool with RDP and TRACERT)

11 Oct 2016, 14:49

jNizM wrote:Here is a flushdns function: FlushDNS.ahk
So there is no need to call cmd
Thanks! Going to update that one in several utility scripts I have.
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Server Monitor (GUI Ping Tool with RDP and TRACERT)

12 Oct 2016, 01:11

Can you test this version of ping?

Code: Select all

#NoEnv
SetBatchLines, -1

icmp := Ping("www.autohotkey.com")
MsgBox % icmp.ipaddr " | " icmp.rttime

icmp := Ping("8.8.8.8")
MsgBox % icmp.ipaddr " | " icmp.rttime

icmp := Ping("127.0.0.1")
MsgBox % icmp.ipaddr " | " icmp.rttime

Ping(addr, timeout := 1024)
{
    static hWS2_32   := DllCall("LoadLibrary", "str", "ws2_32.dll", "ptr")
    static hIPHLPAPI := DllCAll("LoadLibrary", "str", "iphlpapi.dll", "ptr")
    static WSASIZE   := 394 + (A_PtrSize - 2) + A_PtrSize, ECHO := {}

    if !(RegExMatch(addr, "^((|\.)\d{1,3}){4}$")) {
        VarSetCapacity(WSADATA, WSASIZE, 0)
        if (DllCall("ws2_32\WSAStartup", "ushort", 0x0202, "ptr", &WSADATA))
            throw Exception("WSAStartup failed", -1)
        VarSetCapacity(hints, 16 + 4 * A_PtrSize, 0) && NumPut(2, hints, 4, "int") && NumPut(1, hints, 8, "int") && NumPut(6, hints, 12, "int")
        if (DllCall("ws2_32\getaddrinfo", "astr", addr, "ptr", 0, "ptr", &hints, "ptr*", result) != 0)
            throw Exception("getaddrinfo failed", -1)
        addr := StrGet(DllCall("ws2_32\inet_ntoa", "uint", NumGet(NumGet(result+0, 16 + 2 * A_PtrSize) + 4, 0, "uint"), "ptr"), "cp0")
        DllCall("ws2_32\WSACleanup")
    }

    inaddr := DllCall("ws2_32\inet_addr", "astr", addr, "uint")
    if !(inaddr) || (inaddr = 0xFFFFFFFF)
        throw Exception("inet_addr failed", -1)

    if !(hICMP := DllCall("iphlpapi\IcmpCreateFile", "ptr"))
        throw Exception("IcmpCreateFile failed", -1)

    size := VarSetCapacity(buf, 32 + 8, 0)
    if !(DllCall("iphlpapi\IcmpSendEcho", "ptr", hICMP, "uint", inaddr, "ptr", 0, "ushort", 0, "ptr", 0, "ptr", &buf, "uint", size, "uint", timeout, "uint"))
        throw Exception("IcmpSendEcho failed", -1)
    ECHO.IPAddr := StrGet(DllCall("ws2_32.dll\inet_ntoa", "uint", NumGet(buf, 0, "uint"), "ptr"), "cp0")
    ECHO.Status := NumGet(buf, 4, "uint")
    ECHO.RTTime := ((rtt := NumGet(buf, 8, "uint")) < 1 ? 1 : rtt)

    return ECHO, DllCall("iphlpapi\IcmpCloseHandle", "ptr", hICMP)
}
Diff:
- Changed gethostbyname to getaddrinfo
- WSAStartup & WSACleanup only called when needed
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
hoppfrosch
Posts: 443
Joined: 07 Oct 2013, 04:05
Location: Rhine-Maine-Area, Hesse, Germany
Contact:

Re: Server Monitor (GUI Ping Tool with RDP and TRACERT)

12 Oct 2016, 02:38

@jNizM:

Seems to work perfectly within my first tests (I tested your standalone-demo and integrated it into Elesars Host-Monitor) - unless the Member-Variable "name" is missing in your returned value (name is filled via gethostbyaddr in the original function ...)

@elesar
New version of PingMsg.ahk:

Code: Select all

/*
    PingMsg.ahk
    Author: Daniel Thomas
   
   ***************************************************************************************************************
   **** New ping by jNizM ************************************************************************************
   **** New ping integrated by hoppfrosch *****************************************************************
   ***************************************************************************************************************

    This script takes a target script name host name or IP address and hostID as
    parameters and sends a system message to the target script with the reply
    time of the provided host name or IP address. Returns TIMEOUT if no reply.

    Reply is in the format of: hostID|hostName|latency|IPv4Address

    Hostname is only provided if it was used as a parameter. Currently no
    reverse lookup available.
*/
;<=====  System Settings  =====================================================>
#SingleInstance Off
#NoEnv
#NoTrayIcon

;<=====  Parameters  ==========================================================>
targetScript = %1%
host = %2%
hostID = %3%

;<=====  Main  ================================================================>
result := Object()
try {
	result := Ping(host, 2500)
	reply := hostID . "|" . result.Name . "|" . result.RTTime . "ms|" . result.IPAddr
}
catch e
    reply := hostID . "||TIMEOUT"

Send_WM_COPYDATA(reply, targetScript . " ahk_class AutoHotkey")
ExitApp

;<=====  Functions  ===========================================================>
Ping(addr, timeout := 1024)
{
    static hWS2_32   := DllCall("LoadLibrary", "str", "ws2_32.dll", "ptr")
    static hIPHLPAPI := DllCAll("LoadLibrary", "str", "iphlpapi.dll", "ptr")
    static WSASIZE   := 394 + (A_PtrSize - 2) + A_PtrSize, ECHO := {}

    if !(RegExMatch(addr, "^((|\.)\d{1,3}){4}$")) {
        VarSetCapacity(WSADATA, WSASIZE, 0)
        if (DllCall("ws2_32\WSAStartup", "ushort", 0x0202, "ptr", &WSADATA))
            throw Exception("WSAStartup failed", -1)
        VarSetCapacity(hints, 16 + 4 * A_PtrSize, 0)&& NumPut(2, hints, 4, "int") && NumPut(1, hints, 8, "int") && NumPut(6, hints, 12, "int")
        if (DllCall("ws2_32\getaddrinfo", "astr", addr, "ptr", 0, "ptr", &hints, "ptr*", result) != 0)
            throw Exception("getaddrinfo failed", -1)
        addr := StrGet(DllCall("ws2_32\inet_ntoa", "uint", NumGet(NumGet(result+0, 16 + 2 * A_PtrSize) + 4, 0, "uint"), "ptr"), "cp0")
        DllCall("ws2_32\WSACleanup")
    }

    inaddr := DllCall("ws2_32\inet_addr", "astr", addr, "uint")
    if !(inaddr) || (inaddr = 0xFFFFFFFF)
        throw Exception("inet_addr failed", -1)

    if !(hICMP := DllCall("iphlpapi\IcmpCreateFile", "ptr"))
        throw Exception("IcmpCreateFile failed", -1)

    size := VarSetCapacity(buf, 32 + 8, 0)
    if !(DllCall("iphlpapi\IcmpSendEcho", "ptr", hICMP, "uint", inaddr, "ptr", 0, "ushort", 0, "ptr", 0, "ptr", &buf, "uint", size, "uint", timeout, "uint"))
        throw Exception("IcmpSendEcho failed", -1)
    ECHO.IPAddr := StrGet(DllCall("ws2_32.dll\inet_ntoa", "uint", NumGet(buf, 0, "uint"), "ptr"), "cp0")
    ECHO.RTTime := ((rtt := NumGet(buf, 8, "uint")) < 1 ? 1 : rtt)

    return ECHO, DllCall("iphlpapi\IcmpCloseHandle", "ptr", hICMP)
}

Send_WM_COPYDATA(ByRef StringToSend, ByRef TargetScriptTitle){
    VarSetCapacity(CopyDataStruct, 3*A_PtrSize, 0)
    SizeInBytes := (StrLen(StringToSend) + 1) * (A_IsUnicode ? 2 : 1)
    NumPut(SizeInBytes, CopyDataStruct, A_PtrSize)
    NumPut(&StringToSend, CopyDataStruct, 2*A_PtrSize)
    Prev_DetectHiddenWindows := A_DetectHiddenWindows
    Prev_TitleMatchMode := A_TitleMatchMode
    DetectHiddenWindows On
    SetTitleMatchMode 2
    SendMessage, 0x4a, 0, &CopyDataStruct,, %TargetScriptTitle%,,,, 10000
    DetectHiddenWindows %Prev_DetectHiddenWindows%
    SetTitleMatchMode %Prev_TitleMatchMode%
    return ErrorLevel
}
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Server Monitor (GUI Ping Tool with RDP and TRACERT)

12 Oct 2016, 02:57

hoppfrosch wrote:... unless the Member-Variable "name" is missing in your returned value (name is filled via gethostbyaddr in the original function ...)
I can add this, if needed.

I am considering adding my ReverseLookup script into this too.
e.g. 8.8.8.8 -> google-public-dns-a.google.com
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
hoppfrosch
Posts: 443
Joined: 07 Oct 2013, 04:05
Location: Rhine-Maine-Area, Hesse, Germany
Contact:

Re: Server Monitor (GUI Ping Tool with RDP and TRACERT)

12 Oct 2016, 03:37

jNizM wrote:
hoppfrosch wrote:... unless the Member-Variable "name" is missing in your returned value (name is filled via gethostbyaddr in the original function ...)
I can add this, if needed.
I'm not sure whether it's needed - just wanted to note this ...
Elesar
Posts: 70
Joined: 31 Oct 2013, 07:56

Re: Server Monitor (GUI Ping Tool with RDP and TRACERT)

12 Oct 2016, 09:11

jNizM wrote:I am considering adding my ReverseLookup script into this too.
e.g. 8.8.8.8 -> google-public-dns-a.google.com
jNizm: I would appreciate a reverse lookup option, as the PingMsg script is also used in another script I am writing, and it relies on that functionality. That script is currently still using the comspec method as I can parse out the hostname when available in the results. Having it as part of this function would be nice.
just me
Posts: 9442
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Server Monitor (GUI Ping Tool with RDP and TRACERT)

13 Oct 2016, 02:11

PingMsg.ahk:
  1. see here
  2. Because you know for sure that Ping4() will be called you simply can load both DLLs when the script starts to run and remove the LoadLibrary and FreeLibrary calls from the function.
Reverse DNS:
You might try

Code: Select all

; ==================================================================================================================================
; Query DNS to get the host or domain name for the given IPv4 address (reverse lookup).
; ==================================================================================================================================
DNS_QueryName(IP, ByRef NameArray := "") {
   Static OffRR := (A_PtrSize * 2) + 16 ; offset of resource record (RR) within the DNS_RECORD structure
   HDLL := DllCall("LoadLibrary", "Str", "Dnsapi.dll", "UPtr")
   NameArray := []
   IPArray := StrSplit(IP, ".")
   RevIP := IPArray.4 . "." . IPArray.3 . "." . IPArray.2 . "." . IPArray.1 . ".IN-ADDR.ARPA"
   If !DllCall("Dnsapi.dll\DnsQuery_", "Str", RevIP, "Short", 0x0C, "UInt", 0, "Ptr", 0, "PtrP", PDNSREC, "Ptr", 0, "Int") {
      REC_TYPE := NumGet(PDNSREC + 0, A_PtrSize * 2, "UShort")
      If (REC_TYPE = 0x0C) { ; DNS_TYPE_PTR = 0x0C
         PDR := PDNSREC
         While (PDR) {
            Name := StrGet(NumGet(PDR + 0, OffRR, "UPtr"))
            NameArray.Insert(Name)
            PDR := NumGet(PDR + 0, "UPtr")
         }
      }
      DllCall("Dnsapi.dll\DnsRecordListFree", "Ptr", PDNSREC, "Int", 1) ; DnsFreeRecordList = 1
   }
   DllCall("FreeLibrary", "Ptr", HDLL)
   Return NameArray[1] ; returnes the first name from the NameArray on success, otherwise an empty string
}

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: KruschenZ and 75 guests