数据从metrics取值。所以需要在etcd配置文添加此配置
ETCD_LISTEN_METRICS_URLS="http://0.0.0.0:2381"
监控脚本,监控项定义在item_dict字典里,想要监控其实项可以直接在item_dict里添加,zabbix模板配置使用自动发现的方式
# -*- coding:utf-8 -*-
'''
@Author: XGL
@File: etcd_item_discovery.py
@CreateTime: 2021/12/13 16:5
'''
import requests
import re
import sys,json
url = 'http://127.0.0.1:2381/metrics'
item_dict={
'etcd.range.rate':'etcd_debugging_mvcc_range_total',#The number of ranges seen by this member per second.
'etcd.txn.rate':'etcd_debugging_mvcc_txn_total',#Total number of txns seen by this member
'etcd.events.sent.rate':'etcd_debugging_mvcc_events_total',#The number of events sent by this member per second
'etcd.uptime':'process_start_time_seconds',#Etcd server uptime.
'etcd.has.leader':'etcd_server_has_leader',#Whether or not a leader exists. 1 is existence, 0 is not.
'etcd.leader.changes':'etcd_server_leader_changes_seen_total',# The the number of leader changes the member has seen since its start.
'etcd.proposals.committed.rate':'etcd_server_proposals_committed_total', #The number of consensus proposals committed.
'etcd.proposals.applied.rate':'etcd_server_proposals_applied_total',#The number of consensus proposals applied.
'etcd.proposals.failed.rate':'etcd_server_proposals_failed_total',#The number of failed proposals seen.
'etcd.proposals.pending':'etcd_server_proposals_pending',# The current number of pending proposals to commit.
'etcd.reads.rate.get':'etcd_debugging_store_reads_total{action="get"}', #Number of reads action by (get/getRecursive), local to this member.
'etcd.reads.rate.getRecursive':'etcd_debugging_store_reads_total{action="getRecursive"}',#Number of reads action by (get/getRecursive), local to this member.
'etcd.writes.rate':'etcd_debugging_store_writes_total{action="set"}', #Number of writes (e.g. set/compareAndDelete) seen by this member.
'etcd.network.grpc.received.rate':'etcd_network_client_grpc_received_bytes_total', #The number of bytes received from grpc clients per second
'etcd.keys.total':'etcd_debugging_mvcc_keys_total',#Total number of keys.
'etcd.virtual.bytes':'process_virtual_memory_bytes',#Virtual memory size in bytes.
'etcd.res.bytes': 'process_resident_memory_bytes', # Resident memory size in bytes.
'etcd.cpu.util':'process_cpu_seconds_total',#Total user and system CPU time spent in seconds.
'etcd.open.fds':'process_open_fds',#Number of open file descriptors.
'etcd.max.fds':'process_max_fds',#The Maximum number of open file descriptors.
'etcd.put.rate':'etcd_debugging_mvcc_put_total',#The number of puts seen by this member per second.
'etcd.keys.compacted.rate':'etcd_debugging_mvcc_db_compaction_keys_total',#The number of DB keys compacted per second.
}
def get_item(item):
value=item_dict.get(item)
try:
response=requests.get(url)
pattern=r'^{}\s(.*)'.format(value)
result=re.search(pattern,response.text,re.M).group(1)
strings=str(result)
print(eval(strings))
except Exception:
print(0)
def get_leader(item):
try:
response=requests.get(url)
pattern=r'^{}\s(.*)'.format(item)
result=re.search(pattern,response.text,re.M).group(1)
strings=str(result)
print(eval(strings))
except Exception:
print(0)
def discovery():
itemname=list()
for k in item_dict.keys():
data={"{#NAME}":k}
itemname.append(data)
discovername = {"data": itemname}
print(json.dumps(discovername, sort_keys=True, indent=3, ensure_ascii=False))
def main():
if sys.argv[1]=='discovery':
discovery()
elif sys.argv[1]=='isleader':
get_leader('etcd_server_is_leader')
else:
get_item(sys.argv[1])
if __name__ == '__main__':
try:
main()
except Exception:
print('输入有误')
zabbix_agent添加配置文件如:
#/data/conf/zabbix-agentd/zabbix_params/etcd/zbx_etcd.conf
UserParameter=template_etcd,/bin/python /data/sh/zabbix_scripts/etcd/etcd_discovery.py discovery
UserParameter=is_leader,/bin/python /data/sh/zabbix_scripts/etcd/etcd_discovery.py isleader
UserParameter=etcd_get[*],/bin/python /data/sh/zabbix_scripts/etcd/etcd_discovery.py $1
模板配置:
is_etcd_leader 这个item,当etcd作为leader时为1,否则为0,数据变化时即是etcd leader 切换
发现规则