-
Notifications
You must be signed in to change notification settings - Fork 4
/
swap-usage-report.sh
executable file
·41 lines (36 loc) · 1.22 KB
/
swap-usage-report.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/bin/bash
COLUMNS=$(tput cols)
(
cd /proc
for pid in [0-9]*; do
swapused=$(
awk 'BEGIN { swap = 0 }
/^VmSwap/ { swap = swap + $2 }
END { print swap }' /proc/$pid/status 2>/dev/null || echo 0)
if [ $swapused -gt 0 ]; then
swapused_meg=$((swapused/1024))
swapused_meg_total=$((swapused_meg_total+swapused_meg))
#/bin/echo -e "${swapused_meg} M\t$(cat ${PROCESS}/cmdline)"
#echo "%.${COLUMNS}s\n" "${swapused_meg} M ${pid} $(cat /proc/${pid}/cmdline)"
printf "%.${COLUMNS}s\n" "${swapused_meg} M ${pid} $(tr '\0' ' ' < /proc/${pid}/cmdline)"
fi
done
/bin/echo -e "${swapused_meg_total} M All procs combined."
) | sort -n
exit 0
# Alternative method:
# Note: This didn't seem to yield correct figures, only ~1289M while free
# report around 1900+ used.
(for PROCESS in /proc/*/; do
swapused=$(awk '
BEGIN { total = 0 }
/^Swap:[[:blank:]]*[1-9]/ { total = total + $2 }
END { print total }' ${PROCESS}/smaps 2>/dev/null || echo 0)
if [ $swapused -gt 0 ]; then
swapused_meg=$((swapused/1024))
swapused_meg_total=$((swapused_meg_total+swapused_meg))
/bin/echo -e "${swapused_meg} M\t$(cat ${PROCESS}/cmdline)"
fi
done
/bin/echo -e "${swapused_meg_total} M\tAll procs combined."
) | sort -n