Ostatnio aktywny 1757947372

Rewizja 55867c7e4789b66e2c3a7d0462894349bd86d0c3

List LSOF and sort Surowy
1sudo lsof +L1 | awk 'NR==1{print "COMMAND", "FD", "SIZE_MB", "NAME"} NR>1{print $1, $4, $7, $10}' | numfmt --to-unit=1M --field=3 --format="%.2f MB" --header | sort -k3 -nr | column -t
docker-deleted-log-report.sh Surowy
1#!/bin/bash
2
3TMPFILE=$(mktemp)
4
5# Get all deleted Docker logs that are still open
6sudo lsof +L1 | awk '
7NR == 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"
33done
34
35# Output
36#echo -e "Proc\tImage\tSize\tContainer ID\tImage\tCommand\tCreated\tNames"
37sort -nk1 "$TMPFILE"
38
39rm -f "$TMPFILE"
k8s-trim-logs.sh Surowy
1#!/usr/bin/env bash
2# Trim all Docker/K8S container logs to last 1000 lines
3
4KEEP=1000
5
6# Look for all docker json logs
7for f in /var/lib/docker/containers/*/*-json.log; do
8 if [ -f "$f" ]; then
9 echo "Trimming $f ..."
10 tmp=$(mktemp)
11 tail -n $KEEP "$f" > "$tmp" && cat "$tmp" > "$f"
12 rm -f "$tmp"
13 fi
14done
15
16# Also check kubelet-managed logs (optional)
17for f in /var/log/containers/*.log /var/log/pods/*/*/*.log; do
18 if [ -f "$f" ]; then
19 echo "Trimming $f ..."
20 tmp=$(mktemp)
21 tail -n $KEEP "$f" > "$tmp" && cat "$tmp" > "$f"
22 rm -f "$tmp"
23 fi
24done
25
26echo "Done."
27
logfile_with_pods.sh Surowy
1#!/bin/bash
2
3echo -e "SIZE\tPOD_NAME\tNAMESPACE\tCONTAINER_NAME\tLOG_PATH"
4
5# Step 1: Find all container logs and sort by size
6sudo 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"
23done | sort -h
24