-
Notifications
You must be signed in to change notification settings - Fork 0
/
ilohelper.py
168 lines (154 loc) · 6.54 KB
/
ilohelper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import sys
import redfish
import subprocess
import time
import json
import numpy
class ilohelper:
def __init__(self, iLO, login_account, login_password, targetip):
self.target_ip = targetip
self.iLO_host = f"https://{iLO}"
self.login_account = login_account
self.login_password = login_password
self.mintemp = 0
self.maxtemp = 0
print("Initialized")
self.client = redfish.redfish_client(
base_url=self.iLO_host,
username=self.login_account,
password=self.login_password,
default_prefix='/redfish/v1'
)
try:
self.client.login(auth="basic")
print("Login successful.")
except Exception as e:
print(f"Error during login: {str(e)}")
raise
def __exit__(self, exc_type, exc_val, exc_tb):
print("Destroying object")
if self.client:
self.client.logout()
print("Logout successful.")
def get_temperatures(self):
if self.client:
try:
print("------> Getting Temperatures <-------")
response = self.client.get("/redfish/v1/Chassis/1/Thermal/")
sensors_data = response.dict["Temperatures"]
temperatures = []
for sensor in sensors_data:
sensor_value = sensor["ReadingCelsius"]
print(f"Sensor {sensor['Name']} has {sensor_value} degrees")
temperatures.append(sensor_value)
self.mintemp = min(sensor_value, self.mintemp) if sensor_value < self.mintemp else self.mintemp
self.maxtemp = max(sensor_value, self.maxtemp) if sensor_value > self.maxtemp else self.maxtemp
self.avgtemp = numpy.mean(temperatures)
print(f"Mintemp: {self.mintemp}")
print(f"Maxtemp: {self.maxtemp}")
print(f"Avgtemp: {self.avgtemp}")
print("will return: ")
print(temperatures)
return temperatures
except Exception as e:
print(f"Error retrieving temperature data: {str(e)}")
return {}
else:
return {}
def get_server_status(self):
if self.client:
try:
self.get_temperatures()
print("------> Getting ServerStatus <-------")
response = self.client.get("/redfish/v1/Systems/1")
status = response.dict
power_state = status["PowerState"]
self.power_state = False if power_state == "Off" else True
self.memory = status["MemorySummary"]["TotalSystemMemoryGiB"]
self.cpu = status["ProcessorSummary"]
print(f"Power state: {power_state}")
print(f"Memory: {self.memory}")
print(f"CPU: {self.cpu}")
return status
except Exception as e:
print(f"Error retrieving status: {str(e)}")
return None
else:
return None
def start_server(self):
if self.client:
try:
print("------> Starting Server <-------")
response = self.client.post('/redfish/v1/Systems/1/Actions/ComputerSystem.Reset/', body={'ResetType': 'PushPowerButton'})
print(response)
except Exception as e:
print(f"Error starting server: {str(e)}")
def stop_server(self):
if self.client:
try:
print("------> Stopping Server <-------")
response = self.client.post('/redfish/v1/Systems/1/Actions/ComputerSystem.Reset/', body={'ResetType': 'ForceOff'})
print(response)
except Exception as e:
print(f"Error starting server: {str(e)}")
def waitForBoot(self):
print("------> Waiting until OS booted <-------")
ttl_found = False
max_attempts = 100 # Maximum number of ping attempts
attempt_count = 0
self.get_server_status()
print(self.power_state)
if self.power_state is not True:
self.start_server()
print("Waiting until turned on")
print("....waiting 5 seconds")
time.sleep(5)
while not ttl_found and attempt_count < max_attempts:
try:
# Executes the ping command and reads the output
result = subprocess.run(['ping', '-c', '1', self.target_ip], stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True)
print(result)
# Looks for "time=" in the output, indicating a TTL value
if "0% packet loss" in result.stdout:
print("Server has booted")
ttl_found = True
else:
print("Not yet ready...no IP...waiting for DHCP ACK...")
except Exception as e:
print(f"Error pinging: {str(e)}")
break
attempt_count += 1
time.sleep(1) # Delay between attempts
if not ttl_found:
print("Too many retries. Stopping script and logging out")
else:
print(f"Finished after {attempt_count} seconds")
else:
print("Server is already turned on")
# Example usage of the class
def execute_command(command, client):
if command == "temperatures":
client.get_temperatures()
elif command == "serverStatus":
client.get_server_status()
elif command == "startServer":
client.start_server()
elif command == "stopServer":
client.stop_server()
elif command == "waitForBoot":
client.waitForBoot()
else:
print("Invalid command specified.")
if __name__ == "__main__":
import sys
command = sys.argv[1]
iLO = sys.argv[2] # "ILO_Hostname_Or_IP"
login_account = sys.argv[3] # "admin"
login_password = sys.argv[4] # "Password"
targetIp = sys.argv[5] if isinstance(sys.argv[5], str) else 0 # server IP
client = ilohelper(iLO, login_account, login_password, targetIp)
# Check if the command is valid and execute it
if command in ["temperatures", "serverStatus", "startServer", "stopServer", "waitForBoot"]:
execute_command(command, client)
else:
print("Invalid command. Please choose from the following options: get_temperatures, get_server_status, start_server, stop_server, ping_until_boot.")