zabbix企業應用之如何快速查看異常流量

最近機房總流量老是異常,而後我是不斷的接到短信與電話報警,收到後經過cacti查看仍是十分的麻煩與浪費時間,爲了解決這個問題,我本身寫了一個腳本,從數據庫裏獲取全部主機監控數據,而後打印出流量超過10m的主機信息,這樣能快速幫我判斷異常流量主機。python

腳本是使用python編寫,使用MySQLdb從zabbix數據庫裏獲取流量數據,通過流量判斷後在把數據寫入到excel裏。mysql

使用前須要安裝MySQLdb、xlwt模塊,可使用easy_install安裝。sql

下面是腳本內容
數據庫

#!/usr/bin/env python
#-*- coding: utf-8 -*-
#author:Deng Lei
#email: dl528888@gmail.com
import MySQLdb
import time
import sys
import xlwt
reload(sys)
sys.setdefaultencoding('utf8')

if __name__ == "__main__":
    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="utf8")
    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[1]
    except IndexError:
	room='all'
    try:
	old_hour=sys.argv[2]
    except IndexError:
	old_hour=now_hour-1
    try:
        now_hour=sys.argv[3]
    except IndexError:
        now_hour=int(time.strftime('%H'))
    if room == 'all':
        #search network in traffic
        for i in network_device:
            search_sql="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 Network  from 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 on  i.itemid = hi.itemid  where  i.key_='net.if.in[%s]' and  hi.clock >= UNIX_TIMESTAMP('%s %s:00:00') and  hi.clock < UNIX_TIMESTAMP('%s %s:00:00') group by h.host;"%(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[1],'Host':ii[2],'Network_device':i,'Source':'In'}
	        if msg1 not in in_results:
		    in_results.append(msg1)
	            msg={'Date':ii[0],'Group_Name':ii[1],'Host':ii[2],'Network':float(ii[3]),'Network_device':i,'Source':'In'}
	            last_results.append(msg)
        #search network out traffic
        for i in network_device:
            search_sql="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 Network  from 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 on  i.itemid = hi.itemid  where  i.key_='net.if.out[%s]' and  hi.clock >= UNIX_TIMESTAMP('%s %s:00:00') and  hi.clock < UNIX_TIMESTAMP('%s %s:00:00') group by h.host;"%(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[1],'Host':ii[2],'Network_device':i,'Source':'out'}
                if msg1 not in out_results:
                    out_results.append(msg1)
                    msg={'Date':ii[0],'Group_Name':ii[1],'Host':ii[2],'Network':float(ii[3]),'Network_device':i,'Source':'out'}
                    last_results.append(msg)
    else:
        #search network in traffic
        for i in network_device:
            search_sql="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 Network  from 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 on  i.itemid = hi.itemid  where  i.key_='net.if.in[%s]' and g.name like '%s' and hi.clock >= UNIX_TIMESTAMP('%s %s:00:00') and  hi.clock < UNIX_TIMESTAMP('%s %s:00:00') group by h.host;"%(i,room+"%",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[1],'Host':ii[2],'Network_device':i,'Source':'In'}
                if msg1 not in in_results:
                    in_results.append(msg1)
                    msg={'Date':ii[0],'Group_Name':ii[1],'Host':ii[2],'Network':float(ii[3]),'Network_device':i,'Source':'In'}
                    last_results.append(msg)
        #search network out traffic
        for i in network_device:
            search_sql="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 Network  from 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 on  i.itemid = hi.itemid  where  i.key_='net.if.out[%s]' and g.name like '%s' and hi.clock >= UNIX_TIMESTAMP('%s %s:00:00') and  hi.clock < UNIX_TIMESTAMP('%s %s:00:00') group by h.host;"%(i,room+"%",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[1],'Host':ii[2],'Network_device':i,'Source':'out'}
                if msg1 not in out_results:
                    out_results.append(msg1)
                    msg={'Date':ii[0],'Group_Name':ii[1],'Host':ii[2],'Network':float(ii[3]),'Network_device':i,'Source':'out'}
                    last_results.append(msg)
    time="%s-[%s-%s]"%(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[2],reverse = True)
    wb = xlwt.Workbook()
    ws = wb.add_sheet('zabbix', cell_overwrite_ok=True)
    ws.write(0,0,'報警組'.decode("utf-8"))
    ws.write(0,1,'主機'.decode("utf-8"))
    ws.write(0,2,'流量(Mbps)'.decode("utf-8"))
    ws.write(0,3,'網卡名'.decode("utf-8"))
    ws.write(0,4,'方向'.decode("utf-8"))
    for i in range(1,len(sort_list)+1):
        for ii in range(0,len(sort_list[i-1])):
            ws.write(i,ii,sort_list[i-1][ii])
    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()

運行的話,參數信息以下:centos

第一個參數是機房信息,好比我有2個機房,分別是A與B,我就想查看A機房的,那麼第一個參數就寫'A';bash

第二個參數是開始時間,如09;app

第三個參數是結束時間,如13;ide

好比我想查看A機房早上9點到下午13點的超過10m流量,那麼可使用fetch

python check_zabbix_network_traffic.py 'A' 09 13

默認輸出目錄是/tmp/,文件名格式是zabbix_network_traffic-當天-[開始時間-將結束時間],如zabbix_network_traffic-2015-08-19-[0-13].xlsui

效果爲

wKioL1XUJqfwHT1HAAF3fk-GFi4016.jpg

報警組是zabbix裏的報警組描述,主機就是ip信息,網卡名就是檢測的網卡名,方向就是流入或者流出流量。

對於網卡名我在多描述一下,默認腳本里,網卡爲em二、eth一、eth0

network_device=['em2','eth1','eth0']

爲何寫這個呢,就是由於我這裏主機網卡信息很混亂,好比openstack的雲平臺主機就一個網卡eth0,而centos5的網卡就是eth0於eth1,centos6 與centos7就都是em1與em2,就致使不通平臺與不一樣系統,公網的設備名不同,因此我這個腳本里作了判斷,若是有em2網卡,就不檢測eth1與eth0了,依次類推。

我寫的目錄就是爲了快速查看依次流量,你們若是有其餘的需求,能夠本身根據需求改。

相關文章
相關標籤/搜索