欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python自動(dòng)化導(dǎo)出zabbix數(shù)據(jù)并發(fā)郵件腳本

 更新時(shí)間:2019年08月16日 09:03:16   作者:IT派森  
這篇文章主要介紹了Python自動(dòng)化導(dǎo)出zabbix數(shù)據(jù)并發(fā)郵件腳本,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

Zabbix沒有報(bào)表導(dǎo)出的功能,于是通過編寫腳本導(dǎo)出zabbix數(shù)據(jù)并發(fā)郵件。效果如下:

下面是腳本,可根據(jù)自己的具體情況修改:

#!/usr/bin/python`
`#coding:utf-8`
`import MySQLdb`
`import time,datetime`
`import xlsxwriter`
`import smtplib`
`from` `email.mime.text import MIMEText`
`from` `email.mime.multipart import MIMEMultipart`
`from` `email.header import Header`
`#zabbix數(shù)據(jù)庫(kù)信息:`
`zdbhost = ``'127.0.0.1'`
`zdbuser = ``'zabbix'`
`zdbpass = ``'zabbix'`
`zdbport = 3306`
`zdbname = ``'zabbix'`
`#生成文件名稱:`
`xlsfilename = ``'Group_Production_Server.xlsx'`
`#需要查詢的key列表 [名稱,表名,key值,取值,格式化,數(shù)據(jù)整除處理]`
`keys = [`
`#  ['CPU核心數(shù)','trends_uint','system.cpu.num','avg','',1],`
`#['CPU平均空閑值','trends','system.cpu.util[,idle]','avg','%.2f',1],`
`#['CPU最小空閑值','trends','system.cpu.util[,idle]','min','%.2f',1],`
`[``'CPU使用率(%)'``,``'trends'``,``'CPU_used'``,``'avg'``,``'%.2f'``,1],`
`#['內(nèi)存大小(單位G)','trends_uint','vm.memory.size[total]','avg','',1048576000],`
`#['剩余內(nèi)存(單位G)','trends_uint','vm.memory.size[available]','avg','',1048576000],`
`[``'內(nèi)存使用率(%)'``,``'trends'``,``'Memory_used'``,``'avg'``,``'%.2f'``,1],`
`#  ['可用平均內(nèi)存(單位G)','trends_uint','vm.memory.size[available]','avg','',1048576000],`
`#  ['可用最小內(nèi)存(單位G)','trends_uint','vm.memory.size[available]','min','',1048576000],`
`#  ['swap總大小(單位G)','trends_uint','system.swap.size[,total]','avg','',1048576000],`
`#  ['swap平均剩余(單位G)','trends_uint','system.swap.size[,free]','avg','',1048576000],`
`#  ['根分區(qū)總大小(單位G)','trends_uint','vfs.fs.size[/,total]','avg','',1073741824],`
`#  ['根分區(qū)平均剩余(單位G)','trends_uint','vfs.fs.size[/,free]','avg','',1073741824],`
`#['磁盤總大小(單位G)','trends_uint','vfs.fs.size[/fs01,total]','avg','',1073741824],`
`#['磁盤剩余(單位G)','trends_uint','vfs.fs.size[/fs01,free]','avg','',1073741824],`
`[``'磁盤使用率(%)'``,``'trends'``,``'fs01_used'``,``'avg'``,``'%.2f'``,1],`
`#  ['進(jìn)入最大流量(單位Kbps)','trends_uint','net.if.in[eth0]','max','',1000],`
`#  ['進(jìn)入平均流量(單位Kbps)','trends_uint','net.if.in[eth0]','avg','',1000],`
`#  ['出去最大流量(單位Kbps)','trends_uint','net.if.out[eth0]','max','',1000],`
`#  ['出去平均流量(單位Kbps)','trends_uint','net.if.out[eth0]','avg','',1000],`
`]`
`class` `ReportForm:`
`def __init__(self):`
`''``'打開數(shù)據(jù)庫(kù)連接'``''`
`self.conn = MySQLdb.connect(host=zdbhost,user=zdbuser,passwd=zdbpass,port=zdbport,db=zdbname)`
`self.cursor = self.conn.cursor(cursorclass=MySQLdb.cursors.DictCursor)`
`#生成zabbix哪個(gè)分組報(bào)表`
`self.groupname = ``'Group_Production_Server'`
`#獲取IP信息:`
`self.IpInfoList = self.__getHostList()`
`def __getHostList(self):`
`''``'根據(jù)zabbix組名獲取該組所有IP'``''`
`#查詢組ID:`
`sql = ``''``'select groupid from groups where name = '``%s``' '``''` `% self.groupname`
`self.cursor.execute(sql)`
`groupid = self.cursor.fetchone()[``'groupid'``]`
`#根據(jù)groupid查詢?cè)摲纸M下面的所有主機(jī)ID(hostid):`
`sql = ``''``'select hostid from hosts_groups where groupid = '``%s``' '``''` `% groupid`
`self.cursor.execute(sql)`
`hostlist = self.cursor.fetchall()`
`#生成IP信息字典:結(jié)構(gòu)為{'119.146.207.19':{'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,type, itemid, start_time, stop_time):`
`''``'查詢trends_uint表的值,type的值為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`
`return` `result`
`def getTrends_uintValue(self,type, itemid, start_time, stop_time):`
`''``'查詢trends_uint表的值,type的值為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:`
`result = ``int``(result)`
`else``:`
`result = 0`
`return` `result`
`def getLastMonthData(self,type,hostid,table,itemname):`
`''``'根據(jù)hostid,itemname獲取該監(jiān)控項(xiàng)的值'``''`
`#獲取上個(gè)月的第20天和最后1天`
`ts_first = ``int``(time.mktime(datetime.date(datetime.date.today().year,datetime.date.today().month-1,20).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(type,itemid, ts_first, ts_last)`
`def getNowData(self):`
`nowtime = datetime.datetime.now().strftime(``'%Y-%m-%d'``)`
`return` `nowtime`
`def getInfo(self):`
`#循環(huán)讀取IP列表信息`
`for` `ip,resultdict ``in` `zabbix.IpInfoList.items():`
`print ``"正在查詢 IP:%-15s hostid:%5d 的信息!"` `% (ip, resultdict[``'hostid'``])`
`#循環(huán)讀取keys,逐個(gè)key統(tǒng)計(jì)數(shù)據(jù):`
`for` `value ``in` `keys:`
`print ``"\t正在統(tǒng)計(jì) key_:%s"` `% value[2]`
`if` `not value[2] ``in` `zabbix.IpInfoList[ip]:`
`zabbix.IpInfoList[ip][value[2]] = {}`
`data = zabbix.getLastMonthData(value[3], resultdict[``'hostid'``],value[1],value[2])`
`zabbix.IpInfoList[ip][value[2]][value[3]] = data`
`def writeToXls2(self):`
`''``'生成xls文件'``''`
`#創(chuàng)建文件`
`workbook = xlsxwriter.Workbook(xlsfilename)`
`#創(chuàng)建工作薄`
`worksheet = workbook.add_worksheet()`
`#寫入第一列:`
`worksheet.write(0,0,``"主機(jī)"``.decode(``'utf-8'``))`
`i = 1`
`for` `ip ``in` `self.IpInfoList:`
`worksheet.write(i,0,ip)`
`i = i + 1`
`#寫入其他列:`
`i = 1`
`for` `value ``in` `keys:`
`worksheet.write(0,i,value[0].decode(``'utf-8'``))`
`#寫入該列內(nèi)容:`
`j = 1`
`for` `ip,result ``in` `self.IpInfoList.items():`
`if` `value[4]:`
`worksheet.write(j,i, value[4] % result[value[2]][value[3]])`
`else``:`
`worksheet.write(j,i, result[value[2]][value[3]] / value[5])`
`j = j + 1`
`i = i + 1`
`workbook.close()`
`def __del__(self):`
`''``'關(guān)閉數(shù)據(jù)庫(kù)連接'``''`
`self.cursor.close()`
`self.conn.close()`
`def Send_Email(self):`
`sender = ``'from@runoob.com'`
`receivers = [``'hejianlai@pci.cn'``] # 接收郵件,可設(shè)置為你的QQ郵箱或者其他郵箱`
`#創(chuàng)建一個(gè)帶附件的實(shí)例`
`message = MIMEMultipart()`
`message[``'From'``] = Header(``"Zabbix_server"``, ``'utf-8'``)`
`message[``'To'``] = Header(``"it"``, ``'utf-8'``)`
`subject = ``'生產(chǎn)環(huán)境虛機(jī)資源使用情況'`
`message[``'Subject'``] = Header(subject, ``'utf-8'``)`
`#郵件正文內(nèi)容`
`message.attach(MIMEText(``'生產(chǎn)環(huán)境虛機(jī)資源使用情況'``, ``'plain'``, ``'utf-8'``))`
`# 構(gòu)造附件1,傳送當(dāng)前目錄下的 test.txt 文件`
`att1 = MIMEText(open(``'Group_Production_Server.xlsx'``, ``'rb'``).read(), ``'base64'``, ``'utf-8'``)`
`att1[``"Content-Type"``] = ``'application/octet-stream'`
`# 這里的filename可以任意寫,寫什么名字,郵件中顯示什么名字`
`att1[``"Content-Disposition"``] = ``'attachment; filename="Group_Production_Server.xlsx"'`
`message.attach(att1)`
`try``:`
`smtpObj = smtplib.SMTP(``'localhost'``)`
`smtpObj.sendmail(sender, receivers, message.as_string())`
`print ``"郵件發(fā)送成功"`
`except smtplib.SMTPException:`
`print ``"Error: 無法發(fā)送郵件"`
`if` `__name__ == ``"__main__"``:`
`zabbix = ReportForm()`
`zabbix.getInfo()`
`zabbix.writeToXls2()`
`zabbix.Send_Email()`

總結(jié)

以上所述是小編給大家介紹的Python自動(dòng)化導(dǎo)出zabbix數(shù)據(jù)并發(fā)郵件腳本,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!

相關(guān)文章

  • Django實(shí)現(xiàn)簡(jiǎn)單登錄的示例代碼

    Django實(shí)現(xiàn)簡(jiǎn)單登錄的示例代碼

    本文主要介紹了Django實(shí)現(xiàn)簡(jiǎn)單登錄的示例代碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • Python利用hashlib實(shí)現(xiàn)文件MD5碼的批量存儲(chǔ)

    Python利用hashlib實(shí)現(xiàn)文件MD5碼的批量存儲(chǔ)

    這篇文章主要為大家詳細(xì)介紹了如何用Python和hashlib實(shí)現(xiàn)文件MD5碼的批量存儲(chǔ)功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以學(xué)習(xí)一下
    2023-05-05
  • python模擬登陸、POST/GET請(qǐng)求方式

    python模擬登陸、POST/GET請(qǐng)求方式

    這篇文章主要介紹了python模擬登陸、POST/GET請(qǐng)求方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • pytorch使用horovod多gpu訓(xùn)練的實(shí)現(xiàn)

    pytorch使用horovod多gpu訓(xùn)練的實(shí)現(xiàn)

    這篇文章主要介紹了pytorch使用horovod多gpu訓(xùn)練的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Python文件右鍵找不到IDLE打開項(xiàng)解決辦法

    Python文件右鍵找不到IDLE打開項(xiàng)解決辦法

    這篇文章主要介紹了Python文件右鍵找不到IDLE打開項(xiàng)解決辦法,本文使用注冊(cè)表解決了這個(gè)問題,需要的朋友可以參考下
    2015-06-06
  • 一起用Python做個(gè)上課點(diǎn)名器的制作過程

    一起用Python做個(gè)上課點(diǎn)名器的制作過程

    今天給大家分享一個(gè)讀者粉絲投稿的,關(guān)于上課點(diǎn)名的實(shí)戰(zhàn)案例,對(duì)Python上課點(diǎn)名器實(shí)現(xiàn)過程感興趣的朋友,一起來看看是如何實(shí)現(xiàn)的吧
    2021-09-09
  • python去除空格和換行符的實(shí)現(xiàn)方法(推薦)

    python去除空格和換行符的實(shí)現(xiàn)方法(推薦)

    下面小編就為大家?guī)硪黄猵ython去除空格和換行符的實(shí)現(xiàn)方法(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-01-01
  • python?實(shí)現(xiàn)dcmtk關(guān)聯(lián)pacs功能推送下拉影像(推薦)

    python?實(shí)現(xiàn)dcmtk關(guān)聯(lián)pacs功能推送下拉影像(推薦)

    這篇文章主要介紹了python?實(shí)現(xiàn)dcmtk關(guān)聯(lián)pacs功能?推送下拉影像,包含dcmtk關(guān)聯(lián)pacs技術(shù)筆記等相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-10-10
  • python編寫五子棋游戲

    python編寫五子棋游戲

    這篇文章主要為大家詳細(xì)介紹了python編寫五子棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • pytorch?rpc實(shí)現(xiàn)分物理機(jī)器實(shí)現(xiàn)model?parallel的過程詳解

    pytorch?rpc實(shí)現(xiàn)分物理機(jī)器實(shí)現(xiàn)model?parallel的過程詳解

    這篇文章主要介紹了pytorch?rpc實(shí)現(xiàn)分物理機(jī)器實(shí)現(xiàn)model?parallel的過程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-05-05

最新評(píng)論