Last active
August 13, 2021 12:05
-
-
Save dgulinobw/7cf904a716e846fb4a5c84360d8c9130 to your computer and use it in GitHub Desktop.
Show percentage used of 1) per-process ulimit open files, and 2) system-wide open files limit.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
printf "%-20s %10s %10s %14s\n" "Process" "Ulimit" "Used" "% Ulimit Used" | |
length=62 | |
printf -v line '%*s' "$length" | |
echo ${line// /-} | |
# gather proceses to list from all of /var/run/ | |
for f in $(find /var/run/ -type f -name "*.pid" | sort) | |
# gather processes to list from monit | |
#for f in $(grep pidfile /etc/monit.d/* | awk '{print $6}' | sort) | |
do | |
name=$(echo $f | awk -F'/' '{print $NF}' | awk -F"." '{print $1}') | |
n=$(cat $f) | |
if [[ $n && ${n-x} ]] | |
then | |
if [ -d "/proc/$n" ] | |
then | |
current=$(ls /proc/$n/fd/ |wc -l) | |
limit=$(cat /proc/$n/limits | grep "Max open files" | awk '{print $5}') | |
percent=$(echo "scale=4; ( $current / $limit ) * 100.0" | bc) | |
printf "%-20s %10d %10d %13.2f%%\n" $name $limit $current $percent | |
else | |
printf "%-35s %20s\n" $name "ERROR: No Process Found" | |
fi | |
else | |
printf "%-35s %20s\n" $name "ERROR: No PID in file" | |
fi | |
done | |
echo | |
fs_nr=$(sysctl fs.file-nr) | |
fs_used=$(echo $fs_nr | awk '{print $3}') | |
fs_limit=$(echo $fs_nr | awk '{print $5}') | |
fs_percent_used=$(echo "scale=4; ($fs_used / $fs_limit) * 100.0" | bc) | |
printf "%-10s %10s %16s\n" "FS Limit" "FS Used" "% Limit Used" | |
length=62 | |
printf -v line '%*s' "$length" | |
echo ${line// /-} | |
printf "%-10d %10d %15.2f%%\n" $fs_limit $fs_used $fs_percent_used | |
echo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment