随笔 - 911  文章 - 5  评论 - 94  阅读 - 243万

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

 

posted on   momingliu11  阅读(358)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示