forked from danilochilene/Pyora
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyora.py
executable file
·587 lines (524 loc) · 20.5 KB
/
pyora.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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
#!/usr/bin/env python
# coding: utf-8
# vim: tabstop=2 noexpandtab
"""
Author: Danilo F. Chilene
Email: bicofino at gmail dot com
"""
import argparse
import cx_Oracle
import inspect
import json
import re
version = 0.2
class Checks(object):
def check_active(self):
"""Check Intance is active and open"""
sql = "select to_char(case when inst_cnt > 0 then 1 else 0 end, \
'FM99999999999999990') retvalue from (select count(*) inst_cnt \
from v$instance where status = 'OPEN' and logins = 'ALLOWED' \
and database_status = 'ACTIVE')"
self.cur.execute(sql)
res = self.cur.fetchall()
for i in res:
print i[0]
def rcachehit(self):
"""Read Cache hit ratio"""
sql = "SELECT to_char((1 - (phy.value - lob.value - dir.value) / \
ses.value) * 100, 'FM99999990.9999') retvalue \
FROM v$sysstat ses, v$sysstat lob, \
v$sysstat dir, v$sysstat phy \
WHERE ses.name = 'session logical reads' \
AND dir.name = 'physical reads direct' \
AND lob.name = 'physical reads direct (lob)' \
AND phy.name = 'physical reads'"
self.cur.execute(sql)
res = self.cur.fetchall()
for i in res:
print i[0]
def dsksortratio(self):
"""Disk sorts ratio"""
sql = "SELECT to_char(d.value/(d.value + m.value)*100, \
'FM99999990.9999') retvalue \
FROM v$sysstat m, v$sysstat d \
WHERE m.name = 'sorts (memory)' \
AND d.name = 'sorts (disk)'"
self.cur.execute(sql)
res = self.cur.fetchall()
for i in res:
print i[0]
def activeusercount(self):
"""Count of active users"""
sql = "select to_char(count(*)-1, 'FM99999999999999990') retvalue \
from v$session where username is not null \
and status='ACTIVE'"
self.cur.execute(sql)
res = self.cur.fetchall()
for i in res:
print i[0]
def dbsize(self):
"""Size of user data (without temp)"""
sql = "SELECT to_char(sum( NVL(a.bytes - NVL(f.bytes, 0), 0)), \
'FM99999999999999990') retvalue \
FROM sys.dba_tablespaces d, \
(select tablespace_name, sum(bytes) bytes from dba_data_files \
group by tablespace_name) a, \
(select tablespace_name, sum(bytes) bytes from \
dba_free_space group by tablespace_name) f \
WHERE d.tablespace_name = a.tablespace_name(+) AND \
d.tablespace_name = f.tablespace_name(+) \
AND NOT (d.extent_management like 'LOCAL' AND d.contents \
like 'TEMPORARY')"
self.cur.execute(sql)
res = self.cur.fetchall()
for i in res:
print i[0]
def dbfilesize(self):
"""Size of all datafiles"""
sql = "select to_char(sum(bytes), 'FM99999999999999990') retvalue \
from dba_data_files"
self.cur.execute(sql)
res = self.cur.fetchall()
for i in res:
print i[0]
def version(self):
"""Oracle version (Banner)"""
sql = "select banner from v$version where rownum=1"
self.cur.execute(sql)
res = self.cur.fetchall()
for i in res:
print i[0]
def uptime(self):
"""Instance Uptime (seconds)"""
sql = "select to_char((sysdate-startup_time)*86400, \
'FM99999999999999990') retvalue from v$instance"
self.cur.execute(sql)
res = self.cur.fetchmany(numRows=3)
for i in res:
print i[0]
def commits(self):
"""User Commits"""
sql = "select to_char(value, 'FM99999999999999990') retvalue from \
v$sysstat where name = 'user commits'"
self.cur.execute(sql)
res = self.cur.fetchmany(numRows=3)
for i in res:
print i[0]
def rollbacks(self):
"""User Rollbacks"""
sql = "select to_char(value, 'FM99999999999999990') retvalue from " \
"v$sysstat where name = 'user rollbacks'"
self.cur.execute(sql)
res = self.cur.fetchall()
for i in res:
print i[0]
def deadlocks(self):
"""Deadlocks"""
sql = "select to_char(value, 'FM99999999999999990') retvalue from \
v$sysstat where name = 'enqueue deadlocks'"
self.cur.execute(sql)
res = self.cur.fetchall()
for i in res:
print i[0]
def redowrites(self):
"""Redo Writes"""
sql = "select to_char(value, 'FM99999999999999990') retvalue from \
v$sysstat where name = 'redo writes'"
self.cur.execute(sql)
res = self.cur.fetchall()
for i in res:
print i[0]
def tblscans(self):
"""Table scans (long tables)"""
sql = "select to_char(value, 'FM99999999999999990') retvalue from \
v$sysstat where name = 'table scans (long tables)'"
self.cur.execute(sql)
res = self.cur.fetchall()
for i in res:
print i[0]
def tblrowsscans(self):
"""Table scan rows gotten"""
sql = "select to_char(value, 'FM99999999999999990') retvalue from \
v$sysstat where name = 'table scan rows gotten'"
self.cur.execute(sql)
res = self.cur.fetchall()
for i in res:
print i[0]
def indexffs(self):
"""Index fast full scans (full)"""
sql = "select to_char(value, 'FM99999999999999990') retvalue from \
v$sysstat where name = 'index fast full scans (full)'"
self.cur.execute(sql)
res = self.cur.fetchall()
for i in res:
print i[0]
def hparsratio(self):
"""Hard parse ratio"""
sql = "SELECT to_char(h.value/t.value*100,'FM99999990.9999') \
retvalue FROM v$sysstat h, v$sysstat t WHERE h.name = 'parse \
count (hard)' AND t.name = 'parse count (total)'"
self.cur.execute(sql)
res = self.cur.fetchall()
for i in res:
print i[0]
def netsent(self):
"""Bytes sent via SQL*Net to client"""
sql = "select to_char(value, 'FM99999999999999990') retvalue from \
v$sysstat where name = 'bytes sent via SQL*Net to client'"
self.cur.execute(sql)
res = self.cur.fetchall()
for i in res:
print i[0]
def netresv(self):
"""Bytes received via SQL*Net from client"""
sql = "select to_char(value, 'FM99999999999999990') retvalue from \
v$sysstat where name = 'bytes received via SQL*Net from client'"
self.cur.execute(sql)
res = self.cur.fetchall()
for i in res:
print i[0]
def netroundtrips(self):
"""SQL*Net roundtrips to/from client"""
sql = "select to_char(value, 'FM99999999999999990') retvalue from \
v$sysstat where name = 'SQL*Net roundtrips to/from client'"
self.cur.execute(sql)
res = self.cur.fetchall()
for i in res:
print i[0]
def logonscurrent(self):
"""Logons current"""
sql = "select to_char(value, 'FM99999999999999990') retvalue from \
v$sysstat where name = 'logons current'"
self.cur.execute(sql)
res = self.cur.fetchall()
for i in res:
print i[0]
def lastarclog(self):
"""Last archived log sequence"""
sql = "select to_char(max(SEQUENCE#), 'FM99999999999999990') \
retvalue from v$log where archived = 'YES'"
self.cur.execute(sql)
res = self.cur.fetchall()
for i in res:
print i[0]
def lastapplarclog(self):
"""Last applied archive log (at standby).Next items requires
[timed_statistics = true]"""
sql = "select to_char(max(lh.SEQUENCE#), 'FM99999999999999990') \
retvalue from v$loghist lh, v$archived_log al \
where lh.SEQUENCE# = al.SEQUENCE# and applied='YES'"
self.cur.execute(sql)
res = self.cur.fetchall()
for i in res:
print i[0]
def freebufwaits(self):
"""Free buffer waits"""
sql = "select to_char(time_waited, 'FM99999999999999990') retvalue \
from v$system_event se, v$event_name en \
where se.event(+) = en.name and en.name = 'free buffer waits'"
self.cur.execute(sql)
res = self.cur.fetchall()
for i in res:
print i[0]
def bufbusywaits(self):
"""Buffer busy waits"""
sql = "select to_char(time_waited, 'FM99999999999999990') retvalue \
from v$system_event se, v$event_name en where se.event(+) = \
en.name and en.name = 'buffer busy waits'"
self.cur.execute(sql)
res = self.cur.fetchall()
for i in res:
print i[0]
def logswcompletion(self):
"""log file switch completion"""
sql = "select to_char(time_waited, 'FM99999999999999990') retvalue \
from v$system_event se, v$event_name en where se.event(+) \
= en.name and en.name = 'log file switch completion'"
self.cur.execute(sql)
res = self.cur.fetchall()
for i in res:
print i[0]
def logfilesync(self):
"""Log file sync"""
sql = "select to_char(time_waited, 'FM99999999999999990') retvalue \
from v$system_event se, v$event_name en \
where se.event(+) = en.name and en.name = 'log file sync'"
self.cur.execute(sql)
res = self.cur.fetchall()
for i in res:
print i[0]
def logprllwrite(self):
"""Log file parallel write"""
sql = "select to_char(time_waited, 'FM99999999999999990') retvalue \
from v$system_event se, v$event_name en where se.event(+) \
= en.name and en.name = 'log file parallel write'"
self.cur.execute(sql)
res = self.cur.fetchall()
for i in res:
print i[0]
def enqueue(self):
"""Enqueue waits"""
sql = "select to_char(time_waited, 'FM99999999999999990') retvalue \
from v$system_event se, v$event_name en \
where se.event(+) = en.name and en.name = 'enqueue'"
self.cur.execute(sql)
res = self.cur.fetchall()
for i in res:
print i[0]
def dbseqread(self):
"""DB file sequential read waits"""
sql = "select to_char(time_waited, 'FM99999999999999990') retvalue \
from v$system_event se, v$event_name en where se.event(+) \
= en.name and en.name = 'db file sequential read'"
self.cur.execute(sql)
res = self.cur.fetchall()
for i in res:
print i[0]
def dbscattread(self):
"""DB file scattered read"""
sql = "select to_char(time_waited, 'FM99999999999999990') retvalue \
from v$system_event se, v$event_name en where se.event(+) \
= en.name and en.name = 'db file scattered read'"
self.cur.execute(sql)
res = self.cur.fetchall()
for i in res:
print i[0]
def dbsnglwrite(self):
"""DB file single write"""
sql = "select to_char(time_waited, 'FM99999999999999990') retvalue \
from v$system_event se, v$event_name en where se.event(+) \
= en.name and en.name = 'db file single write'"
self.cur.execute(sql)
res = self.cur.fetchall()
for i in res:
print i[0]
def dbprllwrite(self):
"""DB file parallel write"""
sql = "select to_char(time_waited, 'FM99999999999999990') retvalue \
from v$system_event se, v$event_name en where se.event(+) \
= en.name and en.name = 'db file parallel write'"
self.cur.execute(sql)
res = self.cur.fetchall()
for i in res:
print i[0]
def directread(self):
"""Direct path read"""
sql = "select to_char(time_waited, 'FM99999999999999990') retvalue \
from v$system_event se, v$event_name en where se.event(+) \
= en.name and en.name = 'direct path read'"
self.cur.execute(sql)
res = self.cur.fetchall()
for i in res:
print i[0]
def directwrite(self):
"""Direct path write"""
sql = "select to_char(time_waited, 'FM99999999999999990') retvalue \
from v$system_event se, v$event_name en where se.event(+) \
= en.name and en.name = 'direct path write'"
self.cur.execute(sql)
res = self.cur.fetchall()
for i in res:
print i[0]
def latchfree(self):
"""latch free"""
sql = "select to_char(time_waited, 'FM99999999999999990') retvalue \
from v$system_event se, v$event_name en where se.event(+) \
= en.name and en.name = 'latch free'"
self.cur.execute(sql)
res = self.cur.fetchall()
for i in res:
print i[0]
def tablespace(self, name):
"""Get tablespace usage"""
sql='''SELECT tablespace_name,
100-(TRUNC((max_free_mb/max_size_mb) * 100)) AS USED
FROM ( SELECT a.tablespace_name,b.size_mb,a.free_mb,b.max_size_mb,a.free_mb + (b.max_size_mb - b.size_mb) AS max_free_mb
FROM (SELECT tablespace_name,TRUNC(SUM(bytes)/1024/1024) AS free_mb FROM dba_free_space GROUP BY tablespace_name) a,
(SELECT tablespace_name,TRUNC(SUM(bytes)/1024/1024) AS size_mb,TRUNC(SUM(GREATEST(bytes,maxbytes))/1024/1024) AS max_size_mb
FROM dba_data_files GROUP BY tablespace_name) b WHERE a.tablespace_name = b.tablespace_name
) where tablespace_name='{0}' order by 1'''.format(name)
self.cur.execute(sql)
res = self.cur.fetchall()
for i in res:
print i[1]
def tablespace_abs(self, name):
"""Get tablespace in use"""
sql = '''SELECT df.tablespace_name "TABLESPACE", (df.totalspace - \
tu.totalusedspace) "FREEMB" from (select tablespace_name, \
sum(bytes) TotalSpace from dba_data_files group by tablespace_name) \
df ,(select sum(bytes) totalusedspace,tablespace_name from dba_segments \
group by tablespace_name) tu WHERE tu.tablespace_name = \
df.tablespace_name and df.tablespace_name = '{0}' '''.format(name)
self.cur.execute(sql)
res = self.cur.fetchall()
for i in res:
print i[1]
def show_tablespaces(self):
"""List tablespace names in a JSON like format for Zabbix use"""
sql = "SELECT tablespace_name FROM dba_tablespaces ORDER BY 1"
self.cur.execute(sql)
res = self.cur.fetchall()
key = ['{#TABLESPACE}']
lst = []
for i in res:
d = dict(zip(key, i))
lst.append(d)
print json.dumps({'data': lst})
def show_tablespaces_temp(self):
"""List temporary tablespace names in a JSON like
format for Zabbix use"""
sql = "SELECT TABLESPACE_NAME FROM DBA_TABLESPACES WHERE \
CONTENTS='TEMPORARY'"
self.cur.execute(sql)
res = self.cur.fetchall()
key = ['{#TABLESPACE_TEMP}']
lst = []
for i in res:
d = dict(zip(key, i))
lst.append(d)
print json.dumps({'data': lst})
def check_archive(self, archive):
"""List archive used"""
sql = "select trunc((total_mb-free_mb)*100/(total_mb)) PCT from \
v$asm_diskgroup_stat where name='{0}' \
ORDER BY 1".format(archive)
self.cur.execute(sql)
res = self.cur.fetchall()
for i in res:
print i[0]
def show_asm_volumes(self):
"""List als ASM volumes in a JSON like format for Zabbix use"""
sql = "select NAME from v$asm_diskgroup_stat ORDER BY 1"
self.cur.execute(sql)
res = self.cur.fetchall()
key = ['{#ASMVOLUME}']
lst = []
for i in res:
d = dict(zip(key, i))
lst.append(d)
print json.dumps({'data': lst})
def asm_volume_use(self, name):
"""Get ASM volume usage"""
sql = "select round(((TOTAL_MB-FREE_MB)/TOTAL_MB*100),2) from \
v$asm_diskgroup_stat where name = '{0}'".format(name)
self.cur.execute(sql)
res = self.cur.fetchall()
for i in res:
print i[0]
def query_lock(self):
"""Query lock"""
sql = "SELECT count(*) FROM gv$lock l WHERE block=1"
self.cur.execute(sql)
res = self.cur.fetchall()
for i in res:
print i[0]
def query_redologs(self):
"""Redo logs"""
sql = "select COUNT(*) from v$LOG WHERE STATUS='ACTIVE'"
self.cur.execute(sql)
res = self.cur.fetchall()
for i in res:
print i[0]
def query_rollbacks(self):
"""Query Rollback"""
sql = "select nvl(trunc(sum(used_ublk*4096)/1024/1024),0) from \
gv$transaction t,gv$session s where ses_addr = saddr"
self.cur.execute(sql)
res = self.cur.fetchall()
for i in res:
print i[0]
def query_sessions(self):
"""Query Sessions"""
sql = "select count(*) from gv$session where username is not null \
and status='ACTIVE'"
self.cur.execute(sql)
res = self.cur.fetchall()
for i in res:
print i[0]
def tablespace_temp(self, name):
"""Query temporary tablespaces"""
sql = "SELECT round(((TABLESPACE_SIZE-FREE_SPACE)/TABLESPACE_SIZE)*100,2) \
PERCENTUAL FROM dba_temp_free_space where \
tablespace_name='{0}'".format(name)
self.cur.execute(sql)
res = self.cur.fetchall()
for i in res:
print i[0]
def query_sysmetrics(self, name):
"""Query v$sysmetric parameters"""
sql = "select value from v$sysmetric where METRIC_NAME ='{0}' and \
rownum <=1 order by INTSIZE_CSEC".format(name.replace('_', ' '))
self.cur.execute(sql)
res = self.cur.fetchall()
for i in res:
print i[0]
def fra_use(self):
"""Query the Fast Recovery Area usage"""
sql = "select round((SPACE_LIMIT-(SPACE_LIMIT-SPACE_USED))/ \
SPACE_LIMIT*100,2) FROM V$RECOVERY_FILE_DEST"
self.cur.execute(sql)
res = self.cur.fetchall()
for i in res:
print i[0]
def show_users(self):
"""Query the list of users on the instance"""
sql = "SELECT username FROM dba_users ORDER BY 1"
self.cur.execute(sql)
res = self.cur.fetchall()
key = ['{#DBUSER}']
lst = []
for i in res:
d = dict(zip(key, i))
lst.append(d)
print json.dumps({'data': lst})
def user_status(self, dbuser):
"""Determines whether a user is locked or not"""
sql = "SELECT account_status FROM dba_users WHERE username='{0}'" \
.format(dbuser)
self.cur.execute(sql)
res = self.cur.fetchall()
for i in res:
print i[0]
class Main(Checks):
def __init__(self):
parser = argparse.ArgumentParser()
parser.add_argument('--username')
parser.add_argument('--password')
parser.add_argument('--address')
parser.add_argument('--database')
subparsers = parser.add_subparsers()
for name in dir(self):
if not name.startswith("_"):
p = subparsers.add_parser(name)
method = getattr(self, name)
argnames = inspect.getargspec(method).args[1:]
for argname in argnames:
p.add_argument(argname)
p.set_defaults(func=method, argnames=argnames)
self.args = parser.parse_args()
def db_connect(self):
a = self.args
username = a.username
password = a.password
address = a.address
database = a.database
self.db = cx_Oracle.connect("{0}/{1}@{2}/{3}".format(
username, password, address, database))
self.cur = self.db.cursor()
def db_close(self):
self.db.close()
def __call__(self):
try:
a = self.args
callargs = [getattr(a, name) for name in a.argnames]
self.db_connect()
try:
return self.args.func(*callargs)
finally:
self.db_close()
except Exception, err:
print 0
print str(err)
if __name__ == "__main__":
main = Main()
main()