First draft of a script that will parse the output of netstat to simulate the
authorStephen Soltesz <soltesz@cs.princeton.edu>
Thu, 24 Jul 2008 21:07:34 +0000 (21:07 +0000)
committerStephen Soltesz <soltesz@cs.princeton.edu>
Thu, 24 Jul 2008 21:07:34 +0000 (21:07 +0000)
content of /proc/scout/ports/summary.

See https://svn.planet-lab.org/ticket/305 for additional comments.

factory/portsummary [new file with mode: 0755]

diff --git a/factory/portsummary b/factory/portsummary
new file mode 100755 (executable)
index 0000000..7ff7bc2
--- /dev/null
@@ -0,0 +1,56 @@
+#!/usr/bin/python
+
+import os
+import sys
+
+ns = os.popen("vcontext --xid 1 --migrate -- netstat -apnlut", 'r')
+port_summary = {}
+for line in ns:
+       try:
+               ns_fields = line.split()
+               if ns_fields[0] == "tcp" or ns_fields[0] == "udp":
+                       (src_ip, src_port) = ns_fields[3].split(':')
+                       if src_ip is not "0.0.0.0" and src_port in port_summary:
+                               # skip INADDR_ANY addresses and ports we've already seen.
+                               continue
+                       (dst_ip, dst_port) = ns_fields[4].split(':')
+                       conn_state = ns_fields[5]
+                       if ns_fields[0] == "tcp":
+                               proc_field = ns_fields[6]
+                       else:
+                               if conn_state == "ESTABLISHED":
+                                       proc_field = ns_fields[6]
+                               else:
+                                       proc_field = ns_fields[5]
+
+                       if proc_field != "-":
+                               (pid,procname)= proc_field.split('/')
+                       else:
+                               # TODO: ephemeral sockets in TIME_WAIT
+                               # TODO: XXX: THIS IS FALSE.  Need an actual way of finding the
+                               #                       XID associated with a socket in TIME_WAIT
+                               (pid,procname)= ("1","")
+
+                       if ( ns_fields[0] == "tcp" and src_ip == "0.0.0.0" and conn_state == "LISTEN" ) or \
+                          ( ns_fields[0] == "udp" and src_ip == "0.0.0.0" ):
+                               type='C'
+                       elif src_ip == "127.0.0.1":
+                               type='l'
+                       elif src_ip != "0.0.0.0" and src_ip != "127.0.0.1":
+                               type='c'
+                       else:
+                               type='?'
+
+                       xid_stream = os.popen("vserver-info %s XID" % pid)
+                       xid = xid_stream.read()
+
+                       port_summary[src_port] = {'prot' : ns_fields[0], 'port' : src_port, 'slice' : xid[:-1], 'type': type}
+       except:
+               import traceback; traceback.print_exc()
+               print line
+
+ports = port_summary.keys()
+ports.sort()
+for port in ports:
+       print "%(prot)4s\t%(port)6s\t%(slice)5s\t%(type)s" % port_summary[port]
+