forked from appfirst/nagios-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_oracle.py
111 lines (99 loc) · 4.38 KB
/
check_oracle.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
#!/user/bin/env python
"""
Author: Tony Ling
Executes 'sqlplus' with query and parses the output.
For use with AppFirst collector
Code based off: http://www.oracle-base.com/dba/script.php?category=monitoring&file=cache_hit_ratio.sql
Created on: 8/1/14
"""
import sys
import subprocess
import argparse
import re
# Metrics dictionary keys are -t metric arguments
# Key value is an array that holds 3 values
# Index 0 = sql query for the metric, 1 = script output string, 2 = less/greater for warning checks
metrics ={}
metrics['cache_hit_ratio'] = [("SELECT Round(((Sum(Decode(a.name, 'consistent gets', a.value, 0)) + "
"Sum(Decode(a.name, 'db block gets', a.value, 0)) - "
"Sum(Decode(a.name, 'physical reads', a.value, 0)) )/ "
"(Sum(Decode(a.name, 'consistent gets', a.value, 0)) + "
"Sum(Decode(a.name, 'db block gets', a.value, 0)))) "
"*100,2) \"Hit Ratio %\" "
"FROM v$sysstat a;"),'Cache Hit Ratio','less']
metrics['connected_users'] = [("SELECT count(*) FROM v$session "
"WHERE username IS NOT NULL "
"ORDER BY username ASC;"),'Number of connected users','greater']
metrics['db_block_changes'] = [("SELECT Sum(Decode(a.name, 'db block changes', a.value, 0)) \"DB Block Changes\" "
"FROM v$sysstat a;"),'Number of DB Block Changes','less']
metrics['db_block_gets'] = [("SELECT Sum(Decode(a.name, 'db block gets', a.value, 0)) \"DB Block Gets\" "
"FROM v$sysstat a;"),'Number of DB Block Gets','less']
metrics['physical_reads'] = [("SELECT Sum(Decode(a.name, 'physical reads', a.value, 0)) \"Physical Reads\" "
"FROM v$sysstat a;"),'Disk IO Physical Reads','less']
metrics['physical_writes'] = [("SELECT Sum(Decode(a.name, 'physical writes', a.value, 0)) \"Physical Writes\" "
"FROM v$sysstat a;"),'Disk IO Physical Writes','less']
metrics['enqueue_deadlocks'] = [("SELECT VALUE "
"FROM v$sysstat a WHERE NAME ='enqueue deadlocks';"),'Enqueue Deadlocks','less']
metrics['enqueue_waits'] = [("SELECT VALUE "
"FROM v$sysstat a WHERE NAME ='enqueue waits';"),'Enqueue Waits','less']
status_code = 3
parser = argparse.ArgumentParser()
parser.add_argument('-W', '--warning', help="Warning threshold, triggers if less than or equal to", default=None, type=float)
parser.add_argument('-C', '--critical', help="Critical threshold, triggers if less than or equal to", default=None, type=float)
parser.add_argument('-t', '--metric', help='Metric to choose', default=None, required=True)
args = parser.parse_args()
try:
query = metrics[args.metric][0]
msg = metrics[args.metric][1]
oracle_info = subprocess.Popen("echo {query} | sqlplus / as sysdba".format(query=query),
stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=True)
output, err = oracle_info.communicate()
oracle_info.wait()
except KeyError:
print "Error: Unrecognized/unsupported metric '{_metric}'".format(_metric=args.metric)
str_metrics = "Supported Metrics: ["
for metric in metrics:
str_metrics += ("'" + metric + "',")
print str_metrics[:-1] + "]"
sys.exit(status_code)
if (args.critical and args.critical < 0):
print "Error: -C Critical cannot be negative"
sys.exit(status_code)
elif (args.warning and args.warning < 0):
print "Error: -W Warning cannot be negative"
sys.exit(status_code)
counter = 0
value = None
lines = output.split('\n')
for line in lines:
tokens = line.strip().split('\n')
if tokens[0]:
found = re.search('--', tokens[0])
if found:
value = float(lines[counter+1].strip())
break;
counter += 1
if (value is None):
print "ERROR - Script Failed, check if sqlplus command is installed"
sys.exit(status_code)
if (metrics[args.metric][2] == 'less'):
if (args.critical and float(value) <= args.critical):
print "CRITICAL - " + msg + ": " + str(value) + "|" + metric + "=" + str(value)
status_code = 2
elif (args.warning and float(value) <= args.warning):
print "WARNING - " + msg + ": " + str(value) + "|" + metric + "=" + str(value)
status_code = 1
else:
print "OK - " + msg + ": " + str(value) + "|" + metric + "=" + str(value)
status_code = 0
else:
if (args.critical and float(value) >= args.critical):
print "CRITICAL - " + msg + ": " + str(value) + "|" + metric + "=" + str(value)
status_code = 2
elif (args.warning and float(value) >= args.warning):
print "WARNING - " + msg + ": " + str(value) + "|" + metric + "=" + str(value)
status_code = 1
else:
print "OK - " + msg + ": " + str(value) + "|" + metric + "=" + str(value)
status_code = 0
sys.exit(status_code)