
Nginx在生产环境中的应用越来越广泛,所以需要对nginx的性能状态做一些监控,从而发现故障隐患,Ngnx的监控指标可分为:基本活动指标,错误指标,性能指标
监控Nginx思路:
1)首先,要想监控Nginx,需要状态模块的支持,也就是-with-http_stub_status_module;随后在nginx.conf配置开启status状态
2)通过awk取status的各个状态数值
3)自定义模板将取值status的状态写成脚本,放在UserParmeter后面,最后通过zabbix_get测试·agent端是否得到状态值
Accepts:接受的客户端请求数
Active:当前活跃的连接数
Handled:处理的请求数(正常服务器响应)
Requests:客户端处理的请求出(吞吐量)
Reading:当接收到的请求时,连接离开waiting状态,并且该请求本身使Reading状态统计数增加,这种状态下,nginx会读取客户端请求首部,请求首部是比较小的,因此这通常是一种快捷的操作
Writing:请求被读取之后,使得Writing状态计数增加,并保持在该状态,直到响应返回给客户端,这便意味着,该请求在writing状态时,一方面NGINX
[root@nginx-web ~]# yum install -y pcre-devel openssl-devel popt-devel
[root@nginx-web ~]# tar zxvf nginx-1.11.2.tar.gz
[root@nginx-web ~]# cd nginx-1.11.2
[root@nginx-web nginx-1.11.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
[root@nginx-web nginx-1.11.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@nginx-web nginx-1.11.2]# useradd -M -s /sbin/nologin nginx
[root@nginx-web nginx-1.11.2]# 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
[root@nginx-web nginx-1.11.2]# cd /usr/local/nginx/conf/
[root@nginx-web conf]# ls
fastcgi.conf fastcgi_params koi-utf mime.types nginx.conf scgi_params uwsgi_params win-utf
fastcgi.conf.default fastcgi_params.default koi-win mime.types.default nginx.conf.default scgi_params.default uwsgi_params.default
[root@nginx-web conf]# cp nginx.conf nginx.conf.bak
[root@nginx-web conf]# vim nginx.conf
worker_processes ;
events {
worker_connections ;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout ;
server {
listen ;
server_name localhost;
location / {
root html;
index index.html index.htm;
location /status {
stub_status on;
access_log off;
allow 192.168.2.0/24;
allow 127.0.0.1;
deny all;
}
}
error_page /50x.html;
location = /50x.html {
root html;
}
}
}
【Zabbix-agent端配置】
[root@nginx-web zabbix_agentd.conf.d]# vim nginx_monitor.sh
NGINX_PORT=
NGINX_COMMAND=$
nginx_active(){
/usr/bin/curl -s "http://127.0.0.1:"$NGINX_PORT"/status/" |awk '/Active/ {print $NF}'
}
nginx_reading(){
/usr/bin/curl -s "http://127.0.0.1:"$NGINX_PORT"/status/" |awk '/Reading/ {print $2}'
}
nginx_writing(){
/usr/bin/curl -s "http://127.0.0.1:"$NGINX_PORT"/status/" |awk '/Writing/ {print $4}'
}
nginx_waiting(){
/usr/bin/curl -s "http://127.0.0.1:"$NGINX_PORT"/status/" |awk '/Waiting/ {print $6}'
}
nginx_accepts(){
/usr/bin/curl -s "http://127.0.0.1:"$NGINX_PORT"/status/" |awk 'NR==3 {print $1}'
}
nginx_handled(){
/usr/bin/curl -s "http://127.0.0.1:"$NGINX_PORT"/status/" |awk 'NR==3 {print $2}'
}
nginx_requests(){
/usr/bin/curl -s "http://127.0.0.1:"$NGINX_PORT"/status/" |awk 'NR==3 {print $3}'
}
case $NGINX_COMMAND in
active)
nginx_active;
;;
reading)
nginx_reading;
;;
writing)
nginx_writing;
;;
waiting)
nginx_waiting;
;;
accepts)
nginx_accepts;
;;
handled)
nginx_handled;
;;
requests)
nginx_requests;
;;
*)
echo $"USAGE:$0 {active|reading|writing|waiting|accepts|handled|requests}"
esac
[root@nginx-web zabbix_agentd.conf.d]# chmod +x nginx_monitor.sh
root@nginx-web zabbix_agentd.conf.d]#vim /usr/local/zabbix/etc/zabbix_agentd.conf
LogFile=/tmp/zabbix_agentd.log
Server=192.168.2.148
ListenPort=
ServerActive=192.168.2.148
Hostname=192.168.2.146
UserParameter=status[*],/bin/bash /usr/local/zabbix/etc/zabbix_agentd.conf.d/nginx_monitor.sh "$1"
[测试]
在zabbix服务端测试一下能否获取agent端的nginx状态数值
[root@zabbix-server ~]# /usr/local/zabbix/bin/zabbix_get -s 192.168.2.146 -k status
USAGE:/usr/local/zabbix/etc/zabbix_agentd.conf.d/nginx_monitor.sh {active|reading|writing|waiting|accepts|handled|requests}
[root@zabbix-server ~]# /usr/local/zabbix/bin/zabbix_get -s 192.168.2.146 -k status[writing]
1
【创建监控项】
根据zabbix-agent中的nginx_status.sh的参数,总共要创建active、reading、writing、waiting、accepts、handled、requests这7项
目前为止,七个监控项已添加完成了
[图形创建]