mirror of
https://github.com/AlexMacocian/ServerManagementUtils.git
synced 2026-07-15 15:19:58 +00:00
30 lines
810 B
Bash
30 lines
810 B
Bash
#!/bin/bash
|
|
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
. "${SCRIPT_DIR}/../share/logging.sh"
|
|
|
|
LOGFILE=/var/log/power_usage.log
|
|
touch "$LOGFILE"
|
|
chmod 644 "$LOGFILE"
|
|
|
|
# Path to the energy_uj file for the CPU package
|
|
RAPL_PATH="/sys/class/powercap/intel-rapl:0/energy_uj"
|
|
for i in {1..6}; do
|
|
energy_1=$(cat $RAPL_PATH)
|
|
sleep 1
|
|
|
|
energy_2=$(cat $RAPL_PATH)
|
|
energy_diff=$((energy_2 - energy_1))
|
|
|
|
# Convert microjoules to joules (1 J = 1,000,000 µJ)
|
|
energy_joules=$(echo "scale=6; $energy_diff / 1000000" | bc)
|
|
|
|
# Since we waited 1 second, the power usage is simply the energy difference in joules (W)
|
|
power_watts=$(echo "scale=6; $energy_joules / 1" | bc)
|
|
|
|
timestamp=$(GetTimestamp)
|
|
|
|
# Write power usage to log file
|
|
echo "$timestamp $power_watts" >> "$LOGFILE"
|
|
sleep 9
|
|
done |