|
1 | 1 | import psutil
|
2 | 2 |
|
3 | 3 | # Function to retrieve CPU temperature
|
4 |
| -def get_cpu_temperature(): |
| 4 | +def get_cpu_temperature() -> float | str: |
| 5 | + """A function which returns the temperature of the CPU |
| 6 | +
|
| 7 | + Returns: |
| 8 | + float | str: The temperature value in Celsius or 'NA' if temperature info. is unavailable |
| 9 | + """ |
5 | 10 | try:
|
6 | 11 | # Retrieve temperature information using psutil
|
7 | 12 | # and access the first temperature value from the 'coretemp' key
|
8 | 13 | temperature = psutil.sensors_temperatures()['coretemp'][0].current
|
9 | 14 | return temperature
|
10 | 15 | except (KeyError, IndexError):
|
11 | 16 | # Handle cases where temperature information is not available
|
12 |
| - return "CPU temperature information not available." |
| 17 | + return "NA" |
| 18 | + |
| 19 | +def get_ram_and_cpu_util() -> list: |
| 20 | + """Get the utilization of system RAM and CPU |
| 21 | +
|
| 22 | + Returns: |
| 23 | + list: Contains total memory of system, percentage utilized and percantage of CPU utilized |
| 24 | + """ |
| 25 | + memory_stats = psutil.virtual_memory() |
| 26 | + return [ |
| 27 | + memory_stats[0]//(1024*1024), #Total memory available in the system (MB) |
| 28 | + memory_stats[2], #Percentage of memory utilized |
| 29 | + psutil.cpu_percent(interval=4, percpu=True) #Percentage of CPU utilized |
| 30 | + ] |
| 31 | + |
13 | 32 |
|
14 | 33 | # Call the get_cpu_temperature() function to get the CPU temperature
|
15 | 34 | cpu_temperature = get_cpu_temperature()
|
16 | 35 |
|
17 |
| -# Print the CPU temperature |
18 |
| -print("Current CPU Temperature (Celsius):", cpu_temperature) |
| 36 | +#Call the get_ram_and_cpu_util() function to get data on system resource utilization |
| 37 | +system_utils = get_ram_and_cpu_util() |
| 38 | + |
| 39 | +# Print the system info |
| 40 | +if(type(system_utils[2]) == list): |
| 41 | + cpu_percentage = "" |
| 42 | + for i in system_utils[2]: |
| 43 | + cpu_percentage += "{}%, ".format(i) |
| 44 | +else: |
| 45 | + cpu_percentage = '{}%'.format(system_utils[2]) |
| 46 | + |
| 47 | +print(f"Current CPU Temperature in Celsius is {cpu_temperature}°C, with percentage utilized being at {cpu_percentage}") |
| 48 | +print(f"Current RAM utilization is {system_utils[1]}% of {system_utils[0]} MB") |
19 | 49 |
|
0 commit comments