docker-deleted-log-report.sh
· 1.3 KiB · Bash
原始文件
#!/bin/bash
TMPFILE=$(mktemp)
# Get all deleted Docker logs that are still open
sudo lsof +L1 | awk '
NR == 1 { next }
$NF ~ /\(deleted\)$/ && $0 ~ /\/var\/lib\/docker\/containers\/.*-json\.log/ { print }
' | while read -r line; do
PROCESS=$(echo "$line" | awk '{print $1}')
SIZE=$(echo "$line" | awk '{print $(NF-4)}')
FILE=$(echo "$line" | awk '{for (i=9; i<=NF; i++) printf $i " "; print ""}' | sed 's/ (deleted)//')
# Skip if SIZE is not numeric
if ! [[ "$SIZE" =~ ^[0-9]+$ ]]; then
continue
fi
SIZE_MB=$(awk -v s="$SIZE" 'BEGIN {printf "%.2f", s/1048576}')
CONTAINER_ID=$(basename "$(dirname "$FILE")")
# Get container metadata
INFO=$(docker ps -a --no-trunc --filter "id=$CONTAINER_ID" --format "{{.ID}}|{{.Image}}|{{.Command}}|{{.RunningFor}}|{{.Names}}")
if [[ -z "$INFO" ]]; then
ID_SHORT="N/A"; IMAGE="N/A"; CMD="N/A"; CREATED="N/A"; NAMES="N/A"
else
IFS='|' read -r ID IMAGE_RAW CMD CREATED NAMES <<< "$INFO"
ID_SHORT=$(echo "$ID" | cut -c1-12)
IMAGE=$(echo "$IMAGE_RAW" | sed 's|.*/||' | cut -d':' -f1)
fi
echo -e "$SIZE_MB MB\t$PROCESS\t$ID_SHORT\t$CREATED\t$NAMES" >> "$TMPFILE"
done
# Output
#echo -e "Proc\tImage\tSize\tContainer ID\tImage\tCommand\tCreated\tNames"
sort -nk1 "$TMPFILE"
rm -f "$TMPFILE"
| 1 | #!/bin/bash |
| 2 | |
| 3 | TMPFILE=$(mktemp) |
| 4 | |
| 5 | # Get all deleted Docker logs that are still open |
| 6 | sudo lsof +L1 | awk ' |
| 7 | NR == 1 { next } |
| 8 | $NF ~ /\(deleted\)$/ && $0 ~ /\/var\/lib\/docker\/containers\/.*-json\.log/ { print } |
| 9 | ' | while read -r line; do |
| 10 | PROCESS=$(echo "$line" | awk '{print $1}') |
| 11 | SIZE=$(echo "$line" | awk '{print $(NF-4)}') |
| 12 | FILE=$(echo "$line" | awk '{for (i=9; i<=NF; i++) printf $i " "; print ""}' | sed 's/ (deleted)//') |
| 13 | |
| 14 | # Skip if SIZE is not numeric |
| 15 | if ! [[ "$SIZE" =~ ^[0-9]+$ ]]; then |
| 16 | continue |
| 17 | fi |
| 18 | |
| 19 | SIZE_MB=$(awk -v s="$SIZE" 'BEGIN {printf "%.2f", s/1048576}') |
| 20 | CONTAINER_ID=$(basename "$(dirname "$FILE")") |
| 21 | |
| 22 | # Get container metadata |
| 23 | INFO=$(docker ps -a --no-trunc --filter "id=$CONTAINER_ID" --format "{{.ID}}|{{.Image}}|{{.Command}}|{{.RunningFor}}|{{.Names}}") |
| 24 | if [[ -z "$INFO" ]]; then |
| 25 | ID_SHORT="N/A"; IMAGE="N/A"; CMD="N/A"; CREATED="N/A"; NAMES="N/A" |
| 26 | else |
| 27 | IFS='|' read -r ID IMAGE_RAW CMD CREATED NAMES <<< "$INFO" |
| 28 | ID_SHORT=$(echo "$ID" | cut -c1-12) |
| 29 | IMAGE=$(echo "$IMAGE_RAW" | sed 's|.*/||' | cut -d':' -f1) |
| 30 | fi |
| 31 | |
| 32 | echo -e "$SIZE_MB MB\t$PROCESS\t$ID_SHORT\t$CREATED\t$NAMES" >> "$TMPFILE" |
| 33 | done |
| 34 | |
| 35 | # Output |
| 36 | #echo -e "Proc\tImage\tSize\tContainer ID\tImage\tCommand\tCreated\tNames" |
| 37 | sort -nk1 "$TMPFILE" |
| 38 | |
| 39 | rm -f "$TMPFILE" |
logfile_with_pods.sh
· 1.0 KiB · Bash
原始文件
#!/bin/bash
echo -e "SIZE\tPOD_NAME\tNAMESPACE\tCONTAINER_NAME\tLOG_PATH"
# Step 1: Find all container logs and sort by size
sudo find /var/lib/docker/containers/ -type f -name "*.log" | while read -r logfile; do
# echo -e "$logfile"
# Get file size
size=$(sudo du -h "$logfile" | awk '{print $1}')
#echo -e "$size\t$logfile"
# Get container ID from filename or path
container_id=$(basename "$logfile" | cut -d'-' -f1)
#echo -e "$size\t$container_id"
# Use docker inspect to get Kubernetes labels
#pod_name=$(docker inspect "$container_id" 2>/dev/null | grep '"io.kubernetes.pod.name"' | cut -d':' -f2 | tr -d ' ",')
#namespace=$(docker inspect "$container_id" 2>/dev/null | grep '"io.kubernetes.pod.namespace"' | cut -d':' -f2 | tr -d ' ",')
container_name=$(docker inspect "$container_id" 2>/dev/null | grep '"io.kubernetes.container.name"' | cut -d':' -f2 | tr -d ' ",')
# Print results
#echo -e "$size\t$pod_name\t$namespace\t$container_name\t$logfile"
echo -e "$size\t$container_name"
done | sort -h
| 1 | #!/bin/bash |
| 2 | |
| 3 | echo -e "SIZE\tPOD_NAME\tNAMESPACE\tCONTAINER_NAME\tLOG_PATH" |
| 4 | |
| 5 | # Step 1: Find all container logs and sort by size |
| 6 | sudo find /var/lib/docker/containers/ -type f -name "*.log" | while read -r logfile; do |
| 7 | # echo -e "$logfile" |
| 8 | # Get file size |
| 9 | size=$(sudo du -h "$logfile" | awk '{print $1}') |
| 10 | #echo -e "$size\t$logfile" |
| 11 | # Get container ID from filename or path |
| 12 | container_id=$(basename "$logfile" | cut -d'-' -f1) |
| 13 | #echo -e "$size\t$container_id" |
| 14 | |
| 15 | # Use docker inspect to get Kubernetes labels |
| 16 | #pod_name=$(docker inspect "$container_id" 2>/dev/null | grep '"io.kubernetes.pod.name"' | cut -d':' -f2 | tr -d ' ",') |
| 17 | #namespace=$(docker inspect "$container_id" 2>/dev/null | grep '"io.kubernetes.pod.namespace"' | cut -d':' -f2 | tr -d ' ",') |
| 18 | container_name=$(docker inspect "$container_id" 2>/dev/null | grep '"io.kubernetes.container.name"' | cut -d':' -f2 | tr -d ' ",') |
| 19 | |
| 20 | # Print results |
| 21 | #echo -e "$size\t$pod_name\t$namespace\t$container_name\t$logfile" |
| 22 | echo -e "$size\t$container_name" |
| 23 | done | sort -h |
| 24 |