mirror of
https://github.com/AlexMacocian/ServerManagementUtils.git
synced 2026-07-21 01:59:50 +00:00
34 lines
928 B
Bash
34 lines
928 B
Bash
#!/bin/bash
|
|
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
. "${SCRIPT_DIR}/../share/logging.sh"
|
|
|
|
LOGFILE=/var/log/disk_usage.log
|
|
|
|
# Create log file with proper error handling
|
|
if ! touch "$LOGFILE" 2>/dev/null; then
|
|
LogError "Cannot create log file $LOGFILE"
|
|
exit 1
|
|
fi
|
|
chmod 644 "$LOGFILE" 2>/dev/null
|
|
|
|
LogInformation "Starting disk usage monitoring"
|
|
|
|
for i in {1..6}; do
|
|
timestamp=$(GetTimestamp)
|
|
|
|
# Use process substitution instead of pipe to avoid subshell issues
|
|
while read -r mount used total; do
|
|
if [[ -n "$mount" && -n "$used" && -n "$total" ]]; then
|
|
echo "$timestamp $mount $used $total" >> "$LOGFILE"
|
|
fi
|
|
done < <(df --output=target,used,size -B1 2>/dev/null | tail -n +2)
|
|
|
|
LogInformation "Disk usage data collected (iteration $i/6)"
|
|
|
|
if [ $i -lt 6 ]; then
|
|
sleep 10
|
|
fi
|
|
done
|
|
|
|
LogInformation "Disk usage monitoring completed" |