Skip to content

Commit

Permalink
#250: Windows implementation (still incomplete)
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Feb 12, 2015
1 parent d5271ae commit 1999bd9
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 4 deletions.
6 changes: 2 additions & 4 deletions psutil/_psutil_sunos.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down
86 changes: 86 additions & 0 deletions psutil/_psutil_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -3169,6 +3169,90 @@ psutil_net_if_addrs(PyObject *self, PyObject *args)
}


/*
* 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 ---------------------------

Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions psutil/_psutil_windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 10 additions & 0 deletions psutil/_pswindows.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down

0 comments on commit 1999bd9

Please sign in to comment.