2017-06-14 59 views
1

有助於編寫腳本。下面是日誌格式。 我想編寫一個在LIVE日誌中搜索關鍵詞的腳本。假設有些人已經停止服務器,它會顯示關機或force_shutdown ,它也顯示在日誌「服務器關機已由$ user發起」。使用shell腳本和發送郵件進行日誌監控

<Apr 19, 2017 1:11:00 PM EDT> <Notice> <WebLogicServer> <BEA-000365> <Server state changed to STARTING> 
<Apr 19, 2017 1:11:06 PM EDT> <Notice> <Log Management> <BEA-170027> <The Server has established connection with the Domain level Diagnostic Service successfully.> 
<Apr 19, 2017 1:11:06 PM EDT> <Notice> <WebLogicServer> <BEA-000365> <Server state changed to shutdown> 
<Apr 19, 2017 1:11:06 PM EDT> <Notice> <WebLogicServer> <BEA-000365> <Server state changed to force_shutdown> 

<Jan 9, 2008 6:50:30 PM EST> <Notice> <WebLogicServer> <BEA-000388> <JVM 
called WLS shutdown hook. The server will force shutdown now> 
<Jan 9, 2008 6:50:30 PM EST> <Alert> <WebLogicServer> <BEA-000396> <Server shutdown has been requested by <WLS Kernel>> 
<Jan 9, 2008 6:50:30 PM EST> <Notice> <WebLogicServer> <BEA-000365> <Server state changed to FORCE_SHUTTING_DOWN> 

希望到grep與準確的時間戳所有的信息服務器IP和服務器主機,當它是攤牌,並通過用戶。而將郵件發送到該用戶的所有細節,以用戶。 請幫我

回答

0

您可以閱讀使用腳本一樣,過着文件:(備註:該腳本不讀取整個文件,只需將新的未來線)

#!/bin/bash 

LOG_FILE="/var/log/foo" 

tail -n 0 -f "$LOG_FILE" | while IFS= read -r line; do 
    echo $line 
done 

你之後可以輕鬆地搜索你想要的字符串以grep

#!/bin/bash 

LOG_FILE="/var/log/foo" 
SEARCHED="Server shutdown" 

tail -n 0 -f "$LOG_FILE" | while IFS= read -r line; do 
    if [ $(echo "${line}" | grep "${SEARCHED}") ] ; then 
     echo "String find in : $line" 
    fi 
done 

而最後一部分,你可以解析與awk行提取你想要什麼,併發送。在谷歌搜索,你會發現很多例子:)