1 stub_status模块
监控Nginx状态使用到stub_status,需要在将模块编译到nginx
编译的时候增加
--with-http_stub_status_module
nginx安装个完成后查看模块是否存在
# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.14.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx/ --with-http_stub_status_module --with-http_ssl_module --with-stream --with-ld-opt=-Wl,-rpath,/usr/local/nginx/lua/luajit/lib --add-module=/usr/local/ngx_devel_kit-0.3.0/ --add-module=/usr/local/lua-nginx-module-0.10.11/ --add-module=/usr/local/nginx-module-vts
2 编辑nginx匹配配置文件
vim /usr/local/nginx/conf/nginx.conf
server 下增加
location /nginx_status {
allow 127.0.0.1;
deny all;
stub_status on;
access_log off;
}
# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx//conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx//conf/nginx.conf test is successful
# /usr/local/nginx/sbin/nginx -s reload
3)测试访问
# curl -s 127.0.0.1:8080/nginx_status
Active connections: 2
server accepts handled requests
62 62 5166
Reading: 0 Writing: 1 Waiting: 1
Active connections: 对后端发起的活动连接数.
Server accepts handled requests: Nginx总共处理了62个连接,成功创建62次握手(证明中间没有失败的),总共处理了5166个请求.
Reading: Nginx 读取到客户端的Header信息数
Writing: Nginx 返回给客户端的Header信息数
Waiting: 开启keep-alive的情况下,这个值等于 active – (reading + writing),意思就是Nginx已经处理完成,正在等候下一次请求指令的驻留连接.
在访问效率高,请求很快被处理完毕的情况下,Waiting数比较多是正常的.如果reading +writing数较多,则说明并发访问量非常大,正在处理过程中.
4)查看TCP连接数
netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'
或者
ss -s
网络状态解释:
1)、LISTEN:首先服务端需要打开一个socket进行监听,状态为LISTEN./* The socket is listening for incoming connections. 侦听来自远方TCP端口的连接请求 */
2)、SYN_SENT:客户端通过应用程序调用connect进行active open.于是客户端tcp发送一个SYN以请求建立一个连接.之后状态置为SYN_SENT./*The socket is actively attempting to establish a connection. 在发送连接请求后等待匹配的连接请求 */
3)、SYN_RECV:服务端应发出ACK确认客户端的 SYN,同时自己向客户端发送一个SYN. 之后状态置为SYN_RECV/* A connection request has been received from the network. 在收到和发送一个连接请求后等待对连接请求的确认 */
4)、ESTABLISHED: 代表一个打开的连接,双方可以进行或已经在数据交互了。/* The socket has an established connection. 代表一个打开的连接,数据可以传送给用户 */
5)、FIN_WAIT1:主动关闭(active close)端应用程序调用close,于是其TCP发出FIN请求主动关闭连接,之后进入FIN_WAIT1状态./* The socket is closed, and the connection is shutting down. 等待远程TCP的连接中断请求,或先前的连接中断请求的确认 */
6)、CLOSE_WAIT:被动关闭(passive close)端TCP接到FIN后,就发出ACK以回应FIN请求(它的接收也作为文件结束符传递给上层应用程序),并进入CLOSE_WAIT./* The remote end has shut down, waiting for the socket to close. 等待从本地用户发来的连接中断请求 */
7)、FIN_WAIT2:主动关闭端接到ACK后,就进入了 FIN-WAIT-2 ./* Connection is closed, and the socket is waiting for a shutdown from the remote end. 从远程TCP等待连接中断请求 */
8)、LAST_ACK:被动关闭端一段时间后,接收到文件结束符的应用程序将调用CLOSE关闭连接。这导致它的TCP也发送一个 FIN,等待对方的ACK.就进入了LAST-ACK ./* The remote end has shut down, and the socket is closed. Waiting for acknowledgement. 等待原来发向远程TCP的连接中断请求的确认 */
9)、TIME_WAIT:在主动关闭端接收到FIN后,TCP 就发送ACK包,并进入TIME-WAIT状态。/* The socket is waiting after close to handle packets still in the network.等待足够的时间以确保远程TCP接收到连接中断请求的确认 */
10)、CLOSING: 比较少见./* Both sockets are shut down but we still don’t have all our data sent. 等待远程TCP对连接中断的确认 */
11)、CLOSED: 被动关闭端在接受到ACK包后,就进入了closed的状态。连接结束./* The socket is not being used. 没有任何连接状态 */
12)、UNKNOWN: 未知的Socket状态。/* The state of the socket is unknown. */
SYN: (同步序列编号,Synchronize Sequence Numbers)该标志仅在三次握手建立TCP连接时有效。表示一个新的TCP连接请求。
ACK: (确认编号,Acknowledgement Number)是对TCP请求的确认标志,同时提示对端系统已经成功接收所有数据。
FIN: (结束标志,FINish)用来结束一个TCP回话.但对应端口仍处于开放状态,准备接收后续数据。
5) 编写自定义监控项脚本
cd /etc/zabbix/zabbix_agentd.d/
cat nginx_tcp.conf
UserParameter=nginx.active,curl -s 127.0.0.1:8080/nginx_status | grep 'Active' | awk '{print $NF}'
UserParameter=tcp.esb,netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}' | grep 'ESTABLISHED' | awk '{print $NF}'
如果监控项很多可以写脚本这里面直接调用脚本即可
重启agent
systemctl restart zabbix-agent
6)测试
yum install -y zabbix-get
# zabbix_get -s 127.0.0.1 -p 10050 -k "nginx.active"
2
]# zabbix_get -s 127.0.0.1 -p 10050 -k "tcp.esb"
6
说明可以正常拿到数据
注意 我这里是直接在zabbix 服务器器上做的监控,如果你监控的主机不是本机那么需要变化被监控的IP
7)zabbix增加监控
注意:我自己监控就是zabbix本机
8) 创建图形
9) 查看添加的图形
###########################################################################################################
使用 Percona 监控mysql