Update disk-check.sh
This commit is contained in:
parent
8df33c9269
commit
cdd8e6e47b
126
disk-check.sh
126
disk-check.sh
|
@ -1,85 +1,69 @@
|
|||
#!/bin/bash
|
||||
|
||||
echo "Starting disk space report... This may take a few minutes." > /tmp/disk_report.txt
|
||||
REPORT_FILE="/tmp/disk_report.txt"
|
||||
TMP_OUTPUT="/tmp/du_output.txt"
|
||||
HASTE_URL="https://haste.nixc.us/documents"
|
||||
|
||||
# Function to check if Docker is installed
|
||||
echo "Starting disk space report... This may take a few minutes." > "$REPORT_FILE"
|
||||
|
||||
# Check if Docker is installed
|
||||
is_docker_installed() {
|
||||
if ! command -v docker &>/dev/null; then
|
||||
echo "Docker is not installed. Skipping Docker-related checks." >> /tmp/disk_report.txt
|
||||
echo "Docker is not installed. Skipping Docker-related checks." >> "$REPORT_FILE"
|
||||
return 1
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
|
||||
# Function to estimate the size of the apt cache
|
||||
estimate_apt_cache_size() {
|
||||
echo "Estimating the size of the apt cache:" >> /tmp/disk_report.txt
|
||||
sudo du -sh /var/cache/apt 2>/dev/null >> /tmp/disk_report.txt || echo "Error estimating apt cache size." >> /tmp/disk_report.txt
|
||||
echo >> /tmp/disk_report.txt
|
||||
# Function to estimate directory sizes
|
||||
estimate_size() {
|
||||
local description=$1
|
||||
local path=$2
|
||||
echo "Estimating the size of $description:" >> "$REPORT_FILE"
|
||||
sudo du -sh "$path" 2>/dev/null >> "$REPORT_FILE" || echo "Error estimating $description size." >> "$REPORT_FILE"
|
||||
echo >> "$REPORT_FILE"
|
||||
}
|
||||
|
||||
# Function to estimate the size of old installed packages
|
||||
estimate_old_packages_size() {
|
||||
echo "Estimating the size of old installed packages:" >> /tmp/disk_report.txt
|
||||
sudo du -sh /var/lib/apt/lists /var/lib/apt/lists/partial 2>/dev/null >> /tmp/disk_report.txt || echo "Error estimating old packages size." >> /tmp/disk_report.txt
|
||||
echo >> /tmp/disk_report.txt
|
||||
}
|
||||
|
||||
# Function to estimate the size of journal logs
|
||||
estimate_journal_size() {
|
||||
echo "Estimating the size of journal logs:" >> /tmp/disk_report.txt
|
||||
sudo journalctl --disk-usage 2>/dev/null >> /tmp/disk_report.txt || echo "Error estimating journal log size." >> /tmp/disk_report.txt
|
||||
echo >> /tmp/disk_report.txt
|
||||
}
|
||||
|
||||
# Function to estimate the size of temporary files
|
||||
estimate_tmp_size() {
|
||||
echo "Estimating the size of temporary files:" >> /tmp/disk_report.txt
|
||||
sudo du -sh /tmp /var/tmp 2>/dev/null >> /tmp/disk_report.txt || echo "Error estimating temporary files size." >> /tmp/disk_report.txt
|
||||
echo >> /tmp/disk_report.txt
|
||||
}
|
||||
|
||||
# Function to estimate the size of unused Docker volumes
|
||||
# Estimate unused Docker volumes
|
||||
estimate_docker_volumes_size() {
|
||||
if is_docker_installed; then
|
||||
echo "Estimating the size of unused Docker volumes:" >> /tmp/disk_report.txt
|
||||
docker volume ls -qf dangling=true | xargs -I {} docker volume inspect --format '{{ .Mountpoint }}' {} 2>/dev/null | xargs -I {} sudo du -sh {} 2>/dev/null | awk '{ sum += $1 } END { print sum "B" }' >> /tmp/disk_report.txt
|
||||
echo >> /tmp/disk_report.txt
|
||||
echo "Estimating the size of unused Docker volumes:" >> "$REPORT_FILE"
|
||||
docker volume ls -qf dangling=true | \
|
||||
xargs -I {} docker volume inspect --format '{{ .Mountpoint }}' {} 2>/dev/null | \
|
||||
xargs -I {} sudo du -sh {} 2>/dev/null | \
|
||||
awk '{ sum += $1 } END { print sum "B" }' >> "$REPORT_FILE" || echo "Error estimating Docker volumes." >> "$REPORT_FILE"
|
||||
echo >> "$REPORT_FILE"
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to check and suggest logrotate for large Docker logs
|
||||
# Check large Docker logs
|
||||
check_docker_logs() {
|
||||
if is_docker_installed; then
|
||||
echo "Checking Docker logs for large files..." >> /tmp/disk_report.txt
|
||||
large_logs=$(docker ps -q --filter "status=exited" | xargs -I {} docker inspect --format '{{.LogPath}}' {} 2>/dev/null | xargs -I {} sudo find {} -type f -size +1G 2>/dev/null)
|
||||
echo "Checking Docker logs for large files..." >> "$REPORT_FILE"
|
||||
large_logs=$(docker ps -q --filter "status=exited" | \
|
||||
xargs -I {} docker inspect --format '{{.LogPath}}' {} 2>/dev/null | \
|
||||
xargs -I {} sudo find {} -type f -size +1G 2>/dev/null)
|
||||
if [ -n "$large_logs" ]; then
|
||||
echo "The following Docker logs are larger than 1GB:" >> /tmp/disk_report.txt
|
||||
echo "$large_logs" >> /tmp/disk_report.txt
|
||||
echo >> /tmp/disk_report.txt
|
||||
echo "Consider setting up logrotate to manage Docker logs." >> /tmp/disk_report.txt
|
||||
echo "To truncate all Docker logs, run:" >> /tmp/disk_report.txt
|
||||
echo 'sudo find /var/lib/docker/containers/ -type f -name "*.log" -exec truncate -s 0 {} \;' >> /tmp/disk_report.txt
|
||||
echo >> /tmp/disk_report.txt
|
||||
echo "The following Docker logs are larger than 1GB:" >> "$REPORT_FILE"
|
||||
echo "$large_logs" >> "$REPORT_FILE"
|
||||
echo "Consider setting up logrotate to manage Docker logs." >> "$REPORT_FILE"
|
||||
else
|
||||
echo "No large Docker logs found." >> /tmp/disk_report.txt
|
||||
echo >> /tmp/disk_report.txt
|
||||
echo "No large Docker logs found." >> "$REPORT_FILE"
|
||||
fi
|
||||
echo >> "$REPORT_FILE"
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to list directories consuming more than a specified size
|
||||
# List and categorize large directories
|
||||
list_large_directories() {
|
||||
local directory=$1
|
||||
echo "Scanning $directory for large directories..." >> /tmp/disk_report.txt
|
||||
sudo du -ahx $directory --exclude=/home/virtfs 2>/dev/null | sort -hr > /tmp/du_output.txt || echo "Error scanning directories in $directory." >> /tmp/disk_report.txt
|
||||
cat /tmp/du_output.txt >> /tmp/disk_report.txt
|
||||
echo "Scanning $directory for large directories..." >> "$REPORT_FILE"
|
||||
sudo du -ahx "$directory" --exclude=/home/virtfs 2>/dev/null | sort -hr > "$TMP_OUTPUT" || echo "Error scanning $directory." >> "$REPORT_FILE"
|
||||
cat "$TMP_OUTPUT" >> "$REPORT_FILE"
|
||||
}
|
||||
|
||||
|
||||
# Function to parse and categorize directory sizes
|
||||
categorize_large_directories() {
|
||||
echo "Categorizing large directories..." >> /tmp/disk_report.txt
|
||||
echo "Categorizing large directories..." >> "$REPORT_FILE"
|
||||
awk '
|
||||
{
|
||||
size=$1; path=$2;
|
||||
|
@ -93,40 +77,40 @@ categorize_large_directories() {
|
|||
else if (size_val > 5) print "> 5GB: " path;
|
||||
else if (size_val > 1) print "> 1GB: " path;
|
||||
}
|
||||
}' /tmp/du_output.txt >> /tmp/disk_report.txt
|
||||
echo >> /tmp/disk_report.txt
|
||||
}' "$TMP_OUTPUT" >> "$REPORT_FILE"
|
||||
echo >> "$REPORT_FILE"
|
||||
}
|
||||
|
||||
# Estimate storage savings
|
||||
echo "Estimating potential storage savings..." >> /tmp/disk_report.txt
|
||||
estimate_apt_cache_size
|
||||
estimate_old_packages_size
|
||||
estimate_journal_size
|
||||
estimate_tmp_size
|
||||
estimate_docker_volumes_size
|
||||
# Main workflow
|
||||
echo "Estimating potential storage savings..." >> "$REPORT_FILE"
|
||||
|
||||
# Check Docker logs
|
||||
estimate_size "apt cache" "/var/cache/apt"
|
||||
estimate_size "old installed packages" "/var/lib/apt/lists /var/lib/apt/lists/partial"
|
||||
estimate_size "journal logs" "(journalctl --disk-usage)"
|
||||
estimate_size "temporary files" "/tmp /var/tmp"
|
||||
estimate_docker_volumes_size
|
||||
check_docker_logs
|
||||
|
||||
# List large directories and categorize them
|
||||
list_large_directories /
|
||||
list_large_directories "/"
|
||||
categorize_large_directories
|
||||
|
||||
list_large_directories /home
|
||||
list_large_directories "/home"
|
||||
categorize_large_directories
|
||||
|
||||
# Scan and report specifically for /home/virtfs
|
||||
echo "Scanning /home/virtfs for large directories..." >> /tmp/disk_report.txt
|
||||
sudo du -ahx /home/virtfs 2>/dev/null | sort -hr > /tmp/du_output.txt || echo "Error scanning directories in /home/virtfs." >> /tmp/disk_report.txt
|
||||
echo "Scanning /home/virtfs for large directories..." >> "$REPORT_FILE"
|
||||
sudo du -ahx /home/virtfs 2>/dev/null | sort -hr > "$TMP_OUTPUT" || echo "Error scanning /home/virtfs." >> "$REPORT_FILE"
|
||||
categorize_large_directories
|
||||
|
||||
echo "Storage savings estimation and large directory listing completed." >> /tmp/disk_report.txt
|
||||
echo "Storage savings estimation and large directory listing completed." >> "$REPORT_FILE"
|
||||
|
||||
# Upload the report to hastebin
|
||||
response=$(curl -s -X POST -T /tmp/disk_report.txt https://haste.nixc.us/documents)
|
||||
echo "Uploading report to hastebin..." >> "$REPORT_FILE"
|
||||
response=$(curl -s -X POST -T "$REPORT_FILE" "$HASTE_URL")
|
||||
echo "Raw response from hastebin: $response" >> "$REPORT_FILE"
|
||||
if [[ $response == *"key"* ]]; then
|
||||
key=$(echo $response | jq -r '.key')
|
||||
echo "Report available at: https://haste.nixc.us/$key"
|
||||
echo "Report available at: $HASTE_URL/$key"
|
||||
else
|
||||
echo "Failed to upload report to haste.nixc.us"
|
||||
echo "Failed to upload report to haste.nixc.us. Response: $response"
|
||||
fi
|
||||
|
|
Loading…
Reference in New Issue