Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7c96bba

Browse files
Merge pull request avinashkranjan#1936 from infernalsaber/master
Adding new functionalities to CPU Temperature script (Closes Issue avinashkranjan#1416)
2 parents c4dbd1b + 49c1212 commit 7c96bba

File tree

2 files changed

+52
-12
lines changed

2 files changed

+52
-12
lines changed

‎CPU temperature/Readme.md

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,41 @@ This Python script is used to retrieve the CPU temperature using the psutil libr
66
## Prerequisites
77
- Python installed on your machine
88
- `psutil` library installed. You can install it by running the following command:
9-
```bash
9+
10+
```bash
1011
pip install psutil
11-
12+
```
1213

1314
## Explanation of the Script
1415

15-
The script utilizes the `psutil` library's `sensors_temperatures()` function to measure the temperature of the CPU.
16+
The script utilizes the `psutil` library's `sensors_temperatures()`, `virtual_memory()` and `cpu_percent()` functions to output data that tells us about the system's resource consumption.
1617

1718
## Setup Instructions
1819

1920
1. Clone the repository to your local machine.
20-
2. Navigate to the "Cpu Temperature" folder.
21+
2. Navigate to the "CPU Temperature" folder.
2122
3. Install the `psutil` library if you haven't already by running the following command:
22-
```bash
23-
pip install psutil
24-
```
23+
24+
```bash
25+
pip install psutil
26+
```
2527
## Output
2628

27-
The script will display the current CPU temperature in Celsius.
29+
The script will display the current CPU temperature in Celsius alongwith CPU Percentage Used.
30+
In addition, it'll display the percentage used and total RAM of the system.
2831
32+
Sample Output:
33+
34+
```
35+
Current CPU Temperature (Celsius): 60, with percentage utilized being at 3.1%,
36+
Current RAM utilization is 75.3% of 512 MB
37+
```
2938
3039
3140
## Compatibility
3241
3342
Please note that the script is primarily designed for Linux-based systems. While it may work on other platforms, the availability and format of temperature information can vary. Ensure that your system supports the `psutil` library and has the necessary sensors for CPU temperature measurement.
43+
However, the CPU and RAM utilization functionalities would work regaurdless of the Operating System.
3444
3545
## Contributing
3646

‎CPU temperature/temp.py

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,49 @@
11
import psutil
22

33
# 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+
"""
510
try:
611
# Retrieve temperature information using psutil
712
# and access the first temperature value from the 'coretemp' key
813
temperature = psutil.sensors_temperatures()['coretemp'][0].current
914
return temperature
1015
except (KeyError, IndexError):
1116
# 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+
1332

1433
# Call the get_cpu_temperature() function to get the CPU temperature
1534
cpu_temperature = get_cpu_temperature()
1635

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")
1949

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /