# -*- coding:utf-8 -*-
import json
import requests
import time,datetime
import csv,chardet
import smtplib
from email.header import Header
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
def getToken(url, post_headers, url_user, url_password):
post_data = {
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": url_user,
"password": url_password
},
"id": 1
}
ret = requests.post(url, data=json.dumps(post_data), headers=post_headers)
return json.loads(ret.text).get("result")
def getEvent(url, post_headers, auth,allgroupid_list, time_start, time_end):
post_data = {
"jsonrpc": "2.0",
"method": "event.get",
"params": {
"output": "extend",
#"time_from": "1615132800",
#"time_till": "1615737599",
"time_from": time_start,
"time_till": time_end,
"select_acknowledges": "extend",
"selectTags": "extend",
"selectSuppressionData": "extend",
"groupids": allgroupid_list,
"sortfield": ["clock", "eventid"],
"value": 1, #1表示告警事件,0表示恢复事件
"severities": [5,4,3,2], #5:一级,4:二级,3:三级,2:四级,
"sortorder": "DESC"
},
"auth": auth,
"id": 1
}
ret = requests.post(url, data = json.dumps(post_data), headers = post_headers)
ret_dict = json.loads(ret.text)
# for i in ret_dict.get("result"):
# print(i)
# print(len(ret_dict.get("result")))
return ret_dict
def eventAllExportCsv(data,csvfile):
data_list = data.get("result")
severity = []
f = open(csvfile, 'w', newline='')
csv_write = csv.writer(f)
row0 = ["eventid","clock","name","severity"] #csv第一行标题
csv_write.writerow(row0)
for i in data_list:
severity.append(i.get("severity"))
eventlevel = i.get("severity")
if eventlevel == "5":
eventlevel = "一级"
elif eventlevel == "4":
eventlevel = "二级"
elif eventlevel == "3":
eventlevel = "三级"
elif eventlevel == "2":
eventlevel = "四级"
else:
eventlevel = "未知"
eventtime = time.localtime(int(i.get("clock")))
eventtime = time.strftime("%Y-%m-%d %H:%M:%S", eventtime)
#print(i.get("eventid") + " " + eventtime + " " + i.get("name") + " " + eventlevel)
csv_write.writerow([i.get("eventid"),eventtime,i.get("name"),eventlevel])
return severity
def severityCountAddToCsv(severity,csvfile):
f = open(csvfile, 'a+', newline='')
csv_write = csv.writer(f)
csv_write.writerow([])
csv_write.writerow(["告警等级","总数"])
csv_write.writerow(["一级告警", str(severity.count('5'))])
csv_write.writerow(["二级告警", str(severity.count('4'))])
csv_write.writerow(["三级告警", str(severity.count('3'))])
csv_write.writerow(["四级告警", str(severity.count('2'))])
def typeCountAddToCsv(typeDict,csvfile):
f = open(csvfile, 'a+', newline='')
csv_write = csv.writer(f)
csv_write.writerow([])
csv_write.writerow(["监控对象类型","总数"])
for key in typeDict:
csv_write.writerow([key, typeDict[key]])
def typeGroupCount(typegroupname):
typegroupid_list = getHostGroupId(url, post_headers, auth, typegroupname)
intcount = {}
for i in range(len(typegroupid_list)):
eventdata = getEvent(url, post_headers, auth, typegroupid_list[i], time_start, time_end)
strkey = typegroupname[i]
intcount[strkey] = len(eventdata.get("result"))
newadd = {}
venter_data = 0
storage_data = 0
hardware_data = 0
for key in intcount:
if "06-" in key:
storage_data += intcount[key]
elif "10-" in key:
hardware_data += intcount[key]
elif "12-" in key or "Hypervisors" in key:
venter_data += intcount[key]
elif "01-" in key:
newadd_key = "Linux系统"
newadd_value = intcount[key]
elif "02-" in key:
newadd_key = "Windows系统"
newadd_value = intcount[key]
elif "Oracle" in key:
newadd_key = "Oracle"
newadd_value = intcount[key]
elif "Mysql" in key:
newadd_key = "Mysql"
newadd_value = intcount[key]
else :
pass
newadd[newadd_key] = newadd_value
newadd["服务器硬件"] = hardware_data
newadd["venter"] = venter_data
newadd["存储"] = storage_data
return newadd
#按应用分类统计过去一周告警
def appgroupCount(appgroupname):
appgroupid_list = []
for i in appgroupname:
appgroupid_list.append(getHostGroupId(url, post_headers, auth, i))
# print(dict(zip(appgroupname, appgroupid_list)))
intcount = {}
bqx_totalcount = []
zab_totalcount = []
ac_totalcount = []
portal_totalcount = []
sap_totalcount = []
srm_totalcount = []
bk_totalcount = []
plm_totalcount = []
bom_totalcount = []
bigdata_totalcount = []
carnet_totalcount = []
les_totalcount = []
for i in range(len(appgroupid_list)):
eventdata = getEvent(url, post_headers, auth, appgroupid_list[i], time_start, time_end)
#strkey = appgroupname[i]
appcount = []
for j in eventdata.get("result"):
appcount.append(j.get("severity"))
if "50-" in appgroupname[i]:
strkey = "xx系统"
bqx_totalcount += appcount
intcount[strkey] = bqx_totalcount
elif "13-" in appgroupname[i]:
strkey = "xx"
ac_totalcount += appcount
intcount[strkey] = ac_totalcount
elif "20-" in appgroupname[i]:
strkey = "xx"
portal_totalcount += appcount
intcount[strkey] = portal_totalcount
elif "30-" in appgroupname[i]:
strkey = "xx系统"
sap_totalcount += appcount
intcount[strkey] = sap_totalcount
elif "31-" in appgroupname[i]:
strkey = "xx系统"
srm_totalcount += appcount
intcount[strkey] = srm_totalcount
elif "32-" in appgroupname[i]:
strkey = "xx平台"
bk_totalcount += appcount
intcount[strkey] = bk_totalcount
elif "40-" in appgroupname[i]:
strkey = "xx系统"
plm_totalcount += appcount
intcount[strkey] = plm_totalcount
elif "45-" in appgroupname[i]:
strkey = "xx系统"
bom_totalcount += appcount
intcount[strkey] = bom_totalcount
elif "51-" in appgroupname[i]:
strkey = "大数据平台"
bigdata_totalcount += appcount
intcount[strkey] = bigdata_totalcount
elif "70-" in appgroupname[i]:
strkey = "xx"
carnet_totalcount += appcount
intcount[strkey] = carnet_totalcount
elif "11-" in appgroupname[i]:
strkey = "xx系统"
zab_totalcount += appcount
intcount[strkey] = zab_totalcount
elif "52-" in appgroupname[i]:
strkey = "xx系统"
les_totalcount += appcount
intcount[strkey] = les_totalcount
else:
pass
for key in intcount:
intcount[key] = [intcount[key].count("5"),intcount[key].count("4"),intcount[key].count("3"),intcount[key].count("2")]
print(intcount)
return intcount
def appCountAddToCsv(appDict, csvfile):
f = open(csvfile, 'a+', newline='')
csv_write = csv.writer(f)
csv_write.writerow([])
csv_write.writerow(["应用系统", "一级", "二级", "三级", "四级"])
for key in appDict:
csv_write.writerow([key, appDict[key][0], appDict[key][1], appDict[key][2], appDict[key][3]])
def getHostGroupId(url, post_headers, auth, hostgroupname):
post_data = {
"jsonrpc": "2.0",
"method": "hostgroup.get",
"params": {
"output": "extend",
"filter": {
"name": hostgroupname
}
},
"auth": auth,
"id": 1
}
ret = requests.post(url, data = json.dumps(post_data), headers = post_headers)
#print("ret:",json.loads(ret.text))
#print("result:", json.loads(ret.text).get("result"))
groupid_list = []
groupid_dict = {}
for i in json.loads(ret.text).get("result"):
groupid_list.append(i.get("groupid"))
# strkey = i.get("name")
# strvalue = i.get("groupid")
# groupid_dict[strkey] = strvalue
return groupid_list
def sendmail(csvfile,receiver):
sender = 'bnqc-appmonitoring@bngrp.com'
smtpserver = '10.58.167.xx'
username = 'xxxx@xxx.com'
password = '*****'
mail_title = '监控告警统计'
# 创建一个带附件的实例
message = MIMEMultipart()
message['From'] = sender
message['To'] = ','.join(receiver)
message['Subject'] = Header(mail_title, 'utf-8')
# 邮件正文内容
message.attach(MIMEText('监控告警统计,请查收附件!', 'plain', 'utf-8'))
# 构造附件
att1 = MIMEApplication(open(csvfile, 'rb').read()) # 打开附件
att1.add_header('Content-Disposition', 'attachment', filename=csvfile) # 为附件命名
message.attach(att1)
smtpObj = smtplib.SMTP_SSL() # 注意:如果遇到发送失败的情况(提示远程主机拒接连接),这里要使用SMTP_SSL方法
smtpObj.connect(smtpserver)
smtpObj.login(username, password)
smtpObj.sendmail(sender, message['To'].split(','), message.as_string())
print("邮件发送成功!!!")
smtpObj.quit()
def getStarTEndTime(days):
date = datetime.datetime.now() # 获取当前时间
if days == 7: # 获取上一周起止时间
time_start = date - datetime.timedelta(days=date.weekday() + 7) # 上周开始时间
time_end = date - datetime.timedelta(days=date.weekday() + 1) # 上周结束时间
print("startTime:", time_start)
print("startEnd:", time_end)
time_start = time.strptime(time_start.strftime("%Y-%m-%d") + " 00:00:00", "%Y-%m-%d %H:%M:%S")
time_end = time.strptime(time_end.strftime("%Y-%m-%d") + " 23:59:59", "%Y-%m-%d %H:%M:%S")
time_start = int(time.mktime(time_start))
time_end = int(time.mktime(time_end))
csvfile = "过去一周监控告警统计" + time.strftime('_%Y%m%d%H%M%S', time.localtime(time.time())) + ".csv"
elif days == 31 : #获取过去30天起止时间
time_start = date - datetime.timedelta(31) # 过去30天开始时间
time_end = date - datetime.timedelta(1) # 过去30天结束时间
print("startTime:", time_start)
print("startEnd:", time_end)
time_start = time.strptime(time_start.strftime("%Y-%m-%d") + " 00:00:00", "%Y-%m-%d %H:%M:%S")
time_end = time.strptime(time_end.strftime("%Y-%m-%d") + " 23:59:59", "%Y-%m-%d %H:%M:%S")
time_start = int(time.mktime(time_start))
time_end = int(time.mktime(time_end))
csvfile = "过去30天监控告警统计" + time.strftime('_%Y%m%d%H%M%S', time.localtime(time.time())) + ".csv"
else:
pass
return time_start, time_end, csvfile
if __name__ == '__main__':
url = 'http://10.210.xx.xx/api_jsonrpc.php' #zabbix监控系统接口地址
post_headers = {'Content-Type': 'application/json'}
url_user = "Admin"
url_passwd = "*******"
auth = getToken(url,post_headers,url_user,url_passwd) #获取token值
print(auth)
# 获取起止时间,csv文件名
time_start, time_end, csvfile = getStarTEndTime(7)
# 获取所有告警,保存至csv文件
allgroupname = ["01-Linux生产系统", "02-Windows生产系统"]
allgroupid_list = getHostGroupId(url, post_headers, auth, allgroupname)
eventdata = getEvent(url, post_headers, auth, allgroupid_list, time_start, time_end)
severity_list = eventAllExportCsv(eventdata,csvfile)
#按告警等级统计,统计追加到csv文件
severityCountAddToCsv(severity_list,csvfile)
#按对象类型统计,统计追加到csv文件
typegroupname = ["01-Linux生产系统", "02-Windows生产系统"]
typeDict = typeGroupCount(typegroupname)
typeCountAddToCsv(typeDict,csvfile)
# 按应用系统统计,统计追加到csv文件
appgroupname = ["11-信息管理中心-****系统", "13-信息管理中心-系统管控部-****"]
appDict = appgroupCount(appgroupname)
appCountAddToCsv(appDict, csvfile)
receiver = ['huxxxx@xxx.com','xxxxx@xxxxx.com'] # 邮件接收人,多人用逗号区分开
sendmail(csvfile, receiver)