2024-03-21 23:37:29 +00:00
|
|
|
import subprocess
|
2024-03-22 03:58:46 +00:00
|
|
|
import re
|
|
|
|
import argparse
|
2024-03-21 23:37:29 +00:00
|
|
|
|
|
|
|
IPMIHOST = "192.168.1.223"
|
|
|
|
IPMIUSER = "root"
|
2024-03-22 01:04:58 +00:00
|
|
|
IPMIPASS = "<password>"
|
2024-03-21 23:37:29 +00:00
|
|
|
|
|
|
|
def execute_ipmi_command(command):
|
2024-03-22 00:50:07 +00:00
|
|
|
full_command = f"ipmitool -I lanplus -H {IPMIHOST} -U {IPMIUSER} -P \"{IPMIPASS}\" {command}"
|
2024-03-22 03:18:50 +00:00
|
|
|
result = subprocess.run(full_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
2024-03-22 01:04:58 +00:00
|
|
|
print(result.stdout.decode().strip())
|
2024-03-22 00:29:57 +00:00
|
|
|
if result.stderr:
|
2024-03-22 01:04:58 +00:00
|
|
|
print(result.stderr.decode().strip())
|
2024-03-21 23:37:29 +00:00
|
|
|
|
|
|
|
def power_on():
|
|
|
|
print("Powering on the server...")
|
|
|
|
execute_ipmi_command("chassis power on")
|
|
|
|
|
2024-03-22 00:50:07 +00:00
|
|
|
def power_off():
|
|
|
|
print("Powering off the server...")
|
|
|
|
execute_ipmi_command("chassis power off")
|
2024-03-23 17:59:12 +00:00
|
|
|
print("Enabling dynamic fan control...")
|
|
|
|
execute_ipmi_command("raw 0x30 0x30 0x01 0x01")
|
2024-03-22 00:50:07 +00:00
|
|
|
|
2024-03-21 23:37:29 +00:00
|
|
|
def set_fan_speed(speed):
|
|
|
|
print(f"Setting fan speed to {speed}%...")
|
2024-03-22 03:18:50 +00:00
|
|
|
execute_ipmi_command(f"raw 0x30 0x30 0x02 0xff {hex(speed)}")
|
2024-03-21 23:37:29 +00:00
|
|
|
|
2024-03-22 00:50:07 +00:00
|
|
|
def enable_dynamic_fan_control():
|
|
|
|
print("Enabling dynamic fan control...")
|
|
|
|
execute_ipmi_command("raw 0x30 0x30 0x01 0x01")
|
|
|
|
|
|
|
|
def disable_dynamic_fan_control():
|
|
|
|
print("Disabling dynamic fan control...")
|
|
|
|
execute_ipmi_command("raw 0x30 0x30 0x01 0x00")
|
2024-03-21 23:37:29 +00:00
|
|
|
|
2024-03-22 03:58:46 +00:00
|
|
|
|
|
|
|
def get_server_temperature():
|
|
|
|
print("Getting server temperature...")
|
|
|
|
command = f"ipmitool -I lanplus -H {IPMIHOST} -U {IPMIUSER} -P \"{IPMIPASS}\" sdr type temperature"
|
|
|
|
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 temperature: {temp} degrees C")
|
|
|
|
warning_threshold = 75
|
|
|
|
critical_threshold = 90
|
|
|
|
|
|
|
|
# Set fan speed based on temperature ranges
|
|
|
|
if temp >= critical_threshold:
|
|
|
|
print("Temperature is critical, setting fan speed to 100%.")
|
|
|
|
set_fan_speed(100)
|
|
|
|
elif temp >= warning_threshold:
|
|
|
|
print("Temperature is high, setting fan speed to 80%.")
|
|
|
|
set_fan_speed(80)
|
|
|
|
elif temp >= 40:
|
|
|
|
print("Temperature is moderate, setting fan speed to 60%.")
|
|
|
|
set_fan_speed(60)
|
|
|
|
elif temp >= 30:
|
|
|
|
print("Temperature is low, setting fan speed to 50%.")
|
|
|
|
set_fan_speed(50)
|
|
|
|
else:
|
|
|
|
print("Temperature is very low, setting fan speed to 40%.")
|
|
|
|
set_fan_speed(40)
|
|
|
|
|
2024-03-21 23:37:29 +00:00
|
|
|
# Argument Parsing
|
|
|
|
parser = argparse.ArgumentParser(description='Server Management Script')
|
|
|
|
parser.add_argument('-p', '--power', choices=['on', 'off'], help='Power on or off the server')
|
2024-03-22 01:04:58 +00:00
|
|
|
parser.add_argument('-f', '--fan', type=int, choices=range(0, 101), metavar="[0-100]", help='Set fan speed percentage')
|
2024-03-22 03:58:46 +00:00
|
|
|
parser.add_argument('-t', '--temp', action='store_true', help='Adjust fan speed based on temperature')
|
2024-03-22 00:50:07 +00:00
|
|
|
parser.add_argument('-d', '--dynamic', choices=['on', 'off'], help='Toggle dynamic fan control')
|
2024-03-21 23:37:29 +00:00
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2024-03-22 03:18:50 +00:00
|
|
|
# Check for no action
|
2024-03-22 03:58:46 +00:00
|
|
|
if not any([args.power, args.fan, args.dynamic, args.temp]):
|
2024-03-22 03:18:50 +00:00
|
|
|
parser.print_help()
|
|
|
|
exit()
|
|
|
|
|
2024-03-21 23:37:29 +00:00
|
|
|
# Execute based on arguments
|
|
|
|
if args.power == 'on':
|
|
|
|
power_on()
|
|
|
|
elif args.power == 'off':
|
2024-03-22 00:50:07 +00:00
|
|
|
power_off()
|
2024-03-23 17:59:12 +00:00
|
|
|
|
2024-03-22 01:04:58 +00:00
|
|
|
|
2024-03-21 23:37:29 +00:00
|
|
|
if args.fan is not None:
|
|
|
|
set_fan_speed(args.fan)
|
2024-03-22 01:04:58 +00:00
|
|
|
|
2024-03-22 03:58:46 +00:00
|
|
|
if args.temp:
|
|
|
|
adjust_fan_speed_based_on_temp()
|
|
|
|
|
2024-03-22 00:50:07 +00:00
|
|
|
if args.dynamic == 'on':
|
|
|
|
enable_dynamic_fan_control()
|
|
|
|
elif args.dynamic == 'off':
|
2024-03-22 04:07:51 +00:00
|
|
|
disable_dynamic_fan_control()
|