zabbix监控多个nginx vhost网站状态码

需求html

假设一台服务器运行了N个vhost网站,如何肯定在大流量并发时候找到是哪一个网站的问题呢?前端

这彷佛是每一个运维都会遇到的问题,方法有不少好比:一、看nginx日志大小肯定访问量。二、经过前端代理肯定访问量。三、经过防火墙,waf等工具肯定访问量。四、经过elk日志,splunk日志分析系统等等java

这里讲一个nginx的模块利用方法:Nginx Vhost Traffic Statuspython

 

添加nginx模块nginx

添加nginx模块的方法有不少,好比  https://blog.csdn.net/zyw_java/article/details/80558320web

注意生产环境不要覆盖make installjson

最后安装完大概是这个样子,咱们要手动取得里面5xx错误条数,进行监控出图。我来作一块砖,给你们一个思路。其余的功能实现照葫芦画瓢就行。安全

 

 获取他的JSON格式文件,用python解析并汇报到zabbix里bash

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 更详细

  

 字数很少,句句精髓。但愿你们能明白zabbix自动发现规则 灵活运用
相关文章
相关标签/搜索