Amazing-Python-Scripts

Форк
0
54 строки · 1.8 Кб
1
import psutil
2

3
# Function to retrieve CPU temperature
4

5

6
def get_cpu_temperature() -> float | str:
7
    """A function which returns the temperature of the CPU
8

9
    Returns:
10
        float | str: The temperature value in Celsius or 'NA' if temperature info. is unavailable
11
    """
12
    try:
13
        # Retrieve temperature information using psutil
14
        # and access the first temperature value from the 'coretemp' key
15
        temperature = psutil.sensors_temperatures()['coretemp'][0].current
16
        return temperature
17
    except (KeyError, IndexError):
18
        # Handle cases where temperature information is not available
19
        return "NA"
20

21

22
def get_ram_and_cpu_util() -> list:
23
    """Get the utilization of system RAM and CPU
24

25
    Returns:
26
        list: Contains total memory of system, percentage utilized and percantage of CPU utilized
27
    """
28
    memory_stats = psutil.virtual_memory()
29
    return [
30
        # Total memory available in the system (MB)
31
        memory_stats[0]//(1024*1024),
32
        memory_stats[2],  # Percentage of memory utilized
33
        # Percentage of CPU utilized
34
        psutil.cpu_percent(interval=4, percpu=True)
35
    ]
36

37

38
# Call the get_cpu_temperature() function to get the CPU temperature
39
cpu_temperature = get_cpu_temperature()
40

41
# Call the get_ram_and_cpu_util() function to get data on system resource utilization
42
system_utils = get_ram_and_cpu_util()
43

44
# Print the system info
45
if (type(system_utils[2]) == list):
46
    cpu_percentage = ""
47
    for i in system_utils[2]:
48
        cpu_percentage += "{}%, ".format(i)
49
else:
50
    cpu_percentage = '{}%'.format(system_utils[2])
51

52
print(
53
    f"Current CPU Temperature in Celsius is {cpu_temperature}°C, with percentage utilized being at {cpu_percentage}")
54
print(f"Current RAM utilization is {system_utils[1]}% of {system_utils[0]} MB")
55

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

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

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

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