|
| 1 | +# (C) Datadog, Inc. 2023-present |
| 2 | +# All rights reserved |
| 3 | +# Licensed under a 3-clause BSD style license (see LICENSE) |
| 4 | + |
| 5 | +import array |
| 6 | +import fcntl |
| 7 | +import socket |
| 8 | +import struct |
| 9 | +import sys |
| 10 | + |
| 11 | +SIOCETHTOOL = 0x8946 |
| 12 | +ETHTOOL_GSTRINGS = 0x0000001B |
| 13 | +ETHTOOL_GSSET_INFO = 0x00000037 |
| 14 | +ETHTOOL_GSTATS = 0x0000001D |
| 15 | +ETHTOOL_GDRVINFO = 0x00000003 |
| 16 | +ETH_SS_STATS = 0x1 |
| 17 | +ETH_GSTRING_LEN = 32 |
| 18 | + |
| 19 | + |
| 20 | +def _send_ethtool_ioctl(iface, sckt, data): |
| 21 | + """ |
| 22 | + Send an ioctl SIOCETHTOOL call for given interface with given data. |
| 23 | + """ |
| 24 | + print('("{}", {}):'.format(iface, data.tobytes())) |
| 25 | + ifr = struct.pack('16sP', iface.encode('utf-8'), data.buffer_info()[0]) |
| 26 | + fcntl.ioctl(sckt.fileno(), SIOCETHTOOL, ifr) |
| 27 | + print('{},'.format(data.tobytes())) |
| 28 | + |
| 29 | + |
| 30 | +def _get_ethtool_gstringset(iface, sckt): |
| 31 | + """ |
| 32 | + Retrieve names of all ethtool stats for given interface. |
| 33 | + """ |
| 34 | + sset_info = array.array('B', struct.pack('IIQI', ETHTOOL_GSSET_INFO, 0, 1 << ETH_SS_STATS, 0)) |
| 35 | + _send_ethtool_ioctl(iface, sckt, sset_info) |
| 36 | + sset_mask, sset_len = struct.unpack('8xQI', sset_info) |
| 37 | + if sset_mask == 0: |
| 38 | + sset_len = 0 |
| 39 | + strings = array.array('B', struct.pack('III', ETHTOOL_GSTRINGS, ETH_SS_STATS, sset_len)) |
| 40 | + strings.extend([0] * sset_len * ETH_GSTRING_LEN) |
| 41 | + _send_ethtool_ioctl(iface, sckt, strings) |
| 42 | + |
| 43 | + all_names = [] |
| 44 | + for i in range(sset_len): |
| 45 | + offset = 12 + ETH_GSTRING_LEN * i |
| 46 | + name = strings[offset : offset + ETH_GSTRING_LEN] |
| 47 | + name = name.tobytes() |
| 48 | + name = name.partition(b'\x00')[0].decode('utf-8') |
| 49 | + all_names.append(name) |
| 50 | + return all_names |
| 51 | + |
| 52 | + |
| 53 | +def get_ethtool_drvinfo(iface, sckt): |
| 54 | + drvinfo = array.array('B', struct.pack('I', ETHTOOL_GDRVINFO)) |
| 55 | + drvinfo.extend([0] * (4 + 32 + 32 + 32 + 32 + 32 + 12 + 5 * 4)) |
| 56 | + _send_ethtool_ioctl(iface, sckt, drvinfo) |
| 57 | + driver_version = drvinfo[4 + 32 : 32 + 32] |
| 58 | + driver_version = driver_version.tobytes() |
| 59 | + driver_version = driver_version.partition(b'\x00')[0].decode('utf-8') |
| 60 | + driver_name = drvinfo[4 : 4 + 32] |
| 61 | + driver_name = driver_name.tobytes() |
| 62 | + driver_name = driver_name.partition(b'\x00')[0].decode('utf-8') |
| 63 | + |
| 64 | + |
| 65 | +def get_metric(iface, sckt): |
| 66 | + """ |
| 67 | + Get all ENA metrics specified in ENA_METRICS_NAMES list and their values from ethtool. |
| 68 | + """ |
| 69 | + stats_names = list(_get_ethtool_gstringset(iface, sckt)) |
| 70 | + stats_count = len(stats_names) |
| 71 | + |
| 72 | + stats = array.array('B', struct.pack('II', ETHTOOL_GSTATS, stats_count)) |
| 73 | + # we need `stats_count * (length of uint64)` for the result |
| 74 | + stats.extend([0] * len(struct.pack('Q', 0)) * stats_count) |
| 75 | + _send_ethtool_ioctl(iface, sckt, stats) |
| 76 | + |
| 77 | + |
| 78 | +if len(sys.argv) != 1: |
| 79 | + print('Provide a single network interface name as parameter (eth0, ens5...)') |
| 80 | + sys.exit(1) |
| 81 | + |
| 82 | + |
| 83 | +iface = sys.argv[1] |
| 84 | +ethtool_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0) |
| 85 | +get_ethtool_drvinfo(iface, ethtool_socket) |
| 86 | +print() |
| 87 | +get_metric(iface, ethtool_socket) |
0 commit comments