需求
假设一台服务器运行了N个vhost网站,如何确定在大流量并发时候找到是哪个网站的问题呢?
这似乎是每个运维都会遇到的问题,方法有很多比如:1、看nginx日志大小确定访问量。2、通过前端代理确定访问量。3、通过防火墙,waf等工具确定访问量。4、通过elk日志,splunk日志分析系统等等
这里讲一个nginx的模块利用方法:Nginx Vhost Traffic Status
添加nginx模块
添加nginx模块的方法有很多,比如 https://blog.csdn.net/zyw_java/article/details/80558320
注意生产环境不要覆盖make install
最后安装完大概是这个样子,我们要手动取得里面5xx错误条数,进行监控出图。我来做一块砖,给大家一个思路。其他的功能实现照葫芦画瓢就行。
获取他的JSON格式文件,用python解析并汇报到zabbix里
JSON路径为:http://域名或者ip/status/format/json
为了安全起见建议设置nginx的allow和deny
例如下面:
location /status {
vhost_traffic_status_display;
vhost_traffic_status_display_format html;
allow 192.168.80.0/28;
allow 149.60.60.0/24;
deny all;
}
}
创建zabbix模板
在zabbix点配置>模板>创建模板>自动发现规则>创建发现规则
创建监控项原型
创建图形原型
zabbix_agent被控端添加
UserParameter=nginx.response[*],/usr/bin/python3 /etc/zabbix/zabbix_agentd.d/GetNginxStatus.py http://192.168.80.10/status/format/json $1
UserParameter=nginx.site.discovery,/usr/bin/python3 /etc/zabbix/zabbix_agentd.d/GetNginxStatus.py http://192.168.80.10/status/format/json
这个变量学会了,可以在zabbix web界面传递变量,无需每台zabbix_agentd手动设置不同的url。
也可以传递其他变量,让他不仅仅只能监控5xx的错误信息。思路给你们了,具体实现看你能力了。
GetNginxStatus.py代码内容如下
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Site:
# @File: GetJson.py
# @Author: GuoYabin
# @E-mail: hbbdgyb@163.com
# @Time: 12月 13, 2019
import requests
import sys
import json
from datetime import datetime
url = sys.argv[1].strip()
res = requests.get(url)
d = json.loads(res.text)
serverZones = d['serverZones']
def connectionsinfo():
connections=d['connections']
for key,value in connections.items():
print (key,value)
def uptime():
loadMsec = datetime.utcfromtimestamp(d['loadMsec'] / 1000)
nowMsec = datetime.utcfromtimestamp(d['nowMsec'] / 1000)
active = nowMsec - loadMsec
print (active.total_seconds())
def nginxversion():
nginxversion = d['nginxVersion']
print (nginxversion)
def servername():
for servername in serverZones:
if servername == '*':
print('\t\t{"{#SITEDOMAIN}":"'+servername+'"}')
else:
print('\t\t{"{#SITEDOMAIN}":"'+servername+'"},')
def response(key1,key2):
for servername,value in serverZones.items():
for i,v in value.items():
if servername == key2 and i == 'responses':
print (v[key1])
def jsonservername():
print('{\n\t"data":[')
servername()
print('\t]\n}')
if __name__ =='__main__':
try:
domain = sys.argv[2].strip()
response('5xx', domain)
except:
jsonservername()
利用zabbix_get检查返回值
zabbix_get命令是在server端用来检查agent端的一个命令,在添加完主机或者触发器后,不能正常获得数据,可以用zabbix_get来检查能否采集到数据,以便判断问题症结所在。
zabbix_get 参数说明:
-s --host: 指定客户端主机名或者IP
-p --port:客户端端口,默认10050
-I --source-address:指定源IP,写上zabbix server的ip地址即可,一般留空,服务器如果有多ip的时候,你指定一个。
-k --key:你想获取的key
zabbix_sender是一个命令行工具,可以用来发送Zabbix服务器处理性能数据。该工具通常用于长时间运行的用户脚本,用于定期发送可用性和性能数据。
参数说明:
-c --config <file> 配置文件绝对路径
-z --zabbix-server <server> zabbix server的IP地址
-p --port <server port> zabbix server端口.默认10051
-s --host <hostname> 主机名,zabbix里面配置的主机名(不是服务器的hostname),不能使用ip地址
-I --source-address <IP address> 源IP
-k --key <key> 监控项的key
-o --value <key value> key值
-i --input-file <input file> 从文件里面读取hostname、key、value 一行为一条数据,使用空格作为分隔符,如果主机名带空格,那么请使用双引号包起来
-T --with-timestamps 一行一条数据,空格作为分隔符: <hostname> <key> <timestamp> <value>,配合 --input-file option,timestamp为unix时间戳
-r --real-time 将数据实时提交给服务器
-v --verbose 详细模式, -vv 更详细
来源:oschina
链接:https://my.oschina.net/u/4279204/blog/4080268