-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathoracle_query.py
More file actions
85 lines (65 loc) · 2.15 KB
/
Copy pathoracle_query.py
File metadata and controls
85 lines (65 loc) · 2.15 KB
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
#!/usr/bin/env python
import os
import sys
from StringIO import StringIO
# The SQL query you want to run. It should return two columns:
# a string and a number. The string will be used as the metric
# name.
#
# For example, the query "select ename, sal from scott.emp"
# will create metrics like this:
#
# oracle_query.salary.SMITH = 800
# oracle_query.salary.JONES = 2975
# etc.
#
SQL = """
select ename, sal from scott.emp
"""
# Location of your ORACLE_HOME.
ORACLE_HOME = "/u01/app/oracle/product/11.2.0/dbhome_1"
# Login details for the Oracle DB.
ORACLE_USER = "scott"
ORACLE_PASSWORD = "tiger"
# Hostname and port number of the TNS listener.
ORACLE_HOST = "localhost"
ORACLE_PORT = 1521
# Oracle DB service name, as reported by "lsnrctl status".
ORACLE_SVC = "oracle11g"
# If ORACLE_SVC is not set, we will use the SID instead.
ORACLE_SID = "ORCL"
# Additional prefix to add to metric names. The generated metrics
# will be named like "oracle_query.PREFIX.name = value".
PREFIX="salary"
##################################
# DO NOT CHANGE BELOW THIS POINT #
##################################
if 'ORACLE_HOME' not in os.environ:
os.environ['ORACLE_HOME'] = ORACLE_HOME
os.environ['LD_LIBRARY_PATH'] = ORACLE_HOME + '/lib'
try:
os.execv(sys.argv[0], sys.argv)
except Exception, exc:
print 'Failed re-exec:', exc
sys.exit(1)
try:
import cx_Oracle
if ORACLE_SVC:
dsn = cx_Oracle.makedsn(ORACLE_HOST, ORACLE_PORT, service_name=ORACLE_SVC)
else:
dsn = cx_Oracle.makedsn(ORACLE_HOST, ORACLE_PORT, ORACLE_SID)
con = cx_Oracle.connect('{user}/{password}@{dsn}'.format(user=ORACLE_USER, password=ORACLE_PASSWORD, dsn=dsn))
cur = con.cursor()
cur.execute(SQL)
buf = StringIO()
for result in cur:
buf.write("{prefix}.{name}={value};;;; ".format(prefix=PREFIX, name=result[0], value=result[1]))
cur.close()
con.close()
print 'OK | ' + buf.getvalue()
sys.exit(0)
except ImportError as ex:
print 'CRITICAL | Unable to import cx_Oracle: ' + str(ex)
except cx_Oracle.DatabaseError as ex:
print 'CRITICAL | Oracle database error: ' + str(ex.message)
sys.exit(2)