-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpgstats.py
442 lines (404 loc) · 17.1 KB
/
pgstats.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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
####################################################################
######### Copyright 2016-2017 BigSQL ###########
####################################################################
from flask import Blueprint, request, jsonify, session
from flask.views import MethodView
from flask_security import login_required, roles_required, current_user, roles_accepted, auth_required
from pgadmin.model import db, Role, User, Server, ServerGroup, Process
from pgadmin.browser.server_groups.servers.types import ServerType
from flask import g
from config import PG_DEFAULT_DRIVER
from pgadmin.utils.driver import get_driver
from pgadmin.utils.crypto import encrypt, decrypt, pqencryptpassword
from pgadmin.utils.driver.psycopg2 import ServerManager
from pgqueries import *
from datetime import datetime
pgstats = Blueprint('pgstats', 'pgstats', url_prefix='/pgstats')
class ConnectAPI(MethodView):
@auth_required('token', 'session')
@roles_accepted('Administrator', 'User')
def post(self):
json_dict = {}
if not current_user:
json_dict['state'] = "error"
json_dict['msg'] = "Access denied."
return jsonify(json_dict)
else:
args= request.json.get('params')
sid = args.get('sid')
gid = args.get('gid')
pwd = args.get('pwd')
save = args.get('save')
update = args.get('update')
try:
pg_server = Server.query.filter_by(
id=sid,
servergroup_id=gid,
user_id=current_user.id
).first()
if not pg_server:
json_dict['state'] = "error"
json_dict['msg'] = "Server not available in metadata."
return jsonify(json_dict)
else:
json_dict['discovery_id'] = pg_server.discovery_id
manager = get_driver(PG_DEFAULT_DRIVER).connection_manager(int(sid))
if update:
manager.update(pg_server)
conn = manager.connection()
if conn.connected():
try:
cur = conn.conn.cursor()
cur.execute(version_query)
x = cur.fetchone()[0]
cur.close()
ver_platform = x.split(",")[0]
json_dict['version'] = ver_platform
version_info = ver_platform.split()
json_dict['pg_version'] = version_info[1]
json_dict['state'] = "success"
json_dict['msg'] = "Already Connected."
return jsonify(json_dict)
except Exception as e:
pass
password = ""
if pwd:
password = pwd
try:
password = encrypt(password, current_user.password)
except Exception as e:
errmsg = "ERROR: " + str(e)
json_dict['state'] = "error"
json_dict['msg'] = errmsg
return jsonify(json_dict)
else:
if pg_server.password:
password=pg_server.password
else:
json_dict['state'] = "error"
json_dict['need_pwd'] = True
json_dict['msg'] = "Password required."
return jsonify(json_dict)
status = True
try:
status, errmsg = conn.connect(
password=password,
server_types=ServerType.types()
)
except Exception as e:
errmsg = "ERROR: " + str(e)
json_dict['state'] = "error"
json_dict['msg'] = errmsg
return jsonify(json_dict)
if not status:
emsg = errmsg.split("\n")
if len(emsg)>1:
if emsg[0] == emsg[1]:
errmsg = emsg[0]
if hasattr(str, 'decode'):
errmsg = errmsg.decode('utf-8')
if errmsg.find("timeout expired") >= 0:
errmsg = "Connection timed out."
json_dict['state'] = "error"
json_dict['msg'] = errmsg
if errmsg.find("password authentication failed")>=0:
json_dict['need_pwd'] = True
return jsonify(json_dict)
else:
manager.update_session()
cur = conn.conn.cursor()
cur.execute(version_query)
x = cur.fetchone()[0]
cur.close()
ver_platform = x.split(",")[0]
json_dict['version'] = ver_platform
version_info = ver_platform.split()
json_dict['pg_version'] = version_info[1]
json_dict['msg'] = "connected sucessfully"
if save and pwd:
pg_server.password = password
db.session.commit()
except Exception as e:
errmsg = "ERROR: " + str(e)
json_dict['state'] = "error"
json_dict['msg'] = errmsg
return jsonify(json_dict)
pgstats.add_url_rule('/connect/', view_func=ConnectAPI.as_view('connect'))
class ConnStatusAPI(MethodView):
@auth_required('token', 'session')
@roles_accepted('Administrator', 'User')
def get(self):
json_dict = {}
if not current_user:
json_dict['state'] = "error"
json_dict['msg'] = "Access denied."
return jsonify(json_dict)
else:
sid = request.args.get('sid')
gid = request.args.get('gid')
json_dict = {}
pg_server = Server.query.filter_by(
id=sid,
servergroup_id=gid,
user_id=current_user.id
).first()
if pg_server:
json_dict['discovery_id']=pg_server.discovery_id
try:
manager = get_driver(PG_DEFAULT_DRIVER).connection_manager(int(sid))
conn = manager.connection()
if conn.connected():
try:
cur = conn.conn.cursor()
cur.execute(version_query)
x = cur.fetchone()[0]
cur.close()
ver_platform = x.split(",")[0]
json_dict['version'] = ver_platform
version_info = ver_platform.split()
json_dict['pg_version'] = version_info[1]
json_dict['state'] = "success"
json_dict['msg'] = "Already Connected."
return jsonify(json_dict)
except Exception as e:
json_dict['state'] = "error"
json_dict['msg'] = str(e)
return jsonify(json_dict)
else:
json_dict['state'] = "error"
json_dict['msg'] = "Not connected."
return jsonify(json_dict)
except Exception as e:
errmsg = "ERROR: " + str(e)
json_dict['state'] = "error"
json_dict['msg'] = errmsg
return jsonify(json_dict)
pgstats.add_url_rule('/conn_status/', view_func=ConnStatusAPI.as_view('conn_status'))
class StatsAPI(MethodView):
@auth_required('token', 'session')
@roles_accepted('Administrator', 'User')
def get(self):
json_dict = {}
if not current_user:
json_dict['state'] = "error"
json_dict['msg'] = "Access denied."
return jsonify(json_dict)
sid = request.args.get('sid')
gid = request.args.get('gid')
manager = get_driver(PG_DEFAULT_DRIVER).connection_manager(int(sid))
conn = manager.connection()
json_dict = {}
if not conn.connected():
return jsonify({'msg': 'Connection is closed.', 'state':"error"})
else:
try:
stats_timestamp = datetime.utcnow()
stats_time = stats_timestamp.strftime('%Y/%m/%d %H:%M:%S')
cur = conn.conn.cursor()
cur.execute(connection_query)
columns = [desc[0] for desc in cur.description]
result = []
for res in cur:
result.append(dict(zip(columns, res)))
cur.close()
json_dict['connections'] = {}
for r in result:
json_dict['connections'][str(r['state'])] = r['count']
cur = conn.conn.cursor()
cur.execute(metrics_query)
columns = [desc[0] for desc in cur.description]
result = []
for res in cur:
result.append(dict(zip(columns, res)))
cur.close()
tps = result[0]
for key in tps.keys():
json_dict[key]=tps[key]
json_dict['time'] = stats_time
except Exception as e:
errmsg = "ERROR: " + str(e)
json_dict['state'] = "error"
json_dict['msg'] = errmsg
return jsonify(json_dict)
pgstats.add_url_rule('/stats/', view_func=StatsAPI.as_view('stats'))
class UptimeAPI(MethodView):
@auth_required('token', 'session')
@roles_accepted('Administrator', 'User')
def get(self):
json_dict = {}
if not current_user:
json_dict['state'] = "error"
json_dict['msg'] = "Access denied."
return jsonify(json_dict)
sid = request.args.get('sid')
gid = request.args.get('gid')
manager = get_driver(PG_DEFAULT_DRIVER).connection_manager(int(sid))
conn = manager.connection()
json_dict = {}
if not conn.connected():
return jsonify({'msg': 'Connection is closed.', 'state':"error"})
else:
try:
cur = conn.conn.cursor()
cur.execute(up_time_query)
result = cur.fetchone()
uptime = result[0]
cur.close()
import util
up_time = util.get_readable_time_diff(str(uptime).split('.')[0], precision=2)
json_dict['uptime'] = up_time
except Exception as e:
errmsg = "ERROR: " + str(e)
json_dict['state'] = "error"
json_dict['msg'] = errmsg
return jsonify(json_dict)
pgstats.add_url_rule('/uptime/', view_func=UptimeAPI.as_view('uptime'))
class ActivityAPI(MethodView):
@auth_required('token', 'session')
@roles_accepted('Administrator', 'User')
def get(self):
json_dict = {}
if not current_user:
json_dict['state'] = "error"
json_dict['msg'] = "Access denied."
return jsonify(json_dict)
sid = request.args.get('sid')
gid = request.args.get('gid')
manager = get_driver(PG_DEFAULT_DRIVER).connection_manager(int(sid))
conn = manager.connection()
json_dict = {}
if not conn.connected():
return jsonify({'msg': 'Connection is closed.', 'state':"error"})
else:
try:
stats_timestamp = datetime.utcnow()
stats_time = stats_timestamp.strftime('%Y/%m/%d %H:%M:%S')
cur = conn.conn.cursor()
cur.execute(activity_query)
columns = [desc[0] for desc in cur.description]
result = []
for res in cur:
result.append(dict(zip(columns, res)))
cur.close()
json_dict['activity'] = result
json_dict['time'] = stats_time
except Exception as e:
errmsg = "ERROR: " + str(e)
json_dict['state'] = "error"
json_dict['msg'] = errmsg
return jsonify(json_dict)
pgstats.add_url_rule('/activity/', view_func=ActivityAPI.as_view('activity'))
class DbListAPI(MethodView):
@auth_required('token', 'session')
@roles_accepted('Administrator', 'User')
def get(self):
json_dict = {}
if not current_user:
json_dict['state'] = "error"
json_dict['msg'] = "Access denied."
return jsonify(json_dict)
sid = request.args.get('sid')
gid = request.args.get('gid')
manager = get_driver(PG_DEFAULT_DRIVER).connection_manager(int(sid))
conn = manager.connection()
json_dict = {}
if not conn.connected():
return jsonify({'msg': 'Connection is closed.', 'state':"error"})
else:
try:
stats_timestamp = datetime.utcnow()
stats_time = stats_timestamp.strftime('%Y/%m/%d %H:%M:%S')
cur = conn.conn.cursor()
cur.execute(db_list_query)
columns = [desc[0] for desc in cur.description]
result = []
for res in cur:
result.append(dict(zip(columns, res)))
cur.close()
json_dict['activity'] = result
json_dict['time'] = stats_time
except Exception as e:
errmsg = "ERROR: " + str(e)
json_dict['state'] = "error"
json_dict['msg'] = errmsg
return jsonify(json_dict)
pgstats.add_url_rule('/db_list/', view_func=DbListAPI.as_view('db_list'))
class ConfigAPI(MethodView):
@auth_required('token', 'session')
@roles_accepted('Administrator', 'User')
def get(self):
json_dict = {}
if not current_user:
json_dict['state'] = "error"
json_dict['msg'] = "Access denied."
return jsonify(json_dict)
sid = request.args.get('sid')
gid = request.args.get('gid')
manager = get_driver(PG_DEFAULT_DRIVER).connection_manager(int(sid))
conn = manager.connection()
json_dict = {}
if not conn.connected():
return jsonify({'msg': 'Connection is closed.', 'state': "error"})
else:
try:
cur = conn.conn.cursor()
cur.execute(pg_settings_query)
columns = [desc[0] for desc in cur.description]
result = []
for res in cur:
result.append(dict(zip(columns, res)))
cur.close()
import itertools
final_list = []
for key, group in itertools.groupby(result, key=lambda x: x['category']):
final_list.append({'name': str(key), 'settings': list(group)})
json_dict['settings'] = final_list
json_dict['state'] = "success"
except Exception as e:
errmsg = "ERROR: " + str(e)
json_dict['state'] = "error"
json_dict['msg'] = errmsg
return jsonify(json_dict)
pgstats.add_url_rule('/config/', view_func=ConfigAPI.as_view('config'))
class DBCloseAPI(MethodView):
@auth_required('token', 'session')
@roles_accepted('Administrator', 'User')
def get(self):
json_dict = {}
if not current_user:
json_dict['state'] = "error"
json_dict['msg'] = "Access denied."
return jsonify(json_dict)
sid = request.args.get('sid')
gid = request.args.get('gid')
manager = get_driver(PG_DEFAULT_DRIVER).connection_manager(int(sid))
conn = manager.connection()
if not conn.connected():
return jsonify({'msg': 'Connection is already closed.', 'state': "success"})
else:
conn.conn.close()
manager.update_session()
return jsonify({'msg': 'db was closed succesfully', 'state': "success"})
pgstats.add_url_rule('/disconnect/', view_func=DBCloseAPI.as_view('disconnect'))
class CloseAllDBSessionsAPI(MethodView):
@auth_required('token', 'session')
@roles_accepted('Administrator', 'User')
def get(self):
json_dict = {}
if not current_user:
json_dict['state'] = "error"
json_dict['msg'] = "Access denied."
return jsonify(json_dict)
servers_list = Server.query.filter_by(
user_id=current_user.id
)
if servers_list.count()>0:
for server in servers_list:
try:
manager = get_driver(PG_DEFAULT_DRIVER).connection_manager(int(server.id))
conn = manager.connection()
conn.conn.close()
except Exception as e:
pass
return jsonify({"msg": "All Connection released", 'state': "success"})
pgstats.add_url_rule('/disconnectall/', view_func=CloseAllDBSessionsAPI.as_view('disconnectall'))