使用ngxtop即時監控nginx

來源:互聯網
上載者:User

標籤:

Ngxtop即時解析nginx訪問日誌,並且將處理結果輸出到終端,功能類似於系統命令top,所以這個軟體起名ngxtop。有了ngxtop,你可以即時瞭解到當前nginx的訪問狀況,再也不需要tail日誌看螢幕重新整理。

首先在 Linux 系統中安裝依賴庫pip(ngxtop是用python編寫的)。

1.安裝ngxtop

#yum install python python-pip -y

#yum install ngxtop  -y

2. ngxtop使用詳解
# ngxtop --helpngxtop - ad-hoc query for nginx access log. Usage:    ngxtop [options]    ngxtop [options] (print|top|avg|sum) <var> ...    ngxtop info    ngxtop [options] query <query> ... Options:    -l <file>, --access-log <file>  需要分析的訪問日誌    -f <format>, --log-format <format>  log_format指令指定的日誌格式 [預設: combined]    --no-follow  ngxtop default behavior is to ignore current lines in log                     and only watch for new lines as they are written to the access log.                     Use this flag to tell ngxtop to process the current content of the access log instead.    -t <seconds>, --interval <seconds>  report interval when running in follow mode [default: 2.0]     -g <var>, --group-by <var>  根據變數分組 [預設: request_path]    -w <var>, --having <expr>  having clause [default: 1]    -o <var>, --order-by <var>  排序 [預設: count]    -n <number>, --limit <number>  顯示的條數 [default: 10]    -a <exp> ..., --a <exp> ...  add exp (must be aggregation exp: sum, avg, min, max, etc.) into output     -v, --verbose  更多的輸出    -d, --debug  print every line and parsed record    -h, --help  當前協助資訊.    --version  輸出版本資訊.     進階選項:    -c <file>, --config <file>  運行ngxtop解析nginx設定檔    -i <filter-expression>, --filter <filter-expression>  filter in, records satisfied given expression are processed.    -p <filter-expression>, --pre-filter <filter-expression> in-filter expression to check in pre-parsing phase. 範例:    All examples read nginx config file for access log location and format.    If you want to specify the access log file and / or log format, use the -f and -a options.     "top" like view of nginx requests    $ ngxtop     404前十的請求    $ ngxtop top request_path --filter ‘status == 404‘     總流量前十的請求    $ ngxtop --order-by ‘avg(bytes_sent) * count‘     訪問量前十的ip地址    $ ngxtop --group-by remote_addr     輸出400以上狀態嗎的請求以及請求來源    $ ngxtop -i ‘status >= 400‘ print request status http_referer     Average body bytes sent of 200 responses of requested path begin with ‘foo‘:    $ ngxtop avg bytes_sent --filter ‘status == 200 and request_path.startswith("foo")‘     使用common日誌格式分析遠程伺服器Apache訪問日誌    $ ssh remote tail -f /var/log/apache2/access.log | ngxtop -f common

 

  • -l : 指定記錄檔的完整路徑 (Nginx 或 Apache2)
  • -f : 日誌格式
  • --no-follow: 處理當前已經寫入的記錄檔,而不是即時處理新添加到記錄檔的日誌
  • -t : 更新頻率
  • -n : 顯示行號
  • -o : 定序(預設是訪問計數)
  • -a ..., --a ...: 添加運算式(一般是彙總運算式如: sum, avg, min, max 等)到輸出中。
  • -v: 輸出詳細資料
  • -i : 只處理符合規則的記錄

以下是一些內建變數,他們的含義不言自明。使用 ngxtop info 命令也可以查看到可使用的內建變數名

  • body_bytes_sent
  • http_referer
  • http_usera_gent
  • remote_addr
  • remote_user
  • request
  • status
  • time_local

ngxtop print  request  http_user_agent   remote_addr   -l  /usr/local/nginx/logs/access.log

3. ngxtop執行個體

3.1 即時狀態

# ngxtop -c /usr/local/nginx/conf/nginx.conf  

 

3.2 訪問量前十的IP

#ngxtop -c /usr/local/nginx/conf/nginx.conf  top remote_addr 

 

4. 注意事項

4.1 ngxtop單條命令無法執行,提示如下錯誤提示時:

 Error: Access log file or format was not set and nginx config file cannot be detected. Perhaps nginx is not in your PATH?


方法一:軟串連

 ln -sv /usr/local/nginx/sbin/nginx /sbin/ 

方法二:修改環境變數

 # vim /etc/profile    export PATH=$PATH:/usr/local/nginx/sbin# source /etc/profile 

方法三:指定設定檔

# ngxtop -c /usr/local/nginx/conf/nginx.conf 或者還需要指定訪問記錄檔  -l /usr/local/nginx/logs/access.log

 

當提示 Error: access log file "logs/accss.log" does not exist 時,修改一下nginx.conf的日誌存放位置,將其改成絕對路徑後即可。

 

 

4.2 虛擬機器主機設定檔必須在nginx.conf主配置中
一般情況下,我們會將虛擬機器主機單獨寫到一個設定檔中,然後nginx.conf做個include。例如我們網站www.ttlsa.com
設定檔:/usr/local/nginx-1.5.2/conf/vhost/www.ttlsa.com.conf
再nginx.conf的http段中添加include vhost/*.conf

 

使用ngxtop即時監控nginx

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.