inotifywait监控多个目录

inotifywait监控多个目录

一.需求:

1.监控特定的服务配置文件和目录变更情况。

2.监控自定义文件和目录变更情况。

3.可以手动杀掉进程。

4.把所有变更信息弄到日志里。

二.Inotify介绍

Inotify 是一个内核用于通知用户空间程序文件系统变化的机制,是基于inode级别的文件系统监控技术,是一种强大的、细粒度的、异步的机制,它满足各种各样的文件监控需要,不仅限于安全和性能,内核要求2.6.13以上。

2.1 Inotify监控事项
Events 含义
access 文件或目录被读取
modify 文件或目录内容被修改
attrib 文件或目录属性被改变
close 文件或目录封闭,无论读/写模式
open 文件或目录被打开
moved_to 文件或目录被移动至另外一个目录
moved_from 文件或目录被移动到另一个目录或从另一个目录移动至当前目录
create 文件或目录被创建在当前目录
delete 文件或目录被删除
unmount 文件系统被卸载

2.2 Inotify参数说明
参数 说明
-m 持续监听
-r 使用递归形式监视目录
-q 减少冗余信息,只打印出需要的信息
-e 指定要监视的事件,多个时间使用逗号隔开
--timefmt 时间格式
--format 监听到的文件变化的信息

ymd分别表示年月日,H表示小时,M表示分钟
--format 说明:
参数 说明
%w 表示发生事件的目录
%f 表示发生事件的文件
%e 表示发生的事件
%Xe 事件以“X”分隔
%T 使用由–timefmt定义的时间格式

三.脚本如下:

#!/bin/bash
#监控文件和文件夹变更
A=y
B=`grep INOTIFY_USER /boot/config-$(uname -r)`
C=`echo ${B:0-1}`
system=`getconf LONG_BIT`
#定义安装inotify
function Install_inotify()
{
if [ "$A" = "$C" ]
    then
    echo -e "\033[31mstart install INOTIFY-3.14...... \033[0m"
    cd /opt
    tar zxvf inotify-tools-3.14.tar.gz &> /dev/null
    cd /opt/inotify-tools-3.14
    ./configure && make && make install &> /dev/null
    echo -e "\033[31mINOTIFY-3.14 install successful! \033[0m"
    if [ "$system" = 64 ]
    then
        echo -e "\033[31minstall 64-bit system patch......\033[0m"
        ln -s /usr/local/lib/libinotifytools.so.0 /usr/lib64/libinotifytools.so.0
    else
        echo -e "\033[31minstall 32-bit system patch......\033[0m"
        ln -s /usr/local/lib/libinotifytools.so.0 /usr/lib/libinotifytools.so.0
    fi
    exit
else
     echo -e "\033[31mSystem not supported INOTIFY...... \033[0m"
     exit
fi
}
#定义tomcat监控目录
function Inotify_tomcat ()
{ 
#查找tomcat配置文件
 echo -e "\033[31m正在搜索tomcat配置文件目录,正在打印.....\033[0m"
 echo "`find / -name "server.xml"`"
 read -p "Please insert tomcat configuration file": File
 echo -e "\033[31mtomcat configuration file is $File \033[0m"
 read -p "Please insert tomcat Application web directory": Directory 
 echo -e "\033[31mtomcat Application web directory is $Directory \033[0m"
 touch /opt/Inotify_tomcat.log
 echo -n "" > /opt/Inotify_tomcat.log
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w %f %e' -e modify,delete,create,attrib,,moved_from,moved_to,unmount $File $Directory >> /opt/Inotify_tomcat.log &
 echo -e "\033[31mlook /opt/Inotify_tomcat.log  information \033[0m"
 exit
 }

#自定义目录和文件监控
function Inotify_custom ()
{
echo -e "\033[31m编辑自定义文件和目录监控配置文件/opt/mon.txt: \033[0m"
touch /opt/Inotify_custom.log
echo -n "" > /opt/Inotify_custom.log
for line in $(cat /opt/mon.txt)
do
  echo $line
  /usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w %f %e' -e modify,delete,create,attrib,,moved_from,moved_to,unmount $line >> /opt/Inotify_custom.log &
done
exit
}

#定义关闭监控进程
function Inotify_kill ()
{
echo -e "\033[31mMonitoring is turned off \033[0m"
pid=`ps -ef | grep inotifywait | grep -v grep | awk '{print $2}'`
if [ -n "$pid" ]
then
    echo -e "\033[31mkill inotifywait pid : \033[0m"$pid
    kill -9 $pid
    echo -e "\033[31minotifywait turned off successful \033[0m"
    exit
else
    echo -e "\033[31mMonitoring does not exist \033[0m"
    exit
fi
}

#PS3一般为菜单提示信息# 
 PS3="Please Select Menu": 
#select为菜单选择命令,格式为select $var in ..command.. do .... done 
 select i in "Install_inotify" "Inotify_tomcat" "Inotify_custom" "Inotify_kill"
 do
#case 方式,一般用于多种条件下的判断 
case $i in
  Install_inotify ) 
  Install_inotify 
;;
  Inotify_tomcat ) 
  Inotify_tomcat 
;;
  Inotify_custom ) 
  Inotify_custom 
;;
  Inotify_kill ) 
  Inotify_kill 
;;
  *) 
  echo 
  echo "Please Insert $0: Install_inotify(1)|Inotify_tomcat(2) |Inotify_custom(3) |Inotify_kill(4)"
  break
;; 
esac 
done

四.使用说明:

一、功能介绍
1.文件变更删除,创建,移动,修改,卸载,权限改动实时监控
2.文件夹变更删除,创建,移动,修改,卸载,权限改动实时监控

二、使用选项说明
1.安装服务
2.tomcat监控 #配置文件和web应用目录需要提供
如:

找到tomcat配置文件并输入进去
Please insert tomcat configurationfile:
/opt/apache-tomcat-7.0.67/conf/server.xml
找到tomcat-web应用目录并输入进去

Please insert tomcat Application web directory:
/opt/apache-tomcat-7.0.67/webapps/vaiops
相应的监控日志在: /opt/Inotify_tomcat.log
3.自定义监控 #需要手动编辑/opt/mon.txt文件,把需要监控的目录和文件填入。
如:
vim /opt/mon.txt 
/opt/mon.txt
/etc
/opt
4.杀死监控进程

如:

Monitoring is turned off 
kill inotifywait pid : 4538 4539 4540
inotifywait turned off successful

三、脚本运行参数说明
例子:
运行sh intify.sh
1) Install_inotify
2) Inotify_tomcat
3) Inotify_custom
4) Inotify_kill

###为了带孩子,最近都没时间发文章了,感觉天天觉都不够睡,今天有点空闲发一篇脚本记录下。

猜你喜欢

转载自blog.51cto.com/chenhao6/2314043