prometheus-community / windows_exporter

Prometheus exporter for Windows machines
MIT License
2.88k stars 690 forks source link

tcp: use GetExtendedTcpTable to display states Closed, Listening, SynSent, SynRcvd, CloseWait, TimeWait ... #1638

Closed astigmata closed 2 days ago

astigmata commented 1 week ago

Windows API Integration

The program interacts with the Windows API using syscalls to access low-level TCP information:

It loads the iphlpapi.dll library and uses the GetExtendedTcpTable function to retrieve TCP connection information.

The MIB_TCPROW_OWNER_PID struct is defined to represent a row in the TCP connection table.

# HELP windows_tcp_connections_state_count Number of TCP connections by state and address family
# TYPE windows_tcp_connections_state_count gauge
windows_tcp_connections_state_count{af="ipv4",state="CLOSE_WAIT"} 2
windows_tcp_connections_state_count{af="ipv4",state="ESTABLISHED"} 9
windows_tcp_connections_state_count{af="ipv4",state="LISTENING"} 14
windows_tcp_connections_state_count{af="ipv6",state="ESTABLISHED"} 4
windows_tcp_connections_state_count{af="ipv6",state="LISTENING"} 11
windows_tcp_connections_state_count{af="ipv6",state="TIME_WAIT"} 2
jkroepke commented 1 week ago

Hi @astigmata

great PR so far! I liked that WMI is not used. But it needs to be restructured a little bit. Do you have an example output of the metrics?

In another PR (#1635), I'm also start to use iphlpapi.dll to gain informations about routes.

Since the multiple collection may use iphlpapi.dll, the logic to Windows API should be separated to an dedicated packages, like I did it in https://github.com/prometheus-community/windows_exporter/pull/1635/files


btw. in 99% of all cases, golang.org/x/sys/windows is an drop-in replacement for syscall, e.g. windows.AF_INET instead syscall.AF_INET

jkroepke commented 1 week ago

@astigmata After a review, I can take the lint issues, thats fine!

astigmata commented 1 week ago

@astigmata After a review, I can take the lint issues, thats fine!

@jkroepke Thank you for the help, it's my first contrib

jkroepke commented 1 week ago

Oh, you did it very well for the first contribution! :-)

I will leave it open for now, because I would like to deliver some bug-fix only releases in context of 0.29.0, but this PR will be included in the next 0.30.0 release!

Thank you!