一、開啟nginx的監控

1)、nginx簡單狀態監控

在nginx.conf中添加如下代碼即可監控nginx當前的狀態,然后訪問http://serverip/status即可訪問

location /status {
stub_status on;
access_log off;
}

一般顯示為

Active connections: 16 
server accepts handled requests
191226 191226 305915 
Reading: 0 Writing: 1 Waiting: 15

ctive connections: 對后端發起的活動連接數.

Server accepts handled requests: Nginx總共處理了24個連接,成功創建24次握手(證明中間沒有失敗的),總共處理了129個請求.

Reading: Nginx 讀取到客戶端的Header信息數.

Writing: Nginx 返回給客戶端的Header信息數.

Waiting: 開啟keep-alive的情況下,這個值等于 active – (reading + writing),意思就是Nginx已經處理完成,正在等候下一次請求指令的駐留連接.
注意的,本模塊默認是不會編譯進Nginx的,如果你要使用該模塊,則要在編譯安裝Nginx時指定:
 ./configure –with-http_stub_status_module 
 查看已安裝的 Nginx 是否包含 stub_status 模塊
 #/usr/local/nginx/sbin/nginx -V 
 TLS SNI support disabled
configure arguments: --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-file-aio --with-http_ssl_module 
 
2)、nginx的圖形化監控-nginx-RRD stats
 
nginx-rrdnginx官方推薦的一款Nginx監控工具,利用nginx-rrd可以很方便的生成圖表,便于我們查看。

1、運行環境(centos):

在安裝前需要安裝好rrdtool這個畫圖工具和相應的perl模塊,可以先運行:

yum install rrdtool libhtml-parser-perl libwww-perl librrds-perl librrd2-dev

確保rrdtool和相應的perl被安裝上。

2、安裝配置

下載:wget http://soft.vpser.net/status/nginx-rrd/nginx-rrd-0.1.4.tgz

解壓:tar zxvf nginx-rrd-0.1.4.tgz

進入nginx-rrd目錄,cd nginx-rrd-0.1.4/

復制主程序:cp usr/sbin/* /usr/sbin

復制配置文件cp etc/nginx-rrd.conf /etc

復制定時執行文件:cp etc/cron.d/nginx-rrd.cron /etc/cron.d

創建nginx-rrd生成目錄:mkdir /home/wwwroot/nginx && mkdir /home/wwwroot/nginx/rrd

cp html/index.php /home/wwwroot/nginx

編輯/home/wwwroot/nginx/index.php修改登錄密碼

<?php
header("Content-Type: text/html; charset=utf-8");

$password = "admin"; 

.........

編輯配置文件nginx-rrd.conf,修改完成后如下:

#####################################################
#
# dir where rrd databases are stored
RRD_DIR="/home/wwwroot/nginx-rrd/";
# dir where png images are presented
WWW_DIR="/home/wwwroot/nginx/";
# process nice level
NICE_LEVEL="-19";
# bin dir
BIN_DIR="/usr/sbin";
# servers to test
# server_utl;server_name
SERVERS_URL="http://127.0.0.1/status;127.0.0.1"

多個server,可以SERVERS_URL中空格分開,前部分為nginx_status的地址,后面為被監控主機的域名。

SEVERS_URL 格式

注意,nginx.conf虛擬主機server{}中,需要已經加入:

location /status {
stub_status on;
access_log off;
}

以上設置就完成,可以自行運行一下:/usr/sbin/nginx-collect ,啟動收集程序。cron會15分鐘生成一次數據。

如果沒有定時執行生成數據,可以在/etc/crontab最后面加上:

* * * * * root /usr/sbin/nginx-collect
*/15 * * * * root /usr/sbin/nginx-graph

然后輸入然后訪問http://serverip/nginx/即可訪問。

 

二、開啟tomcat的監控

1)、tomcat6的配置

修改tomcat/conftomcat-users.xml文件中</tomcat-users>節點之前添加如下代碼即可。

 <user username="admin" password="admin" roles="manager"/>

然后輸入然后訪問http://serverip:8080/manager/status即可訪問。

2)tomcat7的配置

修改tomcat/conftomcat-users.xml文件中</tomcat-users>節點之前添加如下代碼即可。

<role rolename="manager-gui"/>
<user username="tomcat" admin="admin" roles="manager-gui"/>

然后輸入然后訪問http://serverip:8080/manager/status即可訪問。