A shell tool that supports regular expression log keyword statistics

Source: Internet
Author: User
A shell tool that supports regular expression log keyword statistics Usage: pafa_mon/statistics_pafa.sh-i | -- instance & lt; instancename & gt; [-fn | -- filename & lt; filename & gt;] [-ip | -- instancepath] & lt; instancepath & a shell tool that supports regular expression log keyword statistics Usage: pafa_mon/statistics_pafa.sh-I | -- instance [-Fn | -- filename ] [-Ip | -- instancepath] [-Kw | -- keywords] [-Bt | -- begintime] [-Et | -- endtime] [-T | -- type] Instance name: English name of the instance
Instance path: path of the instance. default value:/wls/applogs/rtlog/file name: file name. default value: pafa. log key words: Match Keywords. if there are multiple keywords, separate them with colons. parameter passing does not support reading keywords with spaces from files, multiple keywords are separated by line breaks. Regular expressions are supported by default from the File "/instance path/instance name/pafa. key "read, such as:/wls/applogs/rtlog/TOA-CORESF6802/pafa. keybegin time: start time. by default, the end time is matched from the beginning of the file, and the end time is matched to the end of the file by default. progress type: Operation type: the default value is 1. 1. specify the keyword to be scanned within the file range and print the content. (the last rows) 2. specify the keyword to be scanned within the file range and the number of statistics. (the last rows) 3, scan keywords within the specified time range and print Capacity; 4. scan keywords within a specified time range, counts; for example :. /statistics_pafa.sh-I TOA-CORESF6801. /statistics_pafa.sh-I TOA-CORESF6801-t 2. /statistics_pafa.sh-I TOA-CORESF6801-t 2-kw ERROR. /statistics_pafa.sh-I TOA-CORESF6801-kw ERROR. statistics_pafa.sh-I TOA-CORESF6801-fn TOA-CORESF6801.acc-t 2-kw ERROR. /statistics_pafa.sh-I TOA-CORESF6801-t 2-kw ERROR-ip/tmp/-bt 08:00:00. /statistics_pafa.sh-I TOA-CORESF6801-t 2-kw ERROR-bt 08:00:00-et 18:00:00. /statistics_pafa.sh-I TOA-CORESF6801-ip/wls/applogs/rtlog/-fn pafa. log-kw ERROR: WARN-bt 08:00:00-et 18:00:00 -- type 2 #! /Bin/bash # File Name: statistics. sh # Function: statistics the keywords from the specified log-file. # Written by: stone # Written on: 2013-01-16 ####################################### ################################### default values for the variables below # HOST_NAME = $ (hostname) HOST_IP = $ (hostname-I) INSTANCE_NAME = ''INSTANCE _ PATH ="/wls/applogs/rtlog/"FILE_NAME = 'pafa. log 'key _ WORDS = ''MONTH = $ (date + % m) DAY = $ (date + % e) BEGIN_TIME = ''end _ TIME = ''ss SS _ TYPE = 1 ######################### ######################################## ########## function for usage of the shell script ############ function Usage () {echo "Usage: $0-I | -- instance [-Fn | -- filename ] [-Ip | -- instancepath] [-Kw | -- keywords] [-Bt | -- begintime] [-Et | -- endtime] [-T | -- type] "Echo" instance name: English name of the instance "echo" instance path: path of the instance. default value:/wls/applogs/rtlog/"echo" file name: file name, the default value is pafa. log "echo 'key words: Match keyword. if multiple keywords exist, separate them with colons. parameter passing does not support the keyword 'echo 'with spaces to read from a file, multiple keyword keywords are separated by line breaks 'echo 'default slave File "/instance path/instance name/pafa. key "read, such as:/wls/applogs/rtlog/TOA-CORESF6802/pafa. key 'echo "begin time: start time. by default," echo "end time: end time is matched from the beginning of the file. by default," echo "progress type: Operation type: The default value is 1. 'echo '1: specify the keyword to be scanned within the file range and print the content; (the last rows) 'echo '2: specify the keyword to be scanned within the file range and the number of times to be counted; (the last rows) 'echo '3: scan keywords within a specified time range and print content; 'echo' 4: scan keywords within a specified time range, counts; 'echo 'for example: 'Echo '. /statistics_pafa.sh-I TOA-CORESF6801 'echo '. /statistics_pafa.sh-I TOA-CORESF6801-t 2 'echo '. /statistics_pafa.sh-I TOA-CORESF6801-t 2-kw ERROR 'echo '. /statistics_pafa.sh-I TOA-CORESF6801-kw ERROR 'echo '. /statist Ics_pafa.sh-I TOA-CORESF6801-fn TOA-CORESF6801.acc-t 2-kw ERROR 'echo '. /statistics_pafa.sh-I TOA-CORESF6801-t 2-kw ERROR-ip/tmp/-bt 08:00:00 'echo '. /statistics_pafa.sh-I TOA-CORESF6801-t 2-kw ERROR-bt 08:00:00-et 18:00:00 'echo '. /statistics_pafa.sh-I TOA-CORESF6801-ip/wls/applogs/rtlog/-fn pafa. log-kw ERROR: WARN-bt 08:00:00-et 18:00:00 -- type 2 'exit 1 }### function fo R setting variables for the shell script ############ function Set_variables () {if [[[$ #= = 0]; then if [[-n $ INSTANCE_NAME]; then FILE_PATH = "$ {INSTANCE_PATH}/$ {INSTANCE_NAME}/" Choose exit 0 else Usage fi case "$1" in "-I ") shift INSTANCE_NAME = $1 shift Set_variables $ @; "-- instance") shift INSTANCE_NAME = $1 shift Set_variables $ @; "-fn ") shift FILE_NAME = $1 shift Set_variables $ @; "-- f Ilename ") shift FILE_NAME = $1 shift Set_variables $ @;"-ip ") shift INSTANCE_PATH = $1 shift Set_variables $ @;" -- instancepath ") shift INSTANCE_PATH = $1 shift Set_variables $ @; "-kw") shift KEY_WORDS = $1 shift Set_variables $ @; "-- keywords ") shift KEY_WORDS = $1 shift Set_variables $ @; "-bt") shift BEGIN_TIME = $1 shift Set_variables $ @; "-- begintime ") shift BEGIN_TIME = $1 shift Set_variables $ @; "-Et") shift END_TIME = $1 shift Set_variables $ @; "-- endtime") shift END_TIME = $1 shift Set_variables $ @; "-t ") shift PROGRESS_TYPE = $1 shift Set_variables $ @; "-- type") shift PROGRESS_TYPE = $1 shift Set_variables $ @;;*) usage esac }#### function for show the value of the shell script's variables ############ function Show_variables () {echo HOST_NAME = $ HOST_NAME echo HOST_IP = $ HOST_IP echo INST ANCE_NAME = $ INSTANCE_NAME echo FILE_PATH = $ FILE_PATH echo INSTANCE_PATH = $ INSTANCE_PATH echo FILE_NAME = $ FILE_NAME echo KEY_WORDS = $ KEY_WORDS echo MONTH = $ MONTH echo DAY = $ DAY echo BEGIN_TIME = $ BEGIN_TIME echo END_TIME = $ END_TIME }### function for statistic the key-words in the specified file from the last 1 million lines ########## function Statistic_contents () {tail-1000000 $ {FILE_PATH }$ {FILE_NAME} | Wk '/^' $ {MONTH} '\/' $ {DAY} '/& amp;/' "$ {KEY}" '/{OFS = ""; print "KeyWord =", "'" $ {KEY }:"'", $0} '| tail-n 10 }#### function for statistic the key-words arise times in the specified file from the last 1 million lines ####### ##### function Statistic_times () {tail-1000000 $ {FILE_PATH }$ {FILE_NAME} | awk '/^' $ {MONTH} '\/' $ {DAY} '/&/' "$ {KEY} "'/{a ++} END {OFS = ""; print "KeyWord =", "'" $ {KEY}: "'", a} '}### funct Ion for statistic the key-words in the specified file by time ########### function Statistic_contents_bytime () {if [[-z $ BEGIN_TIME]; then if [[-z $ END_TIME]; then awk '/^' $ {MONTH} '\/' $ {DAY} '/& amp;/' "$ {KEY}" '/{OFS = ""; print "KeyWord =", "'" $ {KEY }:"'", $0} '$ {FILE_PATH }$ {FILE_NAME} | tail-n 10 else awk'/^ '$ {MONTH}' \/'$ {DAY}'/& $2 <"'$ END_TIME'" & amp;/'"$ {KEY}"'/{OFS = ""; print "KeyWord =", "'" $ {KE Y}: "'", $0}' $ {FILE_PATH} $ {FILE_NAME} | tail-n 10 fi else if [[-z $ END_TIME]; then awk '/^' $ {MONTH} '\/' $ {DAY} '/& amp; $2> "' $ BEGIN_TIME '" & amp;/' "$ {KEY} "'/{OFS = ""; print "KeyWord =", "'" $ {KEY }:"'", $0} '$ {FILE_PATH }$ {FILE_NAME} | tail-n 10 else awk'/^ '$ {MONTH}' \/'$ {DAY}'/& $ 2> "'$ BEGIN_TIME'" & $2 <"'$ END_TIME'" &/'"$ {KEY}"'/{OFS = ""; print "KeyWord =", "'" $ {KEY}: "'", $0} '$ {FILE_PATH }$ {FILE_NAME} | t Ail-n 10 fi }#### function for statistic the key-words arise times in the specified file by time ########### function Statistic_times_bytime () {if [[-z $ BEGIN_TIME]; then if [[-z $ END_TIME]; then awk '/^' $ {MONTH} '\/' $ {DAY} '/&' "$ {KEY}" '/{a ++} END {OFS = ""; print "KeyWord =", "'" $ {KEY }:"'", a} '$ {FILE_PATH }$ {FILE_NAME} else awk'/^ '$ {MONTH}' \/'$ {DAY}'/& $2 <"'$ END_TIME '"&/'" $ {KEY} "'/{a ++} END {OFS = ""; print "KeyWord =", "'" $ {KEY }:"'", a} '$ {FILE_PATH }$ {FILE_NAME} fi else if [[-z $ END_TIME]; then awk '/^' $ {MONTH} '\/' $ {DAY} '/& amp; $2> "' $ BEGIN_TIME '" & amp;/' "$ {KEY} "'/{a ++} END {OFS = ""; print "KeyWord =", "'" $ {KEY }:"'", a} '$ {FILE_PATH }$ {FILE_NAME} else awk'/^ '$ {MONTH}' \/'$ {DAY}'/& $2> "'$ BEGIN_TIME '"& $2 <"' $ END_TIME '"&/'" $ {KEY} "'/{a ++} END {OFS = ""; print "KeyWord =", "'" $ {KEY}: "'", a} '$ {FILE_PATH }$ {FILE_NAME} fi }##### function for progressing for statistic ##################### ##### function Progress () {if [[-z $ KEY_WORDS]; then if [[-f $ {FILE_PATH}/pafa. key]; then grep-v "^ #" $ {FILE_PATH}/pafa. key | while read KEY do $1 done else echo "Attention please! "Echo" You shoshould create the key-word file named pafa. key in the directory $ {FILE_PATH} at first! "Exit 1 fi else Key_Words =" $ (echo $ {KEY_WORDS} | sed s) "for KEY in $ Key_Words do $1 done fi }#### function for choose progress type for statistic ############## function Choose () {case "$ PROGRESS_TYPE" in "1") Progress "Statistic_contents"; "2") Progress "Statistic_times"; "3") Progress "Statistic_contents_bytime ";; "4") Progress "Statistic_times_bytime"; *) Usage ;; esac }#### function for the start of the shell script ################# function Main () {Set_variables $ @} Main $ @

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.