监控LVS

监控LVS

#!/usr/bin/python-2.6.6
#data 2017-10-17
#auth liuchao
import commands,os,time
#------------------------------------------------
zabbix_agent_file='/etc/zabbix/zabbix_agentd.conf'
keepalived_vip=['192.168.41.157']
tmp_file_path='/tmp/lvs_status.txt'
zabbix_server_port=10051
timestamp=int(time.time())
zabbix_server=commands.getstatusoutput('''grep '^ServerActive' %s|awk -F '=' '{print $2}' ''' %zabbix_agent_file)[1].strip()
zabbix_hostname=commands.getstatusoutput('''grep '^Hostname' %s|awk -F '=' '{print $2}' ''' %zabbix_agent_file)[1].strip()
#------------------------------------------------
def monit_lvs():
        lvs_packets_sec=commands.getstatusoutput('''tail -n 1 /proc/net/ip_vs_stats | awk '{print strtonum("0x"$1),strtonum("0x"$2),strtonum("0x"$3),strtonum("0x"$4),strtonum("0x"$5)}
' |awk '{print $2}' ''')[1]
        lvs_bit_sec=commands.getstatusoutput('''tail -n 1 /proc/net/ip_vs_stats | awk '{print strtonum("0x"$1),strtonum("0x"$2),strtonum("0x"$3),strtonum("0x"$4),strtonum("0x"$5)}' |a
wk '{print $4}' ''')[1]
        lvs_conns_sec=commands.getstatusoutput('''awk 'END{print NR-1}' /proc/net/ip_vs_conn | awk '{print $1}' ''')[1]
        lvs_keepalived_vip_status=commands.getstatusoutput('/sbin/ip addr |grep %s |wc -l' %keepalived_vip[0])[1]
        if int(lvs_keepalived_vip_status) != 0:
                status,result_ip=commands.getstatusoutput('''echo %s |awk -F '.' '{print $NF}' ''' %keepalived_vip[0])
                try:
                        lvs_keepalived_vip_status=int(result_ip)
                except:
                        pass
        with open(tmp_file_path,'wb') as f:
                f.write('%s %s %s %s\n'%(zabbix_hostname,'lvs_packets_sec',timestamp,lvs_packets_sec))
                f.write('%s %s %s %s\n'%(zabbix_hostname,'lvs_bit_sec',timestamp,lvs_bit_sec))
                f.write('%s %s %s %s\n'%(zabbix_hostname,'lvs_conns_sec',timestamp,lvs_conns_sec))
                f.write('%s %s %s %s\n'%(zabbix_hostname,'lvs_keepalived_vip_status',timestamp,lvs_keepalived_vip_status))
#-----------------------------------------------------
if __name__=='__main__':
        monit_lvs()
        send_data_cmd='/usr/bin/zabbix_sender -vv -z %s -p %s -T -i %s' %(zabbix_server,zabbix_server_port,tmp_file_path)
        #print send_data_cmd
        os.popen(send_data_cmd)

 

posted @ 2017-12-20 15:36  刘小神  阅读(561)  评论(0编辑  收藏  举报