Skip to content

Instantly share code, notes, and snippets.

@Saruspete
Created May 26, 2021 14:39
Show Gist options
  • Save Saruspete/344304c48ae2af6edea6e856c09236ab to your computer and use it in GitHub Desktop.
Save Saruspete/344304c48ae2af6edea6e856c09236ab to your computer and use it in GitHub Desktop.
#!/usr/bin/env sh
set -u
export LC_ALL=C
# Reexec'ing myself as root
if [[ "$(id -u)" != "0" ]]; then
# Check with sudo
if ! [[ -r /dev/ipmi0 ]] && ! type -P sudo >/dev/null; then
echo >&2 "You need sudo to reexec this script as root for ipmi access"
exit 1
fi
#
exec sudo --non-interactive "$(realpath $0)" "$@"
fi
# Default sampling to 5 seconds. Can do lower
sampling="${1:-2}"
# Maximum number of process in sleep mode
processThreshold="10"
# Use timeout if available
timeoutCmd="$(type -P timeout)"
timeoutCmd+="${timeoutCmd:+ $sampling}"
# Check for mandatory bins
if ! type -P ipmi-dcmi >/dev/null; then
echo >&2 "No binary ipmi-dcmi available in '${PATH//:/ }'"
exit 1
fi
ipmiCmd="ipmi-dcmi --get-system-power-statistics"
# will launch in background to avoid too heavy sampling drift
while sleep $sampling; do
$ipmiCmd &
# Security for avoid too many forks shall IPMI fail
if [[ "$(jobs -lp|wc -l)" -gt $processThreshold ]]; then
echo >&2 "More than $processThreshold proc in background. Try lowering the sampling rate"
# Exiting in hope process will clean themselves and netdata will restart us
exit 0
fi
done | awk '
# First call, generate the graph definition
NR == 1 {
print "CHART ipmi.power \"\" \"Power Consumption\" \"Watts\" ipmi-dcmi.plugin ipmi.power line 100000 '$sampling'"
print "DIMENSION dcmi \"\" absolute 1 1"
}
$1$2 == "CurrentPower" {
print "BEGIN ipmi.power"
print "SET dcmi = "$4
print "END"
fflush()
}
'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment