全部博文(150)
分类: 系统运维
2018-11-05 14:36:55
都是为了自动监控新加入的主机和设备
序号 | 名称 | 主要执行点 | 配置点 | 适用范围 |
---|---|---|---|---|
1 | discovery | server | discovery rule && action | 网络设备等不能安装zabbix agent的主机 |
2 | registration | client | agentd.conf {ServerActive && HostMetadataItem=system.uname | 能安装zabbix agent的主机 |
配置策略
配置示例
需求是将192.168.1.0/24 网段内能ping通的主机放到discovery组
1、Configuration---Create discovery rule输入如下字段
Checks - New # 探测器设置
Device uniqueness criteria : IP address #设备唯一识别符
2、Configuration---Actions---Event Source --- auto registration --- Create action
Operations
Default message:
Host name: {HOST.HOST} Host IP: {HOST.IP} Agent port: {HOST.PORT}
Operations: Send message to user groups:
Zabbix administrators via all media Add host Add to host groups: Linux Link to templates: Template OS Linux
配置逻辑
客户端通过agent自动上报注册
zabbix_agentd.conf 包含
ServerActive=zabbix_server_name or ip ##server的域名或者ip HostMetadataItem=system.uname ##供server端匹配用
服务端根据上报和注册自动将客户端增加到监控范围之内
Configuration---Actions- EVent source -- Auto registration
配置示例大部分同上,略
监控项目不固定
例如 nginx 后端服务,有的多,有的少
监控项目
注:模板文件中的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"}}
要求是分析每个微服务的health返回的json串中的status字段,依据其内容判断服务的健康情况
因为各项目使用的微服务数量不同,且使用的端口号也不尽相同,因此使用discovery规则的模板就是非常合适选项了
centos7.4
zabbix 3.4
python脚本
配置客户端的zabbix_agent.conf
cd /etc/zabbix/zabbix_agentd.d/ cat UserParameter=goluk.spring.http,/etc/zabbix/scripts/discovery_spring_http.py >>spring_http.conf
systemctl restart zabbix-agent
撰写自动发现的脚本
#!/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"}]}]
各个配置中出现的Key的关系
自动发现规则中出现的“xxx.spring.http[{#SNAME2}]“
[{#变量名称}] 这种写法是zabbix 自定义宏变量,按照官方文档的说法,是在自动发现规则中使用的宏,可以返回文件系统名称、网络接口和snmp oids以及创建自动发现的主机和主机组。 宏可用来创建监控项、触发器和图形原型。当发现真实的文件系统、网络接口后,这些宏将被替换成为真实的值,并且以这些值来创建真实的监控项、触发器和图形。 因此 xxx.spring.http[{#SNAME2}]在本示例中就替换成 xxx.spring.http[xxx-core-carbox.http]这样的监控项
配置文件出现的“UserParameter=xxx.spring.http”
配置文件中出现的Key,必须和上面的Key相同,表明或者该Key值需要执行的脚本
脚本中出现的“r["data"].append({"{#SNAME2}":sname, "{#STATUS}":sstatus})”
按照zabbix官方规定,自动发现规则向服务器传值,必须按照规定json格式传递的宏和值对,例如 项目“net.if.discovery”可能会返回两对键值:“{#IFNAME}”→“lo”和“{#IFNAME}”→“eth0”。
自定义发现的宏变量的值和宏变量所代表的监控项目的实际值的关系
配置文件中的“UserParameter=xxx.spring.http” 只能传递该宏变量的实际值,也就是自动发现的实际项目名字
例如本例中自动发现规则配合自定义脚本传递给zabbix server 的宏变量的实际内容 {#SNAME2}": "xxx-core-system.http",...} ,如果通过该方法直接传递 xxx.core-system.http 的状态是UP还是DOWN,是不可以的,只能通过zabbix agent 或者zabbix trapper传递它的值。
传递 宏变量实际值的方法
在自定义配置里面增加 "UserParameter=xxx.spring.http[*], /x/x/x.sh $1" 的配置,xxx.spring.http表明是唯一的监控项Key, [*]定义该Key接收括号内的参数。
自定义定时脚本定时通过zabbix_sender传递给server; 通过上述的自动发现脚本,先传递宏变量及其实际名称的值对,再调用zabbix_sender传递实际监控和实际监控项的值。
自动发现脚本完成的两个功能