python腳本獲取zabbix監控數據,而且經過郵件發送

#!/usr/bin/python
#coding:utf-8

import MySQLdb
import time,datetime
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
import smtplib
import string


#zabbix數據庫信息:
zdbhost = '192.168.47.128'
zdbuser = 'root'
zdbpass = 'root'
zdbport = 3306
zdbname = 'zabbix'

#須要查詢的key列表
keys = {
    'trends_uint':[
        'net.if.in[eth0]',
        'net.if.out[eth0]',
        'vfs.fs.size[/,used]',
        'vm.memory.size[available]',
        ],
    'trends':[
        'system.cpu.load[percpu,avg5]',
        'system.cpu.util[,idle]',
        ],
    }


class ReportForm:

    def __init__(self):
        '''打開數據庫鏈接'''
        self.conn = MySQLdb.connect(host=zdbhost,user=zdbuser,passwd=zdbpass,port=zdbport,db=zdbname)
        self.cursor = self.conn.cursor(cursorclass=MySQLdb.cursors.DictCursor)

        #生成zabbix哪一個分組報表
        self.groupname = 'Test_Server'

        #獲取IP信息:
        self.IpInfoList = self.__getHostList()

    def __getHostList(self):
        '''根據zabbix組名獲取該組全部IP'''

        #查詢組ID:
        sql = '''select groupid from groups where name = '%s' ''' % self.groupname
        self.cursor.execute(sql)
        groupid = self.cursor.fetchone()['groupid']

        #根據groupid查詢該分組下面的全部主機ID(hostid):
        sql = '''select hostid from hosts_groups where groupid = %s''' % groupid
        self.cursor.execute(sql)
        hostlist = self.cursor.fetchall()

        #生成IP信息字典:結構爲{'112.111.222.55':{'hostid':10086L,},}
        IpInfoList = {}
        for i in hostlist:
            hostid = i['hostid']
            sql = '''select host from hosts where status = 0 and hostid = %s''' % hostid
            ret = self.cursor.execute(sql)
            if ret:
                IpInfoList[self.cursor.fetchone()['host']] = {'hostid':hostid}
        return IpInfoList

    def __getItemid(self,hostid,itemname):
        '''獲取itemid'''
        sql = '''select itemid from items where hostid = %s and key_ = '%s' ''' % (hostid, itemname)
        if self.cursor.execute(sql):
            itemid = self.cursor.fetchone()['itemid']
        else:
            itemid = None
        return itemid

    def getTrendsValue(self,itemid, start_time, stop_time):
        '''查詢trends_uint表的值,type的值爲min,max,avg三種'''
        resultlist = {}
        for type in ['min','max','avg']:
            sql = '''select %s(value_%s) as result from trends where itemid = %s and clock >= %s and clock <= %s''' % (type, type, itemid, start_time, stop_time)
            self.cursor.execute(sql)
            result = self.cursor.fetchone()['result']
            if result == None:
                result = 0
            resultlist[type] = result
        return resultlist

    def getTrends_uintValue(self,itemid, start_time, stop_time):
        '''查詢trends_uint表的值,type的值爲min,max,avg三種'''
        resultlist = {}
        for type in ['min','max','avg']:
            sql = '''select %s(value_%s) as result from trends_uint where itemid = %s and clock >= %s and clock <= %s''' % (type, type, itemid, start_time, stop_time)
            self.cursor.execute(sql)
            result = self.cursor.fetchone()['result']
            if result:
                resultlist[type] = int(result)
            else:
                resultlist[type] = 0
        return resultlist


    def getLastMonthData(self,hostid,table,itemname):
        '''根據hostid,itemname獲取該監控項的值'''
        #獲取上個月的第一天和最後一天
        ts_first = int(time.mktime(datetime.date(datetime.date.today().year,datetime.date.today().month-1,1).timetuple()))
        lst_last = datetime.date(datetime.date.today().year,datetime.date.today().month,1)-datetime.timedelta(1)
        ts_last = int(time.mktime(lst_last.timetuple()))

        itemid = self.__getItemid(hostid, itemname)

        function = getattr(self,'get%sValue' % table.capitalize())

        return  function(itemid, ts_first, ts_last)

    def getInfo(self):
        #循環讀取IP列表信息
        for ip,resultdict in  zabbix.IpInfoList.items():
            print "正在查詢 IP:%-15s hostid:%5d 的信息!" % (ip, resultdict['hostid'])
            #循環讀取keys,逐個key統計數據:
            for table, keylists in keys.items():
                for key in keylists:
                    print "\t正在統計 key_:%s" % key
                    data =  zabbix.getLastMonthData(resultdict['hostid'],table,key)
                    zabbix.IpInfoList[ip][key] = data

    def writeToXls(self):
        '''生成xls文件'''
        try:
            import xlsxwriter
            #建立文件
            workbook = xlsxwriter.Workbook('damo.xls')
            #建立工做薄
            worksheet = workbook.add_worksheet()
            #寫入標題(第一行)
            i = 0
            for value in ["主機","CPU平均空閒值","CPU最小空閒值","可用平均內存(單位M)","可用最小內存(單位M)","CPU5分鐘負載","進入最大流量(單位Kbps)","進入平均流量(單位Kbps)","出去最大流量(單位Kbps)","出去平均流量(單位Kbps)"]:
                worksheet.write(0,i, value.decode('utf-8'))
                i = i + 1
                #寫入內容:
            j = 1
            for ip,value in self.IpInfoList.items():
                worksheet.write(j,0, ip)
                worksheet.write(j,1, '%.2f' % value['system.cpu.util[,idle]']['avg'])
                worksheet.write(j,2, '%.2f' % value['system.cpu.util[,idle]']['min'])
                worksheet.write(j,3, '%dM' % int(value['vm.memory.size[available]']['avg'] / 1024 / 1024))
                worksheet.write(j,4, '%dM' % int(value['vm.memory.size[available]']['min'] / 1024 / 1024))
                worksheet.write(j,5, '%.2f' % value['system.cpu.load[percpu,avg5]']['avg'])
                worksheet.write(j,6, value['net.if.in[eth0]']['max']/1000)
                worksheet.write(j,7, value['net.if.in[eth0]']['avg']/1000)
                worksheet.write(j,8, value['net.if.out[eth0]']['max']/1000)
                worksheet.write(j,9, value['net.if.out[eth0]']['avg']/1000)
                j = j + 1
            workbook.close()
        except Exception,e:
            print e



    def __del__(self):
        '''關閉數據庫鏈接'''
        self.cursor.close()
        self.conn.close()

    def sendEmail(self):

    # 建立一個帶附件的實例
        msg = MIMEMultipart()

    # 構造附件1
        xlsxpart = MIMEApplication(open('damo.xls', 'rb').read())
        xlsxpart.add_header('Content-Disposition', 'p_w_upload', filename='damo.xlsx')
        msg.attach(xlsxpart)

    # 寫入郵件正文
        text=" 附件爲運維監控報表,請查收!"
    # 郵件正文亂碼,因此在這裏指定編碼
        part1 = MIMEText(text, 'plain', _charset='utf-8')
        msg.attach(part1)

    # 加郵件

        to="xxxxx@qq.com,xxxxx@163.com"
        to=string.splitfields(to,",")

        msg['from'] = 'xxxxxx@163.com'
        msg['subject'] = '運維部門監控統計報表'.decode('utf-8')

    # 發送郵件
        try:
            server = smtplib.SMTP()

        # 數據中心測試時
            server.connect('smtp.163.com')
        # 用戶名,密碼
            server.login('xxxxx@163.com', '123456')
            server.sendmail(msg['from'], to, msg.as_string())
            server.quit()
            print 'sucessfule'.decode("utf-8")
        except Exception, e:
            print str(e)



if __name__ == "__main__":
    zabbix = ReportForm()
    zabbix.getInfo()
    zabbix.writeToXls()
    zabbix.sendEmail()

注意:發送的郵件很容易被163看成垃圾郵件拒絕了!最好用本身的郵箱服務器發送!python

相關文章
相關標籤/搜索