A shell tool that supports regular expression log keyword statistics

Source: Internet
Author: User

Usage, a shell tool that supports regular expression log keyword statistics: pafa_mon/statistics_pafa.sh-I | -- instance <instance name> [-fn | -- filename <file name>] [-ip | -- instancepath] <instance path> [-kw | -- keywords] <key words> [-bt | -- begintime] <begin time> [-et | -- endtime] <end time> [-t | -- type] <progress type> instance name: instance name
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 <instance name> [-fn | -- filename <file name>] [-ip | -- instancepath] <instance path> [-kw | -- keywords] <key words> [-bt | -- begintime] <begin time> [-et | -- endtime] <end ti Me> [-t | -- type] <progress type> "echo" instance name: instance name "echo" instance path: path of the instance, the default value is/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, the start time matches" echo "end time: end time, which is matched "Echo" progress type: operation type at the end of the file: 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-CORES F6801-kw ERROR 'echo '. /statistics_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 for 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 $ @; "-- filename") 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 BEGI N_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_NAM E echo HOST_IP = $ HOST_IP echo INSTANCE_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-10 00000 $ {FILE_PATH }$ {FILE_NAME} | awk '/^' $ {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} '}#### function 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 =", "'" $ {KEY }:"'", $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} | tail-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}'/& amp; $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 = ""; pri Nt "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 $ @

Related Article

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.