from datetime import datetime, timedelta
import os
from dingtalkchatbot.chatbot import DingtalkChatbot
from kafka import KafkaConsumer
import re
messages = {500: {}, 400: {}}
def sort_dict():
global messages
tmp = messages
for i in tmp:
tmp[i] = sorted(tmp[i].iteritems(), key=lambda item: item[1], reverse=True)
tmp = sorted(tmp.iteritems(), key=lambda tmp: tmp[0], reverse=True)
return tmp
def send_mail():
try:
message = ''
alldict = sort_dict()
for i in alldict:
if i[0] == 500:
t = '5XX'
message = message + '*******' + t + ':' + '\n'
else:
t = '4XX'
message = message + '*******' + t + ':' + '\n'
for j in i[1]:
message = message + j[0] + ' ' + str(j[1]) + '\n'
a = 'curl url -d "business=gateway&content=%s"' % message
print os.system(a)
print 'successfully sent!'
return 0
except Exception as e:
now = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
print('nnnnnnnn', now, e)
return -1
def tactics(time1, send_time, delta):
global messages
rt = send_time
if time1 - send_time > delta:
print 'time1 - send_time > delta'
a = send_mail()
print a
if a == 0:
send_time = time1
rt = send_time
messages = {500: {}, 400: {}}
print 'clean messages'
return rt
def filter(msg):
'''t = ['\"server_name: ' + re.findall(r'\"server_name\": \"(.+?)\",', msg)[0] + '\", '
+ '\"request: ' + re.findall(r'\"request\": \"(.+?)\",', msg)[0] + '\", '
+ '\"upstream_status: ' + re.findall(r'\"upstream_status\": \"(.+?)\",', msg)[0] + '\" '
]'''
'''t = [re.findall(r'\"upstream_status\": \"(.+?)\",', msg)[0] + ' '
+ re.findall(r'\"server_name\": \"(.+?)\",', msg)[0]
+ re.findall(r'\"request\": \"(.+?)\",', msg)[0].split(' ')[1]
]'''
if len(re.findall(r'\"http_x_app_id\": \"(.+?)\",', msg)) != 0:
t = [re.findall(r'\"upstream_status\": \"(.+?)\",', msg)[0] + ' '
+ re.findall(r'\"http_x_app_id\": \"(.+?)\",', msg)[0] + ' '
+ re.findall(r'\"server_name\": \"(.+?)\",', msg)[0] +
re.findall(r'\"request\": \"(.+?)\",', msg)[0].split(' ')[1]
]
else:
t = [re.findall(r'\"upstream_status\": \"(.+?)\",', msg)[0] + ' '
+ ' -- ' + ' '
+ re.findall(r'\"server_name\": \"(.+?)\",', msg)[0] +
re.findall(r'\"request\": \"(.+?)\",', msg)[0].split(' ')[1]
]
return t
def kafka_cli(bootstrap_servers, source_topic):
send_time = datetime.now()
print 'send_time', send_time
delta = timedelta(hours=1)
while True:
try:
consumer = KafkaConsumer(source_topic, bootstrap_servers=bootstrap_servers)
for msg in consumer:
t = msg.value
time1 = datetime.now()
t = t.replace('\\', '')
status = re.findall(r'\"upstream_status\": \"(.+?)\",', t)
if status[0] != '-':
status = int(status[0])
if status < 400:
print "ok", status
else:
t = filter(t)
if status > 499:
print t[0]
if t[0] not in messages[500]:
messages[500][t[0]] = 0
messages[500][t[0]] += 1
rt = tactics(time1, send_time, delta)
send_time = rt
else:
print t[0]
if t[0] not in messages[400]:
messages[400][t[0]] = 0
messages[400][t[0]] += 1
rt = tactics(time1, send_time, delta)
send_time = rt
else:
print status
except Exception as e:
now = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
print(now, e)
consumer.close()
def main():
bootstrap_servers = 'kafkaip:9092'
source_topic = 'kibana'
kafka_cli(bootstrap_servers, source_topic)
if __name__ == '__main__':
main()