1. #!/bin/bash
  2. 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 -- "$@"`
  3. eval set --"$ARGV"
  4. while true
  5. do
  6. case "$1" in
  7. -l|--log_path)
  8. log_path="$2"
  9. shift
  10. ;;
  11. -i|--initial_row)
  12. initial_row="$2"
  13. shift
  14. ;;
  15. -t|--tmp_log)
  16. tmp_log="$2"
  17. shift
  18. ;;
  19. -k|--keywords)
  20. keywords="$2"
  21. shift
  22. ;;
  23. -u|--user_list)
  24. user_list="$2"
  25. shift
  26. ;;
  27. -m|--user_mail)
  28. user_mail="$2"
  29. shift
  30. ;;
  31. -U|--user_file_sms)
  32. user_file="$2"
  33. shift
  34. ;;
  35. -M|--user_file_mail)
  36. user_file_mail="$2"
  37. shift
  38. ;;
  39. -k|--keywords_file)
  40. keywords_file="$2"
  41. shift
  42. ;;
  43. --dameon)
  44. dameon='yes'
  45. shift
  46. ;;
  47. --interval)
  48. interval="$2"
  49. shift
  50. ;;
  51. --help)
  52. echo "
  53. 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.
  54. -l|--log_path : The path of log-file;
  55. -i|--initial_row : Starting with the initial line-NUM.
  56. -t|--tmp_log : Path of which you want to log for matched keywords's line.
  57. -k|--keywords : keywords you want to match ,separate by ','
  58. -u|--user_list : contact list ,separate by ','
  59. -m|--user_mail : Acount of email ,separate by ','
  60. -U|--user_file_sms : Sms contact file ,separate by '\n'
  61. -M|--user_file_maill : Email contact file ,separate by '\n'
  62. -k|--keywords_file : keywords file you want to match ,separate by '\n'
  63. --dameon : Intial dameon
  64. --help : It helps you to use this tool!
  65. "
  66. exit 0
  67. # shift
  68. ;;
  69. --)
  70. shift
  71. break
  72. ;;
  73. *)
  74. echo "Internal error!" ;
  75. echo "
  76. 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.
  77. -l|--log_path : The path of log-file;
  78. -i|--initial_row : Starting with the initial line-NUM.
  79. -t|--tmp_log : Path of which you want to log for matched keywords's line.
  80. -k|--keywords : keywords you want to match ,separate by ','
  81. -u|--user_list : contact list ,separate by ','
  82. -m|--user_mail : Acount of email ,separate by ','
  83. -U|--user_file_sms : Sms contact file ,separate by '\n'
  84. -M|--user_file_maill : Email contact file ,separate by '\n'
  85. -k|--keywords_file : keywords file you want to match ,separate by '\n'
  86. --dameon : Intial dameon
  87. --help : It helps you to use this tool!
  88. "
  89. exit 0
  90. ;;
  91. esac
  92. shift
  93. done
  94. #Test the given options's values
  95. function test_argments(){
  96. [ -z $1 ]
  97. }
  98. test_argments $log_path && echo 'Error! Log_path is null.' && exit 1
  99. test_argments $initial_row && initial_row=1
  100. test_argments $tmp_log && tmp_log='/tmp/log_quary.tmp'
  101. test_argments $keywords && keywords=''
  102. test_argments $user_list && user_list=''
  103. test_argments $interval && interval=5
  104. current_row=$initial_row
  105. function trigger_by_mail(){
  106. local name=`echo $1 | tr ',' '\n'`
  107. for i in $name
  108. do
  109. from='zhangshang@test.com'
  110. to=$i
  111. email_date=`date +%F_%T`
  112. email_content=$2
  113. email_subject=$project"__"$email_date
  114. 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
  115. done
  116. }
  117. function trigger_by_sms(){
  118. local name=`echo $1 | tr ',' '\n'`
  119. for i in $name
  120. do
  121. #This is weiwang's interface for sending sms
  122. 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
  123. done
  124. }
  125. function Cut_log(){
  126. local end_row=`wc -l $log_path | awk '{print $1}'`
  127. [ $current_row -gt $end_row ] && return 0
  128. eval sed -n '$current_row,${end_row}g' $log_path >$tmp_log 1>/dev/null 2>/tmp/Cut_log_error.log
  129. [ $? -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"
  130. current_row=`let end_row+=1`
  131. }
  132. function Quary_log(){
  133. count=0
  134. regex=`echo $keywords | tr -s ',' '|'`
  135. test_argments $regex && echo 'Keywords is null! Do nothing' && return 1
  136. egrep -C 2 "($regex)" $log_path >$tmp_log
  137. [ `wc -l $tmp_log | awk '{print $1}'` -ne 0 ] && tmp_file=$tmp_log.`date +%F_%T` && mv $tmp_log $tmp_file && let count+=1
  138. if [ $count -ne 0 ]
  139. then
  140. local title="$perject monitor error"
  141. local message_email=`cat $tmp_file`
  142. local message_sms="$preject Error!"
  143. trigger_by_mail $user_mail $title $message_email
  144. trigger_by_sms $user_list $message_sms
  145. fi
  146. }
  147. function Change_variables(){
  148. if [ -n "$keywords_file" ]
  149. then
  150. keywords='separate_words_begin'
  151. while read lines
  152. do
  153. $keywords=$keywords','`echo -e "$lines" | tr -d '\n'`
  154. done <$keywords_file
  155. fi
  156. if [ -n "$user_file_sms" ]
  157. then
  158. user_list='separate_words_begin'
  159. while read lines
  160. do
  161. $user_list=$user_list,`echo -e "$lines" | tr -d '\n'`
  162. done <$user_file_sms
  163. fi
  164. if [ -n "$user_file_mail" ]
  165. then
  166. user_mail='separate_words_begin'
  167. while read lines
  168. do
  169. $user_mail=$user_mail,`echo -e "$lines" | tr -d '\n'`
  170. done <$user_file_mail
  171. fi
  172. }
  173. function demeon_init(){
  174. let sleep_time=interval*60
  175. while true
  176. do
  177. Cut_log
  178. Quary_log
  179. sleep $sleep_time
  180. done
  181. }
  182. #Begin to start quary
  183. Change_variables
  184. if [ "$dameon" == "yes" ]
  185. then
  186. demeon_init
  187. else
  188. Cut_log
  189. Quary_log
  190. fi
文档更新时间: 2018-12-12 20:53   作者:张尚