Setup metric utilities

This commit is contained in:
2025-06-28 13:38:21 +02:00
parent 9aec2e80bd
commit e426588cb8
5 changed files with 177 additions and 0 deletions
+19
View File
@@ -0,0 +1,19 @@
#!/bin/bash
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
. "${SCRIPT_DIR}/logging.sh"
LOGFILE=/var/log/disk_usage.log
touch "$LOGFILE"
chmod 644 "$LOGFILE"
for i in {1..6}; do
timestamp=$(GetTimestamp)
df --output=target,used,size -B1 \
| tail -n +2 \
| while read -r mount used total; do
echo "$timestamp $mount $used $total" >> "$LOGFILE"
done
sleep 10
done
+30
View File
@@ -0,0 +1,30 @@
#!/bin/bash
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
. "${SCRIPT_DIR}/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
+16
View File
@@ -0,0 +1,16 @@
#!/bin/bash
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
. "${SCRIPT_DIR}/logging.sh"
LOGFILE=/var/log/temperature.log
touch "$LOGFILE"
chmod 644 "$LOGFILE"
for i in {1..6}; do
timestamp=$(GetTimestamp)
paste <(cat /sys/class/thermal/thermal_zone*/type) <(cat /sys/class/thermal/thermal_zone*/temp) | column -s $'\t' -t | sed 's/\(.\)..$/.\1°C/' | while read line; do
echo "$timestamp $line" >> "$LOGFILE"
done
sleep 10
done
+25
View File
@@ -0,0 +1,25 @@
#!/bin/bash
GetTimestamp(){
date +"%Y-%m-%dT%H:%M:%S"
}
LogInformation(){
local message="$1"
echo -e "[$(GetTimestamp)] [INFO] ${message}"
}
LogWarning(){
local message="$1"
echo -e "[$(GetTimestamp)] [WARN] ${message}"
}
LogError(){
local message="$1"
echo -e "[$(GetTimestamp)] [EROR] ${message}"
}
LogDebug(){
local message="$1"
echo -e "[$(GetTimestamp)] [DBUG] ${message}"
}
+87
View File
@@ -0,0 +1,87 @@
#!/bin/bash
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
. "${SCRIPT_DIR}/logging.sh"
SCRIPTS=(
"check-disk-usage.sh"
"check-power-usage.sh"
"check-temperature.sh"
)
CheckScriptExists() {
local script="$1"
local script_path="${SCRIPT_DIR}/${script}"
if [[ -f "$script_path" && -x "$script_path" ]]; then
return 0
elif [[ -f "$script_path" ]]; then
LogWarning "Script $script exists but is not executable. Making it executable..."
chmod +x "$script_path"
return 0
else
return 1
fi
}
ManageCrontabEntry() {
local script="$1"
local script_path="${SCRIPT_DIR}/${script}"
local cron_pattern="* * * * * ${script_path}"
local current_crontab
current_crontab=$(crontab -l 2>/dev/null || echo "")
if echo "$current_crontab" | grep -q "$script_path"; then
LogInformation "Crontab entry for $script already exists"
if echo "$current_crontab" | grep -q "^\* \* \* \* \* ${script_path}"; then
LogInformation "Entry for $script is already set to run every minute"
else
LogInformation "Updating $script to run every minute"
local new_crontab
new_crontab=$(echo "$current_crontab" | grep -v "$script_path")
new_crontab="${new_crontab}${new_crontab:+$'\n'}${cron_pattern}"
echo "$new_crontab" | crontab -
LogInformation "Updated crontab entry for $script"
fi
else
LogInformation "Adding new crontab entry for $script"
local new_crontab="${current_crontab}${current_crontab:+$'\n'}${cron_pattern}"
echo "$new_crontab" | crontab -
LogInformation "Added crontab entry for $script"
fi
}
BackupCrontab() {
local backup_file="${SCRIPT_DIR}/crontab_backup_$(date +%Y%m%d_%H%M%S)"
crontab -l > "$backup_file" 2>/dev/null
if [[ $? -eq 0 ]]; then
LogInformation "Crontab backed up to: $backup_file"
else
LogWarning "No existing crontab to backup or backup failed"
fi
}
Main() {
LogInformation "Starting crontab setup for server management scripts"
BackupCrontab
for script in "${SCRIPTS[@]}"; do
LogInformation "Processing $script..."
if CheckScriptExists "$script"; then
LogInformation "Script $script found and is executable"
ManageCrontabEntry "$script"
else
LogWarning "Script $script not found or not accessible, skipping..."
fi
done
LogInformation "Crontab setup completed"
LogInformation "Current crontab entries:"
crontab -l 2>/dev/null | grep -E "(check-disk-usage|check-power-usage|check-temperature)" || LogInformation "No matching entries found"
}
Main "$@"