#!/bin/bash
#Content: monitor file changement
#Version: 1.0
#AUTHORS: ***
#Data : 2010-02-24
declare -f getparam
declare -f getstatus
declare -f modification
declare -f loop
declare -i timeval=5
filename=""
lasttime=""
visittime=""
function getparam()
{
if [ $# -le 1 ]; then
echo "usage: $0 [-f filename] [-t timeval]"
exit 1
fi
while getopts "f:t:" opt
do
case $opt in
f)
filename=$OPTARG
;;
t)
timeval=$OPTARG
;;
\?)
echo "usage: $0 [-f filename] [-t timeval]"
exit 1
;;
esac
done
shift $(($OPTIND-1))
}
function getstatus()
{
if [ "$filename" = "" ] || [ ! -e $filename ]; then
echo "invalid flie:$filename"
exit 2
fi
echo "Start monitor $filename changement"
lasttime=`stat $filename | grep 'Modify' | cut -d" " -f3 | cut -d"." -f1`
cp -a $filename .$filename.bak
}
function modification()
{
username=`stat $filename | grep Uid | cut -d"/" -f3 | cut -d")" -f1 |cut -d" " -f2`
ip=`last | grep $username | sed -n '$p'| awk '{print $3}'`
echo "Modify by $username $ip in $visittime"
diff -Naur .$filename.bak $filename > ${visittime}_${username}_${ip}.patch
cp -a $filename .$filename.bak
lasttime=$visittime
}
function loop()
{
while [ 1 ]
do
visittime=`stat $filename | grep Modify | cut -d" " -f3 | cut -d"." -f1`
if [ $lasttime != $visittime ]; then
modification
fi
sleep $timeval
done
}
getparam $@
getstatus
loop
exit 0
阅读(654) | 评论(0) | 转发(0) |