Created
June 22, 2014 18:29
-
-
Save kaz-tk/507a934ff98632caf279 to your computer and use it in GitHub Desktop.
read /proc/net/tcp and parse and make statistics
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from distutils.core import setup | |
| setup(name='tcptuneinfo', | |
| version='0.1', | |
| description='', | |
| author='Kazushige TAKEUCHI', | |
| url='http://d.hatena.ne.jp/graceful_life/', | |
| requires=[ | |
| "netaddr", | |
| ], | |
| # packages=['distutils', 'distutils.command'], | |
| scripts=['tcptune.py'], | |
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding: utf-8 -*- | |
| __author__ = 'Kazushige TAKEUCHI' | |
| from multiprocessing import Process,Pool | |
| from netaddr import * | |
| import daemon | |
| import time | |
| from exceptions import KeyboardInterrupt | |
| import sys | |
| network_type = { | |
| IPNetwork("127.0.0.1"): "localhost", | |
| IPNetwork("172.16.0.0/12"):"management", | |
| } | |
| class TcpConnectionEntry(object): | |
| def __init__(self,entry): | |
| self.local = entry[1] | |
| self.localadr = None | |
| self.remote = entry[2] # addr:port | |
| self.remoteadr = None | |
| self.state = entry[3] # int | |
| queue = entry[4].split(":") # tx:rx | |
| self.queue_tx = int(queue[0],16) # tx:rx | |
| self.queue_rx = int(queue[1],16) # tx:rx | |
| self.user = entry[7] | |
| self.nw_type= "unknown" | |
| pass | |
| def decode_statistics(self): | |
| addr,port = self.remote.split(":") | |
| #print addr | |
| a="%d.%d.%d.%d" % (int(addr[6:8],16),int(addr[4:6],16),int(addr[2:4],16),int(addr[0:2],16)), | |
| self.remoteadr=IPAddress(a[0]) | |
| if self.remoteadr.value ==0: | |
| self.nw_type = "listen" | |
| if self.nw_type =="unknown": | |
| for key in network_type.keys(): | |
| if key.first <= self.remoteadr.value and self.remoteadr.value <= key.last: | |
| self.nw_type=network_type[key] | |
| pass | |
| if self.nw_type=="unknown": | |
| self.nw_type="default" | |
| pass | |
| return self | |
| pass | |
| def __str__(self): | |
| return "<Connection: %s %s>" % (self.nw_type, self.remoteadr) | |
| pass | |
| pass | |
| class TcpStatistics(object): | |
| def __init__(self): | |
| import re | |
| metcher= "" | |
| self.user = {} | |
| self.stat={} | |
| pass | |
| def parse_from_procentry(self,entry): | |
| ent = TcpConnectionEntry(entry) | |
| e= ent.decode_statistics() | |
| if not self.stat.has_key(e.nw_type): | |
| self.stat.update( | |
| {e.nw_type: { | |
| "count": 1, | |
| "tx": e.queue_tx, | |
| "rx": e.queue_rx | |
| } | |
| } | |
| ) | |
| pass | |
| else: | |
| self.stat[e.nw_type]["count"]= self.stat[e.nw_type]["count"]+1 | |
| self.stat[e.nw_type]["tx"]= self.stat[e.nw_type]["tx"]+e.queue_tx | |
| self.stat[e.nw_type]["rx"]= self.stat[e.nw_type]["rx"]+e.queue_rx | |
| pass | |
| # print e | |
| pass | |
| def clear(self): | |
| self.counter=[0,0,0,0,0,0,0,0,0,0,0] | |
| pass | |
| class TcpTuneInfoReader(Process): | |
| def __init__(self,queue=[]): | |
| super(TcpTuneInfoReader, self).__init__() | |
| self.queue=queue | |
| self.tcpinfo_path = "/proc/net/tcp" | |
| self.counter=[0,0,0,0,0,0,0,0,0,0,0] | |
| self.tx = 0 | |
| self.rx = 0 | |
| self.stat= TcpStatistics() | |
| pass | |
| def _parse_queue(self): | |
| fp= open(self.tcpinfo_path , 'r') | |
| flag= True | |
| for line in fp: | |
| if flag: | |
| flag=False | |
| else: | |
| entry=line.split() | |
| idx= int(entry[3],16) | |
| self.counter[idx] = self.counter[idx] +1 | |
| self.stat.parse_from_procentry(entry) | |
| pass | |
| fp.close() | |
| from datetime import datetime | |
| print datetime.now(), | |
| print self.stat.stat | |
| self.stat.stat ={} | |
| time.sleep(0.02) | |
| pass | |
| pass | |
| def run(self): | |
| while True: | |
| self._parse_queue() | |
| pass | |
| class TcpTuneInfoDaemon(object): | |
| def __init__(self): | |
| pass | |
| def start(self): | |
| p=TcpTuneInfoReader() | |
| p.daemon=True | |
| p.start() | |
| self._loop() | |
| pass | |
| def _loop(self): | |
| try: | |
| while True: | |
| time.sleep(1) | |
| pass | |
| except KeyboardInterrupt: | |
| print sys.exc_info()[0] | |
| finally: | |
| pass | |
| exit(0) | |
| pass | |
| pass | |
| def main(argv): | |
| tcptuneinfod = TcpTuneInfoDaemon() | |
| tcptuneinfod.start() | |
| pass | |
| if __name__=='__main__': | |
| main(sys.argv) | |
| pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment