disk-space-report/disk-check.sh

108 lines
4.1 KiB
Bash

#!/bin/bash
# Determine OS
OS=$(uname)
OUTPUT_FILE="/tmp/disk_report.txt"
echo "Starting disk space report... This may take a few minutes." > $OUTPUT_FILE
# Function to estimate the size of the package cache
estimate_package_cache_size() {
echo "Estimating the size of the package cache:" >> $OUTPUT_FILE
if [[ $OS == "Linux" ]]; then
sudo du -sh /var/cache/apt 2>/dev/null >> $OUTPUT_FILE || echo "Error estimating apt cache size." >> $OUTPUT_FILE
elif [[ $OS == "FreeBSD" ]]; then
sudo du -sh /var/cache/pkg 2>/dev/null >> $OUTPUT_FILE || echo "Error estimating pkg cache size." >> $OUTPUT_FILE
fi
echo >> $OUTPUT_FILE
}
# Function to estimate the size of old package files
estimate_old_packages_size() {
echo "Estimating the size of old package files:" >> $OUTPUT_FILE
if [[ $OS == "Linux" ]]; then
sudo du -sh /var/lib/apt/lists /var/lib/apt/lists/partial 2>/dev/null >> $OUTPUT_FILE || echo "Error estimating old packages size." >> $OUTPUT_FILE
elif [[ $OS == "FreeBSD" ]]; then
sudo du -sh /var/db/pkg 2>/dev/null >> $OUTPUT_FILE || echo "Error estimating old packages size." >> $OUTPUT_FILE
fi
echo >> $OUTPUT_FILE
}
# Function to estimate the size of system logs
estimate_log_size() {
echo "Estimating the size of system logs:" >> $OUTPUT_FILE
if [[ $OS == "Linux" ]]; then
sudo journalctl --disk-usage 2>/dev/null >> $OUTPUT_FILE || echo "Error estimating journal log size." >> $OUTPUT_FILE
elif [[ $OS == "FreeBSD" ]]; then
sudo du -sh /var/log 2>/dev/null >> $OUTPUT_FILE || echo "Error estimating log size in /var/log." >> $OUTPUT_FILE
fi
echo >> $OUTPUT_FILE
}
# Function to estimate the size of temporary files
estimate_tmp_size() {
echo "Estimating the size of temporary files:" >> $OUTPUT_FILE
sudo du -sh /tmp /var/tmp 2>/dev/null >> $OUTPUT_FILE || echo "Error estimating temporary files size." >> $OUTPUT_FILE
echo >> $OUTPUT_FILE
}
# Function to list directories consuming more than a specified size
list_large_directories() {
local directory=$1
echo "Scanning $directory for large directories..." >> $OUTPUT_FILE
sudo du -ah $directory 2>/dev/null | sort -hr > /tmp/du_output.txt || echo "Error scanning directories in $directory." >> $OUTPUT_FILE
cat /tmp/du_output.txt >> $OUTPUT_FILE
echo >> $OUTPUT_FILE
}
# Function to categorize directory sizes
categorize_large_directories() {
echo "Categorizing large directories..." >> $OUTPUT_FILE
awk '
{
size=$1; path=$2;
unit=substr(size, length(size));
size_val=substr(size, 1, length(size)-1);
if (unit == "G" || unit == "T") {
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 >> $OUTPUT_FILE
echo >> $OUTPUT_FILE
}
# Main function calls
echo "Estimating potential storage savings..." >> $OUTPUT_FILE
estimate_package_cache_size
estimate_old_packages_size
estimate_log_size
estimate_tmp_size
# List large directories and categorize them
list_large_directories /
categorize_large_directories
list_large_directories /home
categorize_large_directories
# Optional: specific directory scanning (e.g., /home/virtfs if required)
echo "Scanning /home/virtfs for large directories..." >> $OUTPUT_FILE
if [[ -d "/home/virtfs" ]]; then
sudo du -ah /home/virtfs 2>/dev/null | sort -hr > /tmp/du_output.txt || echo "Error scanning directories in /home/virtfs." >> $OUTPUT_FILE
categorize_large_directories
fi
echo "Storage savings estimation and large directory listing completed." >> $OUTPUT_FILE
# Upload the report to hastebin
response=$(curl -s -X POST -T $OUTPUT_FILE 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