demand
Suppose a server running the N vhost website, how to determine when found in large concurrent flow problem which website is it?
This seems to be a problem for each operation and maintenance will be met, there are many ways such as: 1, look at the size of the log determine nginx visits. 2, is determined by the amount of front-end proxy access. 3, to determine traffic through the firewall, waf and other tools. 4, elk by a log, a log analysis system and the like Splunk
A module nginx speaking here of using the method: Nginx Vhost Traffic Status
Add nginx module
There are many ways to add nginx module, such as https://blog.csdn.net/zyw_java/article/details/80558320
Note Do not cover the production environment make install
Finally you have installed is probably like this, we have to manually get several errors inside 5xx, monitoring plot. I do a brick, give us an idea. Copy or imitate other functions to achieve on the line.
Get his JSON format, parsing with python and report to zabbix in
JSON path is: http: // domain or ip / status / format / json
For security reasons the recommended settings allow and deny nginx
Such as the following:
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;
}
}
Create a template zabbix
Zabbix point configuration> Templates> Creating Templates> Automatic discovery rules> Create discovery rules
创建监控项原型
创建图形原型
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: [email protected] # @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 更详细