tursa-energy-efficiency/2-racks/size-loc32/sequana-monitor.sh
2022-09-07 17:31:28 +01:00

61 lines
1.3 KiB
Bash
Executable File

#!/usr/bin/env bash
# to run on tursa-mgmt2
function interrupt()
{
printf '\n'
echo 'monitoring interrupted'
rm -f "${tmp}"
exit 0
}
set -euo pipefail
if (( $# != 3 )); then
echo "usage: $(basename "$0") <database> <table> <period (int sec)>" 1>&2
exit 1
fi
db="$1"
table="$2"
period="$3"
if (( period < 6 )); then
echo 'error: period should be at least 6 seconds' 1>&2
exit 1
fi
sqlite3 -batch "${db}" << EOF
DROP TABLE IF EXISTS ${table};
CREATE TABLE ${table} (
sample INTEGER PRIMARY KEY NOT NULL,
timestamp INTEGER NOT NULL,
rack_1 INTEGER NOT NULL,
rack_2 INTEGER NOT NULL,
rack_3 INTEGER NOT NULL,
rack_4 INTEGER NOT NULL
);
EOF
tmp=$(mktemp)
n=0
trap interrupt SIGINT
echo 'monitoring power...'
echo 'sample:'
while true; do
start=$(date "+%s")
printf '%d ' ${n}
trap "" SIGINT
echo "${n}|$(date '+%s')$(./pmsmRack-all.py 2>/dev/null | grep RACK | awk '{printf("|%s",$6);} END{printf("\n")}')" 1> "${tmp}"
sqlite3 -batch "${db}" ".import ${tmp} ${table}"
trap interrupt SIGINT
end=$(date "+%s")
duration=$((end - start))
wait_time=$((period - duration))
if ((wait_time < 0)); then
echo 'warning: polling took longer than requested period' 1>&2
wait_time=0
fi
sleep ${wait_time}
n=$((++n))
done