Removed time from logging and addressed bug with systemd service stop.

This commit is contained in:
Domen Tabernik 2019-03-08 21:27:41 +00:00
parent 870a47fec6
commit e1ead3dfdf
2 changed files with 40 additions and 32 deletions

View File

@ -72,13 +72,13 @@ def set_fan_with_full_preset(config, speed, zone):
# Make sure fans are on Full setting, or else this won't stick for long # Make sure fans are on Full setting, or else this won't stick for long
s = get_preset(config) s = get_preset(config)
if s is False: if s is False:
print(time.ctime() +": Unable to get current fan status; exiting") print("Unable to get current fan status; exiting")
return False return False
if s != FAN_PRESET_FULL: if s != FAN_PRESET_FULL:
print(time.ctime() +": The fan controller is currently not set to Full mode (required for manual fan settings, which will otherwise be adjusted by the BMC within minutes); setting it now.") print("The fan controller is currently not set to Full mode (required for manual fan settings, which will otherwise be adjusted by the BMC within minutes); setting it now.")
set_preset(config, preset='full') set_preset(config, preset='full')
print(time.ctime() +": Waiting 5 seconds to let fans spin up...") print("Waiting 5 seconds to let fans spin up...")
time.sleep(5) time.sleep(5)
ok = True ok = True
@ -88,10 +88,10 @@ def set_fan_with_full_preset(config, speed, zone):
ok = ipmi_raw_cmd('0x30 0x70 0x66 0x01 0x01 0x%02x' % speed, **config) ok = ipmi_raw_cmd('0x30 0x70 0x66 0x01 0x01 0x%02x' % speed, **config)
if ok: if ok:
print(time.ctime() +": Set %s fans on %s to %d%%." % (zone, config['hostname'], speed)) print("Set %s fans on %s to %d%%." % (zone, config['hostname'], speed))
return True return True
else: else:
print(time.ctime() +": Unable to update fans.") print("Unable to update fans.")
return False return False
def set_fan(config, speed, zone): def set_fan(config, speed, zone):
@ -103,10 +103,10 @@ def set_fan(config, speed, zone):
ok = ipmi_raw_cmd('0x30 0x70 0x66 0x01 0x%02x 0x%02x' % (zone, speed), **config) ok = ipmi_raw_cmd('0x30 0x70 0x66 0x01 0x%02x 0x%02x' % (zone, speed), **config)
if ok: if ok:
print(time.ctime() +": Set %s fans on %s to %d%%." % (FAN_ZONES_STR[zone], config['hostname'], speed)) print("Set %s fans on %s to %d%%." % (FAN_ZONES_STR[zone], config['hostname'], speed))
return True return True
else: else:
print(time.ctime() +": Unable to update fans.") print("Unable to update fans.")
return False return False
def get_fan(config, fan): def get_fan(config, fan):
@ -148,9 +148,9 @@ def _set_preset(config):
if fan_speed2 is False: if fan_speed2 is False:
return False return False
print(time.ctime() +": Preset: %s" % s) print("Preset: %s" % s)
print(time.ctime() +": Current fan speed (CPU Zone): %d%%" % int(fan_speed, 16)) print("Current fan speed (CPU Zone): %d%%" % int(fan_speed, 16))
print(time.ctime() +": Current fan speed (Peripheral zone): %d%%" % int(fan_speed2, 16)) print("Current fan speed (Peripheral zone): %d%%" % int(fan_speed2, 16))
return True return True
@ -159,7 +159,7 @@ def set_preset(config, preset):
return False return False
if ipmi_raw_cmd("0x30 0x45 0x01 0x0%d" % preset, **config): if ipmi_raw_cmd("0x30 0x45 0x01 0x0%d" % preset, **config):
print(time.ctime() +": Updated preset on %s." % config['hostname']) print("Updated preset on %s." % config['hostname'])
return True return True
return False return False
@ -168,7 +168,7 @@ def ipmi_raw_cmd(raw_cmd, hostname = 'localhost', username=None, password=None,
if hostname == 'localhost': if hostname == 'localhost':
if os.geteuid() != 0: if os.geteuid() != 0:
print(time.ctime() +": In order to communicate with the kernel's IPMI module, you must be root.") print("In order to communicate with the kernel's IPMI module, you must be root.")
sys.exit(1) sys.exit(1)
cmd = 'ipmitool raw %s' % raw_cmd cmd = 'ipmitool raw %s' % raw_cmd
else: else:
@ -181,9 +181,9 @@ def ipmi_raw_cmd(raw_cmd, hostname = 'localhost', username=None, password=None,
try: try:
s = subprocess.check_output(cmd + " 2>&1", shell=True) s = subprocess.check_output(cmd + " 2>&1", shell=True)
except subprocess.CalledProcessError, ex: except subprocess.CalledProcessError, ex:
print(time.ctime() +": Error: Problem running ipmitool") print("Error: Problem running ipmitool")
print(time.ctime() +": Command: %s" % cmd) print("Command: %s" % cmd)
print(time.ctime() +": Return code: %d" % ex) print("Return code: %d" % ex)
return False return False
out = s.strip() out = s.strip()
@ -197,7 +197,7 @@ def ipmi_fan_status(hostname = 'localhost', username=None, password=None, use_en
if hostname == 'localhost': if hostname == 'localhost':
if os.geteuid() != 0: if os.geteuid() != 0:
print(time.ctime() +": In order to communicate with the kernel's IPMI module, you must be root.") print("In order to communicate with the kernel's IPMI module, you must be root.")
sys.exit(1) sys.exit(1)
cmd = 'ipmitool sensor | grep FAN ' cmd = 'ipmitool sensor | grep FAN '
else: else:
@ -209,9 +209,9 @@ def ipmi_fan_status(hostname = 'localhost', username=None, password=None, use_en
try: try:
s = subprocess.check_output(cmd + " 2>&1", shell=True) s = subprocess.check_output(cmd + " 2>&1", shell=True)
except subprocess.CalledProcessError, ex: except subprocess.CalledProcessError, ex:
print(time.ctime() +": Error: Problem running ipmitool") print("Error: Problem running ipmitool")
print(time.ctime() +": Command: %s" % cmd) print("Command: %s" % cmd)
print(time.ctime() +": Return code: %d" % ex) print("Return code: %d" % ex)
return False return False
fan_status_return = {} fan_status_return = {}

View File

@ -3,7 +3,16 @@
# author: Domen Tabernik # author: Domen Tabernik
# 2019 # 2019
import time, superfans, subprocess import time, superfans, subprocess, signal, sys
class GracefulKiller:
kill_now = False
def __init__(self):
signal.signal(signal.SIGINT, self.exit_gracefully)
signal.signal(signal.SIGTERM, self.exit_gracefully)
def exit_gracefully(self,signum, frame):
self.kill_now = True
def retrieve_nvidia_gpu_temperature(): def retrieve_nvidia_gpu_temperature():
cmd = 'nvidia-smi --query-gpu=temperature.gpu --format=csv,noheader' cmd = 'nvidia-smi --query-gpu=temperature.gpu --format=csv,noheader'
@ -34,13 +43,13 @@ def superfans_gpu_controller(fan_settings, FAN_INCREASED_MIN_TIME=120, sleep_sec
# save default present before changing anything # save default present before changing anything
default_preset = superfans.get_preset(superfan_config) default_preset = superfans.get_preset(superfan_config)
print(time.ctime() + ': Started fan control using GPU temperature.') print('Started fan control using GPU temperature.')
print(time.ctime() + ': Using settings:') print('Using settings:')
for k in sorted(fan_settings.keys()): for k in sorted(fan_settings.keys()):
print(time.ctime() + ': \t%d C = %d ' % (k, fan_settings[k]) + "%") print('\t%d C = %d ' % (k, fan_settings[k]) + "%")
print(time.ctime() + ':') print(time.ctime() + ':')
try:
try:
FAN_MEMBERS = superfans.FAN_ZONES_MEMBERS[superfans.FAN_ZONE_SYS1] + \ FAN_MEMBERS = superfans.FAN_ZONES_MEMBERS[superfans.FAN_ZONE_SYS1] + \
superfans.FAN_ZONES_MEMBERS[superfans.FAN_ZONE_SYS2] superfans.FAN_ZONES_MEMBERS[superfans.FAN_ZONE_SYS2]
@ -50,7 +59,9 @@ def superfans_gpu_controller(fan_settings, FAN_INCREASED_MIN_TIME=120, sleep_sec
prev_GPU_temp = [] prev_GPU_temp = []
while True: # ensure correct ending when SIGINT and SIGTERM are received
k = GracefulKiller()
while not k.kill_now:
# get GPU temperature # get GPU temperature
GPU_temp = retrieve_nvidia_gpu_temperature() GPU_temp = retrieve_nvidia_gpu_temperature()
@ -100,9 +111,9 @@ def superfans_gpu_controller(fan_settings, FAN_INCREASED_MIN_TIME=120, sleep_sec
superfans.set_fan(superfan_config, target_fan, superfans.FAN_ZONE_SYS2) superfans.set_fan(superfan_config, target_fan, superfans.FAN_ZONE_SYS2)
if update_sys1_fan or update_sys2_fan: if update_sys1_fan or update_sys2_fan:
print(time.ctime() + ': \tCurrent GPU measurements: %s' % ','.join(map(str,GPU_temp))) print('\tCurrent GPU measurements: %s' % ','.join(map(str,GPU_temp)))
print(time.ctime() + ': \tMoving average GPU measurements: %s' % ','.join(map(str,mean_GPU_temp))) print('\tMoving average GPU measurements: %s' % ','.join(map(str,mean_GPU_temp)))
print(time.ctime() + ': \tTarget difference: SYS1 fan = %f; SYS2 fan = %f' % (max(diff_sys1_fan), max(diff_sys2_fan))) print('\tTarget difference: SYS1 fan = %f; SYS2 fan = %f' % (max(diff_sys1_fan), max(diff_sys2_fan)))
print(time.ctime() + ':') print(time.ctime() + ':')
@ -113,10 +124,7 @@ def superfans_gpu_controller(fan_settings, FAN_INCREASED_MIN_TIME=120, sleep_sec
finally: finally:
# revert back to default preset before finishing # revert back to default preset before finishing
superfans.set_preset(superfan_config, default_preset) superfans.set_preset(superfan_config, default_preset)
print(time.ctime() + ': Reverted back to system default.') print('Reverted back to system default.')
if __name__ == "__main__": if __name__ == "__main__":
# fan settings = {[in deg C]: [% fan], ...} # fan settings = {[in deg C]: [% fan], ...}