summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2017-01-26 23:26:13 +0100
committerGiampaolo Rodola <g.rodola@gmail.com>2017-01-26 23:26:13 +0100
commit022cf0a05d34f4274269d4f8002ee95b9f3e32d2 (patch)
tree3311d0d9f1ab71dcc971a3993e622b841de883ee
parente2d8f58a1833686024b013de707c4786abce1279 (diff)
downloadpsutil-022cf0a05d34f4274269d4f8002ee95b9f3e32d2.tar.gz
#955: sensors_batter() freebsd impl
-rw-r--r--psutil/_psbsd.py14
-rw-r--r--psutil/_psutil_bsd.c4
-rw-r--r--psutil/arch/bsd/freebsd.c22
-rw-r--r--psutil/arch/bsd/freebsd.h3
-rwxr-xr-xpsutil/tests/test_bsd.py16
5 files changed, 59 insertions, 0 deletions
diff --git a/psutil/_psbsd.py b/psutil/_psbsd.py
index 022f5758..20f9cbcb 100644
--- a/psutil/_psbsd.py
+++ b/psutil/_psbsd.py
@@ -396,6 +396,20 @@ def net_connections(kind):
# =====================================================================
+# --- sensors
+# =====================================================================
+
+
+def sensors_battery():
+ percent, minsleft = cext.sensors_battery()
+ if minsleft == -1:
+ secsleft = _common.POWER_TIME_UNLIMITED
+ else:
+ secsleft = minsleft * 60
+ return _common.sbattery(percent, secsleft)
+
+
+# =====================================================================
# --- other system functions
# =====================================================================
diff --git a/psutil/_psutil_bsd.c b/psutil/_psutil_bsd.c
index adcedf79..de748dcc 100644
--- a/psutil/_psutil_bsd.c
+++ b/psutil/_psutil_bsd.c
@@ -934,6 +934,10 @@ PsutilMethods[] = {
{"net_connections", psutil_net_connections, METH_VARARGS,
"Return system-wide open connections."},
#endif
+#if defined(PSUTIL_FREEBSD)
+ {"sensors_battery", psutil_sensors_battery, METH_VARARGS,
+ "Return battery information."},
+#endif
{NULL, NULL, 0, NULL}
};
diff --git a/psutil/arch/bsd/freebsd.c b/psutil/arch/bsd/freebsd.c
index 456a50aa..c0286c86 100644
--- a/psutil/arch/bsd/freebsd.c
+++ b/psutil/arch/bsd/freebsd.c
@@ -994,3 +994,25 @@ error:
PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}
+
+
+/*
+ * Return battery information.
+ */
+PyObject *
+psutil_sensors_battery(PyObject *self, PyObject *args) {
+ int percent;
+ int minsleft;
+ size_t size = sizeof(percent);
+
+ if (sysctlbyname("hw.acpi.battery.life", &percent, &size, NULL, 0))
+ goto error;
+ // -1 if power is connected
+ if (sysctlbyname("hw.acpi.battery.time", &minsleft, &size, NULL, 0))
+ goto error;
+ return Py_BuildValue("ii", percent, minsleft);
+
+error:
+ PyErr_SetFromErrno(PyExc_OSError);
+ return NULL;
+}
diff --git a/psutil/arch/bsd/freebsd.h b/psutil/arch/bsd/freebsd.h
index e15706c6..0df66ecc 100644
--- a/psutil/arch/bsd/freebsd.h
+++ b/psutil/arch/bsd/freebsd.h
@@ -27,3 +27,6 @@ PyObject* psutil_proc_threads(PyObject* self, PyObject* args);
PyObject* psutil_swap_mem(PyObject* self, PyObject* args);
PyObject* psutil_virtual_mem(PyObject* self, PyObject* args);
PyObject* psutil_cpu_stats(PyObject* self, PyObject* args);
+#if defined(PSUTIL_FREEBSD)
+PyObject* psutil_sensors_battery(PyObject* self, PyObject* args);
+#endif
diff --git a/psutil/tests/test_bsd.py b/psutil/tests/test_bsd.py
index 244672e6..479237e5 100755
--- a/psutil/tests/test_bsd.py
+++ b/psutil/tests/test_bsd.py
@@ -357,6 +357,22 @@ class FreeBSDSpecificTestCase(unittest.TestCase):
btime = int(s)
self.assertEqual(btime, psutil.boot_time())
+ @unittest.skipUnless(psutil.sensors_battery(), "no battery")
+ def test_sensors_battery(self):
+ def secs2hours(secs):
+ m, s = divmod(secs, 60)
+ h, m = divmod(m, 60)
+ return "%d:%02d" % (h, m)
+
+ out = sh("acpiconf -i 0")
+ fields = dict([(x.split('\t')[0], x.split('\t')[-1])
+ for x in out.split("\n")])
+ metrics = psutil.sensors_battery()
+ percent = int(fields['Remaining capacity:'].replace('%', ''))
+ remaining_time = fields['Remaining time:']
+ self.assertEqual(metrics.percent, percent)
+ self.assertEqual(secs2hours(metrics.secsleft), remaining_time)
+
# =====================================================================
# --- OpenBSD