-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathanalyzers.py
More file actions
288 lines (251 loc) · 10.6 KB
/
analyzers.py
File metadata and controls
288 lines (251 loc) · 10.6 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import re
import textwrap
from pymongo import ASCENDING
from util import safe_divide
class RequestsPerMinuteByType(object):
def __init__(self, mongo_collection, media):
self.mongo = mongo_collection
self.label = 'Media: %s' % media
self.media = media
def run(self, time_limit):
self.mongo.ensure_index([('time', ASCENDING),
('media', ASCENDING),
])
N = self.mongo.find({'time': {'$gt': time_limit[0],
'$lt': time_limit[1]},
'media': self.media,
}).count()
td = time_limit[1] - time_limit[0]
self.data = 60.0 * safe_divide(float(N), td.seconds)
class CacheStatus(object):
def __init__(self, mongo_collection, status, media):
self.mongo = mongo_collection
self.status = status
self.media = media
self.label = ' %s (media: %s)' % (status, media)
def run(self, time_limit):
self.mongo.ensure_index([('time', ASCENDING),
('media', ASCENDING),
('status', ASCENDING)])
count = self.mongo.find({'time': {'$gt': time_limit[0],
'$lt': time_limit[1]},
'media': self.media,
'status': self.status,
}).count()
total = self.mongo.find({'time': {'$gt': time_limit[0],
'$lt': time_limit[1]},
'media': self.media,
'status': {'$ne': '-'},
}).count()
perc = safe_divide(100.0 * count, total)
self.data = perc
class Upstream5xxStatus(object):
def __init__(self, mongo_collection):
self.mongo = mongo_collection
self.label = '5xx'
def run(self, time_limit):
self.mongo.ensure_index([('time', ASCENDING),
('ups_st', ASCENDING)])
N = self.mongo.find({'time': {'$gt': time_limit[0],
'$lt': time_limit[1]},
'ups_st': re.compile(r'5\d\d'),
}).count()
self.data = N
class AvgUpstreamResponseTimePerServer(object):
def __init__(self, mongo_collection, server_address):
self.mongo = mongo_collection
self.server_address = server_address
self.label = ' ' + server_address
def run(self, time_limit):
self.mongo.ensure_index([('time', ASCENDING),
('ups_rt', ASCENDING),
('ups_ad', ASCENDING)])
result = self.mongo.group(
key=None,
condition={'time': {'$gt': time_limit[0], '$lt': time_limit[1]},
'ups_rt': {'$ne': '-'},
'ups_ad': self.server_address, },
initial={'count': 0, 'total': 0},
reduce=textwrap.dedent('''
function(doc, out) {
out.count++;
out.total += parseFloat(doc.ups_rt);
}'''),
finalize='function(out) {out.avg = out.total / out.count}',
)
if result:
avg = result[0]['avg']
self.data = avg
else:
self.data = 0.0
class AvgUpstreamResponseTimePerServerLoggedIn(object):
"""This one is the sh**
"""
data_length = 1
def __init__(self, mongo_collection, logged_in):
self.mongo = mongo_collection
self.logged_in = logged_in
self.label = 'need to auto-generate'
def run(self, time_limit):
self.mongo.ensure_index([('time', ASCENDING),
('ups_rt', ASCENDING),
('ups_ad', ASCENDING),
('wp_login', ASCENDING),
])
result = self.mongo.group(
key=['ups_ad'],
condition={'time': {'$gt': time_limit[0], '$lt': time_limit[1]},
'ups_rt': {'$ne': '-'},
'wp_login': re.compile(self.logged_in),
},
initial={'count': 0, 'total': 0},
reduce=textwrap.dedent('''
function(doc, out) {
out.count++;
out.total += parseFloat(doc.ups_rt);
}'''),
finalize='function(out) {out.avg = out.total / out.count}',
)
result = sorted(result, key=lambda item: item['ups_ad'])
# debug = [r['ups_ad'] for r in result]
# print debug
if result:
self.data = [r['avg'] for r in result]
self.data_length = len(self.data)
else:
self.data = [0.0] * self.data_length
class WordpressLoggedIn(object):
def __init__(self, mongo_collection):
self.mongo = mongo_collection
self.label = 'Wordpress logged in'
def run(self, time_limit):
self.mongo.ensure_index([('time', ASCENDING),
('wp_login', ASCENDING), ])
N = self.mongo.find({'time': {'$gt': time_limit[0],
'$lt': time_limit[1]},
'wp_login': re.compile(r'wordpress_logged_in_'),
}).count()
self.data = N
class WordpressLoggedInByUser(object):
def __init__(self, mongo_collection, wp_user):
self.mongo = mongo_collection
self.wp_user = wp_user
self.label = wp_user
self.pattern = r'wordpress_logged_in_%s' % wp_user
def run(self, time_limit):
self.mongo.ensure_index([('time', ASCENDING),
('wp_login', ASCENDING), ])
N = self.mongo.find({'time': {'$gt': time_limit[0],
'$lt': time_limit[1]},
'wp_login': re.compile(self.pattern),
}).count()
self.data = N
class PhpErrorCountByServer(object):
def __init__(self, mongo_collection, server):
self.mongo = mongo_collection
self.server = server
self.label = server
def run(self, time_limit):
self.mongo.ensure_index([('time', ASCENDING),
('server', ASCENDING)])
N = self.mongo.find({'time': {'$gt': time_limit[0],
'$lt': time_limit[1]},
'server': self.server,
}).count()
self.data = N
class SyslogCountByServerAndProcess(object):
def __init__(self, mongo_collection, server, process):
self.mongo = mongo_collection
self.server = server
self.process = process
self.label = server
def run(self, time_limit):
self.mongo.ensure_index([('time', ASCENDING),
('server', ASCENDING),
('process', ASCENDING),
])
N = self.mongo.find({'time': {'$gt': time_limit[0],
'$lt': time_limit[1]},
# 'server': self.server,
# 'process': re.compile(self.process),
}).count()
self.data = N
class GenericAverageValueAnalyzer(object):
def __init__(self, mongo_collection, server, parameter,
label='generic avg value'):
self.mongo = mongo_collection
self.server = server
self.parameter = parameter
self.label = label
def run(self, time_limit):
self.mongo.ensure_index([('time', ASCENDING),
('server', ASCENDING),
])
result = self.mongo.group(
key=None,
condition={'time': {'$gt': time_limit[0], '$lt': time_limit[1]},
'server': self.server,
},
initial={'count': 0, 'total': 0},
reduce='''function(doc, out) {
out.count++;
out.total += parseFloat(doc.%s)
}''' % self.parameter,
finalize='function(out) {out.avg = out.total / out.count}',
)
if result:
self.data = result[0]['avg']
else:
self.data = 0
class MysqlQuestionsPerSecond(object):
def __init__(self, mongo_collection, server):
self.mongo = mongo_collection
self.label = 'Questions/sec'
self.server = server # master or slave (e.g. us-my1 or us-my2)
def run(self, time_limit):
self.mongo.ensure_index([('time', ASCENDING),
('server', ASCENDING),
('questions_persecond', ASCENDING)])
result = self.mongo.group(
key=None,
condition={'time': {'$gt': time_limit[0], '$lt': time_limit[1]},
'questions_persecond': {'$gt': 0},
'server': self.server,
},
initial={'count': 0, 'total': 0},
reduce='''function(doc, out) {
out.count++;
out.total += parseFloat(doc.questions_persecond)
}''',
finalize='function(out) {out.avg = out.total / out.count}',
)
if result:
self.data = result[0]['avg']
else:
self.data = 0
class MysqlSlowQueriesPerSecond(object):
def __init__(self, mongo_collection, server):
self.mongo = mongo_collection
self.label = 'Slow queries/sec'
self.server = server # master or slave (e.g. us-my1 or us-my2)
def run(self, time_limit):
self.mongo.ensure_index([('time', ASCENDING),
('server', ASCENDING),
('slow_queries_persecond', ASCENDING)])
result = self.mongo.group(
key=None,
condition={'time': {'$gt': time_limit[0], '$lt': time_limit[1]},
'slow_queries_persecond': {'$gt': 0},
'server': self.server,
},
initial={'count': 0, 'total': 0},
reduce='''function(doc, out) {
out.count++;
out.total += parseFloat(doc.slow_queries_persecond)
}''',
finalize='function(out) {out.avg = out.total / out.count}',
)
if result:
self.data = result[0]['avg']
else:
self.data = 0