inotify 监控几个脚本

Posted rongfengliang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 "
Current limits
-------------"
    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 "
Unknown parameter ‘$1‘
" > /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
‘ 2>/dev/null 
    
    | xargs grep -c ‘^inotify‘  
    | sort -n -t: -k2 -r 
}
?
printf "
%10s
" "INOTIFY"
printf "%10s
" "WATCHER"
printf "%10s  %5s     %s
" " COUNT " "PID" "CMD"
printf -- "----------------------------------------
"
?
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
" "$watcher_count" "$pid" "$cmdline"
done

参考资料

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

以上是关于inotify 监控几个脚本的主要内容,如果未能解决你的问题,请参考以下文章

rsync:inotify+rsync监控脚本

bash & inotify - 监控和移动文件

使用 Perl 和 Linux::Inotify2 模块监控 Mac 地址的日志文件

通过inotify实现反调试

inotify监控目录变化重启服务器tornado项目

inotify实时同步数据