yp1234 发表于 2015-11-23 13:06:12

zabbix企业应用之如何快速查看异常流量

最近机房总流量总是异常,然后我是不断的接到短信与电话报警,收到后通过cacti查看还是十分的麻烦与浪费时间,为了解决这个问题,我自己写了一个脚本,从数据库里获取所有主机监控数据,然后打印出流量超过10m的主机信息,这样能快速帮我判断异常流量主机。
脚本是使用python编写,使用MySQLdb从zabbix数据库里获取流量数据,经过流量判断后在把数据写入到excel里。
使用前需要安装MySQLdb、xlwt模块,可以使用easy_install安装。
下面是脚本内容

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102#!/usr/bin/env python#-*- coding: utf-8 -*-#author:Deng Lei#email: dl528888@gmail.comimport MySQLdbimport timeimport sysimport xlwtreload(sys)sys.setdefaultencoding('utf8') if __name__ == &quot;__main__&quot;:    now_hour=int(time.strftime('%H'))    old_hour=now_hour-1    now_time=time.strftime('%Y-%m-%d')    mysql_conn=MySQLdb.connect(host='10.10.14.11',user='zabbix',passwd='zabbix',port=3306,charset=&quot;utf8&quot;)    mysql_cur=mysql_conn.cursor()    mysql_conn.select_db('zabbix')    last_results=[]    in_results=[]    out_results=[]    network_device=['em2','eth1','eth0']    try:      room=sys.argv    except IndexError:    room='all'    try:    old_hour=sys.argv    except IndexError:    old_hour=now_hour-1    try:      now_hour=sys.argv    except IndexError:      now_hour=int(time.strftime('%H'))    if room == 'all':      #search network in traffic      for i in network_device:            search_sql=&quot;select from_unixtime(hi.clock,'%%Y-%%m-%%d %%T') as Date,g.name as Group_Name,h.host as Host,round(max(hi.value_max)/1000,0) as Networkfrom hosts_groups hg join groups g on g.groupid = hg.groupid join items i on hg.hostid = i.hostid join hosts h on h.hostid=i.hostid join trends_uint hi oni.itemid = hi.itemidwherei.key_='net.if.in[%s]' andhi.clock >= UNIX_TIMESTAMP('%s %s:00:00') andhi.clock < UNIX_TIMESTAMP('%s %s:00:00') group by h.host;&quot;%(i,now_time,old_hour,now_time,now_hour)            n=mysql_cur.execute(search_sql)            result=mysql_cur.fetchall()            for ii in result:            msg1={'Group_Name':ii,'Host':ii,'Network_device':i,'Source':'In'}            if msg1 not in in_results:            in_results.append(msg1)                msg={'Date':ii,'Group_Name':ii,'Host':ii,'Network':float(ii),'Network_device':i,'Source':'In'}                last_results.append(msg)      #search network out traffic      for i in network_device:            search_sql=&quot;select from_unixtime(hi.clock,'%%Y-%%m-%%d %%T') as Date,g.name as Group_Name,h.host as Host,round(max(hi.value_max)/1000,0) as Networkfrom hosts_groups hg join groups g on g.groupid = hg.groupid join items i on hg.hostid = i.hostid join hosts h on h.hostid=i.hostid join trends_uint hi oni.itemid = hi.itemidwherei.key_='net.if.out[%s]' andhi.clock >= UNIX_TIMESTAMP('%s %s:00:00') andhi.clock < UNIX_TIMESTAMP('%s %s:00:00') group by h.host;&quot;%(i,now_time,old_hour,now_time,now_hour)            n=mysql_cur.execute(search_sql)            result=mysql_cur.fetchall()            for ii in result:                msg1={'Group_Name':ii,'Host':ii,'Network_device':i,'Source':'out'}                if msg1 not in out_results:                  out_results.append(msg1)                  msg={'Date':ii,'Group_Name':ii,'Host':ii,'Network':float(ii),'Network_device':i,'Source':'out'}                  last_results.append(msg)    else:      #search network in traffic      for i in network_device:            search_sql=&quot;select from_unixtime(hi.clock,'%%Y-%%m-%%d %%T') as Date,g.name as Group_Name,h.host as Host,round(max(hi.value_max)/1000,0) as Networkfrom hosts_groups hg join groups g on g.groupid = hg.groupid join items i on hg.hostid = i.hostid join hosts h on h.hostid=i.hostid join trends_uint hi oni.itemid = hi.itemidwherei.key_='net.if.in[%s]' and g.name like '%s' and hi.clock >= UNIX_TIMESTAMP('%s %s:00:00') andhi.clock < UNIX_TIMESTAMP('%s %s:00:00') group by h.host;&quot;%(i,room&#43;&quot;%&quot;,now_time,old_hour,now_time,now_hour)            n=mysql_cur.execute(search_sql)            result=mysql_cur.fetchall()            for ii in result:                msg1={'Group_Name':ii,'Host':ii,'Network_device':i,'Source':'In'}                if msg1 not in in_results:                  in_results.append(msg1)                  msg={'Date':ii,'Group_Name':ii,'Host':ii,'Network':float(ii),'Network_device':i,'Source':'In'}                  last_results.append(msg)      #search network out traffic      for i in network_device:            search_sql=&quot;select from_unixtime(hi.clock,'%%Y-%%m-%%d %%T') as Date,g.name as Group_Name,h.host as Host,round(max(hi.value_max)/1000,0) as Networkfrom hosts_groups hg join groups g on g.groupid = hg.groupid join items i on hg.hostid = i.hostid join hosts h on h.hostid=i.hostid join trends_uint hi oni.itemid = hi.itemidwherei.key_='net.if.out[%s]' and g.name like '%s' and hi.clock >= UNIX_TIMESTAMP('%s %s:00:00') andhi.clock < UNIX_TIMESTAMP('%s %s:00:00') group by h.host;&quot;%(i,room&#43;&quot;%&quot;,now_time,old_hour,now_time,now_hour)            n=mysql_cur.execute(search_sql)            result=mysql_cur.fetchall()            for ii in result:                msg1={'Group_Name':ii,'Host':ii,'Network_device':i,'Source':'out'}                if msg1 not in out_results:                  out_results.append(msg1)                  msg={'Date':ii,'Group_Name':ii,'Host':ii,'Network':float(ii),'Network_device':i,'Source':'out'}                  last_results.append(msg)    time=&quot;%s-[%s-%s]&quot;%(now_time,old_hour,now_hour)    a=[]    for i in last_results:    if i['Network'] >=10000:      msg=(i['Group_Name'],i['Host'],i['Network']/1000,i['Network_device'],i['Source'])      a.append(msg)    sort_list=sorted(a,key=lambda d:d,reverse = True)    wb = xlwt.Workbook()    ws = wb.add_sheet('zabbix', cell_overwrite_ok=True)    ws.write(0,0,'报警组'.decode(&quot;utf-8&quot;))    ws.write(0,1,'主机'.decode(&quot;utf-8&quot;))    ws.write(0,2,'流量(Mbps)'.decode(&quot;utf-8&quot;))    ws.write(0,3,'网卡名'.decode(&quot;utf-8&quot;))    ws.write(0,4,'方向'.decode(&quot;utf-8&quot;))    for i in range(1,len(sort_list)&#43;1):      for ii in range(0,len(sort_list)):            ws.write(i,ii,sort_list)    ws.col(0).width = 3333*3    ws.col(1).width = 3333    wb.save('/tmp/zabbix_network_traffic-%s.xls'%time)    mysql_cur.close()    mysql_conn.close()运行的话,参数信息如下:
第一个参数是机房信息,比如我有2个机房,分别是A与B,我就想查看A机房的,那么第一个参数就写'A';
第二个参数是开始时间,如09;
第三个参数是结束时间,如13;
比如我想查看A机房早上9点到下午13点的超过10m流量,那么可以使用
1python check_zabbix_network_traffic.py 'A' 09 13默认输出目录是/tmp/,文件名格式是zabbix_network_traffic-当天-[开始时间-将结束时间],如zabbix_network_traffic-2015-08-19-.xls
效果为

报警组是zabbix里的报警组描述,主机就是ip信息,网卡名就是检测的网卡名,方向就是流入或者流出流量。
对于网卡名我在多描述一下,默认脚本里,网卡为em2、eth1、eth0
1network_device=['em2','eth1','eth0']为什么写这个呢,就是因为我这里主机网卡信息很混乱,比如openstack的云平台主机就一个网卡eth0,而centos5的网卡就是eth0于eth1,centos6 与centos7就都是em1与em2,就导致不通平台与不同系统,公网的设备名不一样,所以我这个脚本里做了判断,如果有em2网卡,就不检测eth1与eth0了,依次类推。
我写的目录就是为了快速查看依次流量,大家如果有其他的需求,可以自己根据需求改。
本文出自 “吟—技术交流” 博客,请务必保留此出处http://dl528888.blog.iyunv.com/2382721/1685932
页: [1]
查看完整版本: zabbix企业应用之如何快速查看异常流量