使用Munin来监控Nginx

前言:

Munin是一款很高效的监控工具,拥有大量的插件. 对于Nginx来言,监控需要一点点的设置. 这一篇小轿车的目的就是手把手的来教你使用Munin来监控nginx请求数和状态.

本文中所有的操作都在Centos 7.1下完成, 理论上其他的发行版本也可以套用(或许需要一点点的修改)

 

正文:

首先,确定你的Nginx有http_stub_status_module模块. 执行nginx -V

[root@jp02 ~]# nginx -V
nginx version: nginx/1.11.3
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-4) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabledconfigure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules 
 --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log 
 --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock 
 --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp 
 --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp 
 --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module 
 --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module
 --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module 
 --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module 
 --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic 
 --with-http_perl_module=dynamic --add-dynamic-module=njs-0.1.0/nginx --with-threads --with-stream --with-stream_ssl_module 
 --with-stream_geoip_module=dynamic --with-http_slice_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 
 --with-http_v2_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong 
 --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic'

 

如果你在输出中没有找到–with-http_stub_status_module那你或许需要重新编译你的Nginx,或者安装Epel源中的版本

 

现在,增加nginx status到默认的虚拟主机内

location /nginx_status {
    stub_status on;    # activate stub_status module
    access_log off;    
    allow 127.0.0.1;   # restrict access to local only
    deny all;
}

 

当你完成之后,重新加载你的Nginx配置文件

sudo systemctl reload nginx

 

现在我们来配置Munin-node

使用任意一个你喜欢的文件编辑器,打开/etc/munin/plugin-conf.d/munin-node 增加以下内容(如果不存在)

[nginx_*]
env.url http://localhost/nginx_status

 

如果你的Nginx不是跑在80端口上或者绑定的是其他的域名请自行修改env.url

现在来更新Munin-node的插件

执行 munin-node-configure –suggest –shell | bash -x 将会自动链接Nginx插件.

[root@jp02 ~]# munin-node-configure --suggest --shell | bash -x
# The following plugins caused errors:
# http_loadtime:
# 	Junk printed to stderr
# pgbouncer_connections:
# 	Junk printed to stderr
# pgbouncer_requests:
# 	Junk printed to stderr
# postgres_autovacuum:
# 	Non-zero exit during autoconf (255)
# postgres_bgwriter:
# 	Non-zero exit during autoconf (255)
# postgres_cache_:
# 	Non-zero exit during autoconf (255)
# postgres_checkpoints:
# 	Non-zero exit during autoconf (255)
# postgres_connections_:
# 	Non-zero exit during autoconf (255)
# postgres_connections_db:
# 	Non-zero exit during autoconf (255)
# postgres_locks_:
# 	Non-zero exit during autoconf (255)
# postgres_oldest_prepared_xact_:
# 	Non-zero exit during autoconf (255)
# postgres_prepared_xacts_:
# 	Non-zero exit during autoconf (255)
# postgres_querylength_:
# 	Non-zero exit during autoconf (255)
# postgres_scans_:
# 	Non-zero exit during autoconf (255)
# postgres_size_:
# 	Non-zero exit during autoconf (255)
# postgres_transactions_:
# 	Non-zero exit during autoconf (255)
# postgres_tuples_:
# 	Non-zero exit during autoconf (255)
# postgres_users:
# 	Non-zero exit during autoconf (255)
# postgres_xlog:
# 	Non-zero exit during autoconf (255)
# proc:
# 	In family 'auto' but doesn't have 'autoconf' capability
# redis_:
# 	Junk printed to stderr
# slony_lag_:
# 	Junk printed to stderr
+ ln -s /usr/share/munin/plugins/nginx_request /etc/munin/plugins/nginx_request
+ ln -s /usr/share/munin/plugins/nginx_status /etc/munin/plugins/nginx_status

 

最后,重启Munin-node

sudo systemctl restart munin-node

 

稍等片刻之后即可在Munin的页面中看到Nginx的图表

发表评论

电子邮件地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据