Skip to content

Commit d645e83

Browse files
authored
Merge pull request #513 from TheFenrisLycaon/cpu
Added CPU_temperature
2 parents ef8ca10 + 6d131cb commit d645e83

File tree

2 files changed

+270
-0
lines changed

2 files changed

+270
-0
lines changed

cpu_tempreature/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# CPU Temperature Detector
2+
3+
Run ```main.py```
4+
Specify the unit in which you want to see the info.
5+
6+
Usage information:
7+
8+
```bash
9+
-h / --help Display this usage info.
10+
-s / --seconds [seconds] Run cputemp for specified number of seconds
11+
-C / --celsius Display temperature in Celsius
12+
-F / --fahrenheit Display temperature in Fahrenheit
13+
-K / --kelvin Display temperature in Kelvin
14+
-a / --average Displays only the results (Use with -s and -F/-C)
15+
```

cpu_tempreature/main.py

Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
import os
2+
import sys
3+
import getopt
4+
5+
6+
def rtcheck(seconds):
7+
try:
8+
seconds = int(seconds)
9+
return seconds
10+
except ValueError:
11+
return -1
12+
13+
14+
def usageinfo(check, run, exitmsg):
15+
print("Usage information:\n")
16+
print("-h / --help\t\tDisplay this usage info.")
17+
print(
18+
"-s / --seconds [seconds]\tRun cputemp for specified number of seconds")
19+
print("-C / --celsius\t\tDisplay temperature in Celsius")
20+
print("-F / --fahrenheit\t\tDisplay temperature in Fahrenheit")
21+
print("-K / --kelvin\t\tDisplay temperature in Kelvin")
22+
print("-a / --average\t\tDisplays only the results (Use with -s and -F/-C)\n")
23+
24+
if check == 1 or run == 1:
25+
print(exitmsg)
26+
sys.exit()
27+
28+
29+
def hwcheck():
30+
if os.path.exists("/sys/devices/LNXSYSTM:00/LNXTHERM:00/LNXTHERM:01/thermal_zone/temp"):
31+
return 4
32+
33+
elif os.path.exists("/sys/bus/acpi/devices/LNXTHERM:00/thermal_zone/temp"):
34+
return 5
35+
36+
elif os.path.exists("/proc/acpi/thermal_zone/THM0/temperature"):
37+
return 1
38+
39+
elif os.path.exists("/proc/acpi/thermal_zone/THRM/temperature"):
40+
return 2
41+
42+
elif os.path.exists("/proc/acpi/thermal_zone/THR1/temperature"):
43+
return 3
44+
45+
else:
46+
return 0
47+
48+
49+
def getDegree():
50+
degree = 0
51+
while degree == 0:
52+
degree = input("(F)ahrenheit, (C)elsius, (K)elvin: ")
53+
if degree.upper() == "F" or degree.upper() == "FAHRENHEIT":
54+
degree = "Fahrenheit"
55+
elif degree.upper() == "C" or degree.upper() == "CELSIUS":
56+
degree = "Celsius"
57+
elif degree.upper() == "K" or degree.upper() == "KELVIN":
58+
degree = "Kelvin"
59+
return degree
60+
61+
62+
def getTemp(hardware):
63+
if hardware == 1:
64+
temp = open("/proc/acpi/thermal_zone/THM0/temperature").read(
65+
).strip().lstrip('temperature :').rstrip(' C')
66+
elif hardware == 2:
67+
temp = open("/proc/acpi/thermal_zone/THRM/temperature").read(
68+
).strip().lstrip('temperature :').rstrip(' C')
69+
elif hardware == 3:
70+
temp = open("/proc/acpi/thermal_zone/THR1/temperature").read(
71+
).strip().lstrip('temperature :').rstrip(' C')
72+
elif hardware == 4:
73+
temp = open(
74+
"/sys/devices/LNXSYSTM:00/LNXTHERM:00/LNXTHERM:01/thermal_zone/temp").read().strip().rstrip('000')
75+
elif hardware == 5:
76+
temp = open(
77+
"/sys/bus/acpi/devices/LNXTHERM:00/thermal_zone/temp").read().strip().rstrip('000')
78+
temp = str(float(temp) / 10.0)
79+
else:
80+
return 0
81+
return temp
82+
83+
84+
def dispTemp(temp, degree, minutes, seconds):
85+
print("\rCPU Temperature: ", temp, degree, end=' ')
86+
print("(Time Running: ", minutes, ":", end=' ')
87+
if seconds < 10:
88+
print("0" + str(seconds), ")", end=' ')
89+
else:
90+
print(seconds, ")", end=' ')
91+
92+
93+
def convertDegree(degree, temp):
94+
if degree == "Fahrenheit":
95+
temp = temp * 9 / 5.0 + 32
96+
return temp
97+
if degree == "Kelvin":
98+
temp = temp + 273.15
99+
return temp
100+
else:
101+
return temp
102+
103+
104+
def main():
105+
avg = 0
106+
ver = 0
107+
run = 0
108+
help = 0
109+
check = 0
110+
runtime = 5
111+
degree = 0
112+
seccount = 0
113+
seclast = 0
114+
minutes = 0
115+
temp = 21
116+
tsum = 0
117+
ticker = 0
118+
exitmsg = ""
119+
try:
120+
opts, args = getopt.getopt(sys.argv[1:], "aCFKs:hV", [
121+
"average", "celsius", "kelvin", "fahrenheit", "seconds=", "help", "version"])
122+
except getopt.GetoptError:
123+
help = 1
124+
check = 1
125+
exitmsg = "Argument not recognized."
126+
usageinfo(check, run, exitmsg)
127+
if check == 0:
128+
for opt, arg in opts:
129+
if opt in ("-h", "--help"):
130+
help = 1
131+
if opt in ("-C", "--celsius"):
132+
degree = "Celsius"
133+
if opt in ("-F", "--fahrenheit"):
134+
degree = "Fahrenheit"
135+
if opt in ("-K", "--kelvin"):
136+
degree = "Kelvin"
137+
if opt in ("-V", "--version"):
138+
ver = 1
139+
if opt in ("-s", "--seconds"):
140+
run = 1
141+
runtime = arg
142+
if opt in ("-a", "--average"):
143+
avg = 1
144+
run = 1
145+
146+
if ver == 1:
147+
sys.exit()
148+
if help == 1:
149+
usageinfo(check, run, exitmsg)
150+
if run == 1:
151+
runtime = rtcheck(runtime)
152+
if runtime < 1:
153+
help = 1
154+
exitmsg = "Seconds must be defined as an integer greater than zero."
155+
usageinfo()
156+
hardware = hwcheck()
157+
if hardware == 0:
158+
print("Sorry, your hardware is not yet supported.")
159+
sys.exit()
160+
if avg == 1 and degree == 0:
161+
degree = "Fahrenheit"
162+
elif degree == 0:
163+
degree = getDegree()
164+
import datetime
165+
import time
166+
prevtemp = 21
167+
tmax = 0
168+
tmin = 1000
169+
bseconds = int(time.time())
170+
171+
if avg == 0:
172+
print("Press Ctrl+C to exit")
173+
174+
try:
175+
while True:
176+
temp = getTemp(hardware)
177+
tick = int(time.time()) - int(bseconds)
178+
seccount = int(seccount) + int(tick) - int(seclast)
179+
seclast = tick
180+
if seccount >= 60:
181+
minutes = int(minutes) + 1
182+
seccount = int(seccount) - 60
183+
seconds = int(tick) - (60 * int(minutes))
184+
temp = float(temp)
185+
if prevtemp > 20 and temp < 18:
186+
temp = temp * 10
187+
prevtemp = temp
188+
temp = convertDegree(degree, temp)
189+
if avg == 0:
190+
dispTemp(temp, degree, minutes, seconds)
191+
if temp < tmin:
192+
tmin = temp
193+
if temp > tmax:
194+
tmax = temp
195+
tsum = float(tsum) + float(temp)
196+
ticker = int(ticker) + 1
197+
if run == 1 and runtime <= seccount:
198+
break
199+
time.sleep(1)
200+
sys.stdout.flush()
201+
# When Ctrl-C is pressed, show summary data
202+
203+
except KeyboardInterrupt:
204+
pass
205+
206+
tavg = tsum / ticker
207+
tavg = round(tavg, 1)
208+
if avg == 0:
209+
print("\n\nHighest recorded temperature was",
210+
tmax, "degrees", degree, ".")
211+
print("Lowest recorded temperature was", tmin, "degrees", degree, ".")
212+
print("Average temperature was", tavg, "degrees", degree, ".\n")
213+
214+
import os
215+
if avg == 1:
216+
return 0
217+
elif os.path.exists("/var/log/cputemp.log") and avg == 0:
218+
219+
tod = datetime.datetime.now()
220+
templog = open("/var/log/cputemp.log", "a")
221+
templog.write("Session started at ")
222+
templog.write(str(tod))
223+
templog.write("\b\b\b\b\b\b\b : \n")
224+
templog.write("cputemp ")
225+
templog.write(" was run for ")
226+
templog.write(str(minutes))
227+
templog.write(" : ")
228+
if seconds < 10:
229+
templog.write("0")
230+
templog.write(str(seconds))
231+
templog.write(".\n")
232+
templog.write("Highest recorded temperature was ")
233+
templog.write(str(tmax))
234+
templog.write(" degrees ")
235+
templog.write(str(degree))
236+
templog.write(".\n")
237+
templog.write("Lowest recorded temperature was ")
238+
templog.write(str(tmin))
239+
templog.write(" degrees ")
240+
templog.write(str(degree))
241+
templog.write(".\n")
242+
templog.write("Average temperature was ")
243+
templog.write(str(tavg))
244+
templog.write(" degrees ")
245+
templog.write(str(degree))
246+
templog.write(".\n")
247+
templog.write("---------------\n\n")
248+
print("Log has been updated")
249+
else:
250+
print("Could not locate log file. Please re-install.")
251+
return 1
252+
253+
254+
if __name__ == '__main__':
255+
main()

0 commit comments

Comments
 (0)