zabbix discovery 和 auto registration 分析及应用

1880阅读 0评论2018-11-05 missing-cn
分类:系统运维

zabbix discovery 和 auto registration 分析及应用

两者相同点

都是为了自动监控新加入的主机和设备

不同点

序号 名称 主要执行点 配置点 适用范围
1 discovery server discovery rule && action 网络设备等不能安装zabbix agent的主机
2 registration client agentd.conf {ServerActive && HostMetadataItem=system.uname 能安装zabbix agent的主机

性能探讨

discovery 配置说明

Auto Registration 配置说明

巧用discovery规则监控变化较快的项目

使用场景

设计思路

注:模板文件中的key和配置文件中的key和脚本中的key必须名字相同

配置示例

需求

某项目开发使用了spring 微服务,每个类型的微服务都使用一个端口,可以通过取得某项微服务的监控情况

31199   xx-core-admin
31103   xx-core-carbox
31181   xx-core-dormancy
31197   xx-core-message
31102   xx-core-metadata
31180   k1-core-online
......
31901   xx-gw-service
31091   xx-ss-admin
{"description":"Spring Cloud Eureka Discovery Client",
"status":"UP",
"discoveryComposite":{"description":"Spring Cloud Eureka Discovery Client","status":"UP","discoveryClient":{"description":"Spring Cloud Eureka Discovery Client","status":"UP","services":["cs-admin","core-dormancy","core-admin","cs-carbox","ss-admin","core-track","cs-track","core-message","core-carbox","cs-user","core-system","eureka-cluster","core-online","core-metadata","gw-service","core-user","gw-admin","cs-system","cs-message"]},"eureka":{"description":"Remote status from Eureka server","status":"UP","applications":{"CS-ADMIN":1,"CORE-DORMANCY":1,"CORE-ADMIN":1,"CS-CARBOX":1,"SS-ADMIN":1,"CORE-TRACK":1,"CS-TRACK":1,"CORE-MESSAGE":1,"CORE-CARBOX":1,"CS-USER":1,"CORE-SYSTEM":1,"EUREKA-CLUSTER":3,"CORE-ONLINE":1,"CORE-METADATA":1,"GW-SERVICE":1,"CORE-USER":1,"GW-ADMIN":1,"CS-SYSTEM":1,"CS-MESSAGE":1}}},
"diskSpace":{"status":"UP","total":52709400576,"free":49455300608,"threshold":10485760},
"redis":{"status":"UP","version":"xxx"},
"mongo":{"status":"UP","xxxx"},
"refreshScope":{"status":"UP"},
"hystrix":{"status":"UP"}}

配置自动发现模板文件

 #!/bin/env python

import os
import re
import json
import requests
import subprocess
SPRING_ROOT = "/XXX"


if __name__ == "__main__":

    config_file = "%s/spring.conf" % (SPRING_ROOT)
    tmp_file = "/tmp/XXX_send_to_zabbix.tmp"
    r = {"data": []}
    ftmp = open(tmp_file,'w')
    with open(config_file) as f:
        for line in f:
        #print(line)
        y = line.split()
        if y:
                s_name  = y[1] + '.http'
        s_port  = y[0]
                url='' + s_port + '/health'
                resp = requests.get(url)
                #print resp.status_code
                if resp.status_code == 200 :
                  #print resp.text
                  json_data = json.loads(resp.text)
                  #print json_data.get("status")
                  s_status = json_data.get("status")
                else :
                  s_status = "DOWN"
                #print(s_name,s_port)
                ftmp.write("- " + "XXX.spring.http[" + s_name + "] " + s_status + "\n")
            r["data"].append({"{#SNAME2}":s_name, "{#STATUS}":s_status})
    ftmp.close()
    ## 本脚本唯一的输出是将下面的json传递给zabbix server
    print json.dumps(r, sort_keys=True)
    ## 需要将zabbix_sender的输出过滤掉,因为会破坏掉上面的传递给zabbix server的json串
    subprocess.call("zabbix_sender -z x.x.x.x -s xxx -i /tmp/k1_send_to_zabbix.tmp >/dev/null",stdout=None, stderr=None, shell=True) 

调试方法

./discovery_spring_http.py
{"data": [{"{#SNAME2}": "xxx-core-admin.http", "{#STATUS}": "UP"}, {"{#SNAME2}": "xxx-core-carbox.http", "{#STATUS}": "UP"}, {"{#SNAME2}": "xxx-core-dormancy.http", "{#STATUS}": "UP"}, {"{#SNAME2}": "xxx-core-message.http", "{#STATUS}": "UP"}, {"{#SNAME2}": "xxx-core-metadata.http", "{#STATUS}": "UP"}, {"{#SNAME2}": "xxx-core-online.http", "{#STATUS}": "UP"}, {"{#SNAME2}": "xxx-core-system.http", "{#STATUS}": "UP"}, {"{#SNAME2}": "k1-core-track.http", "{#STATUS}": "UP"}, .......... "xxx-ss-admin.http", "{#STATUS}": "UP"}]}
zabbix_agentd -p |grep spring.http
xxx.spring.http   [t|{"data": {"data": [{"{#SNAME2}": "xxx-core-admin.http", "{#STATUS}": "UP"}, {"{#SNAME2}": "xxx-core-carbox.http", "{#STATUS}": "UP"}, {"{#SNAME2}": "xxx-core-dormancy.http", "{#STATUS}": "UP"}, {"{#SNAME2}": "xxx-core-message.http", "{#STATUS}": "UP"}, {"{#SNAME2}": "xxx-core-metadata.http", "{#STATUS}": "UP"}, {"{#SNAME2}": "xxx-core-online.http", "{#STATUS}": "UP"}, {"{#SNAME2}": "xxx-core-system.http", "{#STATUS}": "UP"}, {"{#SNAME2}": "k1-core-track.http", "{#STATUS}": "UP"}, .......... "xxx-ss-admin.http", "{#STATUS}": "UP"}]}]
 10274:20180903:144902.385 EXECUTE_STR() command:'/etc/zabbix/scripts/discovery_spring_http.py' len:1015 cmd_result:'{"data": [{"{#SNAME2'
 10274:20180903:144902.385 Sending back [{"data": {"data": [{"{#SNAME2}": "xxx-core-admin.http", "{#STATUS}": "UP"}, {"{#SNAME2}": "xxx-core-carbox.http", "{#STATUS}": "UP"}, {"{#SNAME2}": "xxx-core-dormancy.http", "{#STATUS}": "UP"}, {"{#SNAME2}": "xxx-core-message.http", "{#STATUS}": "UP"}, {"{#SNAME2}": "xxx-core-metadata.http", "{#STATUS}": "UP"}, {"{#SNAME2}": "xxx-core-online.http", "{#STATUS}": "UP"}, {"{#SNAME2}": "xxx-core-system.http", "{#STATUS}": "UP"}, {"{#SNAME2}": "k1-core-track.http", "{#STATUS}": "UP"}, .......... "xxx-ss-admin.http", "{#STATUS}": "UP"}]}]

上述配置中的关键点说明

上一篇:zabbix开展自动化运维的一些探索之---zabbix remote command应用自动删除过期日志
下一篇:gitlab-ce 在docker swarm集群上的安装使用