-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshow_filefd_info.sh
executable file
·72 lines (56 loc) · 1.98 KB
/
show_filefd_info.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/bin/bash
#
# Show file description open for process and limit
SCRIPT_NAME=`basename $0`;
SORT="fd"; # {pid|fd|name} as first parameter, [default: fd]
[ "$1" != "" ] && { SORT="$1"; }
[ ! -x `which mktemp` ] && { echo "ERROR: mktemp is not available!"; exit; }
MKTEMP=`which mktemp`;
TMP=`${MKTEMP} -d`;
[ ! -d "${TMP}" ] && { echo "ERROR: unable to create temp dir!"; exit; }
>${TMP}/${SCRIPT_NAME}.pid;
>${TMP}/${SCRIPT_NAME}.fd;
>${TMP}/${SCRIPT_NAME}.name;
OVERALL=0;
echo "${OVERALL}" > ${TMP}/${SCRIPT_NAME}.overal;
for DIR in `find /proc/ -maxdepth 1 -type d -regex "^/proc/[0-9]+"`;
do
PID=`echo $DIR | cut -d / -f 3`
if [ -d "/proc/$PID/fd" ]; then
FD_COUNT=`ls -1 /proc/$PID/fd | wc -l`
FD_LIMIT=`prlimit --pid $PID -n -o SOFT --noheadings`
PROGNAME=`ps -p $PID -o comm --no-headers`
let "LIMIT_PERC=$FD_COUNT*100/$FD_LIMIT"
if (( $LIMIT_PERC > 1 ));
then
echo -n ".";
echo -e "${PROGNAME}\t${LIMIT_PERC}\t${PID}" >> ${TMP}/${SCRIPT_NAME}.name;
echo -e "${PID}\t${PROGNAME}\t${LIMIT_PERC}" >> ${TMP}/${SCRIPT_NAME}.pid;
echo -e "${LIMIT_PERC}\t${PROGNAME}\t${PID}" >> ${TMP}/${SCRIPT_NAME}.fd;
fi
let OVERALL=$OVERALL+$FD_COUNT
fi
done
### render
echo "${OVERALL}" > ${TMP}/${SCRIPT_NAME}.overal;
echo;
echo "Overall fd opened: ${OVERALL}";
echo "========================================================================";
case "${SORT}" in
name )
echo -e "name\tfd%\tpid";
echo "========================================================================";
cat ${TMP}/${SCRIPT_NAME}.name|sort -r;
;;
fd )
echo -e "fd%\tname\tpid";
echo "========================================================================";
cat ${TMP}/${SCRIPT_NAME}.fd|sort -rh;
;;
pid | * )
echo -e "pid\tname\tfd%";
echo "========================================================================";
cat ${TMP}/${SCRIPT_NAME}.pid|sort -rh;
;;
esac
rm -fR "${TMP}/";