Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new TCP metrics for Network integration on Windows #18294

Merged
merged 16 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions network/changelog.d/18294.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add new TCP metrics for Network integration on Windows
80 changes: 80 additions & 0 deletions network/datadog_checks/network/check_windows.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,45 @@
# (C) Datadog, Inc. 2022-present
# All rights reserved
# Licensed under Simplified BSD License (see LICENSE)
import socket
from collections import defaultdict
from ctypes import Structure, byref, windll
from ctypes.wintypes import DWORD

import psutil
from six import PY3, iteritems

from . import Network

Iphlpapi = windll.Iphlpapi
if PY3:
long = int


class TCPSTATS(Structure):
"""
https://learn.microsoft.com/en-us/windows/win32/api/tcpmib/ns-tcpmib-mib_tcpstats_lh
"""

_fields_ = [
("dwRtoAlgorithm", DWORD),
("dwRtoMin", DWORD),
("dwRtoMax", DWORD),
("dwMaxConn", DWORD),
("dwActiveOpens", DWORD),
("dwPassiveOpens", DWORD),
("dwAttemptFails", DWORD),
("dwEstabResets", DWORD),
("dwCurrEstab", DWORD),
("dwInSegs", DWORD),
("dwOutSegs", DWORD),
("dwRetransSegs", DWORD),
("dwInErrs", DWORD),
("dwOutRsts", DWORD),
("dwNumConns", DWORD),
]


class WindowsNetwork(Network):
"""
Gather metrics about connections states and interfaces counters
Expand All @@ -25,6 +53,7 @@ def check(self, _):
custom_tags = self.instance.get('tags', [])
if self._collect_cx_state:
self._cx_state_psutil(tags=custom_tags)
self._tcp_stats(tags=custom_tags)

self._cx_counters_psutil(tags=custom_tags)

Expand Down Expand Up @@ -74,6 +103,57 @@ def _cx_counters_psutil(self, tags=None):
}
self.submit_devicemetrics(iface, metrics, tags)

def _get_tcp_stats(self, inet):
stats = TCPSTATS()
try:
Iphlpapi.GetTcpStatisticsEx(byref(stats), inet)
except OSError as e:
self.log.error("OSError: %s", e)
return None
return stats

def _tcp_stats(self, tags):
"""
Collect metrics from Microsoft's TCPSTATS
"""
tags = [] if tags is None else tags

tcpstats_dict = {
'dwActiveOpens': '.active_opens',
'dwPassiveOpens': '.passive_opens',
'dwAttemptFails': '.attempt_fails',
'dwEstabResets': '.established_resets',
'dwCurrEstab': '.current_established',
'dwInSegs': '.in_segs',
'dwOutSegs': '.out_segs',
'dwRetransSegs': '.retrans_segs',
'dwInErrs': '.in_errors',
'dwOutRsts': '.out_resets',
'dwNumConns': '.connections',
}

proto_dict = {}
tcp4stats = self._get_tcp_stats(socket.AF_INET)
if tcp4stats:
proto_dict["tcp4"] = tcp4stats
tcp6stats = self._get_tcp_stats(socket.AF_INET6)
if tcp6stats:
proto_dict["tcp6"] = tcp6stats

tcpAllstats = TCPSTATS()
# Create tcp metrics that are a sum of tcp4 and tcp6 metrics
if 'tcp4' in proto_dict and 'tcp6' in proto_dict:
for fieldname, _ in tcpAllstats._fields_:
tcp_sum = getattr(proto_dict['tcp4'], fieldname) + getattr(proto_dict['tcp6'], fieldname)
setattr(tcpAllstats, fieldname, tcp_sum)
proto_dict["tcp"] = tcpAllstats

for proto, stats in proto_dict.items():
for fieldname in tcpstats_dict:
fieldvalue = getattr(stats, fieldname)
metric_name = "system.net." + str(proto) + tcpstats_dict[fieldname]
self.submit_netmetric(metric_name, fieldvalue, tags)

def _parse_protocol_psutil(self, conn):
Comment on lines +145 to 157
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this what Linux implementation has. Metrics for tcp4, tcp6 and just tcp?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The linux implementation only submits the combined value for these metrics. The tcp4 and tcp6 metrics added by this change are not available on the linux implementation

"""
Returns a string describing the protocol for the given connection
Expand Down
Loading
Loading