zabbix监控nginx
2019-11-30 本文已影响0人
互联网中的一个咸鱼
1、安装软件以及修改配置文件
首先安装nginx,可以直接yum安装nginx
yum -y install nginx
安装完nginx之后,修改配置文件
vim /etc/nginx/nginx.conf
server内添加如下信息
location /stub_status {
allow 127.0.0.1;
stub_status on;
}
执行如下命令
/usr/sbin/nginx
curl http://127.0.0.1/stub_status
输出如下消息
Active connections: 1
server accepts handled requests
33672 33672 33672
Reading: 0 Writing: 1 Waiting: 0
2、编写自定义键值
vim /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf
UserParameter=nginx.status[*],sh /etc/zabbix/zabbix_agentd.d/nginx.sh $1
对应的nginx.sh脚本
HOST="127.0.0.1"
PORT="80"
stub_status=stub_status
function active() {
/usr/bin/curl -s "http://$HOST:$PORT/${stub_status}/" 2>/dev/null| grep 'Active' | awk '{print $NF}'
}
function accepts() {
/usr/bin/curl -s "http://$HOST:$PORT/${stub_status}/" 2>/dev/null| awk NR==3 | awk '{print $(1)}'
}
function handled() {
/usr/bin/curl -s "http://$HOST:$PORT/${stub_status}/" 2>/dev/null| awk NR==3 | awk '{print int($2)}'
}
function requests() {
/usr/bin/curl -s "http://$HOST:$PORT/${stub_status}/" 2>/dev/null| awk NR==3 | awk '{print int($3)}'
}
function reading() {
/usr/bin/curl -s "http://$HOST:$PORT/${stub_status}/" 2>/dev/null| grep 'Reading' | awk '{print int($2)}'
}
function writing() {
/usr/bin/curl -s "http://$HOST:$PORT/${stub_status}/" 2>/dev/null| grep 'Writing' | awk '{print int($4)}'
}
function waiting() {
/usr/bin/curl -s "http://$HOST:$PORT/${stub_status}/" 2>/dev/null| grep 'Waiting' | awk '{print int($6)}'
}
function ping() {
/sbin/pidof nginx |wc -l
}
if [ -z $1 ];then
echo $"Usage $0 {active|accepts|handled|requests|reading|writing|waiting}"
exit
fi
$1
pidof命令可以通过yum安装sysvinit-tools-2.88-14.dsf.el7.x86_64
现在,自定义键值已经准备完毕,接下来就是创建监控项
3、导入模块,创建监控项
nginx模块链接 https://www.ixdba.net/zabbix/zabbix-nginx.zip
导入模块之后在对应的主机上添加nignx模块
添加完模块之后会看到如下几个监控项
监控项
4、查看监控项
我们可以以一个监控项为例子,查看他是如何工作的
监控项
我们可以看到 导入模块内的监控项还是依赖与我们自定义的键值与编写的脚本
所以 最重要的是脚本
对nginx的监控我们先了解到这里,想要了解更多信息,请关注我 持续更新哦
补充,下面给大家提供了对应的python脚本
import sys
import requests
import re
help_msg = """
usage:
{} [active,accepts,handled,requests,reading,writing,waiting]
""".format(sys.argv[0])
def usage():
print(help_msg)
if len(sys.argv) < 2:
usage()
try:
r = requests.get(url='http://127.0.0.1:80/stub_status/')
isAlive = r.status_code
except Exception as e:
print(e)
data = {}
lists = ["active", "accepts", "handled",
"requests", "reading", "writing", "waiting"]
num = 0
for text in re.findall('\d+', r.text):
data[lists[num]] = text
num += 1
for i in sys.argv[1:]:
if not i in lists:
usage()
exit()
for i in sys.argv[1:]:
print(data[i])