diff options
author | Eugeni Dodonov <eugeni@mandriva.com> | 2010-06-05 17:48:58 -0300 |
---|---|---|
committer | Eugeni Dodonov <eugeni@mandriva.com> | 2010-06-05 17:48:58 -0300 |
commit | c48c9cfc1cdbcb71d236f2f7852626289947f14c (patch) | |
tree | d8432bc8c44608b3bc20cbe2455aba6f4d05a585 /src | |
parent | 978262c25363b68c6723dade1291806f7767d530 (diff) | |
download | net_monitor-c48c9cfc1cdbcb71d236f2f7852626289947f14c.tar net_monitor-c48c9cfc1cdbcb71d236f2f7852626289947f14c.tar.gz net_monitor-c48c9cfc1cdbcb71d236f2f7852626289947f14c.tar.bz2 net_monitor-c48c9cfc1cdbcb71d236f2f7852626289947f14c.tar.xz net_monitor-c48c9cfc1cdbcb71d236f2f7852626289947f14c.zip |
add support for reading connection status
Diffstat (limited to 'src')
-rw-r--r-- | src/monitor.py | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/src/monitor.py b/src/monitor.py index f52d2cd..b330409 100644 --- a/src/monitor.py +++ b/src/monitor.py @@ -236,6 +236,32 @@ class Monitor: pass return routes, default_routes + def get_connections(self, proto="tcp"): + """Reads active connections""" + connections=[] + try: + with open("/proc/net/%s" % proto) as fd: + data = fd.readlines()[1:] + except: + # unable to read connections + traceback.print_exc() + return connections + + # parse connections + for l in data: + fields = l.strip().split() + loc=fields[1] + rem=fields[2] + status=fields[3] + loc_a,loc_p = loc.split(":") + rem_a,rem_p = rem.split(":") + loc_addr = socket.inet_ntoa(struct.pack('i', int(loc_a, 16))) + loc_port = (int(loc_p, 16)) + rem_addr = socket.inet_ntoa(struct.pack('i', int(rem_a, 16))) + rem_port = (int(rem_p, 16)) + connections.append((loc_addr, loc_port, rem_addr, rem_port, status)) + return connections + def load_uptime_log(self): """Loads network uptime log, handled by /etc/sysconfig/network-scripts/if{up,down}.d/netprofile*""" self.uptime_log = {} |