diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2015-02-12 13:26:55 -0800 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2015-02-12 13:26:55 -0800 |
commit | 1999bd95aefbdee025a9f3eb41e409e803083791 (patch) | |
tree | 3b96997b84b8cf8cfbd9d1a43c86b9b1a18e50e0 | |
parent | d5271aed9a343ea3c34d56a8e4cd55499dfb950b (diff) | |
download | psutil-nicstats-250.tar.gz |
#250: Windows implementation (still incomplete)nicstats-250
-rw-r--r-- | psutil/_psutil_sunos.c | 6 | ||||
-rw-r--r-- | psutil/_psutil_windows.c | 86 | ||||
-rw-r--r-- | psutil/_psutil_windows.h | 1 | ||||
-rw-r--r-- | psutil/_pswindows.py | 10 |
4 files changed, 99 insertions, 4 deletions
diff --git a/psutil/_psutil_sunos.c b/psutil/_psutil_sunos.c index 2b03f3de..380212cb 100644 --- a/psutil/_psutil_sunos.c +++ b/psutil/_psutil_sunos.c @@ -1166,9 +1166,6 @@ psutil_net_if_stats(PyObject* self, PyObject* args) kstat_named_t *knp; int ret; int sock = 0; - int speed; - int duplex; - int mtu; PyObject *py_retdict = PyDict_New(); PyObject *py_ifc_info = NULL; @@ -1226,7 +1223,8 @@ psutil_net_if_stats(PyObject* self, PyObject* args) // speed if ((knp = kstat_data_lookup(ksp, "ifspeed")) != NULL) - speed = knp->value.ui64 / 10000000; + // expressed in bits per sec, we want mega bits per sec + speed = knp->value.ui64 / 1000000; else speed = 0; diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c index 0ee9d9a3..4d68eecb 100644 --- a/psutil/_psutil_windows.c +++ b/psutil/_psutil_windows.c @@ -3169,6 +3169,90 @@ error: } +/* + * Provides stats about NIC interfaces installed on the system. + * TODO: filter what's not a NIC + * TODO: return the same (friendly) names as net_io_counters() and + net_if_addrs() + * TODO: get 'duplex' (currently it's hard coded to '2', aka + 'full duplex') + */ +static PyObject * +psutil_net_if_stats(PyObject *self, PyObject *args) +{ + int i; + DWORD dwSize = 0; + DWORD dwRetVal = 0; + MIB_IFTABLE *pIfTable; + MIB_IFROW *pIfRow; + + PyObject *py_retdict = PyDict_New(); + PyObject *py_ifc_info = NULL; + PyObject *py_is_up = NULL; + + if (py_retdict == NULL) + return NULL; + pIfTable = (MIB_IFTABLE *) malloc(sizeof (MIB_IFTABLE)); + if (pIfTable == NULL) { + PyErr_NoMemory(); + goto error; + } + dwSize = sizeof(MIB_IFTABLE); + if (GetIfTable(pIfTable, &dwSize, FALSE) == ERROR_INSUFFICIENT_BUFFER) { + free(pIfTable); + pIfTable = (MIB_IFTABLE *) malloc(dwSize); + if (pIfTable == NULL) { + PyErr_NoMemory(); + goto error; + } + } + // Make a second call to GetIfTable to get the actual + // data we want. + if ((dwRetVal = GetIfTable(pIfTable, &dwSize, FALSE)) != NO_ERROR) { + PyErr_SetString(PyExc_RuntimeError, "GetIfTable() failed"); + goto error; + } + + for (i = 0; i < (int) pIfTable->dwNumEntries; i++) { + pIfRow = (MIB_IFROW *) & pIfTable->table[i]; + + // is up? + if((pIfRow->dwOperStatus == MIB_IF_OPER_STATUS_CONNECTED || + pIfRow->dwOperStatus == MIB_IF_OPER_STATUS_OPERATIONAL) && + pIfRow->dwAdminStatus == 1 ) { + py_is_up = Py_True; + } + else { + py_is_up = Py_False; + } + Py_INCREF(py_is_up); + + py_ifc_info = Py_BuildValue( + "(Oikk)", + py_is_up, + 2, // there's no way to know duplex so let's assume 'full' + pIfRow->dwSpeed / 1000000, // expressed in bytes, we want Mb + pIfRow->dwMtu + ); + if (!py_ifc_info) + goto error; + if (PyDict_SetItemString(py_retdict, pIfRow->bDescr, py_ifc_info)) + goto error; + Py_DECREF(py_ifc_info); + } + + free(pIfTable); + return py_retdict; + +error: + Py_XDECREF(py_is_up); + Py_XDECREF(py_ifc_info); + Py_DECREF(py_retdict); + if (pIfTable != NULL) + free(pIfTable); + return NULL; +} + // ------------------------ Python init --------------------------- @@ -3276,6 +3360,8 @@ PsutilMethods[] = "Return system-wide connections"}, {"net_if_addrs", psutil_net_if_addrs, METH_VARARGS, "Return NICs addresses."}, + {"net_if_stats", psutil_net_if_stats, METH_VARARGS, + "Return NICs stats."}, // --- windows API bindings {"win32_QueryDosDevice", psutil_win32_QueryDosDevice, METH_VARARGS, diff --git a/psutil/_psutil_windows.h b/psutil/_psutil_windows.h index 862da9f2..271787d9 100644 --- a/psutil/_psutil_windows.h +++ b/psutil/_psutil_windows.h @@ -61,6 +61,7 @@ static PyObject* psutil_ppid_map(PyObject* self, PyObject* args); static PyObject* psutil_users(PyObject* self, PyObject* args); static PyObject* psutil_virtual_mem(PyObject* self, PyObject* args); static PyObject* psutil_net_if_addrs(PyObject* self, PyObject* args); +static PyObject* psutil_net_if_stats(PyObject* self, PyObject* args); // --- windows API bindings diff --git a/psutil/_pswindows.py b/psutil/_pswindows.py index 43008487..ad3645b4 100644 --- a/psutil/_pswindows.py +++ b/psutil/_pswindows.py @@ -208,6 +208,16 @@ def net_connections(kind, _pid=-1): nt = _common.pconn(fd, fam, type, laddr, raddr, status) ret.add(nt) return list(ret) + + +def net_if_stats(): + ret = cext.net_if_stats() + for name, items in ret.items(): + isup, duplex, speed, mtu = items + if hasattr(_common, 'NicDuplex'): + duplex = _common.NicDuplex(duplex) + ret[name] = _common.snicstats(isup, duplex, speed, mtu) + return ret def users(): |