summaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2017-02-13 19:18:17 +0100
committerGiampaolo Rodola <g.rodola@gmail.com>2017-02-13 19:18:17 +0100
commit66ea5055819ffdd031a299dd2037bfce77323627 (patch)
tree09a8a4088263e6485d58703cd555d77704322c1d /scripts
parent98babf25006c3f688c46b06d39b27385a65b9313 (diff)
downloadpsutil-66ea5055819ffdd031a299dd2037bfce77323627.tar.gz
add sensors.py example script
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/battery.py2
-rwxr-xr-xscripts/sensors.py108
2 files changed, 109 insertions, 1 deletions
diff --git a/scripts/battery.py b/scripts/battery.py
index 23e0f669..abbad878 100755
--- a/scripts/battery.py
+++ b/scripts/battery.py
@@ -7,7 +7,7 @@
"""
Show battery information.
-$ python battery.py
+$ python scripts/battery.py
charge: 74%
left: 2:11:31
status: discharging
diff --git a/scripts/sensors.py b/scripts/sensors.py
new file mode 100755
index 00000000..99d41596
--- /dev/null
+++ b/scripts/sensors.py
@@ -0,0 +1,108 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+# Copyright (c) 2009, Giampaolo Rodola'. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+"""
+A clone of 'sensors' utility on Linux printing hardware temperatures.
+
+$ python scripts/sensors.py
+SENSORS
+=======
+
+asus
+ Temperatures:
+ asus 50.0 °C (high=None °C, critical=None °C)
+ Fans:
+ cpu_fan 3300 RPM
+
+acpitz
+ Temperatures:
+ acpitz 50.0 °C (high=108.0 °C, critical=108.0 °C)
+
+coretemp
+ Temperatures:
+ Physical id 0 51.0 °C (high=87.0 °C, critical=105.0 °C)
+ Core 0 49.0 °C (high=87.0 °C, critical=105.0 °C)
+ Core 1 51.0 °C (high=87.0 °C, critical=105.0 °C)
+
+BATTERY
+=======
+
+ charge: 87.51%
+ left: 1:12:28
+ status: discharging
+ plugged in: no
+"""
+
+from __future__ import print_function
+import sys
+
+import psutil
+
+
+def secs2hours(secs):
+ mm, ss = divmod(secs, 60)
+ hh, mm = divmod(mm, 60)
+ return "%d:%02d:%02d" % (hh, mm, ss)
+
+
+def main():
+ if hasattr(psutil, "sensors_temperatures"):
+ temps = psutil.sensors_temperatures()
+ else:
+ temps = {}
+ if hasattr(psutil, "sensors_fans"):
+ fans = psutil.sensors_fans()
+ else:
+ fans = {}
+ if hasattr(psutil, "sensors_battery"):
+ battery = psutil.sensors_battery()
+ else:
+ battery = None
+
+ if not any((temps, fans, battery)):
+ return sys.exit("can't read any temperature, fans or battery info")
+
+ if temps or fans:
+ print("SENSORS")
+ print("=======\n")
+
+ names = set(temps.keys() + fans.keys())
+ for name in names:
+ print(name)
+ # Temperatures.
+ if name in temps:
+ print(" Temperatures:")
+ for entry in temps[name]:
+ print(" %-20s %s °C (high=%s °C, critical=%s °C)" % (
+ entry.label or name, entry.current, entry.high,
+ entry.critical))
+ # Fans.
+ if name in fans:
+ print(" Fans:")
+ for entry in fans[name]:
+ print(" %-20s %s RPM" % (
+ entry.label or name, entry.current))
+
+ print()
+
+ # Battery
+ if battery:
+ print("BATTERY")
+ print("=======\n")
+ print(" charge: %s%%" % round(battery.percent, 2))
+ if battery.power_plugged:
+ print(" status: %s" % (
+ "charging" if battery.percent < 100 else "fully charged"))
+ print(" plugged in: yes")
+ else:
+ print(" left: %s" % secs2hours(battery.secsleft))
+ print(" status: %s" % "discharging")
+ print(" plugged in: no")
+
+
+if __name__ == '__main__':
+ main()