110 lines
4.1 KiB
Bash
110 lines
4.1 KiB
Bash
#!/bin/bash
|
|
|
|
LOG_FILE="/tmp/disk_space_report.log"
|
|
HASTE_URL="https://haste.nixc.us/documents"
|
|
|
|
# Clear the log file if it exists
|
|
> "$LOG_FILE"
|
|
|
|
echo "Starting disk space report... This may take a few minutes." | tee -a "$LOG_FILE"
|
|
|
|
# Function to estimate the size of the apt cache
|
|
estimate_apt_cache_size() {
|
|
echo "Estimating the size of the apt cache:" | tee -a "$LOG_FILE"
|
|
sudo du -sh /var/cache/apt | tee -a "$LOG_FILE"
|
|
echo | tee -a "$LOG_FILE"
|
|
}
|
|
|
|
# Function to estimate the size of old installed packages
|
|
estimate_old_packages_size() {
|
|
echo "Estimating the size of old installed packages:" | tee -a "$LOG_FILE"
|
|
sudo du -sh /var/lib/apt/lists /var/lib/apt/lists/partial | tee -a "$LOG_FILE"
|
|
echo | tee -a "$LOG_FILE"
|
|
}
|
|
|
|
# Function to estimate the size of journal logs
|
|
estimate_journal_size() {
|
|
echo "Estimating the size of journal logs:" | tee -a "$LOG_FILE"
|
|
sudo journalctl --disk-usage | tee -a "$LOG_FILE"
|
|
echo | tee -a "$LOG_FILE"
|
|
}
|
|
|
|
# Function to estimate the size of temporary files
|
|
estimate_tmp_size() {
|
|
echo "Estimating the size of temporary files:" | tee -a "$LOG_FILE"
|
|
sudo du -sh /tmp /var/tmp | tee -a "$LOG_FILE"
|
|
echo | tee -a "$LOG_FILE"
|
|
}
|
|
|
|
# Function to estimate the size of unused Docker volumes
|
|
estimate_docker_volumes_size() {
|
|
echo "Estimating the size of unused Docker volumes:" | tee -a "$LOG_FILE"
|
|
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" }' | tee -a "$LOG_FILE"
|
|
echo | tee -a "$LOG_FILE"
|
|
}
|
|
|
|
# Function to check and suggest logrotate for large Docker logs
|
|
check_docker_logs() {
|
|
echo "Checking Docker logs for large files..." | tee -a "$LOG_FILE"
|
|
large_logs=$(docker ps -q --filter "status=exited" | xargs -I {} docker inspect --format '{{.LogPath}}' {} | xargs -I {} sudo find {} -type f -size +1G)
|
|
if [ -n "$large_logs" ]; then
|
|
echo "The following Docker logs are larger than 1GB:" | tee -a "$LOG_FILE"
|
|
echo "$large_logs" | tee -a "$LOG_FILE"
|
|
echo | tee -a "$LOG_FILE"
|
|
echo "Consider setting up logrotate to manage Docker logs." | tee -a "$LOG_FILE"
|
|
echo "To truncate all Docker logs, run:" | tee -a "$LOG_FILE"
|
|
echo 'sudo find /var/lib/docker/containers/ -type f -name "*.log" -exec truncate -s 0 {} \;' | tee -a "$LOG_FILE"
|
|
echo | tee -a "$LOG_FILE"
|
|
else
|
|
echo "No large Docker logs found." | tee -a "$LOG_FILE"
|
|
echo | tee -a "$LOG_FILE"
|
|
fi
|
|
}
|
|
|
|
# Function to list directories consuming more than a specified size
|
|
list_large_directories() {
|
|
local directory=$1
|
|
local size_limit=$2
|
|
echo "Directories in $directory consuming more than ${size_limit}GB:" | tee -a "$LOG_FILE"
|
|
sudo du -ahx "$directory" 2>/dev/null | awk -v limit="$size_limit" '{
|
|
size=$1; unit=substr(size, length(size));
|
|
size_val=substr(size, 1, length(size)-1);
|
|
if ((unit=="G" && size_val+0 > limit) || (unit=="T" && size_val*1024 > limit)) {
|
|
print
|
|
}
|
|
}' | tee -a "$LOG_FILE"
|
|
echo | tee -a "$LOG_FILE"
|
|
}
|
|
|
|
# Estimate storage savings
|
|
echo "Estimating potential storage savings..." | tee -a "$LOG_FILE"
|
|
estimate_apt_cache_size
|
|
estimate_old_packages_size
|
|
estimate_journal_size
|
|
estimate_tmp_size
|
|
estimate_docker_volumes_size
|
|
|
|
# Check Docker logs
|
|
check_docker_logs
|
|
|
|
# List large directories
|
|
echo "Listing directories consuming more than 5GB and 10GB:" | tee -a "$LOG_FILE"
|
|
list_large_directories / 5
|
|
list_large_directories /home 5
|
|
list_large_directories / 10
|
|
list_large_directories /home 10
|
|
|
|
echo "Storage savings estimation and large directory listing completed." | tee -a "$LOG_FILE"
|
|
|
|
# Upload the log file to Hastebin
|
|
echo "Uploading report to Hastebin..." | tee -a "$LOG_FILE"
|
|
response=$(curl -s -X POST -T "$LOG_FILE" "$HASTE_URL")
|
|
if [[ $response == *"key"* ]]; then
|
|
key=$(echo "$response" | jq -r '.key')
|
|
# Remove /documents/ and use the key to form the correct URL
|
|
report_url="https://haste.nixc.us/$key"
|
|
echo "Report available at: $report_url" | tee -a "$LOG_FILE"
|
|
else
|
|
echo "Failed to upload report to Hastebin. Response: $response" | tee -a "$LOG_FILE"
|
|
fi
|