Apache-2.2

僅列出特定欄位的 apache 實時監控腳本

  • June 16, 2013

我正在實時監控我的網站日誌,但想創建一個腳本,只在漂亮漂亮的列中顯示以下內容。

正在使用的日誌:/var/log/httpd/access_log(預設日誌文件和設置)

主機名 - IP 地址 - 頁面 - 日期和時間

目前通過終端使用的命令:tail -f /var/log/httpd/access_log | awk ‘{ 列印“主機名” $ 1 $ 7 $4 }’

我發現有人創造了我想做的事情,但他們沒有回應我。這是我想要結束的螢幕截圖

http://qph.cf.quoracdn.net/main-qimg-fde4d84bf459d14aff0ef930e4f8c7fe

還想合併netstat -anp | grep :80 | wc -l 在頂部的腳本中顯示目前連接數

我目前的輸出是亂碼,如下圖:

IPPAGETIME

沒有分離等…

一般來說,我是腳本新手,所以這對我來說是全新的,我花了一點時間讓 awk 為我工作,如果已經有這個文章,請分享

   #!/bin/sh

   # Define some variables#
   TAIL="/usr/bin/tail -f"
   # TAIL command can also be -10, -15, -20, -30
   LOG="/var/log/httpd/access_log"
   TOTAL=`netstat -pant | grep :80 | wc -l`
   TOTAL2=`netstat -pant | grep :443 | wc -l`
   echo "There are $TOTAL port 80 connections."
   echo "There are $TOTAL2 port 443 connections."
   uptime | awk '{print $8,$9,$10,$11,$12,$13,$14,$15}'

   #Let's do it#
   $TAIL $LOG | awk '{print$3 " " $8 " " $4 " " $6 " " $8 " " $9 " " $7}'

   #DONE

@Stephan 想把原始問題的連結放在一起嗎?

使用者157574

您最有可能希望將 printf 或 tput 與 column 一起使用。儘管我意識到這不是實時的,但對我來說使用這些作品:

less access_log | grep 05/Feb/2013 | awk '{print $1,$4,$7;}'

less access_log | grep 05/Feb/2013 | awk '{ x = $2 " " $4 " " $7 ; printf "%-15s %-20s \n", $1, x, $7}'

編輯:

您可以嘗試更改版本:

#!/bin/bash
ACCESSLOG="/var/log/apache2/access_log"
TOTAL=`netstat -pant | grep :80 | wc -l`

echo "There are a total of $TOTAL port 80 connections."
uptime | awk '{print $8,$9,$10,$11,$12}'

tail -5 $ACCESSLOG | grep "05/Feb/2013" | awk '{ x = $2 " " $4 " " $7 ; printf "%-15s %-20s \n", $1, x, $7}'

您可以將其編輯為 tail -f 使其處於活動狀態或 tail -5 、 -10 、 -20 等。

引用自:https://serverfault.com/questions/475765