#!/bin/bash
ARGV=`getopt -o l:,i:,t:,k:,u:,m:,U:,M:,k:,K: -l log_path:,initial_row:,tmp_log:,keywords:,user_list:,user_mail:,user_file_sms:,user_file_mail:,keywords_file:,dameon,interval:,help -n test.sh -- "$@"`
eval set --"$ARGV"
while true
do
case "$1" in
-l|--log_path)
log_path="$2"
shift
;;
-i|--initial_row)
initial_row="$2"
shift
;;
-t|--tmp_log)
tmp_log="$2"
shift
;;
-k|--keywords)
keywords="$2"
shift
;;
-u|--user_list)
user_list="$2"
shift
;;
-m|--user_mail)
user_mail="$2"
shift
;;
-U|--user_file_sms)
user_file="$2"
shift
;;
-M|--user_file_mail)
user_file_mail="$2"
shift
;;
-k|--keywords_file)
keywords_file="$2"
shift
;;
--dameon)
dameon='yes'
shift
;;
--interval)
interval="$2"
shift
;;
--help)
echo "
This tool can helps you monitor log-files by matching keywords,and contact the users which you want to contact.Loging a file what will matched.
-l|--log_path : The path of log-file;
-i|--initial_row : Starting with the initial line-NUM.
-t|--tmp_log : Path of which you want to log for matched keywords's line.
-k|--keywords : keywords you want to match ,separate by ','
-u|--user_list : contact list ,separate by ','
-m|--user_mail : Acount of email ,separate by ','
-U|--user_file_sms : Sms contact file ,separate by '\n'
-M|--user_file_maill : Email contact file ,separate by '\n'
-k|--keywords_file : keywords file you want to match ,separate by '\n'
--dameon : Intial dameon
--help : It helps you to use this tool!
"
exit 0
# shift
;;
--)
shift
break
;;
*)
echo "Internal error!" ;
echo "
This tool can helps you monitor log-files by matching keywords,and contact the users which you want to contact.Loging a file what will matched.
-l|--log_path : The path of log-file;
-i|--initial_row : Starting with the initial line-NUM.
-t|--tmp_log : Path of which you want to log for matched keywords's line.
-k|--keywords : keywords you want to match ,separate by ','
-u|--user_list : contact list ,separate by ','
-m|--user_mail : Acount of email ,separate by ','
-U|--user_file_sms : Sms contact file ,separate by '\n'
-M|--user_file_maill : Email contact file ,separate by '\n'
-k|--keywords_file : keywords file you want to match ,separate by '\n'
--dameon : Intial dameon
--help : It helps you to use this tool!
"
exit 0
;;
esac
shift
done
#Test the given options's values
function test_argments(){
[ -z $1 ]
}
test_argments $log_path && echo 'Error! Log_path is null.' && exit 1
test_argments $initial_row && initial_row=1
test_argments $tmp_log && tmp_log='/tmp/log_quary.tmp'
test_argments $keywords && keywords=''
test_argments $user_list && user_list=''
test_argments $interval && interval=5
current_row=$initial_row
function trigger_by_mail(){
local name=`echo $1 | tr ',' '\n'`
for i in $name
do
from='zhangshang@test.com'
to=$i
email_date=`date +%F_%T`
email_content=$2
email_subject=$project"__"$email_date
echo $email_content | formail -I "From: $from" -I "MIME-Version:1.0" -I "Content-type:text/html;charset=gb2312" -I "Subject: $email_subject" | /usr/sbin/sendmail -oi $to
done
}
function trigger_by_sms(){
local name=`echo $1 | tr ',' '\n'`
for i in $name
do
#This is weiwang's interface for sending sms
curl https://cf.51welink.com/submitdata/service.asmx/g_Submit?sname='(username)'&spwd='(password)'&scorpid=&sprdid=(serial_num)&sdst=$i&smsg='$1' 1>/dev/null 2>/tmp/sms_error.log
done
}
function Cut_log(){
local end_row=`wc -l $log_path | awk '{print $1}'`
[ $current_row -gt $end_row ] && return 0
eval sed -n '$current_row,${end_row}g' $log_path >$tmp_log 1>/dev/null 2>/tmp/Cut_log_error.log
[ $? -ne 0 ] && trigger_by_mail $user_mail "Get cut-log Error,Check permission or disk room $tmp_log" && trigger_by_sms $user_list "Get cut-log Error,Check permission or disk room $tmp_log"
current_row=`let end_row+=1`
}
function Quary_log(){
count=0
regex=`echo $keywords | tr -s ',' '|'`
test_argments $regex && echo 'Keywords is null! Do nothing' && return 1
egrep -C 2 "($regex)" $log_path >$tmp_log
[ `wc -l $tmp_log | awk '{print $1}'` -ne 0 ] && tmp_file=$tmp_log.`date +%F_%T` && mv $tmp_log $tmp_file && let count+=1
if [ $count -ne 0 ]
then
local title="$perject monitor error"
local message_email=`cat $tmp_file`
local message_sms="$preject Error!"
trigger_by_mail $user_mail $title $message_email
trigger_by_sms $user_list $message_sms
fi
}
function Change_variables(){
if [ -n "$keywords_file" ]
then
keywords='separate_words_begin'
while read lines
do
$keywords=$keywords','`echo -e "$lines" | tr -d '\n'`
done <$keywords_file
fi
if [ -n "$user_file_sms" ]
then
user_list='separate_words_begin'
while read lines
do
$user_list=$user_list,`echo -e "$lines" | tr -d '\n'`
done <$user_file_sms
fi
if [ -n "$user_file_mail" ]
then
user_mail='separate_words_begin'
while read lines
do
$user_mail=$user_mail,`echo -e "$lines" | tr -d '\n'`
done <$user_file_mail
fi
}
function demeon_init(){
let sleep_time=interval*60
while true
do
Cut_log
Quary_log
sleep $sleep_time
done
}
#Begin to start quary
Change_variables
if [ "$dameon" == "yes" ]
then
demeon_init
else
Cut_log
Quary_log
fi
文档更新时间: 2018-12-12 20:53 作者:张尚