Results 1 to 25 of 30

Thread: Bash script

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User dir2cas's Avatar
    Join Date: Aug:2008
    Location:
    Posts: 1,003

    Bash script

    Bash script for live log monitoring

    . /bash , .

    ( ), - , "live".

    :
    1) - line by line ( ..)
    :
    Code:
    <date_GMT> <hostname> : <date_EET> <message> <object> <event>
    event- up down,

    :
    Code:
    Dec  7 20:19:38 hostname 527037: Dec  7 18:24:27: %LINEPROTO-5-UPDOWN: Line protocol on Interface GigabitEthernet1/4, changed state to down
    2) () , ;
    3) <message>, <event> <object>, <hostname>, /;
    4) , ;

    :
    - , , file1=/path/...
    - ${file1} ..
    - while line ;
    - $line ( grep, awk, sed...)
    - .
    -
    - - $line ..

    , live ( ) .

    file descriptors output echo, , .

    : , .

    , , EOF, . ,

    Code:
    tail -n <lines> -f /path/to/file
    append- .

    , - ., bash, -, , .
    Last edited by dir2cas; 16th February 2011 at 21:51.

  2. #2
    ɐ-əpoɔᴉu⋂ ɐ ə anrieff's Avatar
    Join Date: Apr:2004
    Location: Sofia
    Posts: 8,448
    select() ( bash ).
    , , ?
    ( , fork-), IPC.

    tail -f <1> <2>. , - ( "tail -f file1 file2" file1 file2 , tail)

    Code:
    ==> file1 <==
    
    ==> file2 <==
    
    ==> file1 <==
    zoozoo1
    ==> file2 <==
    zaxzaxs
    ==> file1 <==
    xa
    xa2
    - , C, bash.
    , . .
    "640K ught to be enough for anybody" - Bill Gates, 1981
    ::Machine specs::Fract::AGG::::Baileys::blog::YouTube channel

  3. #3
    Registered User
    Join Date: Oct:2003
    Location:
    Posts: 4,317
    , Open Source ? SNMP, syslog, ping:
    http://en.wikipedia.org/wiki/Compari...toring_systems

  4. #4
    Registered User dir2cas's Avatar
    Join Date: Aug:2008
    Location:
    Posts: 1,003
    Quote Originally Posted by bsb View Post
    , Open Source ? SNMP, syslog, ping:
    http://en.wikipedia.org/wiki/Compari...toring_systems
    , (- 3- ), . embed. - , , . event-. SNMP - , , multivendor , .
    , , , .
    live , event- ..

    ( , fork-), IPC.
    , . -, , , . , , , .
    tail -f <1> <2>. , - ( "tail -f file1 file2" file1 file2 , tail)
    . , , / n=10 () / :
    Code:
    tail -n 10 -f /path/to/logfile1
    line by line live tail -f.
    file descriptors,
    Code:
    #let''s bind two file descriptors to our two logs
    exec 3<`tail -n 10 -f /var/log/logfile1`
    exec 4<`tail -n 10 -f /var/log/logfile1`
    ..........
    while read -r line1 <&3
    do
    # for example using echo to test:
    echo $line1
    ..........
    , , , "ambiguous redirect".

  5. #5
    Registered User dir2cas's Avatar
    Join Date: Aug:2008
    Location:
    Posts: 1,003
    , , , , . .
    #main_simple_script
    /path/to/script_log1&
    /path/to/script_log2&
    .........
    Code:
    #let''s bind two file descriptors to our two logs
    exec 3<$(tail -n 10 -f /var/log/logfile1)
    #exec 4<$(tail -n 10 -f /var/log/logfile1)
    ..........
    while read -r line1 <&3
    do
    # for example using echo to test:
    echo $line1
    ..........
    , . , , .
    Code:
    cho TEST >> /var/log/logfile
    , tail -f. , real time . ?
    Last edited by dir2cas; 18th February 2011 at 11:12.

  6. #6
    Nuclear's Avatar
    Join Date: Sep:2004
    Location: , . ()
    Posts: 3,352
    watch ?
    Code:
    watch 'cat /var/log/apache2/error.log | tail -10'
    -n .
    . HWBG !

  7. #7
    Registered User dir2cas's Avatar
    Join Date: Aug:2008
    Location:
    Posts: 1,003
    Quote Originally Posted by Nuclear View Post
    watch ?
    Code:
    watch 'cat /var/log/apache2/error.log | tail -10'
    -n .
    . , "" , /.
    watch fake , , 10 , line by line. tail -n 1 , fake , , ( ) , .

    , , , , , 8 , / ( ) ?

  8. #8
    ɐ-əpoɔᴉu⋂ ɐ ə anrieff's Avatar
    Join Date: Apr:2004
    Location: Sofia
    Posts: 8,448
    :
    , :

    Code:
    log1_size=`wc -c log1.txt | sed s/\ .\*//g`
    ...
    sleep- 1 . - tail .
    bash- : wc -c wc -l .

    , , - inotify, bash.

    .
    , . .
    "640K ught to be enough for anybody" - Bill Gates, 1981
    ::Machine specs::Fract::AGG::::Baileys::blog::YouTube channel

  9. #9
    Registered User dir2cas's Avatar
    Join Date: Aug:2008
    Location:
    Posts: 1,003
    @anrieff. , dirty enough.
    . :
    - :
    - (wc -l -, )
    - ,
    Code:
    lines=$(cat /var/log/logfile1 | wc -l)
    - 1 sleep 1
    Code:
    current_lines=$(cat /var/log/logfile1 | wc -l)
    - :
    Code:
    new_lines=$(echo "scale=0; ${current_lines}-${lines}" | bc -l)
    lines=${current_lines}
    - , tail temp ,
    Code:
    tail -n ${new_lines} /var/log/logfile1 > /path/to/temp_log1
    ">" , temp_log1 temp_log1
    -
    - , temp_log1 .

    , ?

    inotify , . bash, lib-a.
    Last edited by dir2cas; 18th February 2011 at 17:08.

  10. #10
    ɐ-əpoɔᴉu⋂ ɐ ə anrieff's Avatar
    Join Date: Apr:2004
    Location: Sofia
    Posts: 8,448

    bc - - , expr:

    Code:
    new_lines=`expr ${current_lines} - ${lines}`
    ( bash, ).
    , . .
    "640K ught to be enough for anybody" - Bill Gates, 1981
    ::Machine specs::Fract::AGG::::Baileys::blog::YouTube channel

  11. #11
    Registered User dir2cas's Avatar
    Join Date: Aug:2008
    Location:
    Posts: 1,003
    . ( ) . :




    ....................

    pattern , : pattern=""

    linenum=$(grep -n -E "${pattern}" /file | cut -f1 -d: ) #

    line=" "
    $linenum /file $line

    , :
    Code:
    sed -i '"${linenum}"s/.*/"${line}"/' /file
    "", , sed parse- . , awk
    awk -v n="${linenum}" -v line1="${line}" 'NR==$n{$0="$line1"}' /file
    , . , , ,

  12. #12
    ɐ-əpoɔᴉu⋂ ɐ ə anrieff's Avatar
    Join Date: Apr:2004
    Location: Sofia
    Posts: 8,448
    ( Python, tail head) :

    Code:
    def changeLine(lines, lineNumber, newLine):
    	return lines[:lineNumber] + [newLine] + lines[lineNumber + 1:]
    , . .
    "640K ught to be enough for anybody" - Bill Gates, 1981
    ::Machine specs::Fract::AGG::::Baileys::blog::YouTube channel

  13. #13

  14. #14
    Registered User dir2cas's Avatar
    Join Date: Aug:2008
    Location:
    Posts: 1,003
    , $line , ${line[${i}]} , , . sed awk , , . ${pattern} , , , - -, . a workaround , .

    - , anrieff. , .

    ---------- 22:01 ---------- 13:42 ----------

    E sed. $pattern, , pattetn - , patterna. , - , .

    - .
    Code:
    sed '/"$pattern"/ s//"${line}"/g' /file
    , $pattern $line

    ..

    ---------- 22:19 ---------- 22:01 ----------

    sed, :
    Code:
    sed '/'"${pattern}"'/ c'"${line}"'' /file
    '" "' quotes . -... , , , ...

    /file , pattern- /. stdout . ?
    sed '/'"${pattern}"'/ c'"${line}"'' < /file > /temp

  15. #15
    Registered User dir2cas's Avatar
    Join Date: Aug:2008
    Location:
    Posts: 1,003
    anrieff JustNick . - , , . , . OpenWrt, . penWrt ( ) , () .

    , , .

  16. #16
    Registered User dir2cas's Avatar
    Join Date: Aug:2008
    Location:
    Posts: 1,003
    , /, timing sleep , .

    - - log parser&handling tool, .

    ,
    Code:
    # Defining Log files to monitor
    LOG1="/path/to/file1.log"
    LOG2="/path/to/file2.log"
    LOG3="/path/to/file3.log"
    
    # Number of lines in the files to examine before starting live monitoring
    LOG_HISTORY="20"
    
    # Setting up some filtering
    BLACKLIST="RegExp1|RegExp2|...."
    WHITELIST="RegExp_1|RegExp_2|...."
    
    # Start log files live monitoring
    $(which tail) -q -n ${LOG_HISTORY} -F ${LOG1} ${LOG2} ${LOG3} |
    while read -r LINE
    do
    	# Applying filters
    	LINE=$(echo "${LINE}" | egrep -wv "${BLACKLIST}" | egrep "${WHITELIST}")
    	[ -z "${LINE}" ] && continue
    	#echo "${LINE}"
            # Doing all kind of checks and executions with the line
    	echo "${LINE}" | egrep "pattern" > /dev/null && { ExecuteFunction; continue; };
            # etc......
    done
    . , ...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Copyright © 1999-2011 . .
iskamPC.com | mobility.BG | Bloody's Techblog | | 3D Vision Blog |