Repository navigation
Check ping windows iphlpapi - #498
Conversation
Replace the ping.exe call and localized output parser on Windows with the unprivileged ICMP echo API from iphlpapi.dll: - IPv4 uses IcmpCreateFile/IcmpSendEcho, IPv6 uses Icmp6CreateFile/Icmp6SendEcho2. - No child process and no locale-dependent output parsing. - Send one echo per second, using the per-packet timeout from -t or the 4 second default. The overall runtime is capped by the check deadline. - Keep packet results indexed so timeouts and out-of-order completions retain their packet position. - Write rta and pl with %.3f. IPv4 replies provide ttl; the IPv6 reply structure has no ttl field. - Reject -4 together with -6 and non-positive packet counts. The ping.exe parser is unchanged for Linux, macOS and FreeBSD. Add a build-tagged Windows implementation and a non-Windows stub. Add tests for the ICMP reply layouts, IP status mapping, result aggregation, indexed reply collection and IcmpCreateFile availability. The live check_ping test now accepts the %.3f packet loss output.
this is to prevent big slice allocation if a user wants absurdly high packet number. sending 1000 packets should take around 16 minutes already.
|
There is a catch when testing this. If you are building on windows, and the folder that you are building it from has a low mandatory level, likely from some kind of harness sandboxing on your workspace (deepseek harness had it) It infect new files you create in the directory with that mark, including snclient executable you built inside. They inherit the "Low mandatory level". Windows then does not allow usage of iphlpapi calls of that executable. Moving the file does not remove the mark. You have to either copy it elsewhere, or remove the access right mark from it. This probably wont happen in the CI build, but still is something to be careful about. If it is built normally, it gets "Medium Mandatory Label" and ping works. |
| res = snc.RunCheck("check_ping", []string{"host=10.99.99.99", "packets=2", "-t", "1"}) | ||
| assert.Equalf(t, CheckExitCritical, res.State, "state critical") | ||
| assert.Regexpf(t, `^CRITICAL - Packet loss = 100(|\.0)% \|'rta'=U 'pl'=100%;30;80;0`, string(res.BuildPluginOutput()), "output matches") | ||
| assert.Regexpf(t, `^CRITICAL - Packet loss = 100(\.0+)?% \|'rta'=U 'pl'=100%;30;80;0`, string(res.BuildPluginOutput()), "output matches") |
There was a problem hiding this comment.
packet loss is now printed with snprintf %3.f format
| err error | ||
| } | ||
|
|
||
| type indexedPingReply struct { |
There was a problem hiding this comment.
this type is used to in the channel.
I want to save individual ping requests in the order that I sent them to the channel, not in the possibly reorganized order that they come out.
| func collectPingReplies(replies <-chan indexedPingReply, sent, packets int, err error) []pingReply { | ||
| results := make([]pingReply, packets) | ||
| for i := range results { | ||
| results[i] = pingReply{status: ipReqTimedOut} |
There was a problem hiding this comment.
default allocate results slice with timeouts, will get overriden with actual results if they come
|
|
||
| timeout := perPacket | ||
| if deadline, ok := ctx.Deadline(); ok { | ||
| if remaining := time.Until(deadline); remaining < timeout { |
There was a problem hiding this comment.
remaining to the check context timeout.
If that is smaller than per ping packet timeout, update it
| 0, // Event | ||
| 0, // ApcRoutine | ||
| 0, // ApcContext | ||
| uintptr(unsafe.Pointer(&source)), |
There was a problem hiding this comment.
procIcmp6SendEcho2 accepts source and destination addresses without specifying length.
likely since they are of known windows.RawSockaddrInet6 type.
what is weird is that it needs a source address? In this case it just sets it as "::" and leaves the windows routing stack pick where it gets sent from.
But why would you need to set it as something else here? It can bind to different ipv6 addresses if the node has it. The same would go for ipv4 if we used IcmpSendEcho2Ex .
But there is no need to support that in snclient, too in depth for monitoring agent.
AI GENERATED
===============================
Replace the ping.exe call and localized output parser on Windows with the
unprivileged ICMP echo API from iphlpapi.dll:
Icmp6CreateFile/Icmp6SendEcho2.
4 second default. The overall runtime is capped by the check deadline.
retain their packet position.
structure has no ttl field.
The ping.exe parser is unchanged for Linux, macOS and FreeBSD. Add a
build-tagged Windows implementation and a non-Windows stub.
Add tests for the ICMP reply layouts, IP status mapping, result
aggregation, indexed reply collection and IcmpCreateFile availability.
The live check_ping test now accepts the %.3f packet loss output.
================================
check_ping: clamp packet count to maximum of 1000
this is to prevent big slice allocation if a user wants absurdly high packet number.
sending 1000 packets should take around 16 minutes already.