psutil

Форк
0
/
sensors.py 
97 строк · 2.8 Кб
1
#!/usr/bin/env python3
2
# -*- coding: utf-8 -*-
3

4
# Copyright (c) 2009, Giampaolo Rodola'. All rights reserved.
5
# Use of this source code is governed by a BSD-style license that can be
6
# found in the LICENSE file.
7

8
"""A clone of 'sensors' utility on Linux printing hardware temperatures,
9
fans speed and battery info.
10

11
$ python3 scripts/sensors.py
12
asus
13
    Temperatures:
14
        asus                 57.0°C (high=None°C, critical=None°C)
15
    Fans:
16
        cpu_fan              3500 RPM
17
acpitz
18
    Temperatures:
19
        acpitz               57.0°C (high=108.0°C, critical=108.0°C)
20
coretemp
21
    Temperatures:
22
        Physical id 0        61.0°C (high=87.0°C, critical=105.0°C)
23
        Core 0               61.0°C (high=87.0°C, critical=105.0°C)
24
        Core 1               59.0°C (high=87.0°C, critical=105.0°C)
25
Battery:
26
    charge:     84.95%
27
    status:     charging
28
    plugged in: yes
29
"""
30

31
from __future__ import print_function
32

33
import psutil
34

35

36
def secs2hours(secs):
37
    mm, ss = divmod(secs, 60)
38
    hh, mm = divmod(mm, 60)
39
    return "%d:%02d:%02d" % (hh, mm, ss)
40

41

42
def main():
43
    if hasattr(psutil, "sensors_temperatures"):
44
        temps = psutil.sensors_temperatures()
45
    else:
46
        temps = {}
47
    fans = psutil.sensors_fans() if hasattr(psutil, "sensors_fans") else {}
48
    if hasattr(psutil, "sensors_battery"):
49
        battery = psutil.sensors_battery()
50
    else:
51
        battery = None
52

53
    if not any((temps, fans, battery)):
54
        print("can't read any temperature, fans or battery info")
55
        return
56

57
    names = set(list(temps.keys()) + list(fans.keys()))
58
    for name in names:
59
        print(name)
60
        # Temperatures.
61
        if name in temps:
62
            print("    Temperatures:")
63
            for entry in temps[name]:
64
                s = "        %-20s %s°C (high=%s°C, critical=%s°C)" % (
65
                    entry.label or name,
66
                    entry.current,
67
                    entry.high,
68
                    entry.critical,
69
                )
70
                print(s)
71
        # Fans.
72
        if name in fans:
73
            print("    Fans:")
74
            for entry in fans[name]:
75
                print(
76
                    "        %-20s %s RPM"
77
                    % (entry.label or name, entry.current)
78
                )
79

80
    # Battery.
81
    if battery:
82
        print("Battery:")
83
        print("    charge:     %s%%" % round(battery.percent, 2))
84
        if battery.power_plugged:
85
            print(
86
                "    status:     %s"
87
                % ("charging" if battery.percent < 100 else "fully charged")
88
            )
89
            print("    plugged in: yes")
90
        else:
91
            print("    left:       %s" % secs2hours(battery.secsleft))
92
            print("    status:     %s" % "discharging")
93
            print("    plugged in: no")
94

95

96
if __name__ == '__main__':
97
    main()
98

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.