Results 1 to 25 of 30
Thread: Bash script
Hybrid View
-
16th February 2011 16:14 #1
Bash script
Bash script for live log monitoring
. /bash , .
( ), - , "live".
:
1) - line by line ( ..)
:
event- up down,Code:<date_GMT> <hostname> : <date_EET> <message> <object> <event>
:
2) () , ;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
3) <message>, <event> <object>, <hostname>, /;
4) , ;
:
- , , file1=/path/...
- ${file1} ..
- while line ;
- $line ( grep, awk, sed...)
- .
-
- - $line ..
, live ( ) .
file descriptors output echo, , .
: , .
, , EOF, . ,
append- .Code:tail -n <lines> -f /path/to/file
, - ., bash, -, , .Last edited by dir2cas; 16th February 2011 at 21:51.
-
16th February 2011 17:20 #2
select() ( bash ).
, , ?
( , fork-), IPC.
tail -f <1> <2>. , - ( "tail -f file1 file2" file1 file2 , tail)
- , C, bash.Code:==> file1 <== ==> file2 <== ==> file1 <== zoozoo1 ==> file2 <== zaxzaxs ==> file1 <== xa xa2
, . .
"640K ught to be enough for anybody" - Bill Gates, 1981
::Machine specs::Fract::AGG::::Baileys::blog::YouTube channel
-
16th February 2011 18:18 #3Registered User
Join Date: Oct:2003
Location:
Posts: 4,317
, Open Source ? SNMP, syslog, ping:
http://en.wikipedia.org/wiki/Compari...toring_systems
-
16th February 2011 20:29 #4
, (- 3- ), . embed. - , , . event-. SNMP - , , multivendor , .
, , , .
live , event- ..
, . -, , , . , , , .( , fork-), IPC.
. , , / n=10 () / :tail -f <1> <2>. , - ( "tail -f file1 file2" file1 file2 , tail)line by line live tail -f.Code:tail -n 10 -f /path/to/logfile1
file descriptors,, , , "ambiguous redirect".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 ..........
-
18th February 2011 10:58 #5
, , , , . .
#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 ..........
, tail -f. , real time . ?Code:cho TEST >> /var/log/logfile
Last edited by dir2cas; 18th February 2011 at 11:12.
-
18th February 2011 11:44 #6
watch ?
-n .Code:watch 'cat /var/log/apache2/error.log | tail -10'
. HWBG !
-
18th February 2011 13:04 #7
-
18th February 2011 15:31 #8, . .
"640K ught to be enough for anybody" - Bill Gates, 1981
::Machine specs::Fract::AGG::::Baileys::blog::YouTube channel
-
18th February 2011 16:54 #9
@anrieff. , dirty enough.
. :
- :
- (wc -l -, )
- ,- 1 sleep 1Code:lines=$(cat /var/log/logfile1 | wc -l)
- :Code:current_lines=$(cat /var/log/logfile1 | wc -l)
- , tail temp ,Code:new_lines=$(echo "scale=0; ${current_lines}-${lines}" | bc -l) lines=${current_lines}
">" , temp_log1 temp_log1Code:tail -n ${new_lines} /var/log/logfile1 > /path/to/temp_log1
-
- , temp_log1 .
, ?
inotify , . bash, lib-a.Last edited by dir2cas; 18th February 2011 at 17:08.
-
18th February 2011 17:18 #10
bc - - , expr:
( bash, ).Code:new_lines=`expr ${current_lines} - ${lines}`, . .
"640K ught to be enough for anybody" - Bill Gates, 1981
::Machine specs::Fract::AGG::::Baileys::blog::YouTube channel
-
11th March 2011 05:02 #11
. ( ) . :
....................
pattern , : pattern=""
linenum=$(grep -n -E "${pattern}" /file | cut -f1 -d: ) #
line=" "
$linenum /file $line
, :
"", , sed parse- . , awkCode:sed -i '"${linenum}"s/.*/"${line}"/' /file
, . , , ,awk -v n="${linenum}" -v line1="${line}" 'NR==$n{$0="$line1"}' /file

-
11th March 2011 10:33 #12
( 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
-
11th March 2011 11:12 #13/Steve Jobs, Stanford University (2005)/
-
11th March 2011 22:19 #14
, $line , ${line[${i}]} , , . sed awk , , . ${pattern} , , , - -, . a workaround , .
- , anrieff. , .
---------- 22:01 ---------- 13:42 ----------
E sed. $pattern, , pattetn - , patterna. , - , .
- .
, $pattern $lineCode:sed '/"$pattern"/ s//"${line}"/g' /file


..
---------- 22:19 ---------- 22:01 ----------
sed, :
'" "' quotes . -... , , , ...Code:sed '/'"${pattern}"'/ c'"${line}"'' /file
/file , pattern- /. stdout . ?
sed '/'"${pattern}"'/ c'"${line}"'' < /file > /temp
-
14th March 2011 01:10 #15
-
13th March 2012 00:57 #16
, /, 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




Reply With Quote

Lenovo ThinkPad 15 IdeaPad 15
5th May 2023, 22:16 in