-
Notifications
You must be signed in to change notification settings - Fork 1
/
http-stats.py
executable file
·162 lines (133 loc) · 5.66 KB
/
http-stats.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
#!/usr/bin/env python3
import elasticsearch
import gmail
import kibana
import collections
import datetime
import io
import logging
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO, format='%(asctime)s:%(levelname)s:%(filename)s:%(lineno)d:%(message)s')
#logging.getLogger().disabled = True
#
# Gather the data
#
start = datetime.datetime.now() - datetime.timedelta(days=7)
query = {
'size': 5000,
'query': {
'bool': {
'must': [
{
'match': {
'event_type': 'http'
}
},
{
'range': {
'timestamp': {
'gte': start.isoformat(),
'lte': 'now',
'time_zone': '-05:00'
}
}
}
]
}
},
'sort': [
{ 'timestamp': 'asc' },
{ '_id': 'asc' }
]
}
http_entries = {}
for record in elasticsearch.search(query, start):
flow_id = record['_source']['flow_id']
if 'hostname' not in record['_source']['http']:
logging.warning('http record with http.hostname missing (flow_id = %d)', flow_id)
continue
hostname = record['_source']['http']['hostname']
if flow_id in http_entries:
assert http_entries[flow_id]['hostname'] == hostname
else:
flow_record = elasticsearch.flow(flow_id, start)
if flow_record is None:
logging.warning('flow record with flow_id = %d missing', flow_id)
bandwidth = 0
else:
bandwidth = flow_record['_source']['flow']['bytes_toclient'] + \
flow_record['_source']['flow']['bytes_toserver']
http_entries[flow_id] = {'hostname':hostname, 'bandwidth':bandwidth}
query['query']['bool']['must'][0]['match']['event_type'] = 'tls'
tls_entries = {}
for record in elasticsearch.search(query, start):
flow_id = record['_source']['flow_id']
hostname = record['_source']['tls']['sni']
if flow_id in tls_entries:
assert tls_entries[flow_id]['hostname'] == hostname
else:
flow_record = elasticsearch.flow(flow_id, start)
if flow_record is None:
logging.warning('flow record with flow_id = %d missing', flow_id)
bandwidth = 0
else:
bandwidth = flow_record['_source']['flow']['bytes_toclient'] + \
flow_record['_source']['flow']['bytes_toserver']
tls_entries[flow_id] = {'hostname':hostname, 'bandwidth':bandwidth}
#
# Organize the data
#
http_entries_by_count = collections.defaultdict(int)
for entry in http_entries.values():
http_entries_by_count[entry['hostname']] += 1
http_entries_by_bandwidth = collections.defaultdict(int)
for entry in http_entries.values():
http_entries_by_bandwidth[entry['hostname']] += entry['bandwidth']
tls_entries_by_count = collections.defaultdict(int)
for entry in tls_entries.values():
tls_entries_by_count[entry['hostname']] += 1
tls_entries_by_bandwidth = collections.defaultdict(int)
for entry in tls_entries.values():
tls_entries_by_bandwidth[entry['hostname']] += entry['bandwidth']
#
# Display the data
#
body = io.StringIO()
body.write('<html><body>')
# Gmail strips out specific css fields, defining columns using tables and whitelisted properties:
# https://julie.io/writing/gmail-first-strategy-for-responsive-emails/
prefix = '<table width="100%" align="left" style="width:100%; max-width:200px"><tr><td style="padding-left:10px; padding-right:10px">'
postfix = '</td></tr></table>'
body.write(f'{prefix}<h1>http connections</h1><ul>')
for entry in sorted(http_entries_by_count.items(), key=lambda i:i[1], reverse=True):
url = kibana.url(f'http.hostname:{entry[0]}', start)
body.write(f'<li><a href="{url}">{entry[0]}</a> {entry[1]}</li>')
body.write(f'</ul>{postfix}')
def filesize(n: int) -> str:
if n < 1024:
return f'{n}B'
elif n < 1024**2:
return f'{n / float(1024):.2f}KB'
elif n < 1024**3:
return f'{n / float(1024**2):.2f}MB'
elif n < 1024**4:
return f'{n / float(1024**3):.2f}GB'
else:
return f'{n / float(1024**4):.2f}TB'
body.write(f'{prefix}<h1>http bandwidth</h1><ul>')
for entry in sorted(http_entries_by_bandwidth.items(), key=lambda i:i[1], reverse=True):
url = kibana.url(f'http.hostname:{entry[0]}', start)
body.write(f'<li><a href="{url}">{entry[0]}</a> {filesize(entry[1])}</li>')
body.write(f'</ul>{postfix}')
body.write(f'{prefix}<h1>tls connections</h1><ul>')
for entry in sorted(tls_entries_by_count.items(), key=lambda i:i[1], reverse=True):
url = kibana.url(f'tls.sni:{entry[0]}', start)
body.write(f'<li><a href="{url}">{entry[0]}</a> {entry[1]}</li>')
body.write(f'</ul>{postfix}')
body.write(f'{prefix}<h1>tls bandwidth</h1><ul>')
for entry in sorted(tls_entries_by_bandwidth.items(), key=lambda i:i[1], reverse=True):
url = kibana.url(f'tls.sni:{entry[0]}', start)
body.write(f'<li><a href="{url}">{entry[0]}</a> {filesize(entry[1])}</li>')
body.write(f'</ul>{postfix}')
body.write('</body></html>')
gmail.send(None, 'frosty http-stats', body.getvalue(), html=True)