Zabbix监控LVS
该功能用来监控lvs各端口连接数、vip状态、接收流量
发现lvs ports:
#!/usr/bin/env python #encoding=utf8 #function:Discover lvs ports from keepalived.conf import sys,commands,json #cmd_res = commands.getstatusoutput('''awk '{print $7}' /proc/net/ip_vs_conn |sort |uniq |grep -v DPrt |grep -v FFFF''')[1] cmd_res = commands.getstatusoutput('''grep 'virtual_server' /etc/keepalived/keepalived.conf |awk '{print $3}' ''')[1] # cmd_res返回结果格式如下: # 25 # 80 # 443 ports_list = cmd_res.split('\n') ports_all = [] for item in ports_list: ports_all += [{'{#PORT}':item.strip()}] zbx_res = (json.dumps({'data':ports_all},sort_keys=True,separators=(',',':'))) print zbx_res
发现lvs vip:
#!/usr/bin/env python #encoding=utf8 #function:Discover lvs vip from keepalived.conf import sys,commands,json #cmd_res = commands.getstatusoutput(''' ip addr |grep inet |grep global |grep -v brd |awk '{print $2}' |awk -F '/' '{print $1}' ''')[1] cmd_res = commands.getstatusoutput(''' grep -A 1 'virtual_ipaddress' /etc/keepalived/keepalived.conf |egrep '172|10' ''')[1] #根据实际IP段去查找vIP if cmd_res is not None: vip = cmd_res.strip() vip_list = [{'{#VIP}':vip}] zbx_res = (json.dumps({'data':vip_list},sort_keys=True,separators=(',',':'))) print zbx_res
监控lvs端口和vip状态:
#!/usr/bin/env python #encoding=utf8 #function:Get lvs ports、vip value import sys,commands #check if there is argv from exec script if len(sys.argv) <> 2: exit() arg = sys.argv[1] #Get Incoming bytes value if arg == 'bitsec': res = commands.getstatusoutput('''tail -1 /proc/net/ip_vs_stats | /usr/bin/awk '{print strtonum("0x"$1),strtonum("0x"$2), strtonum("0x"$3),strtonum("0x"$4), strtonum("0x"$5)}'|awk '{print $4}' ''')[1] #Get vip value elif len(arg.split('.')) == 4: res = commands.getstatusoutput('ip addr |grep %s | wc -l' %arg)[1] #Get lvs connection for every port else: try: arg = int(arg) conn_port = (str((hex(int(arg))[2:]).upper())).rjust(4,'0') #print conn_port res = commands.getstatusoutput('fgrep %s /proc/net/ip_vs_conn |wc -l ' %conn_port)[1] except Exception as ErrorMsg: pass print res
Zabbix.agent.conf配置:
Timeout=30 UserParameter=discovery_lvs_port,python /etc/zabbix/script/discovery_lvs_port.py UserParameter=discovery_lvs_vip,python /etc/zabbix/script/discovery_lvs_vip.py UserParameter=check_lvs[*],python /etc/zabbix/script/check_lvs.py $1
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?