inotify监控Nginx目录且将Log文件放入Flume监控文件中

本文介绍了一种使用inotify监控Nginx日志文件,并将其传输至Flume进行进一步处理的方法。该方案包括了自定义脚本实现文件完整性检查、解压缩及移动等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

1、需求

2、框架步鄹

3、脚本和Nginx日志展示

4、运行结果

       1)日志展示和移动数据到监控目录

       2) 启动监控脚本、查看运行前后、查看监控日志、查看结果路径数据



1、需求

通过inotify监控Nginx目录,将生产Log文件放入Flume监控文件中

2、框架步鄹

这里写图片描述

3、脚本和Nginx日志展示

这里写图片描述

备注:*.tar.gz.MD5结尾的文件,是验证数据完整性。
这里写图片描述

文件为:monitorfile monitorTemp monitorResult NginxLOG
monitorfile :为inotify被监控的路径
monitorTemp:monitorfile中有文件改变,将改变的数据放入临时目录
monitorResult :通过check_md5_decompression.sh方法将临时目录的文件解压放入结果目录

脚本为:
check_md5_decompression.sh
start.sh
monitor.sh

monitor.sh

#!/bin/bash    
  
src=/home/bigdataservice/yuhui/monitorfile # directory to monitor  
/usr/local/bin/inotifywait -rmq  -e close_write $src |  while read k_dir mon_event k_file
do  
echo "new file"
pd=".gz"
end=${k_file:0-3}
#echo $end
if [ "$end" == "$pd" ]; then  
 echo $k_dir$k_file
 sh check_md5_decompression.sh $k_dir$k_file /home/bigdataservice/yuhui/monitorTemp/  /home/bigdataservice/yuhui/push.log
 mv /home/bigdataservice/yuhui/monitorTemp/* /home/bigdataservice/yuhui/monitorResult
 echo "file unzip done"
fi  
done 

start.sh

nohup /bin/bash monitor.sh &

check_md5_decompression.sh

#!/bin/bash

## =====================================================================
# Script Name : check_md5_decompression.sh
# Description : check md5/unzip file/modify file
# Author      : ddl
# Date        : 2016-09-23
#             ./check_md5_decompression.sh
#
# Revision    : 1.0
# Organization: donews
## =====================================================================

#The default variable
CurrentDate=`date "+%Y%m%d_%H%M%S"`
ScriptOwnDir=$(cd "$(/usr/bin/dirname "$0")"; pwd)
check_file=$1
unzip_dir=$2
output_log=$3
check_file_dir=$(cd "$(/usr/bin/dirname "$check_file")";pwd)

# =========colour========
blue="\033[0;34m"
green="\033[0;32m"
red="\033[0;31m"
yellow="\033[1;33m"
End="\033[0m"
#===================

usage () {
  echo -n -e "$green Usage: check_md5_decompression.sh check_file unzip_dir output_log $End \n"
  echo -n -e "$red exp:$End \n"
  echo -n -e "	check_md5_decompression.sh /data/temp/m.donews.com_access.log_20160922_000000.tar.gz /tmp/001 /tmp/a.log \n"
  exit 1
}

if [ "$#" -ne 3 ]; then
  usage
fi

# Set record log function
RecordLog(){
    local ShellName LogFile

    ShellName=${0##*/}
    LogFile=${ScriptOwnDir}/${ShellName}.log
    echo "[ `date "+%F %T"` ] | SysUser : $USER | ${ShellName} | ${*:-"Null Log"}" >> ${LogFile}

    output_writelog=$output_log
    if [ -f "$output_writelog" ];then
        echo "[ `date "+%F %T"` ] | SysUser : $USER | ${ShellName} | ${*:-"Null Log"}" >> ${output_writelog}
    else
        touch $output_writelog 
        echo "[ `date "+%F %T"` ] | SysUser : $USER | ${ShellName} | ${*:-"Null Log"}" >> ${output_writelog}
    fi

}

check_md5(){
    #echo -e "$green begin check MD5.... $End"
    RecordLog "begin check MD5...."
    check_file_md5=$check_file.MD5 
    if [ -f "$check_file_md5" ];then
	  #echo -e "$check_file_md5 is exits.....................................[$green OK $End]"
	  RecordLog "$check_file_md5 is exits OK"
          cd $check_file_dir
          /usr/bin/md5sum -c $check_file_md5 > /dev/null 2>&1
          if [ $? -eq 0 ];then
              #echo -e "check MD5 file is ....................................................................................[$green OK $End]"
              RecordLog "check $check_file MD5 file is success!"
          else
              #echo -e "check MD5 file is ....................................................................................[$red FAILD $End]"
              RecordLog "check $check_file MD5 file is faild!"
              exit 0
          fi 
    else
	  #echo -e "$check_file_md5 is not exits........................................[$red FAILD $End]"
	  RecordLog "$check_file_md5 is not exits is FAILD"
          min=0
          max=600
          while [ $min -le $max ]
          do
              #echo $min
              sleep 5
              min=`expr $min + 5`
              if [ -f "$check_file_md5" ];then 
                  cd $check_file_dir
                  /usr/bin/md5sum -c $check_file_md5 > /dev/null 2>&1
                  if [ $? -eq 0 ];then
                      #echo -e "check MD5 file is .................................................................................................[$green OK $End]"
                      RecordLog "check $check_file MD5 file is success!"
                  else
                      #echo -e "check MD5 file is .................................................................................................[$red FAILD $End]"
                      RecordLog "check $check_file MD5 file is faild!"
                      exit 0
                  fi
                  break
	      else
                  continue 
              fi
          done  
    fi


}

decompressing_files(){
    #echo -e "$green begin decompressing_files $check_file.... $End"
    RecordLog "begin decompressing_files $check_file...."
    cd $check_file_dir
    if [ -d $unzip_dir ];then
        #echo -e "unzip_dir $unzip_dir is exit ...............................................................................[$green OK $End]"
        RecordLog "unzip_dir $unzip_dir is exit!"
    else
        #echo -e "$unzip_dir is not exit .....................................................................................[$red FAILD $End]"
        RecordLog "$unzip_dir is not exit!"
        /bin/mkdir -p $unzip_dir
        #echo -e "create dir $unzip_dir!  .....................................................................................[$green OK $End]"
        RecordLog "create dir $unzip_dir!" 
    fi
    if [ -f "$check_file" ];then
        /bin/tar -zxvf $check_file -C $unzip_dir 2>& 1 > /dev/null
        if [ $? -eq 0 ];then
            #echo -e "decompressing_files $check_file is .............................................[$green OK $End]"
            RecordLog "decompressing_files $check_file is success!"
        else
            #echo -e "decompressing_files $check_file is .............................................[$red FAILD $End]"
            RecordLog "decompressing_files $check_file is faild!"
            exit 0
        fi       
    else
        #echo -e "$check_file is not exits"
        RecordLog "$check_file is not exits"
        exit 0
    fi


}



check_md5
decompressing_files

4、运行结果

1)日志展示和移动数据到监控目录

这里写图片描述

2) 启动监控脚本、查看运行前后、查看监控日志、查看结果路径数据

这里写图片描述

北京小辉微信公众号

在这里插入图片描述

大数据资料分享请关注
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

辉哥大数据

你的鼓舞将是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值