Skip to content

Instantly share code, notes, and snippets.

@mitsuhiko
Created July 9, 2014 17:30
Show Gist options
  • Save mitsuhiko/890b3f971f5c3b263b53 to your computer and use it in GitHub Desktop.
Save mitsuhiko/890b3f971f5c3b263b53 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import re
from subprocess import Popen, PIPE
def get_cpu_temp():
rv = Popen(['smc', '-k', 'TC0D', '-r'],
stdout=PIPE).communicate()[0]
match = re.search(r'bytes (\S+) (.+?)\)', rv)
a, b = match.groups()
temp = (int(a, 16) * 256 + int(b, 16)) >> 2
return temp / 64.0
def get_fan_count():
rv = Popen(['smc', '-k', 'FNum', '-r'],
stdout=PIPE).communicate()[0]
match = re.search(r' (\d+) \(bytes', rv)
return int(match.group(1))
def get_fan_max_speed(num):
rv = Popen(['smc', '-k', 'F%dMx' % num, '-r'],
stdout=PIPE).communicate()[0]
match = re.search(r' (\d+) \(bytes', rv)
return int(match.group(1))
def set_fan_max_speed(num, value):
value = '%x' % (value << 2)
Popen(['smc', '-k', 'F%dMx' % num, '-w', value],
stdout=PIPE).wait()
def set_all_fans_max_speed(value):
print 'Setting all fans to %s' % value
for fan in xrange(get_fan_count()):
set_fan_max_speed(fan, value)
def main():
temp = get_cpu_temp()
print 'CPU Temp: %s' % temp
if temp > 82:
set_all_fans_max_speed(6200)
elif temp > 79:
set_all_fans_max_speed(4000)
elif temp > 75:
set_all_fans_max_speed(3000)
else:
set_all_fans_max_speed(2000)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment