|
说明:
使用zabbix监控nginx,首先nginx需要配置ngx_status
在编译安装nginx时需要使用--with-http_stub_status_module参数
在nginx的配置文件nginx.conf里添加如下:
1
2
3
4
5
6
7
8
| location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
allow 27.115.xxx.xxx;
allow 211.95.xxx.xxx;
deny all;
}
|
注意:
自定义key(键值)参考:http://www.ttlsa.com/zabbix/zabbix-user-parameters/
方法一:使用shell实现:
zabbix客户端:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
| cd /root/scripts
vim ngx_status.sh
#!/bin/bash
# Description:zabbix监控nginx性能以及进程状态
# Note:此脚本需要配置在被监控端
HOST="192.168.1.92"
PORT="80"
# 检测nginx性能
function active {
/usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null| grep 'Active' | awk '{print $NF}'
}
function reading {
/usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null| grep 'Reading' | awk '{print $2}'
}
function writing {
/usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null| grep 'Writing' | awk '{print $4}'
}
function waiting {
/usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null| grep 'Waiting' | awk '{print $6}'
}
function accepts {
/usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null| awk NR==3 | awk '{print $1}'
}
function handled {
/usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null| awk NR==3 | awk '{print $2}'
}
function requests {
/usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null| awk NR==3 | awk '{print $3}'
}
# 执行function
$1
chmod +x /root/scripts/ngx_status.sh
|
1
2
3
4
5
6
| ##将自定义的UserParameter加入配置文件,然后重启agentd
vim /opt/zabbix/etc/zabbix_agentd.conf
添加:
UserParameter=nginx.status,/root/scripts/ngx_status.sh $1
/etc/init.d/zabbix_agentd restart
|
1
2
3
4
| ##服务端:
##zabbix_get测试:
[iyunv@localhost ~]# /opt/zabbix/bin/zabbix_get -s 192.168.1.92 -k nginx[requests]
43232
|
方法二:使用python实现:
zabbix客户端:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
| cd /root/scripts
vim ngx_status.py
#!/usr/bin/env python
#encoding:utf-8
import sys
import requests
r = requests.get('http://192.168.1.92/nginx_status')
statusList = r.text.split()
def connections():
connections = statusList[2]
print connections
def accepts():
accepts = statusList[7]
print accepts
def handled():
handled = statusList[8]
print handled
def requests():
requests = statusList[9]
print requests
def Reading():
Reading = statusList[11]
print Reading
def Writing():
Writing = statusList[13]
print Writing
def Waiting():
Waiting = statusList[15]
print Waiting
fun_name = eval(sys.argv[1]) #将字符串类型转换为函数类型
fun_name()
chmod +x /root/scripts/ngx_status.sh
|
1
2
3
4
5
6
| ##将自定义的UserParameter加入配置文件,然后重启agentd
vim /opt/zabbix/etc/zabbix_agentd.conf
添加:
UserParameter=nginx.status,/root/scripts/ngx_status.sh $1
/etc/init.d/zabbix_agentd restart
|
1
2
3
4
| #服务端:
##zabbix_get测试:
[iyunv@localhost ~]# /opt/zabbix/bin/zabbix_get -s 192.168.1.92 -k nginx[requests]
43232
|
最后在zabbix的web界面操作:
创建模板--->创建分组--->创建监控项--->创建触发器--->创建图形
注:
附件有一个模板
nginx status.zip
(557 Bytes, 下载次数: 6)
|
|
|
|
|
|
|