Update disk-check.sh
This commit is contained in:
parent
805f472732
commit
8df33c9269
147
disk-check.sh
147
disk-check.sh
|
@ -1,81 +1,85 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
set -x
|
|
||||||
# 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 "Starting disk space report... This may take a few minutes." > /tmp/disk_report.txt
|
||||||
echo "Report started at: $start_time" >> $OUTPUT_FILE
|
|
||||||
|
|
||||||
# Function to estimate the size of the package cache
|
# Function to check if Docker is installed
|
||||||
estimate_package_cache_size() {
|
is_docker_installed() {
|
||||||
echo "Estimating the size of the package cache:" >> $OUTPUT_FILE
|
if ! command -v docker &>/dev/null; then
|
||||||
if [[ $OS == "Linux" && -d /var/cache/apt ]]; then
|
echo "Docker is not installed. Skipping Docker-related checks." >> /tmp/disk_report.txt
|
||||||
du -sh /var/cache/apt 2>>$OUTPUT_FILE >> $OUTPUT_FILE || echo "Error estimating apt cache size." >> $OUTPUT_FILE
|
return 1
|
||||||
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
|
fi
|
||||||
echo >> $OUTPUT_FILE
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
# Function to estimate the size of old package files
|
# 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 the size of old installed packages
|
||||||
estimate_old_packages_size() {
|
estimate_old_packages_size() {
|
||||||
echo "Estimating the size of old package files:" >> $OUTPUT_FILE
|
echo "Estimating the size of old installed packages:" >> /tmp/disk_report.txt
|
||||||
if [[ $OS == "Linux" && -d /var/lib/apt/lists ]]; then
|
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
|
||||||
du -sh /var/lib/apt/lists /var/lib/apt/lists/partial 2>>$OUTPUT_FILE >> $OUTPUT_FILE || echo "Error estimating old packages size." >> $OUTPUT_FILE
|
echo >> /tmp/disk_report.txt
|
||||||
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
|
# Function to estimate the size of journal logs
|
||||||
estimate_log_size() {
|
estimate_journal_size() {
|
||||||
echo "Estimating the size of system logs:" >> $OUTPUT_FILE
|
echo "Estimating the size of journal logs:" >> /tmp/disk_report.txt
|
||||||
if [[ $OS == "Linux" && $(command -v journalctl) ]]; then
|
sudo journalctl --disk-usage 2>/dev/null >> /tmp/disk_report.txt || echo "Error estimating journal log size." >> /tmp/disk_report.txt
|
||||||
journalctl --disk-usage 2>>$OUTPUT_FILE >> $OUTPUT_FILE || echo "Error estimating journal log size." >> $OUTPUT_FILE
|
echo >> /tmp/disk_report.txt
|
||||||
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
|
# Function to estimate the size of temporary files
|
||||||
estimate_tmp_size() {
|
estimate_tmp_size() {
|
||||||
echo "Estimating the size of temporary files:" >> $OUTPUT_FILE
|
echo "Estimating the size of temporary files:" >> /tmp/disk_report.txt
|
||||||
if [[ -d /tmp && -d /var/tmp ]]; then
|
sudo du -sh /tmp /var/tmp 2>/dev/null >> /tmp/disk_report.txt || echo "Error estimating temporary files size." >> /tmp/disk_report.txt
|
||||||
du -sh /tmp /var/tmp 2>>$OUTPUT_FILE >> $OUTPUT_FILE || echo "Error estimating temporary files size." >> $OUTPUT_FILE
|
echo >> /tmp/disk_report.txt
|
||||||
else
|
}
|
||||||
echo "/tmp or /var/tmp directories not found." >> $OUTPUT_FILE
|
|
||||||
|
# Function to estimate the size of 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
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# Function to check and suggest logrotate for 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)
|
||||||
|
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
|
||||||
|
else
|
||||||
|
echo "No large Docker logs found." >> /tmp/disk_report.txt
|
||||||
|
echo >> /tmp/disk_report.txt
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
echo >> $OUTPUT_FILE
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# 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
|
||||||
echo "Scanning $directory for large directories..." >> $OUTPUT_FILE
|
echo "Scanning $directory for large directories..." >> /tmp/disk_report.txt
|
||||||
if [[ -d $directory ]]; then
|
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
|
||||||
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 >> /tmp/disk_report.txt
|
||||||
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
|
|
||||||
|
# Function to parse and categorize directory sizes
|
||||||
categorize_large_directories() {
|
categorize_large_directories() {
|
||||||
echo "Categorizing large directories..." >> $OUTPUT_FILE
|
echo "Categorizing large directories..." >> /tmp/disk_report.txt
|
||||||
awk '
|
awk '
|
||||||
{
|
{
|
||||||
size=$1; path=$2;
|
size=$1; path=$2;
|
||||||
|
@ -89,16 +93,20 @@ categorize_large_directories() {
|
||||||
else if (size_val > 5) print "> 5GB: " path;
|
else if (size_val > 5) print "> 5GB: " path;
|
||||||
else if (size_val > 1) print "> 1GB: " path;
|
else if (size_val > 1) print "> 1GB: " path;
|
||||||
}
|
}
|
||||||
}' /tmp/du_output.txt >> $OUTPUT_FILE
|
}' /tmp/du_output.txt >> /tmp/disk_report.txt
|
||||||
echo >> $OUTPUT_FILE
|
echo >> /tmp/disk_report.txt
|
||||||
}
|
}
|
||||||
|
|
||||||
# Main function calls with timestamps
|
# Estimate storage savings
|
||||||
echo "Estimating potential storage savings..." >> $OUTPUT_FILE
|
echo "Estimating potential storage savings..." >> /tmp/disk_report.txt
|
||||||
estimate_package_cache_size
|
estimate_apt_cache_size
|
||||||
estimate_old_packages_size
|
estimate_old_packages_size
|
||||||
estimate_log_size
|
estimate_journal_size
|
||||||
estimate_tmp_size
|
estimate_tmp_size
|
||||||
|
estimate_docker_volumes_size
|
||||||
|
|
||||||
|
# Check Docker logs
|
||||||
|
check_docker_logs
|
||||||
|
|
||||||
# List large directories and categorize them
|
# List large directories and categorize them
|
||||||
list_large_directories /
|
list_large_directories /
|
||||||
|
@ -107,20 +115,15 @@ categorize_large_directories
|
||||||
list_large_directories /home
|
list_large_directories /home
|
||||||
categorize_large_directories
|
categorize_large_directories
|
||||||
|
|
||||||
# Optional: specific directory scanning (e.g., /home/virtfs if required)
|
# Scan and report specifically for /home/virtfs
|
||||||
echo "Scanning /home/virtfs for large directories..." >> $OUTPUT_FILE
|
echo "Scanning /home/virtfs for large directories..." >> /tmp/disk_report.txt
|
||||||
if [[ -d "/home/virtfs" ]]; then
|
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
|
||||||
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
|
categorize_large_directories
|
||||||
fi
|
|
||||||
|
|
||||||
# Output end time
|
echo "Storage savings estimation and large directory listing completed." >> /tmp/disk_report.txt
|
||||||
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
|
# Upload the report to hastebin
|
||||||
response=$(curl -s -X POST -T $OUTPUT_FILE https://haste.nixc.us/documents)
|
response=$(curl -s -X POST -T /tmp/disk_report.txt https://haste.nixc.us/documents)
|
||||||
if [[ $response == *"key"* ]]; then
|
if [[ $response == *"key"* ]]; then
|
||||||
key=$(echo $response | jq -r '.key')
|
key=$(echo $response | jq -r '.key')
|
||||||
echo "Report available at: https://haste.nixc.us/$key"
|
echo "Report available at: https://haste.nixc.us/$key"
|
||||||
|
|
Loading…
Reference in New Issue