-
Notifications
You must be signed in to change notification settings - Fork 19
/
Top.py
182 lines (148 loc) · 5.69 KB
/
Top.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# -*- coding: utf-8 -*-
"""
@author: Ye
"""
#==============================================================================
# 用于生成Top IP,Top URL,历史 Top IP,Top URL
#==============================================================================
import pandas as pd
from pandas import DataFrame
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
from elasticsearch import Elasticsearch
import arrow
import smtplib
from email.Header import Header
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
##pandas columns width
pd.set_option("display.max_colwidth",200)
##es api
es = Elasticsearch(["http://10.10.99.195:9200/"])
##时间设定
#time_now = arrow.now().format("X") + "000"
index_today = "logstash-" + arrow.now().format("YYYY.MM.DD")
index_all = "logstash-*"
#time_yesterday = arrow.now().replace(days=-1).format("X") + "000"
#time_year = arrow.now().replace(years=-1).format("X") + "000"
##查询字段
#q_url="Request_URL"
#q_ip="X-Forwarded"
q_url="request"
q_ip="remote_addr"
##查询语句设定函数
def top_search(query_str):
rets = """{
"size":0,
"query":{
"filtered":{
"filter":{
"bool":{
"must":[
{ "term":{"type":"nginx_access"}}
]
}
},
"query": {
"query_string": {
"query": "!test.jsp",
"analyze_wildcard": true
}
}
}
},
"aggs":{
"%s":{
"terms":{"field":"%s",
"size":15}
}
}
}""" %(query_str,query_str + ".raw")
return rets
#执行查询
today_top_ip = es.search(index=index_today,body=top_search(q_ip))
today_top_url = es.search(index=index_today,body=top_search(q_url))
year_top_ip = es.search(index=index_all,body=top_search(q_ip))
year_top_url = es.search(index=index_all,body=top_search(q_url))
df_today_ip = DataFrame(today_top_ip["aggregations"][q_ip]["buckets"])
df_today_url = DataFrame(today_top_url["aggregations"][q_url]["buckets"])
df_today_url["name"] = df_today_url.index
df_all_ip = DataFrame(year_top_ip["aggregations"][q_ip]["buckets"])
df_all_url = DataFrame(year_top_url["aggregations"][q_url]["buckets"])
df_all_url["name"] = df_all_url.index
#print df_all_url.head()
#print df_all_ip.head()
#print str(df_today_url["key"])
#print df_today_ip.head()
p1 = sns.factorplot(y="key",x="doc_count",data=df_today_ip,
kind="bar",palette="summer")
p1.set_xticklabels(rotation=90)
#p1.set_titles("Today Top 15 IP")
p1.savefig("topip_today.png",dpi=100)
p2 = sns.factorplot(y="doc_count",x="name",data=df_today_url,
kind="bar",palette="summer")
p2.set_xticklabels(rotation=30)
#p2.set_titles("Today Top 15 URL")
p2.savefig("topurl_today.png",dpi=100)
p3 = sns.factorplot(y="key",x="doc_count",data=df_all_ip,
kind="bar",palette="summer")
p3.set_xticklabels(rotation=90)
#p3.set_titles("Top 15 IP")
p3.savefig("topip_all.png",dpi=100)
p4 = sns.factorplot(y="doc_count",x="name",data=df_all_url,
kind="bar",palette="summer")
p4.set_xticklabels(rotation=30)
#p4.set_titles("Top 15 URL")
p4.savefig("topurl_all.png",dpi=100)
msg = MIMEMultipart('alternative')
msg['Subject'] = Header(u"测试",'utf-8')
text = u"""
<html>
<body>
<h3>当日IP Top 15</h3>
<img src="cid:topip_today">
<h3>历史IP Top 15</h3>
<img src="cid:topip_all">
</p>
<h3>当日URL</h3>
""" + df_today_url[["key","name"]].to_html() + """<img src="cid:topurl_today">""" \
+ """<h3>历史URL</h3>""" + df_all_url[["key","name"]].to_html()\
+ """<img src="cid:topurl_all"></html>"""
#print htmls
htmls = MIMEText(text,'html','utf-8')
msg.attach(htmlss)
fp1 = open("topip_today.png","rb")
msgImage1 = MIMEImage(fp1.read())
fp1.close()
msgImage1.add_header('Content-ID','<topip_today>')
msg.attach(msgImage1)
fp2 = open("topip_all.png","rb")
msgImage2 = MIMEImage(fp2.read())
fp1.close()
msgImage2.add_header('Content-ID','<topip_all>')
msg.attach(msgImage2)
fp3 = open("topurl_all.png","rb")
msgImage3 = MIMEImage(fp3.read())
fp3.close()
msgImage3.add_header('Content-ID','<topurl_today>')
msg.attach(msgImage3)
fp4 = open("topurl_all.png","rb")
msgImage4 = MIMEImage(fp4.read())
fp4.close()
msgImage4.add_header('Content-ID','<topurl_all>')
msg.attach(msgImage4)
sender = '[email protected]'
receiver = '[email protected]'
subject = 'Top IP 测试'
server = smtplib.SMTP()
#server.set_debuglevel(1)
server.connect("mail.example.com",'25')
server.starttls()
server.login("[email protected]","password")
server.sendmail(sender,receiver,msg.as_string())
server.quit()