1 # -*- coding: utf-8 -*-
3 from __future__
import absolute_import
15 sys
.stdout
.write(curses
.tigetstr("clear"))
18 class Monitor(object):
22 self
.last_conntrack
= {}
27 self
.fd_map
.update(proc
.get_fd_map())
28 self
.last_conntrack
= copy
.deepcopy(self
.conntrack
)
29 self
.conntrack
.update(proc
.parse_ip_conntrack())
30 self
.connections
.update(proc
.get_connections())
33 for con
in self
.connections
.itervalues():
34 inode
= con
.get('inode', None)
35 process
= self
.fd_map
.get(inode
, None)
40 key_in
= proc
.ip_hash(con
['remote'], con
['local'])
41 key_out
= proc
.ip_hash(con
['local'], con
['remote'])
42 keys
= {'in': key_in
, 'out': key_out
}
43 new_byte
= {'in': 0, 'out': 0}
45 for direction
in ('in', 'out'):
47 if k
in self
.conntrack
:
48 if key_in
in self
.last_conntrack
:
49 new_byte
[direction
] = int(self
.conntrack
[k
]['bytes']) - int(self
.last_conntrack
[k
]['bytes'])
51 new_byte
[direction
] = int(self
.conntrack
[k
]['bytes'])
53 if process
['cmd'] in self
.tracking
:
54 old_in
, old_out
= self
.tracking
[process
['cmd']]
59 self
.tracking
[process
['cmd']] = (old_in
+ new_byte
['in'], old_out
+ new_byte
['out'])
63 return cmp(a
[1], b
[1])
66 for cmd
, bytes
in sorted(self
.tracking
.iteritems(), cmp=compare
):
68 cmd
= cmd
[:57] + '...'
69 print '%10d / %10d -- %s' % (bytes
[0], bytes
[1], cmd
)
80 # prints a list of processes
84 # for con in connections.itervalues():
86 # if not con['inode']:
89 # if not con['inode'] in fd_map:
90 # print 'inode %s not im map' % con['inode']
93 # process = fd_map[con['inode']]
96 # key1 = proc.ip_hash(con['local'], con['remote'])
97 # key2 = proc.ip_hash(con['remote'], con['local'])
100 # if key1 in conntrack:
102 # elif key2 in conntrack:
105 # if key and 'bytes' in conntrack[key]:
106 # byte_count = conntrack[key]['bytes']
110 # print '%d: %s: %s -> %s (%s Bytes)' % (process['pid'], process['cmd'], con['local'], con['remote'], byte_count)