Skip to content

Commit

Permalink
Add inital IPMItools functionality for Powermon extension. (#405)
Browse files Browse the repository at this point in the history
* Add inital IPMItools functionality

created `getIPMIdata()` based on existing `getHPASMData()`. 
Leverages ipmitool to gather power usage data.

* fix some lint errors
  • Loading branch information
adamus1red authored Oct 3, 2024
1 parent 11092a2 commit 04896e1
Showing 1 changed file with 43 additions and 2 deletions.
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

0 comments on commit 04896e1

Please sign in to comment.