Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add inital IPMItools functionality for Powermon extension. #405

Merged
merged 3 commits into from
Oct 3, 2024
Merged
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
45 changes: 43 additions & 2 deletions snmp/powermon-snmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@
# 20210204 - v1.2 - added top-level reading, librenms option
# 20210205 - v1.3 - added cents per kWh
# 20210205 - v1.4 - improvement to UI
# 20220513 - v1.5 - Add inital IPMItool method

version = 1.4
version = 1.5

### Libraries

Expand Down Expand Up @@ -97,7 +98,7 @@
+ " [-m|--method <method>] [-N|--no-librenms] [-p|--pretty]"
+ " [-v|--verbose] [-w|--warnings] | -l|--list-methods | -h|--help"
)
methods = ["sensors", "hpasmcli"]
methods = ["sensors", "hpasmcli", "ipmitool"]
# costPerkWh = 0.15 # <<<< CHANGE

### General functions
Expand Down Expand Up @@ -138,6 +139,10 @@ def getData(method):

elif method == "hpasmcli":
data = getHPASMData()

elif method == "ipmitool":
data = getIPMIdata()

else:
usageError("You must specify a method.")

Expand Down Expand Up @@ -290,6 +295,42 @@ def getHPASMData():
return hdata


def getIPMIdata():
global error, errorString
error = 2
errorString = "No power sensor found"

exe = shutil.which("ipmitool")
# if not os.access(candidate, os.W_OK):
cmd = [exe, "dcmi", "power", "reading"]
warningMsg("ipmitool only runs as root")

try:
output = subprocess.run(
cmd, capture_output=True, check=True, text=True, timeout=2
)

except subprocess.CalledProcessError as e:
errorMsg(str(e) + ": " + str(e.stdout).strip("\n"))
sys.exit(1)

psu_reading = "^\s+Instantaneous power reading:\s+"

rawdata = str(output.stdout).replace("\t", " ").replace("\n ", "\n").split("\n")

hdata = {}
hdata["psu"] = {} # Init PSU data structure
hdata["psu"][0] = {} # Only one value is returned.

for line in rawdata:
if re.match(psu_reading, line):
verboseMsg("found power meter reading: " + line)
junk, meter_reading = line.split(":", 1)
hdata["psu"][0]["reading"] = psu_reading.replace("Watts", "").strip()

return hdata


# Argument Parsing
try:
opts, args = getopt.gnu_getopt(
Expand Down
Loading