Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 22nd, 2012  |  syntax: None  |  size: 3.33 KB  |  hits: 4  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/env python
  2. # ---------------------------------------------------------
  3.  
  4. import sys,time, math
  5.  
  6. # ---------------------------------------------------------
  7.  
  8. class ProcReader:
  9.   """
  10. Format of /proc/net/tcp
  11.  
  12. 46: 010310AC:9C4C 030310AC:1770 01
  13. |      |      |      |      |   |--> connection state
  14. |      |      |      |      |------> remote TCP port number
  15. |      |      |      |-------------> remote IPv4 address
  16. |      |      |--------------------> local TCP port number
  17. |      |---------------------------> local IPv4 address
  18. |----------------------------------> number of entry
  19.  
  20. 00000150:00000000 01:00000019 00000000
  21. |        |     |     |       |--> number of unrecovered RTO timeouts
  22. |        |     |     |----------> number of jiffies until timer expires
  23. |        |     |----------------> timer_active (see below)
  24. |        |----------------------> receive-queue
  25. |-------------------------------> transmit-queue
  26.  
  27. 1000        0 54165785 4 cd1e6040 25 4 27 3 -1
  28. |          |    |     |    |     |  | |  | |--> slow start size threshold,
  29. |          |    |     |    |     |  | |  |      or -1 if the threshold
  30. |          |    |     |    |     |  | |  |      is >= 0xFFFF
  31. |          |    |     |    |     |  | |  |----> sending congestion window
  32. |          |    |     |    |     |  | |-------> (ack.quick<<1)|ack.pingpong
  33. |          |    |     |    |     |  |---------> Predicted tick of soft clock
  34. |          |    |     |    |     |              (delayed ACK control data)
  35. |          |    |     |    |     |------------> retransmit timeout
  36. |          |    |     |    |------------------> location of socket in memory
  37. |          |    |     |-----------------------> socket reference count
  38. |          |    |-----------------------------> inode
  39. |          |----------------------------------> unanswered 0-window probes
  40. |---------------------------------------------> uid
  41. """
  42.  
  43.   def __init__(self,port):
  44.     self.port = port
  45.     self.f = open( "/proc/net/tcp" )
  46.     self.porthex = ":%04X" % (port)
  47.  
  48.   def read(self):
  49.     now = time.time()
  50.     self.f.seek(0)
  51.     lines = self.f.readlines()
  52.     n, sum_tx, sum_rx = 0, 0, 0
  53.     for line in lines:
  54.       t = line.split()
  55.       if not t[1].endswith(self.porthex):
  56.         continue
  57.       x = t[4].split(':')
  58.       n += 1
  59.       sum_tx += int(x[0],16)
  60.       sum_rx += int(x[1],16)
  61.     return now,sum_tx,sum_rx,n
  62.  
  63. # ---------------------------------------------------------
  64.        
  65. def main(port):
  66.   """
  67.      Display: timestamp
  68.               nb sockets
  69.               tx = pending data to transmit for all connections (bytes)
  70.               rx = pending data to receive for all connections (bytes)
  71.               running moving average of tx (1 sec, 4 points)
  72.               running moving average of rx (1 sec, 4 points)
  73.   """
  74.   p = ProcReader(port)
  75.   t, tx, rx = 0.0, 0.0, 0.0
  76.   print "%16s %6s %12s %12s %16s %16s" % ( "Timestamp", "Nb", "TX bytes", "RX bytes", "TX RMA", "RX RMA" )
  77.   while 1:
  78.     x = p.read()
  79.     tx = (3*tx+x[1])/4
  80.     rx = (3*rx+x[2])/4
  81.     if int(x[0]) != t:
  82.       print "%16.3f %6d %12d %12d %16.3f %16.3f" % (x[0],x[3],x[1],x[2],tx,rx)
  83.       t = int(x[0])
  84.     time.sleep(0.250-(time.time()-x[0]))
  85.  
  86. # ---------------------------------------------------------
  87.  
  88. if len(sys.argv) != 2:
  89.   sys.stderr.write( "Usage: %s port\n" % (sys.argv[0]) )
  90.   sys.exit( -1 )
  91.  
  92. main( int(sys.argv[1]) )
  93.  
  94. # ---------------------------------------------------------