Press "Enter" to skip to content

Zabbix监控Nginx

主机环境:
[root@test ~]# uname -a
Linux test 3.10.0-693.el7.x86_64 #1 SMP Thu Jul 6 19:56:57 EDT 2017 x86_64 x86_64 x86_64 GNU/Linux
[root@test ~]# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 7.4 (Maipo)
[root@test ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.139.132 netmask 255.255.255.0 broadcast 192.168.139.255
ether 00:0c:29:ca:b7:70 txqueuelen 1000 (Ethernet)
RX packets 356 bytes 41989 (41.0 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 224 bytes 41057 (40.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
Zabbix版本:3.4.9
nginx版本:1.14.0
主机防火墙和selinux关闭
Zabbix yum安装参考:https://www.tracymc.cn/archives/614,编译安装可参考https://www.tracymc.cn/archives/218.
nginx安装参考:https://www.tracymc.cn/archives/431
nginx模板地址:https://github.com/adubkov/zbx_nginx_template
前提条件:
主机安装了python(因为脚本是python写的)、nginx编译参数包含http_stub_status_module并且开启了access.log
查看nginx编译参数:
[root@test zabbix]# nginx -V
nginx version: nginx/1.14.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure 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-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_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 -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

具体步骤:
1.下载脚本并配置相关参数
[root@test home]# wget https://github.com/adubkov/zbx_nginx_template/archive/master.zip
[root@test home]# unzip master.zip
[root@test home]# cd zbx_nginx_template-master/
[root@test zbx_nginx_template-master]# ll
总用量 56
-rwxr-xr-x 1 root root 1979 12月 16 11:28 README.md
-rwxr-xr-x 1 root root 5614 12月 16 11:28 zbx_nginx_stats.py
-rwxr-xr-x 1 root root 42775 12月 16 11:28 zbx_nginx_templates.xml
将zbx_nginx_stats.py复制到zabbix脚本执行目录,如下:
[root@test zbx_nginx_template-master]# cp zbx_nginx_stats.py /etc/zabbix/scripts/ //根据自己情况定义脚本路径
修改zbx_nginx_stats.py内容:
zabbix_host = '127.0.0.1' # Zabbix server IP
zabbix_port = 10051 # Zabbix server port
hostname = 'Zabbix server' # Name of monitored host, like it shows in zabbix web ui,这里的名字一定要和web页面配置的名字一样!!!!!!不然可能会没有数据.如果web配置的名字和主机名不一样的话,以web名字为准!!!!!!
time_delta = 1 # grep interval in minutes

# URL to nginx stat (http_stub_status_module)
stat_url = 'http://127.0.0.1:8080/nginx_stat'

# Nginx log file path
nginx_log_file_path = '/var/log/nginx/access.log'

# Optional Basic Auth
username = 'user'
password = 'pass'

# Temp file, with log file cursor position
seek_file = '/tmp/nginx_log_stat'

[root@test scripts]# chmod u+x zbx_nginx_stats.py //赋予执行权限

2.修改nginx配置
nginx配置文件增加如下配置(在server段):
location /nginx_stat {
stub_status on; # Turn on nginx stats
access_log off; # We do not need logs for stats
allow 127.0.0.1; # Security: Only allow access from IP
allow ::1; # Security IPv6: allow only from localhost
deny all; # Deny requests from the other of the world
}
截图如下:
[root@test conf.d]# nginx -t //验证配置文件是否正确
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@test conf.d]# systemctl restart nginx //重启下
主机浏览器打开http://127.0.0.1:8080/nginx_stat验证nginx配置修改是否生效,如下:

3.设置crontab
[root@test scripts]# crontab -l
*/1 * * * * /etc/zabbix/scripts/zbx_nginx_stats.py

4.导入模板
nginx模板地址:https://github.com/adubkov/zbx_nginx_template
导入下载模板,如下:
导入成功后可以在配置-模板查看已导入的模板,如下:

5.关联模板
添加模板:
配置-主机点击相关主机进去配置模板,如下:

6.zabbix web验证是否有数据
关联模板后,稍等一会,查看web页面是否有数据.
部分截图如下:

注:模板没有设置触发器,可自行设定.

Be First to Comment

发表评论

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