Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions ilorest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env python3
import subprocess
import json
import argparse

def run_ilorest_command(ilorest_command, url, username, password):
command = f'ilorest {ilorest_command} --url {url} -u {username} -p {password} --json'.split()
try:
result = subprocess.run(command, stdout=subprocess.PIPE, check=True)
return json.loads(''.join(result.stdout.decode('utf-8').split('\n')[1:]))
except Exception as e:
print(f'Error running iLO REST command: {e}')
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI Comment:
Line 12: Potential IndexError or JSONDecodeError in json.loads(''.join(result.stdout.decode('utf-8').split('\n')[1:])); if the output has fewer than 2 lines, split('\n')[1:] will be empty, leading to an empty string and JSON decoding failure.
Line 20: Potential KeyError when accessing memory_info['memory']; if the key does not exist, it will raise an exception.
Line 23: Potential KeyError when accessing stick_attr['Status'] or stick_attr['Location']; if these keys are missing in stick_attr, it will cause a runtime error.
logicalErrors=3

exit(1)

def get_memory_health(url, username, password, debug):
ilorest_command = 'serverinfo --memory'
status = 0
memory_info = run_ilorest_command(ilorest_command, url, username, password)
if debug:
print(json.dumps(memory_info))
exit(status)
for stick_attr in memory_info['memory'].values():
if stick_attr.get('Health', 'OK') != 'OK':
print(f"Status: {stick_attr['Status']} Location: {stick_attr['Location']}")
status = 1
exit(status)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Get health details from HPE iLO REST API')
parser.add_argument('-H', '--url', required=True, help='iLO URL')
parser.add_argument('-u', '--username', required=True, help='iLO username')
parser.add_argument('-p', '--password', required=True, help='iLO password')
parser.add_argument('--debug', action='store_true')
args = parser.parse_args()
get_memory_health(**vars(args))