Amazing-Python-Scripts
54 строки · 1.8 Кб
1import psutil2
3# Function to retrieve CPU temperature
4
5
6def get_cpu_temperature() -> float | str:7"""A function which returns the temperature of the CPU8
9Returns:
10float | str: The temperature value in Celsius or 'NA' if temperature info. is unavailable
11"""
12try:13# Retrieve temperature information using psutil14# and access the first temperature value from the 'coretemp' key15temperature = psutil.sensors_temperatures()['coretemp'][0].current16return temperature17except (KeyError, IndexError):18# Handle cases where temperature information is not available19return "NA"20
21
22def get_ram_and_cpu_util() -> list:23"""Get the utilization of system RAM and CPU24
25Returns:
26list: Contains total memory of system, percentage utilized and percantage of CPU utilized
27"""
28memory_stats = psutil.virtual_memory()29return [30# Total memory available in the system (MB)31memory_stats[0]//(1024*1024),32memory_stats[2], # Percentage of memory utilized33# Percentage of CPU utilized34psutil.cpu_percent(interval=4, percpu=True)35]36
37
38# Call the get_cpu_temperature() function to get the CPU temperature
39cpu_temperature = get_cpu_temperature()40
41# Call the get_ram_and_cpu_util() function to get data on system resource utilization
42system_utils = get_ram_and_cpu_util()43
44# Print the system info
45if (type(system_utils[2]) == list):46cpu_percentage = ""47for i in system_utils[2]:48cpu_percentage += "{}%, ".format(i)49else:50cpu_percentage = '{}%'.format(system_utils[2])51
52print(53f"Current CPU Temperature in Celsius is {cpu_temperature}°C, with percentage utilized being at {cpu_percentage}")54print(f"Current RAM utilization is {system_utils[1]}% of {system_utils[0]} MB")55