-
Notifications
You must be signed in to change notification settings - Fork 1
/
cooldbprobe.py
223 lines (207 loc) · 11 KB
/
cooldbprobe.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
# Any copyright is dedicated to the Public Domain.
# http://creativecommons.org/publicdomain/zero/1.0/
from __future__ import with_statement
import os.path
import threading
import sys
import logging
from logging.config import fileConfig
from java.util.concurrent import Callable
from com.ziclix.python.sql import PyConnection
from com.ziclix.python.sql import zxJDBC
import com.xhaus.jyson.JysonCodec as json
import org.apache.tomcat.jdbc.pool as dbpool
import java.util.Properties as Properties
import traceback
from dummyprobe import DummyProbe
import org.joda.time.DateTime as DateTime
logger = logging.getLogger(__name__)
class DatabaseProbe(DummyProbe):
def initialize(self):
#Instanciate tomcat's connection pool
p = dbpool.PoolProperties()
p.setUrl(self.getInputProperty("url"))
p.setDriverClassName(self.getInputProperty("driverClassName"))
p.setUsername(self.getInputProperty("username"))
p.setPassword(self.getInputProperty("password"))
#TODO: check encoding and charset select * from nls_database_parameters where parameter='NLS_NCHAR_CHARACTERSET' - {"PARAMETER":"NLS_NCHAR_CHARACTERSET","VALUE":"AL16UTF16"}
try:
p.setMinIdle(int(self.getInputProperty("minIdle")))
except:
p.setMinIdle(2)
try:
p.setMaxIdle(int(self.getInputProperty("maxIdle")))
except:
p.setMaxIdle(2)
try:
p.setMaxAge(int(self.getInputProperty("maxAge")))
except:
p.setMaxAge(86400)
try:
p.setValidationQuery(self.getInputProperty("validationQuery"))
except:
logger.debug("Dude, no validation query for connection pool")
try:
p.setInitSQL(self.getInputProperty("initSQL"))
except:
logger.debug("Dude, no initial query for connection pool")
if self.getInputProperty("dbProperties") != None:
dbProperties = Properties()
for prop in self.getInputProperty("dbProperties"):
value = str(self.getInputProperty("dbProperties")[prop])
dbProperties.setProperty(prop, value)
logger.debug("Dude, database property found: %s = %s", prop, value)
p.setDbProperties(dbProperties)
self.metrics = {}
if self.getInputProperty("metrics") != None:
for metric in self.getInputProperty("metrics"):
self.metrics[metric] = 1
self.terms = {}
if self.getInputProperty("terms") != None:
for term in self.getInputProperty("terms"):
self.terms[term] = 1
self.jdbcPoolProperties = p
try:
self.interval = int(self.getInputProperty("interval"))
except:
self.interval = 60
try:
#open file if exists
if isinstance(self.getInputProperty("sql"), basestring) and os.path.isfile(self.getInputProperty("sql")):
with open(self.getInputProperty("sql"), "r") as ins:
self.sql = []
for line in ins:
self.sql.append({ "statement": line.rstrip() })
else:
self.sql = self.getInputProperty("sql")
except:
self.sql = []
idx = 0
for phrase in self.sql:
if "id" not in phrase:
phrase["id"] = "q" + str(idx)
idx += 1
logger.debug("Dude, queries setup: %s", self.sql)
logger.debug("Dude, connection pool setup: %s", p)
logger.debug("Dude, connection pool dbProperties: %s", p.getDbProperties())
p.setJdbcInterceptors('org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;' + 'org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer')
self.datasource = dbpool.DataSource()
self.datasource.setPoolProperties(self.jdbcPoolProperties)
self.cycle["queryParameters"] = {}
def startQueryCycle(self, queryId):
self.runtime[queryId + "jodaStart"] = DateTime()
self.cycle["queryParameters"][queryId + "qstart"] = self.runtime[queryId + "jodaStart"].getMillis()
self.cycle["queryParameters"][queryId + "qstartdt"] = str(self.runtime[queryId + "jodaStart"])
def finishQueryCycle(self, queryId):
self.runtime[queryId + "jodaEnd"] = DateTime()
self.cycle["queryParameters"][queryId + "qend"] = self.runtime[queryId + "jodaEnd"].getMillis()
self.cycle["queryParameters"][queryId + "qenddt"] = str(self.runtime[queryId + "jodaEnd"])
self.cycle["queryParameters"][queryId + "qlaststart"] = self.cycle["queryParameters"][queryId + "qstart"]
self.cycle["queryParameters"][queryId + "qlaststartdt"] = self.cycle["queryParameters"][queryId + "qstartdt"]
self.cycle["queryParameters"][queryId + "qelapsed"] = self.runtime[queryId + "jodaEnd"].getMillis() - self.runtime[queryId + "jodaStart"].getMillis()
logger.info("Finished query %s cycle in %d", queryId, self.cycle["queryParameters"][queryId + "qelapsed"])
def getQueryParameter(self, queryId, paramName):
if queryId + paramName in self.cycle["queryParameters"]:
return self.cycle["queryParameters"][queryId + paramName]
else:
return None
def tick(self):
for phrase in self.sql:
self.startQueryCycle(phrase["id"])
preparedStatementParams = []
preparedStatementParamsDict = {}
logger.info(phrase["statement"])
numrows = 0
conn = PyConnection(self.datasource.getConnection())
try:
with conn.cursor(1) as cursor:
#TODO: review, index out of range very possible
if "parameter" in phrase:
logger.debug("Dude, got parameters!!")
for parameter in phrase["parameter"]:
strlist = parameter.split("$cycle.")
if len(strlist) > 0:
if strlist[1] in ["qend","qstart","qlaststart","qenddt","qstartdt","qlaststartdt","qelapsed"]:
value = self.getQueryParameter(phrase["id"], strlist[1])
if value == None:
logger.debug("Dude, couldn't fetch local parameter %s = %s, going global", parameter, value)
value = self.getCycleProperty(strlist[1][1:])
preparedStatementParams.append(value)
preparedStatementParamsDict[parameter] = value
logger.debug("Dude, got local parameter %s = %s", parameter, value)
else:
value = self.getCycleProperty(strlist[1])
if value == None:
value = phrase[strlist[1]]
preparedStatementParams.append(value)
preparedStatementParamsDict[parameter] = value
logger.debug("Dude, got global parameter %s = %s", parameter, value)
logger.debug(strlist[1])
logger.debug("Dude, preparing statement: %s", phrase["id"])
query = cursor.prepare(phrase["statement"])
logger.debug("Dude, executing statement: %s", phrase["id"])
cursor.execute(query, preparedStatementParams)
row = None
logger.debug("Dude, starting fetch for statement: %s", phrase["id"])
if cursor.description != None:
fields = [i[0] for i in cursor.description]
row = cursor.fetchone()
else:
fields = []
conn.commit()
self.processData({ '@timestamp':self.getQueryParameter(phrase["id"], "qstart"), "statement": phrase["statement"], "parameters": preparedStatementParamsDict } )
#TODO: process data with commit timestamp and whatnot
while row is not None:
idx = 0
out = {}
rowDict = {}
metrics = {}
terms = {}
for field in fields:
if isinstance(row[idx], str):
if self.getInputProperty("ignoreFieldIfEmptyString") and len(row[idx]) == 0:
logger.warning("Ignoring key %s due to empty value", field)
else:
rowDict[field] = row[idx]
elif isinstance(row[idx], unicode):
if self.getInputProperty("ignoreFieldIfEmptyString") and len(row[idx]) == 0:
logger.warning("Ignoring key %s due to empty value", field)
else:
rowDict[field] = row[idx]
else:
rowDict[field] = row[idx]
idx = idx + 1
for key in rowDict:
if key in self.metrics:
metrics[key] = rowDict[key]
elif key in self.terms:
terms[key] = rowDict[key]
else:
out[key] = rowDict[key]
self.processData(out)
for key in metrics:
try:
out["metric"] = key
if self.getInputProperty("decimalMark"):
metrics[key] = metrics[key].replace(self.getInputProperty("decimalMark"), ".")
out["value"] = float(metrics[key])
self.processData(out)
except Exception, ex:
logger.warning("Failure to parse %s as float for metric %s", key, metrics[key])
#self.processData(out)
if 'value' in out:
del out['value']
for key in terms:
out["metric"] = key
out["term"] = str(terms[key])
self.processData(out)
row = cursor.fetchone()
query.close()
assert query.closed
except Exception, ex:
logger.debug("\n _ _ _ \n __| |_ _ __| | ___| |\n / _` | | | |/ _` |/ _ \ |\n| (_| | |_| | (_| | __/_|\n \__,_|\__,_|\__,_|\___(_)\n")
logger.debug(ex)
raise
finally:
conn.close()
self.finishQueryCycle(phrase["id"])