Update disk-check.sh

This commit is contained in:
colin 2024-10-29 10:34:43 -04:00
parent a08f30fa56
commit 2e26be15ab
1 changed files with 84 additions and 45 deletions

View File

@ -1,77 +1,102 @@
#!/bin/bash #!/bin/bash
echo "Starting disk space report... This may take a few minutes." echo "Starting disk space report... This may take a few minutes." > /tmp/disk_report.txt
# Function to 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
return 1
fi
return 0
}
# Function to estimate the size of the apt cache # Function to estimate the size of the apt cache
estimate_apt_cache_size() { estimate_apt_cache_size() {
echo "Estimating the size of the apt cache:" echo "Estimating the size of the apt cache:" >> /tmp/disk_report.txt
sudo du -sh /var/cache/apt sudo du -sh /var/cache/apt 2>/dev/null >> /tmp/disk_report.txt || echo "Error estimating apt cache size." >> /tmp/disk_report.txt
echo echo >> /tmp/disk_report.txt
} }
# Function to estimate the size of old installed packages # Function to estimate the size of old installed packages
estimate_old_packages_size() { estimate_old_packages_size() {
echo "Estimating the size of old installed packages:" echo "Estimating the size of old installed packages:" >> /tmp/disk_report.txt
sudo du -sh /var/lib/apt/lists /var/lib/apt/lists/partial 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 echo >> /tmp/disk_report.txt
} }
# Function to estimate the size of journal logs # Function to estimate the size of journal logs
estimate_journal_size() { estimate_journal_size() {
echo "Estimating the size of journal logs:" echo "Estimating the size of journal logs:" >> /tmp/disk_report.txt
sudo journalctl --disk-usage sudo journalctl --disk-usage 2>/dev/null >> /tmp/disk_report.txt || echo "Error estimating journal log size." >> /tmp/disk_report.txt
echo echo >> /tmp/disk_report.txt
} }
# Function to estimate the size of temporary files # Function to estimate the size of temporary files
estimate_tmp_size() { estimate_tmp_size() {
echo "Estimating the size of temporary files:" echo "Estimating the size of temporary files:" >> /tmp/disk_report.txt
sudo du -sh /tmp /var/tmp sudo du -sh /tmp /var/tmp 2>/dev/null >> /tmp/disk_report.txt || echo "Error estimating temporary files size." >> /tmp/disk_report.txt
echo echo >> /tmp/disk_report.txt
} }
# Function to estimate the size of unused Docker volumes # Function to estimate the size of unused Docker volumes
estimate_docker_volumes_size() { estimate_docker_volumes_size() {
echo "Estimating the size of unused Docker volumes:" if is_docker_installed; then
docker volume ls -qf dangling=true | xargs -I {} docker volume inspect --format '{{ .Mountpoint }}' {} | xargs -I {} sudo du -sh {} | awk '{ sum += $1 } END { print sum "B" }' echo "Estimating the size of unused Docker volumes:" >> /tmp/disk_report.txt
echo 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
fi
} }
# Function to check and suggest logrotate for large Docker logs # Function to check and suggest logrotate for large Docker logs
check_docker_logs() { check_docker_logs() {
echo "Checking Docker logs for large files..." if is_docker_installed; then
large_logs=$(docker ps -q --filter "status=exited" | xargs -I {} docker inspect --format '{{.LogPath}}' {} | xargs -I {} sudo find {} -type f -size +1G) echo "Checking Docker logs for large files..." >> /tmp/disk_report.txt
if [ -n "$large_logs" ]; then 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 "The following Docker logs are larger than 1GB:" if [ -n "$large_logs" ]; then
echo "$large_logs" echo "The following Docker logs are larger than 1GB:" >> /tmp/disk_report.txt
echo echo "$large_logs" >> /tmp/disk_report.txt
echo "Consider setting up logrotate to manage Docker logs." echo >> /tmp/disk_report.txt
echo "To truncate all Docker logs, run:" echo "Consider setting up logrotate to manage Docker logs." >> /tmp/disk_report.txt
echo 'sudo find /var/lib/docker/containers/ -type f -name "*.log" -exec truncate -s 0 {} \;' echo "To truncate all Docker logs, run:" >> /tmp/disk_report.txt
echo echo 'sudo find /var/lib/docker/containers/ -type f -name "*.log" -exec truncate -s 0 {} \;' >> /tmp/disk_report.txt
else echo >> /tmp/disk_report.txt
echo "No large Docker logs found." else
echo echo "No large Docker logs found." >> /tmp/disk_report.txt
echo >> /tmp/disk_report.txt
fi
fi fi
} }
# Function to list directories consuming more than a specified size # Function to list directories consuming more than a specified size
list_large_directories() { list_large_directories() {
local directory=$1 local directory=$1
local size_limit=$2 echo "Scanning $directory for large directories..." >> /tmp/disk_report.txt
echo "Directories in $directory consuming more than ${size_limit}GB:" 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
sudo du -ahx $directory 2>/dev/null | awk -v limit=$size_limit '{ }
size=$1; unit=substr(size, length(size));
# Function to parse and categorize directory sizes
categorize_large_directories() {
echo "Categorizing large directories..." >> /tmp/disk_report.txt
awk '
{
size=$1; path=$2;
unit=substr(size, length(size));
size_val=substr(size, 1, length(size)-1); size_val=substr(size, 1, length(size)-1);
if ((unit=="G" && size_val+0 > limit) || (unit=="T" && size_val*1024 > limit)) { if (unit == "G" || unit == "T") {
print if (unit == "T") size_val *= 1024;
if (size_val > 50) print "> 50GB: " path;
else if (size_val > 20) print "> 20GB: " path;
else if (size_val > 10) print "> 10GB: " path;
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 echo >> /tmp/disk_report.txt
} }
# Estimate storage savings # Estimate storage savings
echo "Estimating potential storage savings..." echo "Estimating potential storage savings..." >> /tmp/disk_report.txt
estimate_apt_cache_size estimate_apt_cache_size
estimate_old_packages_size estimate_old_packages_size
estimate_journal_size estimate_journal_size
@ -81,11 +106,25 @@ estimate_docker_volumes_size
# Check Docker logs # Check Docker logs
check_docker_logs check_docker_logs
# List large directories # List large directories and categorize them
echo "Listing directories consuming more than 5GB and 10GB:" list_large_directories /
list_large_directories / 5 categorize_large_directories
list_large_directories /home 5
list_large_directories / 10
list_large_directories /home 10
echo "Storage savings estimation and large directory listing completed." 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
categorize_large_directories
echo "Storage savings estimation and large directory listing completed." >> /tmp/disk_report.txt
# Upload the report to hastebin
response=$(curl -s -X POST -T /tmp/disk_report.txt https://haste.nixc.us/documents)
if [[ $response == *"key"* ]]; then
key=$(echo $response | jq -r '.key')
echo "Report available at: https://haste.nixc.us/$key"
else
echo "Failed to upload report to haste.nixc.us"
fi