公司板卡经常ping不同,想到zabbix会报警,于是去数据库取出相关报警内容,
查询zabbix数据库
select from_unixtime(clock) as DateTime,subject from alerts WHERE sendto = ‘xiaofeng@sunspeedy.com‘ and subject like ‘%ping down%‘ \G;
每个子元组里面的格式是前面是时间(teTime):,后面是ubject的内容
sql = ((‘av‘,‘csvbbb‘),(‘vb‘,‘ssdfff‘),(‘bn‘,‘sdcbbb‘),(‘df‘,‘sdffff‘)) #数据库查到的内容,自行替换
a = [[] for i in range(len(sql))]
for i in range(len(sql)):
for j in sql[i]:
a[i].append(sql[i])
num_list = []
# for i in a:
# num_list.append(i)
#列表可以增删改,先把元组转换成列表
r = [[] for i in range(len(a))]
# num_list = [1, 2, 3, 4,2,6, 5]
#num_list = ["(‘av‘,‘csvbbb‘)","(‘vb‘,‘ssdfff‘)","(‘bn‘,‘sdcbbb‘)","(‘df‘,‘sdffff‘)"]
for p in range(len(a)):
for i in range(len(num_list) - 1, -1, -1):
if num_list[i - 1][-1][-9:] == num_list[-1][-1][-9:]:
r[p].append(num_list[i - 1])
num_list.pop(i - 1)
else:
# print(num_list[i-1])
pass
for z in range(len(r) - 1, -1, -1):
if len(r[z]):
pass # this list is not None
else:
r.remove(r[z])
print(r) #这里是吧具有相同格式的数据归类到一起
import re
pattern = re.compile(r"((?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d))))")
# y = "fjlsfjla10.10.10.10jkfsjf"
for m in range(len(r)):
bbb = pattern.findall(r[m][0][1])
ip = [g[0] for g in bbb]
if len(ip):
r[m] = ip + r[m]
else:
pass
##########在列表的每个列表里添加ip
#num_list = [[‘av‘,‘cspingvbbb‘],[‘vb‘,‘sspingdfff‘],[‘bn‘,‘sdpingcbbb‘],[‘df‘,‘sdfpingfff‘],[‘1234‘]]
pat=‘ok‘
# print(re.search(pat,s).group())
newpat=‘1‘
# news=re.sub(pat,newpat,s) # 正则部分替换,将s中的所有符合pat的全部替换为newpat,newpat也可以是函数
# print(news)
for i in num_list:
for ii in i:
if i[-1].count(‘ing‘) == 0:
print(i[-1].count(‘ing‘))
pass
#re.sub(pat,newpat,i[-1])
else:
if i[-1].find(‘ok‘) != -1:
i[-1]= 1
else:
i[-1]= 0
print(num_list) #查找列表数据替换,输出格式
然后写到excel,发送邮件出来ok了