Skip to content

Instantly share code, notes, and snippets.

@zoidyzoidzoid
Created June 7, 2018 15:11
Show Gist options
  • Save zoidyzoidzoid/981e661c5fedb565d2458edd0bad99a4 to your computer and use it in GitHub Desktop.
Save zoidyzoidzoid/981e661c5fedb565d2458edd0bad99a4 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# Some code borrowed from yapcg/measurements/perftools/udpstat at https://code.google.com/archive/p/yapcg/source/default/source?page=1
#
# man proc
# /proc/net/udp
# Holds a dump of the UDP socket table. Much of the information is not of use apart from debugging. The "sl" value is
# the kernel hash slot for the socket, the "local_address" is the local address and port number pair. The
# "rem_address" is the remote address and port number pair (if connected). "St" is the internal status of the socket.
# The "tx_queue" and "rx_queue" are the outgoing and incoming data queue in terms of kernel memory usage. The "tr",
# "tm->when", and "rexmits" fields are not used by UDP. The "uid" field holds the effective UID of the creator of the
# socket. The format is:
#
# sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode ref pointer drops
# 2353: 0100007F:0BD6 00000000:0000 07 00000000:00000000 00:00000000 00000000 999 0 177687670 2 0000000000000000 0
# 6106: 00000000:1A7F 00000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 90418 2 0000000000000000 13836
# 6107: 00000000:1A80 00000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 75393 2 0000000000000000 0
# 12203: 00000000:B250 00000000:0000 07 00000000:00000000 00:00000000 00000000 104 0 26038 2 0000000000000000 0
# 15830: 01E0220A:007B 00000000:0000 07 00000000:00000000 00:00000000 00000000 111 0 93186 2 0000000000000000 0
# 15830: E812020A:007B 00000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 16056 2 0000000000000000 0
# 15830: 0100007F:007B 00000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 16054 2 0000000000000000 0
# 15830: 00000000:007B 00000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 16050 2 0000000000000000 0
#
# rmem_buffer=`sysctl -n net.core.rmem_max 2>/dev/null`.to_i / divider
# tmem_buffer=`sysctl -n net.core.wmem_max 2>/dev/null`.to_i / divider
# m[6].to_i(16)/divider,rmem_buffer, m[5].to_i(16)/divider,tmem_buffer, m[7]
# print("%12s %15s:%-5s %15s:%-5s %9s %9s %9s %9s %7s" % ("Timestamp", "Local IP", "port", "Remote IP", "port", "rx" + "[B]", "rbuf" + "[B]", "tx" + "[B]", "tbuf" + "[B]", "drops"))
# print("%12s %15s:%-5s %15s:%-5s %9s %9s %9s %9s %7s" % ("", local_addr, local_port, rem_addr, rem_port, int(rx, 16), "", int(tx, 16), "", drops))
def parse_addr(addr):
addr, port = addr.split(':')
return '.'.join(reversed([str(int(addr[i * 2:(i + 1) * 2], 16)) for i in range(int(len(addr) / 2))])), str(int(port, 16))
if __name__ == '__main__':
# with open('/proc/net/udp') as f:
with open('udp-output') as f:
data = f.readlines()
print("%12s %15s:%-5s %15s:%-5s %9s %9s %7s" % ("Timestamp", "Local IP", "port", "Remote IP", "port", "rx" + "[B]", "tx" + "[B]", "drops"))
for line in data[1:]:
sl, local_addr, rem_addr, st, queues, _, _, uid, timeout, inode, ref, pointer, drops = line.split()
local_addr, local_port = parse_addr(local_addr)
rem_addr, rem_port = parse_addr(rem_addr)
tx, rx = queues.split(":")
print("%12s %15s:%-5s %15s:%-5s %9s %9s %7s" % ("", local_addr, local_port, rem_addr, rem_port, int(rx, 16), int(tx, 16), drops))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment