-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a plugin to get information from ipmitool
- get_all_sensors: run `ipmitool sdr list` - returns a JSON string with all sensors - get_sensor: run `ipmitool sdr get <sensor>` - returns details about sensors passed as parameter - get_ipmi_lan: returns network info of the IPMI server as a JSON Signed-off-by: Guillaume <[email protected]>
- Loading branch information
Showing
3 changed files
with
399 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
import json | ||
import sys | ||
import XenAPIPlugin | ||
|
||
sys.path.append(".") | ||
from xcpngutils import ( | ||
configure_logging, | ||
run_command, | ||
error_wrapped, | ||
ProcessException, | ||
) | ||
|
||
|
||
@error_wrapped | ||
def run_ipmitool(args): | ||
try: | ||
output = run_command(["ipmitool", "sdr", "list"]) | ||
except ProcessException as e: | ||
# Check if it is a "normal" error. | ||
if "Could not open device" in e.stderr: | ||
return ( | ||
False, | ||
"IPMI device not found. Ensure the IPMI module is loaded and that your system supports IPMI.", | ||
) | ||
else: | ||
raise e | ||
else: | ||
return True, output | ||
|
||
|
||
@error_wrapped | ||
def sensor_data(_session, _args): | ||
sensor_data = [] | ||
try: | ||
success, output = run_ipmitool(["sdr", "list"]) | ||
except ProcessException as e: | ||
raise e | ||
|
||
if not success: | ||
return json.dumps(output) | ||
|
||
for line in output["stdout"].splitlines(): | ||
if not line: | ||
continue | ||
sensor_fields = line.split("|") | ||
sensor_data.append( | ||
{ | ||
"name": sensor_fields[0].strip(), | ||
"value": sensor_fields[1].strip(), | ||
"event": sensor_fields[2].strip(), | ||
} | ||
) | ||
|
||
return json.dumps(sensor_data) | ||
|
||
|
||
@error_wrapped | ||
def sensor_info(_session, args): | ||
sensors_info = [] | ||
sensors = args.get("sensors") | ||
|
||
if not sensors: | ||
return "{}" | ||
|
||
for sensor in sensors.split(","): | ||
sensor = sensor.strip() | ||
info = [] | ||
try: | ||
success, output = run_ipmitool(["sdr", "get", sensor]) | ||
except ProcessException as e: | ||
raise e | ||
|
||
if not success: | ||
return json.dumps(output) | ||
|
||
for line in output["stdout"].splitlines(): | ||
if ":" not in line: | ||
continue | ||
name, value = line.split(":", 1) | ||
info.append( | ||
{ | ||
"name": name.strip(), | ||
"value": value.strip(), | ||
} | ||
) | ||
|
||
sensors_info.append( | ||
{ | ||
"name": sensor, | ||
"info": info, | ||
} | ||
) | ||
|
||
return json.dumps(sensors_info) | ||
|
||
|
||
@error_wrapped | ||
def ipmi_lan(_session, _args): | ||
lan_info = [] | ||
wanted = [ | ||
"IP Address", | ||
"Subnet Mask", | ||
"MAC Address", | ||
"BMC ARP Control", | ||
"Default Gateway IP", | ||
"802.1q VLAN", | ||
"RMCP+ Cipher Suites", | ||
] | ||
|
||
try: | ||
success, output = run_ipmitool(["lan", "print"]) | ||
except ProcessException as e: | ||
raise e | ||
|
||
if not success: | ||
return json.dumps(output) | ||
|
||
for line in output["stdout"].splitlines(): | ||
if any(word in line for word in wanted): | ||
name, value = line.split(":", 1) | ||
lan_info.append( | ||
{ | ||
"name": name.strip(), | ||
"value": value.strip(), | ||
} | ||
) | ||
|
||
return json.dumps(lan_info) | ||
|
||
|
||
_LOGGER = configure_logging("ipmitool-xapi-plugin") | ||
if __name__ == "__main__": | ||
XenAPIPlugin.dispatch( | ||
{ | ||
"get_all_sensors": sensor_data, | ||
"get_sensor": sensor_info, | ||
"get_ipmi_lan": ipmi_lan, | ||
} | ||
) |
Oops, something went wrong.