-
Notifications
You must be signed in to change notification settings - Fork 1
/
pynethogs.py
47 lines (34 loc) · 1.51 KB
/
pynethogs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env python
import os
import json
import ctypes
LIBNETHOGS_VERSION = "0.8.5-54-gac5af1d"
NETHOGS_APP_ACTION_SET = 1
NETHOGS_APP_ACTION_REMOVE = 2
CMPFUNC_t = None
class NethogsMonitorRecord(ctypes.Structure):
_fields_ = [
('record_id', ctypes.c_int),
('name', ctypes.c_char_p),
('pid', ctypes.c_int),
('uid', ctypes.c_uint),
('device_name', ctypes.c_char_p),
('sent_bytes', ctypes.c_ulonglong),
('recv_bytes', ctypes.c_ulonglong),
('sent_kbs', ctypes.c_float),
('recv_kbs', ctypes.c_float)]
def asdict(self):
return {'record_id': self.record_id, 'name': self.name.decode('utf-8'), 'pid': self.pid, 'uid': self.uid, 'device_name': self.device_name.decode('utf-8'), 'sent_bytes': self.sent_bytes, 'recv_bytes': self.recv_bytes, 'sent_kbs': self.sent_kbs, 'recv_kbs': self.recv_kbs}
def main(queue):
global CMPFUNC_t
libnethogs = ctypes.CDLL("lib/libnethogs.so.%s" % LIBNETHOGS_VERSION)
NethogsMonitorRecord_ptr = ctypes.POINTER(NethogsMonitorRecord)
CMPFUNC_t = ctypes.CFUNCTYPE(None, ctypes.c_int, NethogsMonitorRecord_ptr)
def callback(action, update):
update_dict = update.contents.asdict()
update_dict['action'] = action
queue.put(json.dumps(update_dict))
dist_callback = CMPFUNC_t(callback)
libnethogs.nethogsmonitor_loop.argtypes = [CMPFUNC_t, ctypes.c_bool]
libnethogs.nethogsmonitor_loop.restype = ctypes.c_int
libnethogs.nethogsmonitor_loop(dist_callback, None)