-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrontier-failed-q.py
252 lines (217 loc) · 8.48 KB
/
frontier-failed-q.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
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
# Checks number of failed queries (rejected/unprocessed queries and DB disconnections) (TEST)
# ====
# This notebook checks if there are failed queries:
# - Rejected queries: server is busy and doesn't respond to the query
# - DB disconnections: the query was processed by the Frontier server but the Oracle DB
# terminated the connection
# - Unprocessed queries: Oracle DB returned data, but it wasn't sent to the querying job
#
# It is run every hour from a cron job.
import sys
import datetime
from alerts import alarms
from elasticsearch import Elasticsearch
# from elasticsearch.helpers import scan
import json
with open('/config/config.json') as json_data:
config = json.load(json_data,)
# Period to check from now backwards
nhours = 1
# Limit of unsatisfied queries on a given server
ntotfail = 100
# Limit of unsatisfied queries for a given task
ntottask = 100
# Following 2 lines are for testing purposes only
# curtime = '20170126T120000.000Z'
# ct = datetime.datetime.strptime(curtime, "%Y%m%dT%H%M%S.%fZ")
# ### Get starting and current time for query interval
#
# We need :
# 1. Current UTC time (as set in timestamp on ES DB)
# 2. Previous date stamp (**nhours** ago) obtained from a time delta
#
# In order to subtract the time difference we need **ct** to be a datetime object
ct = datetime.datetime.utcnow()
ind = 'frontier_sql'
print(ind)
curtime = ct.strftime('%Y%m%dT%H%M%S.%f')[:-3] + 'Z'
td = datetime.timedelta(hours=nhours)
st = ct - td
starttime = st.strftime('%Y%m%dT%H%M%S.%f')[:-3] + 'Z'
print('start time', starttime)
print('current time', curtime)
# ### Establish connection to ES-DB and submit query
#
# Send a query to the ES-DB for documents containing information of failed queries
es = Elasticsearch(
hosts=[{'host': config['ES_HOST'], 'port':9200, 'scheme':'https'}],
basic_auth=(config['ES_USER'], config['ES_PASS']),
request_timeout=60)
if es.ping():
print('connected to ES.')
else:
print('no connection to ES.')
sys.exit(1)
condition = 'rejected:true OR disconn:true OR procerror:true'
my_query = {
"size": 0,
"query": {
"range": {
"@timestamp": {
"gte": starttime,
"lte": curtime,
"format": "basic_date_time"
}
}
},
"aggs": {
"servers": {
"terms": {
"size": 20,
"field": "frontierserver"
},
"aggs": {
"unserved": {
"filters": {
"filters": {
"rejected": {
"query_string": {
"query": "rejected:true"
}
},
"disconnect": {
"query_string": {
"query": "disconn:true"
}
},
"procerror": {
"query_string": {
"query": "procerror:true"
}
}
}
},
"aggs": {
"taskid": {
"terms": {
"field": "taskid",
"size": 5,
"order": {
"_count": "desc"
}
},
"aggs": {
"taskname": {
"terms": {
"field": "taskname",
"size": 5,
"order": {
"_count": "desc"
}
}
}
}
}
}
}
}
}
}
}
res = es.search(index=ind, body=my_query)
res = res['aggregations']['servers']['buckets']
taskinfo = {}
# Loop over Frontier servers
for r in res:
tkid = r['unserved']['buckets']['rejected']['taskid']['buckets']
for ti in tkid:
tkname = ti['taskname']['buckets']
for tn in tkname:
if ti['key'] not in taskinfo:
taskinfo[ti['key']] = [tn['key'], [int(tn['doc_count']), 0, 0]]
else:
count = int(taskinfo[ti['key']][1][0])
taskinfo[ti['key']][1][0] = count + int(tn['doc_count'])
tkid = r['unserved']['buckets']['disconnect']['taskid']['buckets']
for ti in tkid:
tkname = ti['taskname']['buckets']
for tn in tkname:
if ti['key'] not in taskinfo:
taskinfo[ti['key']] = [tn['key'], [0, int(tn['doc_count']), 0]]
else:
count = int(taskinfo[ti['key']][1][1])
taskinfo[ti['key']][1][1] = count + int(tn['doc_count'])
tkid = r['unserved']['buckets']['procerror']['taskid']['buckets']
for ti in tkid:
tkname = ti['taskname']['buckets']
for tn in tkname:
if ti['key'] not in taskinfo:
taskinfo[ti['key']] = [tn['key'], [0, 0, int(tn['doc_count'])]]
else:
count = int(taskinfo[ti['key']][1][2])
taskinfo[ti['key']][1][2] = count + int(tn['doc_count'])
taskid = {}
for key in taskinfo:
if sum(taskinfo[key][1]) > ntottask:
taskid[key] = taskinfo[key]
print('problematic tasks:', taskid)
frontiersrvr = {}
frsrvs = []
for r in res:
ub = r['unserved']['buckets']
rej = ub['rejected']['doc_count']
# if rej>0:
# print(ub['rejected']['taskid'])
dis = ub['disconnect']['doc_count']
# if dis>0:
# print(ub['rejected']['taskid'])
pre = ub['procerror']['doc_count']
# if pre>0:
# print(ub['rejected']['taskid'])
if rej + dis + pre < ntotfail:
continue
mes = ''
if rej > 0:
mes += str(rej) + " rejected "
if dis > 0:
mes += str(dis) + " disconnected "
if pre > 0:
mes += str(pre) + " unprocessed "
frontiersrvr[r['key']] = mes + 'queries.'
frsrvs.append(r['key'])
print('problematic servers:', frontiersrvr)
# ### Any non-zero value for any Frontier server triggers the alert
#
# The alert contains every Frontier server with failed queries and which kind of failures happened.
if len(frontiersrvr) > 0 or len(taskid) > 0:
ALARM = alarms('Analytics', 'Frontier', 'Failed queries')
ALARM.addAlarm(
body='Failed Frontier queries',
tags=frsrvs,
source={'servers': frontiersrvr, 'tasks': list(taskid)}
)
# body += '\tthis mail is to let you know that in the past ' + \
# str(nhours) + ' hours \n'
# if len(frontiersrvr) > 0:
# body += '\tthe following servers present failed queries: \n'
# body += '\t(attached numbers correspond to rejected, disconnected and unprocessed queries) \n\n'
# for fkey in frontiersrvr:
# body += fkey
# body += ' : '
# body += frontiersrvr[fkey]
# body += '\n'
# body += '\n'
# if len(taskid) > 0:
# body += '\tthe following tasks present not completed requests: \n'
# body += '\n'
# for tkey in taskid:
# body += 'Task id ' + \
# str(tkey) + ' with name ' + \
# taskid[tkey][0] + ' has ' + \
# str(taskid[tkey][1][0]) + ' rejected '
# body += str(taskid[tkey][1][1]) + ' disconnected and ' + \
# str(taskid[tkey][1][2]) + ' unprocessed queries \n'
# body += 'http://bigpanda.cern.ch/tasknew/' + str(tkey) + '\n'
# body += '\nConsult the following link to get a table with the most relevant taskids (beware that\n'
# body += 'you will have to select the appropriate time period in the upper right corner)\n'
# body += 'https://atlas-kibana.mwt2.org:5601/s/frontier/goto/c72d263c3e2b86f394ab99211c99b613\n'