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 2021-12-09 11:52  momingliu11  阅读(355)  评论(0编辑  收藏  举报