Script: check_log.sh
Main function: Used to check whether the log file is uploaded to the FTP server, if not uploaded, send an email alarm
-------------------
#!/bin/bash#auto check log files and notice by email#by colin#revision on 2015-11-03# #Useage: ./check_log.sh /home/upload/ # 10 * * * * /root/check_log.sh################################ #RUN_LOG = '/var/log/check_log_run_stats.log ' [ ! -f ${run_log} ] && touch ${run_log}echogoodlog () { echo -e "\033[32m ' date +%f" "%T": "%N ' $*\033[0m" >> ${run _log}}echobadlog () { echo -e "\033[31m ' date +%f" "%T": "%N" $*\ 033[0m " >> ${RUN_LOG}}echoGoodLog " now, script: ' basename $0 ' run. " ######################## #EMAIL = ( [email protected] mail address. cn) #需要检查的目录DIR_LIST = ( /home/upload/aclog /home/upload/eglog /home/ Upload/nginxlog /home/upload/mysql /home/upload/gonet) #需要检测的目录应包含的关键词LOGFILE_TYPE = ( aclog eglog nginxlog gonet mysql) #需要检查的省份NEED_TO_DONE = ( sd &NBSP;HLJ) check_interval=3600check_interval_1_hour=1check_interval_24_hour=24#24-hour check list dir_list_24= ( mysql gonet) #在指定时间段内不检查的目录EXCLUDE_DIR = ( gonet) temp_dir_files= '/tmp/temp_check_log_dir_list.txt ' temp_email_files= '/tmp/temp_email_files.txt ' [ ! -f ${temp_email_files} ] && touch ${temp_email_files}now_time= ' Date +%F ' "%T ' sys_time= ' date -d " ${now_time} " +%s ' Hourtime= ' echo $ (date +%k) |sed ' s/ //g ' Todonegrep () { dir=$1 logfile_type_tmp= ' echo ${logfile_type[@]} |sed "s/ /|/g" ' casecondition= ' echo ${dir} |grep -eo "$ {logfile_type_tmp} ' |tail -1 ' case ${CASECONDITION} in aclog) grepcondition= ' [A-z]{5}[0-9]{4} (-[0-9]{2}) {3} ';; eglog) grepcondition= ' [A-z]{7}[0-9]{4} (-[0-9]{2}) {3} ';; nginxlog) grepcondition= ' [0-9]{4} (-[0-9]{2}) {3}. [a-z]+[0-9]*.nginxlog.tar.gz ';; gonet) grepcondition= ' gonet[0-9]{4} (-[0-9]{2}) {2}.tar.gz ';; mysql) grepcondition= ' [0-9]{4} (-[0-9]{2}) {2}.rht_[a-zA-Z]+. Tar.gz ';; esac}addtoemail () { dir=$1 [ ! -f ${temp_email_files} ] && touch ${temp_email_files} [ $4&Nbsp;-eq 1 ] && { if [ $2 -ge $3 ];then cat >>${temp_email_files} <<eofdir: $1 more than ${interval_hour} hour not upload a new log file, please check! eof echobadlog "$x time, The ${DIR} over $2 hour did not create a new log FILE&NBSP: " elif [ $5 -lt $6 ];then cat >>${temp_ email_files} <<eofdir: $1 loss some new log files, please check! Eof echobadlog "dir: ${dir} loss some new log files, please check! " fi } }checklog () { dir=$1 last_file_time= ' Ls --full-time -lt |head -2|sed -n 2p |awk ' {print $6,$7} ' |awk -f. ' {print $1} ' file_time= ' date -d "${last_file_time}" +%s ' interval= ' Expr ${sys_time} - ${file_time} ' interval_hour= ' Expr ${INTERVAL} / ${check_interval} ' temp_dir_list_24= ' echo ${ dir_list_24[@]} |sed "s/ /|/g" ' tmp_i= ' echo ${dir} |grep -e "${temp_dir_list_24}" |wc -l ' file_num= ' Ls -lh ${DIR} |grep "^-" |wc -l ' [ ${FILE_NUM} -gt 0 ] & & [ ${INTERVAL_HOUR} -le 48 ] && { cd ${DIR} && { if [ ${TMP_I} -eq 0 ];then toDoneGrep ${DIR} file_num2= ' find . -maxdepth 1 -type f -mmin -120| grep -Eo "${grepcondition}" |wc -l ' file_num3= ' Find . -maxdepth 1 -type f -mmin -60| grep -eo "${grepcondition}" |wc -L ' file_num_ 120= ' expr ${file_num2} - ${file_num3} ' [ ${file_num2} -eq 0 ] && file _num2=1 fileexist_num= ' expr ${file_num2} % 2 ' addtoemail ${dir} ${interval_hour} ${check_interval_1 _hour} ${fileexist_num} ${file_num3} ${file_num_120} else toDoneGrep ${DIR} &nbsP; file_num2= ' Find . -maxdepth 1 -type f -mtime -2| grep -eo "${grepcondition}" |wc -l ' file_num3= ' find . -maxdepth 1 -type f -mtime -1| grep -eo "${grepcondition}" |wc -l ' file_num_120= ' Expr ${file_num2} - ${file_ NUM3} ' [ $ {file_num2} -eq 0 ] && file_num2=1 fileexist_num= ' expr ${file_num2} % 2 ' addtoemail ${dir} ${interval_hour} ${check_interval_24_hour} ${fileexist_num} ${file_num3} ${file_num_120} fi } }}checkdir () { tempdir= ' echo ${need_to_done[@]} |sed "s# #/|/#g" ' tempdirnum= ' echo "${dir}" |grep -ve "[A-z]" |grep -ve "[0-9]{4} (. [ 0-9]{2}) {2} "|grep -ve "/[0-9]+ "|grep -e "/${tempdir}/" |wc -l" [ "${tempdirnum}" -eq 1 ] && { dir=$1 last_log_time= ' Tail -500 ${RUN_LOG } |grep ${dir} |tail -1|grep -eo "[0-9]{4} (-[0-9]{2}) {2} ([0-9]{2}:) {2}[0-9 ]{2} "' [ -z " ${last_log_time}" ] && last_log_time= ' date +%f" "%T" log_time= ' date -d ' ${last_log_time} ' +%s ' log_interval= ' Expr ${sys_time} - ${log_time} ' x= ' tail -500 ${run_log} |grep ${dir}|grep "Time" |tail -1|awk ' {print $3} ' [ -z $x ] & & x=0 cd ${DIR} if [ "${log_interval}" -ge "${check_interval}" ];then [ "$x" -eq 3 ] && x=1 | | i= ' expr $x + 1 ' Checklog ${dir} else [ "$x" -lt 3 ] && { [ "$x" &NBSP;-EQ&NBSP;3&NBSP;] && x=1 | | i= ' expr $x + 1 '; checkLog ${DIR} } fi }}excludecheckdir () { dir=$1 excludecondition= ' echo ' ${DIR} ' |awk -F/ ' {print $NF} ' tempnum= ' echo "${exclude_dir[@]}" |grep -wo "${ Excludecondition} " |wc -l ' if [ " ${tempnum} " -ne 1 ];then checkdir ${dir} #elif [ "${hourtime}" -ge 8 -a "${hourtime}" -le 22 ];then elif [ "${hourtime}" -ge 8 -a "${hourtime}" -le 8 ]; Then checkdir ${dir} fi}loopdir () { for i in $1/* do if [ -d $i ];then tempdir= ' echo ${need_to_done[@]} |sed "s# #/|/#g" tempdirnum= ' echo "$i |grep -vE " [ A-z] "|grep -ve " [0-9]{4} (. [ 0-9]{2}) {2} "|grep -e "/${tempdir}/" |wc -l" &Nbsp; [ ${tempdirnum} -eq 1 ] && echo "$i" >>${TEMP_DIR_FILES} loopDir $i fi done}if [ -z "$*" ];then for i in ${DIR_LIST[@]} do echo ' echo ' $i |sed ' s#/$# #g ' >>${TEMP_DIR_FILES} loopDir ' echo ' $i " |sed ' s#/$# #g ' doneelse for i in $* do [ -d $i ] & & { echo ' echo ' $i " |sed ' s#/$# #g" >>${TEMP_DIR_FILES} loopdir ' echo ' $i |sed ' s#/$# #g ' } | | echoBadLog "the $i is ' t a directory, please check the ARGUMENTS&NBSP: " donefi[ -f ${TEMP_EMAIL_FILES} ] && { while read line do excludeCheckDir $line done < ${temp_dir_files }}[ ' cat ${temp_email_files}|wc -l ' -eq 0 ] | | { for i in ${EMAIL[@]} do dos2unix -k ${TEMP_EMAIL_FILES} mail -s "The log is not exists" ${i} < ${TEMP_EMAIL_FILES} echoGoodLog "Send email to ${i}, please &NBSP;CHECK&NBSP: " done}[ -f ${TEMP_EMAIL_FILES} ] && rm -rf ${TEMP_EMAIL_FILES}[ -f ${TEMP_DIR_FILES} ] && { cp -a ${TEMP_DIR_FILES} /home/upload/uploadDirList.txt rm -rf ${temp_dir_files} }temp_wc= ' Cat ${run_log} |wc -l ' if [ "$ {TEMP_WC} " -gt 10000 ];then sed -i " 1,5000d " ${run_log} echoGoodLog "Clean up the ${run_log} ..." echogoodlog "script: ' basename $0 ' run done." else echogoodlog "script: ' basename&Nbsp;$0 ' run done. " exitfi
-------------------
Script: set_for_check_log.sh
Scripting Features: A running environment for deploying Check_log scripts
-------------------
#! /bin/sh#auto set the system environment for the check_log.sh#by colin on 2015-06-25echo "\033[32mnow, to set the system environment for the check_log.sh\033[0m "#install command mailx and dos2unixapt-get -y install heirloom-mailx dos2unix#set /etc/nail.rc, use command mailx to send mailsed -i "/sendcharsets/s/=.*/=gb2312/g" /etc/ nail.rccat >> /etc/nail.rc <<eofset from= User name @126.com smtp= smtp.126.comset smtp-auth-user= User name smtp-auth-password= password set smtp-auth=logineofscripts= '/ Root/check_log/check_log.sh ' If [ -f ${scripts} ];then chmod +x ${scripts}; echo "* * * * * /bin/sh ${scripts} >>/var/log/check_log_run_stats.log " >>/var/spool/cron/crontabs/rootelse echo "\033[31mplease check if there is the check_log.sh in the root directory.\033[0m " fiecho "\033[32mdone.\033[0m"
This article from "Mountain Road 18 Bend" blog, reproduced please contact the author!
Shell script: Check that the logs on the FTP log storage server are properly uploaded