130 lines
4.9 KiB
Bash
130 lines
4.9 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
|
|
start_time=$(date)
|
|
|
|
# Output start time
|
|
echo "Report started at: $start_time" >> $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" && -d /var/cache/apt ]]; then
|
|
du -sh /var/cache/apt 2>>$OUTPUT_FILE >> $OUTPUT_FILE || echo "Error estimating apt cache size." >> $OUTPUT_FILE
|
|
elif [[ $OS == "FreeBSD" && -d /var/cache/pkg ]]; then
|
|
du -sh /var/cache/pkg 2>>$OUTPUT_FILE >> $OUTPUT_FILE || echo "Error estimating pkg cache size." >> $OUTPUT_FILE
|
|
else
|
|
echo "Package cache directory not found for $OS." >> $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" && -d /var/lib/apt/lists ]]; then
|
|
du -sh /var/lib/apt/lists /var/lib/apt/lists/partial 2>>$OUTPUT_FILE >> $OUTPUT_FILE || echo "Error estimating old packages size." >> $OUTPUT_FILE
|
|
elif [[ $OS == "FreeBSD" && -d /var/db/pkg ]]; then
|
|
du -sh /var/db/pkg 2>>$OUTPUT_FILE >> $OUTPUT_FILE || echo "Error estimating old packages size." >> $OUTPUT_FILE
|
|
else
|
|
echo "Old package directory not found for $OS." >> $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" && $(command -v journalctl) ]]; then
|
|
journalctl --disk-usage 2>>$OUTPUT_FILE >> $OUTPUT_FILE || echo "Error estimating journal log size." >> $OUTPUT_FILE
|
|
elif [[ $OS == "FreeBSD" && -d /var/log ]]; then
|
|
du -sh /var/log 2>>$OUTPUT_FILE >> $OUTPUT_FILE || echo "Error estimating log size in /var/log." >> $OUTPUT_FILE
|
|
else
|
|
echo "Log directory not found for $OS." >> $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
|
|
if [[ -d /tmp && -d /var/tmp ]]; then
|
|
du -sh /tmp /var/tmp 2>>$OUTPUT_FILE >> $OUTPUT_FILE || echo "Error estimating temporary files size." >> $OUTPUT_FILE
|
|
else
|
|
echo "/tmp or /var/tmp directories not found." >> $OUTPUT_FILE
|
|
fi
|
|
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
|
|
if [[ -d $directory ]]; then
|
|
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
|
|
sleep 2 # Added delay to ensure command has sufficient runtime
|
|
else
|
|
echo "$directory directory not found." >> $OUTPUT_FILE
|
|
fi
|
|
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 with timestamps
|
|
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
|
|
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
|
|
|
|
# Output end time
|
|
end_time=$(date)
|
|
echo "Storage savings estimation and large directory listing completed." >> $OUTPUT_FILE
|
|
echo "Report completed at: $end_time" >> $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
|