inotify 监控几个脚本

inotify 监控简单shell

for foo in /proc/*/fd/*; do readlink -f $foo; done | grep inotify | sort | uniq -c | sort -nr

一个很不错的script

#!/bin/sh
# Get the procs sorted by the number of inotify watchers
# @author Carl-Erik Kopseng
# @latest https://github.com/fatso83/dotfiles/blob/master/utils/scripts/inotify-consumers
# Discussion leading up to answer: https://unix.stackexchange.com/questions/15509/whos-consuming-my-inotify-resources
usage(){
    cat << EOF
Usage: $0 [--help|--limits]
    -l, --limits    Will print the current related limits and how to change them
    -h, --help      Show this help
EOF
}
limits(){
    echo "\nCurrent limits\n-------------"
    sysctl fs.inotify.max_user_instances  fs.inotify.max_user_watches
    cat <<- EOF 
Changing settings permanently
-----------------------------
echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf
sudo sysctl -p # re-read config
EOF
}
if [ "$1" = "--limits" -o "$1" = "-l" ]; then
    limits
    exit 0
fi
if [ "$1" = "--help" -o "$1" = "-h" ]; then
    usage 
    exit 0
fi
if [ -n "$1" ]; then
    echo "\nUnknown parameter '$1'\n" > /dev/stderr
    usage
    exit 1
fi
generateRawData(){
    # From `man find`: 
    #    %h     Leading directories of file's name (all but the last element).  If the file name contains no slashes  (since  it
    #           is in the current directory) the %h specifier expands to `.'.
    #    %f     File's name with any leading directories removed (only the last element).
    #
    find /proc/*/fd \
    -lname anon_inode:inotify \
    -printf '%hinfo/%f\n' 2>/dev/null \
    \
    | xargs grep -c '^inotify'  \
    | sort -n -t: -k2 -r 
}
printf "\n%10s\n" "INOTIFY"
printf "%10s\n" "WATCHER"
printf "%10s  %5s     %s\n" " COUNT " "PID" "CMD"
printf -- "----------------------------------------\n"
IFS=''; # to avoid `read` from interpreting whitespace and keep whole lines
generateRawData | while read line; do
    watcher_count=$(echo $line | sed -e 's/.*://')
    pid=$(echo $line | sed -e 's/\/proc\/\([0-9]*\)\/.*/\1/')
    cmdline=$(ps --columns 120 -o command -h -p $pid) 
    printf "%8d  %7d  %s\n" "$watcher_count" "$pid" "$cmdline"
done

参考资料

https://github.com/fatso83/dotfiles/blob/master/utils/scripts/inotify-consumers

posted on   荣锋亮  阅读(483)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2017-05-25 Google、IBM和Lyft开源其大型微服务系统管理工具Istio

导航

< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
点击右上角即可分享
微信分享提示