-
Notifications
You must be signed in to change notification settings - Fork 4
/
sgas-db-tool
executable file
·385 lines (318 loc) · 9.31 KB
/
sgas-db-tool
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
#!/usr/bin/python
"""
Small tool for showing and removing entries in SGAS database.
Author: Magnus Jonsson <[email protected]>
Copyright: NeIC 2015
"""
import types
import decimal
import psycopg2
import psycopg2.extensions # not used, but enables tuple adaption
# load the psycopg extras module
import psycopg2.extras
import sys, getopt
from sgas.server import config
DEFAULT_POSTGRESQL_PORT = 5432
# Usage Records fields
UR_FIELDS = ('record_id', 'create_time', 'global_user_name', 'vo_type', 'vo_issuer',
'vo_name', 'vo_attributes', 'machine_name', 'global_job_id', 'local_job_id',
'local_user', 'job_name', 'charge', 'status', 'queue', 'host', 'node_count',
'processors', 'project_name', 'submit_host', 'start_time', 'end_time',
'submit_time', 'cpu_duration', 'wall_duration', 'cpu_duration_scaled',
'wall_duration_scaled', 'user_time', 'kernel_time', 'major_page_faults',
'runtime_environments', 'exit_code', 'insert_host', 'insert_identity', 'insert_time')
# Storage Records fields
SR_FIELDS = ('record_id', 'create_time', 'storage_system', 'storage_share', 'storage_media',
'storage_class', 'file_count', 'directory_path', 'local_user', 'local_group',
'user_identity', 'group_identity', 'group_attribute', 'site', 'start_time',
'end_time', 'resource_capacity_used', 'logical_capacity_used', 'insert_host',
'insert_identity', 'insert_time')
# defult location of sgas.conf (can be changed with -c option)
conf = '/etc/sgas.conf'
db = None
def options():
print("db-tool.py [options] <action> ...")
print("-h help")
print("Available actions:")
print(" listur list UR")
print(" showur show UR")
print(" deleteur delete UR")
print(" listsr list SR")
print(" showsr show SR")
print(" deletesr delete SR")
print("")
print("Use <action> -h for more help")
def commandline(argv):
global conf
try:
opts, args = getopt.getopt(argv,"hc:",["conf="])
except getopt.GetoptError:
options()
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
options()
sys.exit()
elif opt in ("-c", "--conf"):
conf = arg
return args
def listsr_options():
print("listur options")
print("-h help")
print("--output output format. Standard python % output using field name for usagerecord view")
print("--start_time starttime for record (will show SR within --start && --end) ISO-format")
print("--end_time endtime for record (will show SR within --start && --end) ISO-format")
print("--storage_system name of storage system to list")
print("--site site of project to list")
def listsr(argv):
output = "%(storage_system)-25s %(site)-15s %(start_time)-27s %(end_time)-27s %(record_id)-78s"
sql = 'SELECT * FROM storagerecords WHERE '
sqls = []
sqla = {}
try:
opts, args = getopt.getopt(argv,"h",["output=","start_time=","end_time=","machine_name=","project_name="])
except getopt.GetoptError:
listur_options()
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
listur_options()
sys.exit()
elif opt == '--output':
output = arg
elif opt == '--start_time':
sqls += ["end_time > %(endtime)s"]
sqla['endtime'] = arg
elif opt == '--end_time':
sqls += ["start_time > %(starttime)s"]
sqla['starttime'] = arg
elif out == '--storage_system':
sqls += ["storage_system = %(storage_system)s"]
sqla['storage_system'] = arg
elif out == '--site':
sqls += ["site = %(site)s"]
sqla['site'] = arg
if not len(sqls):
print("No constraints given")
sys.exit(1)
try:
cur = db.cursor()
cur.execute(sql + " AND ".join(sqls),sqla)
except psycopg2.Error as e:
print("DB Error: %s" % e)
sys.exit(1)
map = {}
for t in SR_FIELDS:
map[t] = t
print(output % map)
print("-" * len(output % map))
for row in cur.fetchall():
map= {}
for t in SR_FIELDS:
map[t] = row[0]
row = row[1:]
print(output % map)
def showsr_options():
print("showur options <recordid(s)>")
print( "-h help")
def showsr(argv):
output = "%(machine_name)-25s %(start_time)-27s %(end_time)-27s %(record_id)-78s"
sql = 'SELECT * FROM storagerecords WHERE '
try:
opts, args = getopt.getopt(argv,"h",[])
except getopt.GetoptError:
listur_options()
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
listur_options()
sys.exit()
for rid in args:
sqls = ["record_id = %(record_id)s"]
sqla = { 'record_id' : rid }
try:
cur = db.cursor()
cur.execute(sql + " AND ".join(sqls),sqla)
except psycopg2.Error as e:
print("DB Error: %s" % e)
sys.exit(1)
for row in cur.fetchall():
for t in SR_FIELDS:
print("%-22s %s" % (t,row[0]))
row = row[1:]
def deletesr_options():
print("deleteur options <recordid(s)>")
print("-h help")
def deletesr(argv):
sql = 'DELETE FROM storagedata WHERE '
try:
opts, args = getopt.getopt(argv,"h",[])
except getopt.GetoptError:
listur_options()
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
listur_options()
sys.exit()
for rid in args:
sqls = "record_id = %(record_id)s"
sqla = { 'record_id' : rid }
try:
cur = db.cursor()
cur.execute("BEGIN")
cur.execute(sql + sqls,sqla)
if cur.rowcount == 0:
print("Record not found")
sys.exit(1)
cur.execute("ROLLBACK")
except psycopg2.Error as e:
print("DB Error: %s" % e)
sys.exit(1)
def listur_options():
print("listur options")
print("-h help")
print("--output output format. Standard python % output using field name for usagerecord view")
print("--start_time starttime for record (will show UR within --start && --end) ISO-format")
print("--end_time endtime for record (will show UR within --start && --end) ISO-format")
print("--machine_name name of machine to show")
print("--project_name name of project")
def listur(argv):
output = "%(machine_name)-25s %(start_time)-27s %(end_time)-27s %(record_id)-78s"
sql = 'SELECT * FROM usagerecords WHERE '
sqls = []
sqla = {}
try:
opts, args = getopt.getopt(argv,"h",["output=","start_time=","end_time=","machine_name=","project_name="])
except getopt.GetoptError:
listur_options()
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
listur_options()
sys.exit()
elif opt == '--output':
output = arg
elif opt == '--start_time':
sqls += ["end_time > %(endtime)s"]
sqla['endtime'] = arg
elif opt == '--end_time':
sqls += ["start_time > %(starttime)s"]
sqla['starttime'] = arg
elif out == '--machine_name':
sqls += ["machine_name = %(machine_name)s"]
sqla['machinename'] = arg
if not len(sqls):
print("No constraints given")
sys.exit(1)
try:
cur = db.cursor()
cur.execute(sql + " AND ".join(sqls),sqla)
except psycopg2.Error as e:
print("DB Error: %s" % e)
sys.exit(1)
map = {}
for t in UR_FIELDS:
map[t] = t
print(output % map)
print("-" * len(output % map))
for row in cur.fetchall():
map= {}
for t in UR_FIELDS:
map[t] = row[0]
row = row[1:]
print(output % map)
def showur_options():
print("showur options <recordid(s)>")
print("-h help")
def showur(argv):
output = "%(machine_name)-25s %(start_time)-27s %(end_time)-27s %(record_id)-78s"
sql = 'SELECT * FROM usagerecords WHERE '
try:
opts, args = getopt.getopt(argv,"h",[])
except getopt.GetoptError:
listur_options()
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
listur_options()
sys.exit()
for rid in args:
sqls = ["record_id = %(record_id)s"]
sqla = { 'record_id' : rid }
try:
cur = db.cursor()
cur.execute(sql + " AND ".join(sqls),sqla)
except psycopg2.Error as e:
print("DB Error: %s" % e)
sys.exit(1)
for row in cur.fetchall():
for t in UR_FIELDS:
print("%-20s %s" % (t,row[0]))
row = row[1:]
def deleteur_options():
print("deleteur options <recordid(s)>")
print("-h help")
def deleteur(argv):
sql = 'DELETE FROM usagedata WHERE '
try:
opts, args = getopt.getopt(argv,"h",[])
except getopt.GetoptError:
listur_options()
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
listur_options()
sys.exit()
for rid in args:
sqls = "record_id = %(record_id)s"
sqla = { 'record_id' : rid }
try:
cur = db.cursor()
cur.execute("BEGIN")
cur.execute(sql + sqls,sqla)
if cur.rowcount == 0:
print("Record not found")
sys.exit(1)
except psycopg2.Error as e:
print("DB Error: %s" % e)
sys.exit(1)
def actions(args):
if not len(args):
print("No action given")
options()
sys.exit(1)
action = args[0]
args = args[1:]
if action == 'listur':
listur(args)
elif action == 'showur':
showur(args)
elif action == 'deleteur':
deleteur(args)
elif action == 'listsr':
listsr(args)
elif action == 'showsr':
showsr(args)
elif action == 'deletesr':
deletesr(args)
else:
print("Unknown action")
sys.exit(1)
def connectDb(dbstring):
args = [ e or None for e in dbstring.split(':') ]
host, port, database, user, password = args[:5]
if port is None:
port = DEFAULT_POSTGRESQL_PORT
try:
return psycopg2.connect(host=host, port=port, database=database, user=user, password=password)
except psycopg2.Error as e:
print("DB Error: %s" % e)
sys.exit(1)
def main(argv):
global db
args = commandline(argv)
cfg = config.readConfig(conf)
db = connectDb(cfg.get(config.SERVER_BLOCK,config.DB))
actions(args)
if __name__ == "__main__":
main(sys.argv[1:])