aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEugeni Dodonov <eugeni@mandriva.com>2009-09-29 14:18:24 -0300
committerEugeni Dodonov <eugeni@mandriva.com>2009-09-29 14:18:24 -0300
commite6aa6bf53cb954f1a937f6cafc79e4c60c172cd3 (patch)
treed3bc3b61d6e44a39a7c24ab4b970810e6ecb9e66
parent9795b7728134d4d939c0f364f37ac1a378de434c (diff)
downloadnet_monitor-e6aa6bf53cb954f1a937f6cafc79e4c60c172cd3.tar
net_monitor-e6aa6bf53cb954f1a937f6cafc79e4c60c172cd3.tar.gz
net_monitor-e6aa6bf53cb954f1a937f6cafc79e4c60c172cd3.tar.bz2
net_monitor-e6aa6bf53cb954f1a937f6cafc79e4c60c172cd3.tar.xz
net_monitor-e6aa6bf53cb954f1a937f6cafc79e4c60c172cd3.zip
converted to python module format
-rw-r--r--setup.py8
-rw-r--r--src/__init__.py0
-rw-r--r--src/_native.c (renamed from native/net_monitor.c)8
-rwxr-xr-xsrc/net_monitor (renamed from net_monitor)0
-rw-r--r--src/wifi.py75
5 files changed, 84 insertions, 7 deletions
diff --git a/setup.py b/setup.py
index 3d2af8c..fdd32bf 100644
--- a/setup.py
+++ b/setup.py
@@ -2,8 +2,8 @@ from version import *
from distutils.core import setup, Extension
-module1 = Extension('net_monitor',
- sources=['native/net_monitor.c'])
+module1 = Extension('net_monitor/_native',
+ sources=['src/_native.c'])
setup (name='net_monitor',
version=version,
@@ -19,5 +19,7 @@ old net_monitor from drakx-net. It supports graphical network monitoring and
some advanced features, such as network profiling, activity monitoring,
detailed logging and network traffic statistics with help of vnstat reporting.
""",
- scripts=["net_monitor"],
+ packages=["net_monitor"],
+ package_dir = {"net_monitor": "src"},
+ scripts=["src/net_monitor"],
ext_modules=[module1])
diff --git a/src/__init__.py b/src/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/__init__.py
diff --git a/native/net_monitor.c b/src/_native.c
index 2594a65..5524c66 100644
--- a/native/net_monitor.c
+++ b/src/_native.c
@@ -187,7 +187,7 @@ iw_get_range_info(int skfd, const char *ifname, iwrange * range)
}
static PyObject *
- get_max_quality(PyObject *self, PyObject *args)
+ wifi_get_max_quality(PyObject *self, PyObject *args)
{
const char *iface;
int max_quality;
@@ -216,14 +216,14 @@ static PyObject *
/* python module details */
static PyMethodDef net_monitor_Methods[] = {
- {"get_max_quality", get_max_quality, METH_VARARGS,
+ {"wifi_get_max_quality", wifi_get_max_quality, METH_VARARGS,
"Find maximum quality value for a wireless interface."},
{NULL, NULL, 0, NULL} /* Sentinel */
};
PyMODINIT_FUNC
-initnet_monitor(void)
+init_native(void)
{
- (void) Py_InitModule("net_monitor", net_monitor_Methods);
+ (void) Py_InitModule("_native", net_monitor_Methods);
}
diff --git a/net_monitor b/src/net_monitor
index fc05f2d..fc05f2d 100755
--- a/net_monitor
+++ b/src/net_monitor
diff --git a/src/wifi.py b/src/wifi.py
new file mode 100644
index 0000000..28e06a6
--- /dev/null
+++ b/src/wifi.py
@@ -0,0 +1,75 @@
+#!/usr/bin/python
+"""net_monitor: wifi monitoring"""
+
+# native librari implements a few bits
+import _native
+import socket
+import fcntl
+import struct
+import traceback
+import array
+
+
+class Wireless:
+ # based on http://svn.pardus.org.tr/pardus/tags/pardus-1.0/system/base/wireless-tools/comar/link.py
+
+ # wireless IOCTL constants
+ SIOCGIWMODE = 0x8B07 # get operation mode
+ SIOCGIWRATE = 0x8B21 # get default bit rate
+ SIOCGIWESSID = 0x8B1B # get essid
+
+ # wireless modes
+ modes = ['Auto', 'Ad-Hoc', 'Managed', 'Master', 'Repeat', 'Second', 'Monitor']
+
+ def __init__(self):
+ self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+
+ def ioctl(self, func, params):
+ return fcntl.ioctl(self.sock.fileno(), func, params)
+
+ def get_max_quality(self, iface):
+ """Gets maximum quality value"""
+ return _native.wifi_get_max_quality(iface)
+
+ def call(self, iface, func, arg=None):
+ if not arg:
+ data = (iface + '\0' * 32)[:32]
+ else:
+ data = (iface + '\0' * 16)[:16] + arg
+ try:
+ res = self.ioctl(func, data)
+ return res
+ except:
+ traceback.print_exc()
+ return None
+
+ def get_essid(self, iface):
+ """Get current essid for an interface"""
+ buffer = array.array('c', '\0' * 16)
+ addr, length = buffer.buffer_info()
+ arg = struct.pack('Pi', addr, length)
+ self.call(iface, self.SIOCGIWESSID, arg)
+ return buffer.tostring().strip('\0')
+
+ def get_mode(self, iface):
+ """Get current mode from an interface"""
+ result = self.call(iface, self.SIOCGIWMODE)
+ mode = struct.unpack("i", result[16:20])[0]
+ return self.modes[mode]
+
+ def get_bitrate(self, iface):
+ """Gets current operating rate from an interface"""
+ # Note: KILO is not 2^10 in wireless tools world
+
+ result = self.call(iface, self.SIOCGIWRATE)
+
+ if result:
+ size = struct.calcsize('ihbb')
+ m, e, i, pad = struct.unpack('ihbb', result[16:16+size])
+ if e == 0:
+ bitrate = m
+ else:
+ bitrate = float(m) * 10**e
+ return bitrate
+ else:
+ return -1