revert and start over lmao

This commit is contained in:
Tristan Smith 2024-03-21 23:18:50 -04:00
parent 5215334d9b
commit b27238cffb

View file

@ -1,6 +1,5 @@
import argparse import argparse
import subprocess import subprocess
import re
# Server IPMI details # Server IPMI details
IPMIHOST = "192.168.1.223" IPMIHOST = "192.168.1.223"
@ -9,7 +8,7 @@ IPMIPASS = "<password>"
def execute_ipmi_command(command): def execute_ipmi_command(command):
full_command = f"ipmitool -I lanplus -H {IPMIHOST} -U {IPMIUSER} -P \"{IPMIPASS}\" {command}" full_command = f"ipmitool -I lanplus -H {IPMIHOST} -U {IPMIUSER} -P \"{IPMIPASS}\" {command}"
result = subprocess.run(full_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) result = subprocess.run(full_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print(result.stdout.decode().strip()) print(result.stdout.decode().strip())
if result.stderr: if result.stderr:
print(result.stderr.decode().strip()) print(result.stderr.decode().strip())
@ -24,7 +23,7 @@ def power_off():
def set_fan_speed(speed): def set_fan_speed(speed):
print(f"Setting fan speed to {speed}%...") print(f"Setting fan speed to {speed}%...")
execute_ipmi_command(f"raw 0x30 0x30 0x02 0xff {speed}") execute_ipmi_command(f"raw 0x30 0x30 0x02 0xff {hex(speed)}")
def enable_dynamic_fan_control(): def enable_dynamic_fan_control():
print("Enabling dynamic fan control...") print("Enabling dynamic fan control...")
@ -34,39 +33,19 @@ def disable_dynamic_fan_control():
print("Disabling dynamic fan control...") print("Disabling dynamic fan control...")
execute_ipmi_command("raw 0x30 0x30 0x01 0x00") execute_ipmi_command("raw 0x30 0x30 0x01 0x00")
def get_server_temperature():
command = "ipmitool -I lanplus -H {0} -U {1} -P \"{2}\" sdr type temperature".format(IPMIHOST, IPMIUSER, IPMIPASS)
result = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
temperature_readings = result.stdout
temperatures = re.findall(r'\| (\d+) degrees C', temperature_readings)
temperatures = [int(temp) for temp in temperatures]
average_temp = sum(temperatures) / len(temperatures) if temperatures else 0
return average_temp
def adjust_fan_speed_based_on_temp():
temp = get_server_temperature()
print(f"Current average temperature: {temp}C")
warning_threshold = 75
critical_threshold = 90
if temp >= critical_threshold:
print("Temperature is critical, setting fan speed to 100%.")
set_fan_speed(100)
elif temp >= warning_threshold:
print("Temperature is nearing warning level, setting fan speed to 75%.")
set_fan_speed(75)
else:
print("Temperature is normal, setting fan speed to dynamic control.")
enable_dynamic_fan_control()
# Argument Parsing # Argument Parsing
parser = argparse.ArgumentParser(description='Server Management Script') parser = argparse.ArgumentParser(description='Server Management Script')
parser.add_argument('-p', '--power', choices=['on', 'off'], help='Power on or off the server') parser.add_argument('-p', '--power', choices=['on', 'off'], help='Power on or off the server')
parser.add_argument('-f', '--fan', type=int, choices=range(0, 101), metavar="[0-100]", help='Set fan speed percentage') parser.add_argument('-f', '--fan', type=int, choices=range(0, 101), metavar="[0-100]", help='Set fan speed percentage')
parser.add_argument('-t', '--temp', action='store_true', help='Adjust fan speed based on temperature')
parser.add_argument('-d', '--dynamic', choices=['on', 'off'], help='Toggle dynamic fan control') parser.add_argument('-d', '--dynamic', choices=['on', 'off'], help='Toggle dynamic fan control')
args = parser.parse_args() args = parser.parse_args()
# Check for no action
if not any([args.power, args.fan, args.dynamic]):
parser.print_help()
exit()
# Execute based on arguments # Execute based on arguments
if args.power == 'on': if args.power == 'on':
power_on() power_on()
@ -76,10 +55,7 @@ elif args.power == 'off':
if args.fan is not None: if args.fan is not None:
set_fan_speed(args.fan) set_fan_speed(args.fan)
if args.temp:
adjust_fan_speed_based_on_temp()
if args.dynamic == 'on': if args.dynamic == 'on':
enable_dynamic_fan_control() enable_dynamic_fan_control()
elif args.dynamic == 'off': elif args.dynamic == 'off':
disable_dynamic_fan_control() disable_dynamic_fan_control()