| 1 |
/** |
| 2 |
* collectd - src/oracle.c |
| 3 |
* Copyright (C) 2008,2009 noris network AG |
| 4 |
* Copyright (C) 2012 Florian octo Forster |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or modify it |
| 7 |
* under the terms of the GNU General Public License as published by the |
| 8 |
* Free Software Foundation; only version 2 of the License is applicable. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, but |
| 11 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License along |
| 16 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 17 |
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 |
* |
| 19 |
* Linking src/oracle.c ("the oracle plugin") statically or dynamically with |
| 20 |
* other modules is making a combined work based on the oracle plugin. Thus, |
| 21 |
* the terms and conditions of the GNU General Public License cover the whole |
| 22 |
* combination. |
| 23 |
* |
| 24 |
* In addition, as a special exception, the copyright holders of the oracle |
| 25 |
* plugin give you permission to combine the oracle plugin with free software |
| 26 |
* programs or libraries that are released under the GNU LGPL and with code |
| 27 |
* included in the standard release of the Oracle® Call Interface (OCI) under |
| 28 |
* the Oracle® Technology Network (OTN) License (or modified versions of such |
| 29 |
* code, with unchanged license). You may copy and distribute such a system |
| 30 |
* following the terms of the GNU GPL for the oracle plugin and the licenses of |
| 31 |
* the other code concerned. |
| 32 |
* |
| 33 |
* Note that people who make modified versions of the oracle plugin are not |
| 34 |
* obligated to grant this special exception for their modified versions; it is |
| 35 |
* their choice whether to do so. The GNU General Public License gives |
| 36 |
* permission to release a modified version without this exception; this |
| 37 |
* exception also makes it possible to release a modified version which carries |
| 38 |
* forward this exception. However, without this exception the OTN License does |
| 39 |
* not allow linking with code licensed under the GNU General Public License. |
| 40 |
* |
| 41 |
* Oracle® is a registered trademark of Oracle Corporation and/or its |
| 42 |
* affiliates. Other names may be trademarks of their respective owners. |
| 43 |
* |
| 44 |
* Authors: |
| 45 |
* Florian octo Forster <octo at collectd.org> |
| 46 |
**/ |
| 47 |
|
| 48 |
#include "collectd.h" |
| 49 |
|
| 50 |
#include "plugin.h" |
| 51 |
#include "utils/common/common.h" |
| 52 |
#include "utils/db_query/db_query.h" |
| 53 |
|
| 54 |
#include <oci.h> |
| 55 |
|
| 56 |
/* |
| 57 |
* Data types |
| 58 |
*/ |
| 59 |
struct o_database_s { |
| 60 |
char *name; |
| 61 |
char *host; |
| 62 |
char *connect_id; |
| 63 |
char *username; |
| 64 |
char *password; |
| 65 |
char *plugin_name; |
| 66 |
|
| 67 |
udb_query_preparation_area_t **q_prep_areas; |
| 68 |
udb_query_t **queries; |
| 69 |
size_t queries_num; |
| 70 |
|
| 71 |
OCISvcCtx *oci_service_context; |
| 72 |
}; |
| 73 |
typedef struct o_database_s o_database_t; |
| 74 |
|
| 75 |
/* |
| 76 |
* Global variables |
| 77 |
*/ |
| 78 |
static udb_query_t **queries; |
| 79 |
static size_t queries_num; |
| 80 |
static o_database_t **databases; |
| 81 |
static size_t databases_num; |
| 82 |
|
| 83 |
OCIEnv *oci_env = NULL; |
| 84 |
OCIError *oci_error = NULL; |
| 85 |
|
| 86 |
/* |
| 87 |
* Functions |
| 88 |
*/ |
| 89 |
static void o_report_error(const char *where, /* {{{ */ |
| 90 |
const char *db_name, const char *query_name, |
| 91 |
const char *what, OCIError *eh) { |
| 92 |
char buffer[2048]; |
| 93 |
sb4 error_code; |
| 94 |
int status; |
| 95 |
|
| 96 |
if (db_name == NULL) |
| 97 |
db_name = "(none)"; |
| 98 |
if (query_name == NULL) |
| 99 |
query_name = "(none)"; |
| 100 |
|
| 101 |
/* An operation may cause / return multiple errors. Loop until we have |
| 102 |
* handled all errors available (with a fail-save limit of 16). */ |
| 103 |
for (unsigned int record_number = 1; record_number <= 16; record_number++) { |
| 104 |
memset(buffer, 0, sizeof(buffer)); |
| 105 |
error_code = -1; |
| 106 |
|
| 107 |
status = OCIErrorGet(eh, (ub4)record_number, |
| 108 |
/* sqlstate = */ NULL, &error_code, (text *)&buffer[0], |
| 109 |
(ub4)sizeof(buffer), OCI_HTYPE_ERROR); |
| 110 |
buffer[sizeof(buffer) - 1] = '\0'; |
| 111 |
|
| 112 |
if (status == OCI_NO_DATA) |
| 113 |
return; |
| 114 |
|
| 115 |
if (status == OCI_SUCCESS) { |
| 116 |
size_t buffer_length; |
| 117 |
|
| 118 |
buffer_length = strlen(buffer); |
| 119 |
while ((buffer_length > 0) && (buffer[buffer_length - 1] < 32)) { |
| 120 |
buffer_length--; |
| 121 |
buffer[buffer_length] = 0; |
| 122 |
} |
| 123 |
|
| 124 |
ERROR("oracle plugin: %s (db = %s, query = %s): %s failed: %s", where, |
| 125 |
db_name, query_name, what, buffer); |
| 126 |
} else { |
| 127 |
ERROR("oracle plugin: %s (db = %s, query = %s): %s failed. " |
| 128 |
"Additionally, OCIErrorGet failed with status %i.", |
| 129 |
where, db_name, query_name, what, status); |
| 130 |
return; |
| 131 |
} |
| 132 |
} |
| 133 |
} /* }}} void o_report_error */ |
| 134 |
|
| 135 |
static void o_database_free(o_database_t *db) /* {{{ */ |
| 136 |
{ |
| 137 |
if (db == NULL) |
| 138 |
return; |
| 139 |
|
| 140 |
sfree(db->name); |
| 141 |
sfree(db->connect_id); |
| 142 |
sfree(db->username); |
| 143 |
sfree(db->password); |
| 144 |
sfree(db->queries); |
| 145 |
sfree(db->plugin_name); |
| 146 |
|
| 147 |
if (db->q_prep_areas != NULL) |
| 148 |
for (size_t i = 0; i < db->queries_num; ++i) |
| 149 |
udb_query_delete_preparation_area(db->q_prep_areas[i]); |
| 150 |
free(db->q_prep_areas); |
| 151 |
|
| 152 |
sfree(db); |
| 153 |
} /* }}} void o_database_free */ |
| 154 |
|
| 155 |
/* Configuration handling functions {{{ |
| 156 |
* |
| 157 |
* <Plugin oracle> |
| 158 |
* <Query "plugin_instance0"> |
| 159 |
* Statement "SELECT name, value FROM table" |
| 160 |
* <Result> |
| 161 |
* Type "gauge" |
| 162 |
* InstancesFrom "name" |
| 163 |
* ValuesFrom "value" |
| 164 |
* </Result> |
| 165 |
* </Query> |
| 166 |
* |
| 167 |
* <Database "plugin_instance1"> |
| 168 |
* ConnectID "db01" |
| 169 |
* Username "oracle" |
| 170 |
* Password "secret" |
| 171 |
* Query "plugin_instance0" |
| 172 |
* </Database> |
| 173 |
* </Plugin> |
| 174 |
*/ |
| 175 |
|
| 176 |
static int o_config_add_database(oconfig_item_t *ci) /* {{{ */ |
| 177 |
{ |
| 178 |
o_database_t *db; |
| 179 |
int status; |
| 180 |
|
| 181 |
if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) { |
| 182 |
WARNING("oracle plugin: The `Database' block " |
| 183 |
"needs exactly one string argument."); |
| 184 |
return -1; |
| 185 |
} |
| 186 |
|
| 187 |
db = calloc(1, sizeof(*db)); |
| 188 |
if (db == NULL) { |
| 189 |
ERROR("oracle plugin: calloc failed."); |
| 190 |
return -1; |
| 191 |
} |
| 192 |
db->name = NULL; |
| 193 |
db->host = NULL; |
| 194 |
db->connect_id = NULL; |
| 195 |
db->username = NULL; |
| 196 |
db->password = NULL; |
| 197 |
db->plugin_name = NULL; |
| 198 |
|
| 199 |
status = cf_util_get_string(ci, &db->name); |
| 200 |
if (status != 0) { |
| 201 |
sfree(db); |
| 202 |
return status; |
| 203 |
} |
| 204 |
|
| 205 |
/* Fill the `o_database_t' structure.. */ |
| 206 |
for (int i = 0; i < ci->children_num; i++) { |
| 207 |
oconfig_item_t *child = ci->children + i; |
| 208 |
|
| 209 |
if (strcasecmp("ConnectID", child->key) == 0) |
| 210 |
status = cf_util_get_string(child, &db->connect_id); |
| 211 |
else if (strcasecmp("Host", child->key) == 0) |
| 212 |
status = cf_util_get_string(child, &db->host); |
| 213 |
else if (strcasecmp("Username", child->key) == 0) |
| 214 |
status = cf_util_get_string(child, &db->username); |
| 215 |
else if (strcasecmp("Password", child->key) == 0) |
| 216 |
status = cf_util_get_string(child, &db->password); |
| 217 |
else if (strcasecmp("Plugin", child->key) == 0) |
| 218 |
status = cf_util_get_string(child, &db->plugin_name); |
| 219 |
else if (strcasecmp("Query", child->key) == 0) |
| 220 |
status = udb_query_pick_from_list(child, queries, queries_num, |
| 221 |
&db->queries, &db->queries_num); |
| 222 |
else { |
| 223 |
WARNING("oracle plugin: Option `%s' not allowed here.", child->key); |
| 224 |
status = -1; |
| 225 |
} |
| 226 |
|
| 227 |
if (status != 0) |
| 228 |
break; |
| 229 |
} |
| 230 |
|
| 231 |
/* Check that all necessary options have been given. */ |
| 232 |
while (status == 0) { |
| 233 |
if (db->connect_id == NULL) { |
| 234 |
WARNING("oracle plugin: `ConnectID' not given for query `%s'", db->name); |
| 235 |
status = -1; |
| 236 |
} |
| 237 |
if (db->username == NULL) { |
| 238 |
WARNING("oracle plugin: `Username' not given for query `%s'", db->name); |
| 239 |
status = -1; |
| 240 |
} |
| 241 |
if (db->password == NULL) { |
| 242 |
WARNING("oracle plugin: `Password' not given for query `%s'", db->name); |
| 243 |
status = -1; |
| 244 |
} |
| 245 |
|
| 246 |
break; |
| 247 |
} /* while (status == 0) */ |
| 248 |
|
| 249 |
while ((status == 0) && (db->queries_num > 0)) { |
| 250 |
db->q_prep_areas = calloc(db->queries_num, sizeof(*db->q_prep_areas)); |
| 251 |
if (db->q_prep_areas == NULL) { |
| 252 |
WARNING("oracle plugin: calloc failed"); |
| 253 |
status = -1; |
| 254 |
break; |
| 255 |
} |
| 256 |
|
| 257 |
for (int i = 0; i < db->queries_num; ++i) { |
| 258 |
db->q_prep_areas[i] = udb_query_allocate_preparation_area(db->queries[i]); |
| 259 |
|
| 260 |
if (db->q_prep_areas[i] == NULL) { |
| 261 |
WARNING("oracle plugin: udb_query_allocate_preparation_area failed"); |
| 262 |
status = -1; |
| 263 |
break; |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
break; |
| 268 |
} |
| 269 |
|
| 270 |
/* If all went well, add this query to the list of queries within the |
| 271 |
* database structure. */ |
| 272 |
if (status == 0) { |
| 273 |
o_database_t **temp; |
| 274 |
|
| 275 |
temp = realloc(databases, sizeof(*databases) * (databases_num + 1)); |
| 276 |
if (temp == NULL) { |
| 277 |
ERROR("oracle plugin: realloc failed"); |
| 278 |
status = -1; |
| 279 |
} else { |
| 280 |
databases = temp; |
| 281 |
databases[databases_num] = db; |
| 282 |
databases_num++; |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
if (status != 0) { |
| 287 |
o_database_free(db); |
| 288 |
return -1; |
| 289 |
} |
| 290 |
|
| 291 |
return 0; |
| 292 |
} /* }}} int o_config_add_database */ |
| 293 |
|
| 294 |
static int o_config(oconfig_item_t *ci) /* {{{ */ |
| 295 |
{ |
| 296 |
for (int i = 0; i < ci->children_num; i++) { |
| 297 |
oconfig_item_t *child = ci->children + i; |
| 298 |
if (strcasecmp("Query", child->key) == 0) |
| 299 |
udb_query_create(&queries, &queries_num, child, |
| 300 |
/* callback = */ NULL); |
| 301 |
else if (strcasecmp("Database", child->key) == 0) |
| 302 |
o_config_add_database(child); |
| 303 |
else { |
| 304 |
WARNING("oracle plugin: Ignoring unknown config option `%s'.", |
| 305 |
child->key); |
| 306 |
} |
| 307 |
|
| 308 |
if (queries_num > 0) { |
| 309 |
DEBUG( |
| 310 |
"oracle plugin: o_config: queries_num = %" PRIsz "; queries[0] = %p; " |
| 311 |
"udb_query_get_user_data (queries[0]) = %p;", |
| 312 |
queries_num, (void *)queries[0], udb_query_get_user_data(queries[0])); |
| 313 |
} |
| 314 |
} /* for (ci->children) */ |
| 315 |
|
| 316 |
return 0; |
| 317 |
} /* }}} int o_config */ |
| 318 |
|
| 319 |
/* }}} End of configuration handling functions */ |
| 320 |
|
| 321 |
static int o_init(void) /* {{{ */ |
| 322 |
{ |
| 323 |
int status; |
| 324 |
|
| 325 |
if (oci_env != NULL) |
| 326 |
return 0; |
| 327 |
|
| 328 |
status = OCIEnvCreate(&oci_env, |
| 329 |
/* mode = */ OCI_THREADED, |
| 330 |
/* context = */ NULL, |
| 331 |
/* malloc = */ NULL, |
| 332 |
/* realloc = */ NULL, |
| 333 |
/* free = */ NULL, |
| 334 |
/* user_data_size = */ 0, |
| 335 |
/* user_data_ptr = */ NULL); |
| 336 |
if (status != 0) { |
| 337 |
ERROR("oracle plugin: OCIEnvCreate failed with status %i.", status); |
| 338 |
return -1; |
| 339 |
} |
| 340 |
|
| 341 |
status = OCIHandleAlloc(oci_env, (void *)&oci_error, OCI_HTYPE_ERROR, |
| 342 |
/* user_data_size = */ 0, /* user_data = */ NULL); |
| 343 |
if (status != OCI_SUCCESS) { |
| 344 |
ERROR("oracle plugin: OCIHandleAlloc (OCI_HTYPE_ERROR) failed " |
| 345 |
"with status %i.", |
| 346 |
status); |
| 347 |
return -1; |
| 348 |
} |
| 349 |
|
| 350 |
return 0; |
| 351 |
} /* }}} int o_init */ |
| 352 |
|
| 353 |
static int o_read_database_query(o_database_t *db, /* {{{ */ |
| 354 |
udb_query_t *q, |
| 355 |
udb_query_preparation_area_t *prep_area) { |
| 356 |
char **column_names; |
| 357 |
char **column_values; |
| 358 |
size_t column_num; |
| 359 |
|
| 360 |
OCIStmt *oci_statement; |
| 361 |
|
| 362 |
/* List of `OCIDefine' pointers. These defines map columns to the buffer |
| 363 |
* space declared above. */ |
| 364 |
OCIDefine **oci_defines; |
| 365 |
|
| 366 |
int status; |
| 367 |
|
| 368 |
oci_statement = udb_query_get_user_data(q); |
| 369 |
|
| 370 |
/* Prepare the statement */ |
| 371 |
if (oci_statement == NULL) /* {{{ */ |
| 372 |
{ |
| 373 |
const char *statement; |
| 374 |
|
| 375 |
statement = udb_query_get_statement(q); |
| 376 |
assert(statement != NULL); |
| 377 |
|
| 378 |
status = OCIHandleAlloc(oci_env, (void *)&oci_statement, OCI_HTYPE_STMT, |
| 379 |
/* user_data_size = */ 0, /* user_data = */ NULL); |
| 380 |
if (status != OCI_SUCCESS) { |
| 381 |
o_report_error("o_read_database_query", db->name, udb_query_get_name(q), |
| 382 |
"OCIHandleAlloc", oci_error); |
| 383 |
oci_statement = NULL; |
| 384 |
return -1; |
| 385 |
} |
| 386 |
|
| 387 |
status = OCIStmtPrepare(oci_statement, oci_error, (text *)statement, |
| 388 |
(ub4)strlen(statement), |
| 389 |
/* language = */ OCI_NTV_SYNTAX, |
| 390 |
/* mode = */ OCI_DEFAULT); |
| 391 |
if (status != OCI_SUCCESS) { |
| 392 |
o_report_error("o_read_database_query", db->name, udb_query_get_name(q), |
| 393 |
"OCIStmtPrepare", oci_error); |
| 394 |
OCIHandleFree(oci_statement, OCI_HTYPE_STMT); |
| 395 |
oci_statement = NULL; |
| 396 |
return -1; |
| 397 |
} |
| 398 |
udb_query_set_user_data(q, oci_statement); |
| 399 |
|
| 400 |
DEBUG("oracle plugin: o_read_database_query (%s, %s): " |
| 401 |
"Successfully allocated statement handle.", |
| 402 |
db->name, udb_query_get_name(q)); |
| 403 |
} /* }}} */ |
| 404 |
|
| 405 |
assert(oci_statement != NULL); |
| 406 |
|
| 407 |
/* Execute the statement */ |
| 408 |
status = OCIStmtExecute(db->oci_service_context, /* {{{ */ |
| 409 |
oci_statement, oci_error, |
| 410 |
/* iters = */ 0, |
| 411 |
/* rowoff = */ 0, |
| 412 |
/* snap_in = */ NULL, /* snap_out = */ NULL, |
| 413 |
/* mode = */ OCI_DEFAULT); |
| 414 |
if (status != OCI_SUCCESS) { |
| 415 |
o_report_error("o_read_database_query", db->name, udb_query_get_name(q), |
| 416 |
"OCIStmtExecute", oci_error); |
| 417 |
return -1; |
| 418 |
} /* }}} */ |
| 419 |
|
| 420 |
/* Acquire the number of columns returned. */ |
| 421 |
do /* {{{ */ |
| 422 |
{ |
| 423 |
ub4 param_counter = 0; |
| 424 |
status = OCIAttrGet(oci_statement, OCI_HTYPE_STMT, /* {{{ */ |
| 425 |
¶m_counter, /* size pointer = */ NULL, |
| 426 |
OCI_ATTR_PARAM_COUNT, oci_error); |
| 427 |
if (status != OCI_SUCCESS) { |
| 428 |
o_report_error("o_read_database_query", db->name, udb_query_get_name(q), |
| 429 |
"OCIAttrGet", oci_error); |
| 430 |
return -1; |
| 431 |
} /* }}} */ |
| 432 |
|
| 433 |
column_num = (size_t)param_counter; |
| 434 |
} while (0); /* }}} */ |
| 435 |
|
| 436 |
/* Allocate the following buffers: |
| 437 |
* |
| 438 |
* +---------------+-----------------------------------+ |
| 439 |
* ! Name ! Size ! |
| 440 |
* +---------------+-----------------------------------+ |
| 441 |
* ! column_names ! column_num x DATA_MAX_NAME_LEN ! |
| 442 |
* ! column_values ! column_num x DATA_MAX_NAME_LEN ! |
| 443 |
* ! oci_defines ! column_num x sizeof (OCIDefine *) ! |
| 444 |
* +---------------+-----------------------------------+ |
| 445 |
* |
| 446 |
* {{{ */ |
| 447 |
#define NUMBER_BUFFER_SIZE 64 |
| 448 |
|
| 449 |
#define FREE_ALL \ |
| 450 |
if (column_names != NULL) { \ |
| 451 |
sfree(column_names[0]); \ |
| 452 |
sfree(column_names); \ |
| 453 |
} \ |
| 454 |
if (column_values != NULL) { \ |
| 455 |
sfree(column_values[0]); \ |
| 456 |
sfree(column_values); \ |
| 457 |
} \ |
| 458 |
sfree(oci_defines) |
| 459 |
|
| 460 |
#define ALLOC_OR_FAIL(ptr, ptr_size) \ |
| 461 |
do { \ |
| 462 |
size_t alloc_size = (size_t)((ptr_size)); \ |
| 463 |
(ptr) = calloc(1, alloc_size); \ |
| 464 |
if ((ptr) == NULL) { \ |
| 465 |
FREE_ALL; \ |
| 466 |
ERROR("oracle plugin: o_read_database_query: calloc failed."); \ |
| 467 |
return -1; \ |
| 468 |
} \ |
| 469 |
} while (0) |
| 470 |
|
| 471 |
/* Initialize everything to NULL so the above works. */ |
| 472 |
column_names = NULL; |
| 473 |
column_values = NULL; |
| 474 |
oci_defines = NULL; |
| 475 |
|
| 476 |
ALLOC_OR_FAIL(column_names, column_num * sizeof(char *)); |
| 477 |
ALLOC_OR_FAIL(column_names[0], column_num * DATA_MAX_NAME_LEN); |
| 478 |
for (size_t i = 1; i < column_num; i++) |
| 479 |
column_names[i] = column_names[i - 1] + DATA_MAX_NAME_LEN; |
| 480 |
|
| 481 |
ALLOC_OR_FAIL(column_values, column_num * sizeof(char *)); |
| 482 |
ALLOC_OR_FAIL(column_values[0], column_num * DATA_MAX_NAME_LEN); |
| 483 |
for (size_t i = 1; i < column_num; i++) |
| 484 |
column_values[i] = column_values[i - 1] + DATA_MAX_NAME_LEN; |
| 485 |
|
| 486 |
ALLOC_OR_FAIL(oci_defines, column_num * sizeof(OCIDefine *)); |
| 487 |
/* }}} End of buffer allocations. */ |
| 488 |
|
| 489 |
/* ``Define'' the returned data, i. e. bind the columns to the buffers |
| 490 |
* allocated above. */ |
| 491 |
for (size_t i = 0; i < column_num; i++) /* {{{ */ |
| 492 |
{ |
| 493 |
char *column_name; |
| 494 |
ub4 column_name_length; |
| 495 |
OCIParam *oci_param; |
| 496 |
|
| 497 |
oci_param = NULL; |
| 498 |
|
| 499 |
status = OCIParamGet(oci_statement, OCI_HTYPE_STMT, oci_error, |
| 500 |
(void *)&oci_param, (ub4)(i + 1)); |
| 501 |
if (status != OCI_SUCCESS) { |
| 502 |
/* This is probably alright */ |
| 503 |
DEBUG("oracle plugin: o_read_database_query: status = %#x (= %i);", |
| 504 |
status, status); |
| 505 |
o_report_error("o_read_database_query", db->name, udb_query_get_name(q), |
| 506 |
"OCIParamGet", oci_error); |
| 507 |
status = OCI_SUCCESS; |
| 508 |
break; |
| 509 |
} |
| 510 |
|
| 511 |
column_name = NULL; |
| 512 |
column_name_length = 0; |
| 513 |
status = OCIAttrGet(oci_param, OCI_DTYPE_PARAM, &column_name, |
| 514 |
&column_name_length, OCI_ATTR_NAME, oci_error); |
| 515 |
if (status != OCI_SUCCESS) { |
| 516 |
OCIDescriptorFree(oci_param, OCI_DTYPE_PARAM); |
| 517 |
o_report_error("o_read_database_query", db->name, udb_query_get_name(q), |
| 518 |
"OCIAttrGet (OCI_ATTR_NAME)", oci_error); |
| 519 |
continue; |
| 520 |
} |
| 521 |
|
| 522 |
OCIDescriptorFree(oci_param, OCI_DTYPE_PARAM); |
| 523 |
oci_param = NULL; |
| 524 |
|
| 525 |
/* Copy the name to column_names. Warning: The ``string'' returned by OCI |
| 526 |
* may not be null terminated! */ |
| 527 |
memset(column_names[i], 0, DATA_MAX_NAME_LEN); |
| 528 |
if (column_name_length >= DATA_MAX_NAME_LEN) |
| 529 |
column_name_length = DATA_MAX_NAME_LEN - 1; |
| 530 |
memcpy(column_names[i], column_name, column_name_length); |
| 531 |
column_names[i][column_name_length] = 0; |
| 532 |
|
| 533 |
DEBUG("oracle plugin: o_read_database_query: column_names[%" PRIsz "] = %s;" |
| 534 |
" column_name_length = %" PRIu32 ";", |
| 535 |
i, column_names[i], (uint32_t)column_name_length); |
| 536 |
|
| 537 |
status = OCIDefineByPos(oci_statement, &oci_defines[i], oci_error, |
| 538 |
(ub4)(i + 1), column_values[i], DATA_MAX_NAME_LEN, |
| 539 |
SQLT_STR, NULL, NULL, NULL, OCI_DEFAULT); |
| 540 |
if (status != OCI_SUCCESS) { |
| 541 |
o_report_error("o_read_database_query", db->name, udb_query_get_name(q), |
| 542 |
"OCIDefineByPos", oci_error); |
| 543 |
continue; |
| 544 |
} |
| 545 |
} /* for (j = 1; j <= param_counter; j++) */ |
| 546 |
/* }}} End of the ``define'' stuff. */ |
| 547 |
|
| 548 |
status = udb_query_prepare_result( |
| 549 |
q, prep_area, (db->host != NULL) ? db->host : hostname_g, |
| 550 |
/* plugin = */ (db->plugin_name != NULL) ? db->plugin_name : "oracle", |
| 551 |
db->name, column_names, column_num); |
| 552 |
if (status != 0) { |
| 553 |
ERROR("oracle plugin: o_read_database_query (%s, %s): " |
| 554 |
"udb_query_prepare_result failed.", |
| 555 |
db->name, udb_query_get_name(q)); |
| 556 |
FREE_ALL; |
| 557 |
return -1; |
| 558 |
} |
| 559 |
|
| 560 |
/* Fetch and handle all the rows that matched the query. */ |
| 561 |
while (42) /* {{{ */ |
| 562 |
{ |
| 563 |
status = OCIStmtFetch2(oci_statement, oci_error, |
| 564 |
/* nrows = */ 1, /* orientation = */ OCI_FETCH_NEXT, |
| 565 |
/* fetch offset = */ 0, /* mode = */ OCI_DEFAULT); |
| 566 |
if (status == OCI_NO_DATA) { |
| 567 |
status = OCI_SUCCESS; |
| 568 |
break; |
| 569 |
} else if ((status != OCI_SUCCESS) && (status != OCI_SUCCESS_WITH_INFO)) { |
| 570 |
o_report_error("o_read_database_query", db->name, udb_query_get_name(q), |
| 571 |
"OCIStmtFetch2", oci_error); |
| 572 |
break; |
| 573 |
} |
| 574 |
|
| 575 |
status = udb_query_handle_result(q, prep_area, column_values); |
| 576 |
if (status != 0) { |
| 577 |
WARNING("oracle plugin: o_read_database_query (%s, %s): " |
| 578 |
"udb_query_handle_result failed.", |
| 579 |
db->name, udb_query_get_name(q)); |
| 580 |
} |
| 581 |
} /* }}} while (42) */ |
| 582 |
|
| 583 |
udb_query_finish_result(q, prep_area); |
| 584 |
|
| 585 |
/* DEBUG ("oracle plugin: o_read_database_query: This statement succeeded: |
| 586 |
* %s", q->statement); */ |
| 587 |
FREE_ALL; |
| 588 |
|
| 589 |
return 0; |
| 590 |
#undef FREE_ALL |
| 591 |
#undef ALLOC_OR_FAIL |
| 592 |
} /* }}} int o_read_database_query */ |
| 593 |
|
| 594 |
static int o_read_database(o_database_t *db) /* {{{ */ |
| 595 |
{ |
| 596 |
int status; |
| 597 |
|
| 598 |
if (db->oci_service_context != NULL) { |
| 599 |
OCIServer *server_handle; |
| 600 |
ub4 connection_status; |
| 601 |
|
| 602 |
server_handle = NULL; |
| 603 |
status = OCIAttrGet((void *)db->oci_service_context, OCI_HTYPE_SVCCTX, |
| 604 |
(void *)&server_handle, /* size pointer = */ NULL, |
| 605 |
OCI_ATTR_SERVER, oci_error); |
| 606 |
if (status != OCI_SUCCESS) { |
| 607 |
o_report_error("o_read_database", db->name, NULL, "OCIAttrGet", |
| 608 |
oci_error); |
| 609 |
return -1; |
| 610 |
} |
| 611 |
|
| 612 |
if (server_handle == NULL) { |
| 613 |
connection_status = OCI_SERVER_NOT_CONNECTED; |
| 614 |
} else /* if (server_handle != NULL) */ |
| 615 |
{ |
| 616 |
connection_status = 0; |
| 617 |
status = OCIAttrGet((void *)server_handle, OCI_HTYPE_SERVER, |
| 618 |
(void *)&connection_status, /* size pointer = */ NULL, |
| 619 |
OCI_ATTR_SERVER_STATUS, oci_error); |
| 620 |
if (status != OCI_SUCCESS) { |
| 621 |
o_report_error("o_read_database", db->name, NULL, "OCIAttrGet", |
| 622 |
oci_error); |
| 623 |
return -1; |
| 624 |
} |
| 625 |
} |
| 626 |
|
| 627 |
if (connection_status != OCI_SERVER_NORMAL) { |
| 628 |
INFO("oracle plugin: Connection to %s lost. Trying to reconnect.", |
| 629 |
db->name); |
| 630 |
OCIHandleFree(db->oci_service_context, OCI_HTYPE_SVCCTX); |
| 631 |
db->oci_service_context = NULL; |
| 632 |
} |
| 633 |
} /* if (db->oci_service_context != NULL) */ |
| 634 |
|
| 635 |
if (db->oci_service_context == NULL) { |
| 636 |
status = OCILogon(oci_env, oci_error, &db->oci_service_context, |
| 637 |
(OraText *)db->username, (ub4)strlen(db->username), |
| 638 |
(OraText *)db->password, (ub4)strlen(db->password), |
| 639 |
(OraText *)db->connect_id, (ub4)strlen(db->connect_id)); |
| 640 |
if ((status != OCI_SUCCESS) && (status != OCI_SUCCESS_WITH_INFO)) { |
| 641 |
char errfunc[256]; |
| 642 |
|
| 643 |
ssnprintf(errfunc, sizeof(errfunc), "OCILogon(\"%s\")", db->connect_id); |
| 644 |
|
| 645 |
o_report_error("o_read_database", db->name, NULL, errfunc, oci_error); |
| 646 |
DEBUG("oracle plugin: OCILogon (%s): db->oci_service_context = %p;", |
| 647 |
db->connect_id, db->oci_service_context); |
| 648 |
db->oci_service_context = NULL; |
| 649 |
return -1; |
| 650 |
} else if (status == OCI_SUCCESS_WITH_INFO) { |
| 651 |
/* TODO: Print NOTIFY message. */ |
| 652 |
} |
| 653 |
assert(db->oci_service_context != NULL); |
| 654 |
} |
| 655 |
|
| 656 |
DEBUG("oracle plugin: o_read_database: db->connect_id = %s; " |
| 657 |
"db->oci_service_context = %p;", |
| 658 |
db->connect_id, db->oci_service_context); |
| 659 |
|
| 660 |
for (size_t i = 0; i < db->queries_num; i++) |
| 661 |
o_read_database_query(db, db->queries[i], db->q_prep_areas[i]); |
| 662 |
|
| 663 |
return 0; |
| 664 |
} /* }}} int o_read_database */ |
| 665 |
|
| 666 |
static int o_read(void) /* {{{ */ |
| 667 |
{ |
| 668 |
size_t i; |
| 669 |
|
| 670 |
for (i = 0; i < databases_num; i++) |
| 671 |
o_read_database(databases[i]); |
| 672 |
|
| 673 |
return 0; |
| 674 |
} /* }}} int o_read */ |
| 675 |
|
| 676 |
static int o_shutdown(void) /* {{{ */ |
| 677 |
{ |
| 678 |
size_t i; |
| 679 |
|
| 680 |
for (i = 0; i < databases_num; i++) |
| 681 |
if (databases[i]->oci_service_context != NULL) { |
| 682 |
OCIHandleFree(databases[i]->oci_service_context, OCI_HTYPE_SVCCTX); |
| 683 |
databases[i]->oci_service_context = NULL; |
| 684 |
} |
| 685 |
|
| 686 |
for (i = 0; i < queries_num; i++) { |
| 687 |
OCIStmt *oci_statement; |
| 688 |
|
| 689 |
oci_statement = udb_query_get_user_data(queries[i]); |
| 690 |
if (oci_statement != NULL) { |
| 691 |
OCIHandleFree(oci_statement, OCI_HTYPE_STMT); |
| 692 |
udb_query_set_user_data(queries[i], NULL); |
| 693 |
} |
| 694 |
} |
| 695 |
|
| 696 |
OCIHandleFree(oci_env, OCI_HTYPE_ENV); |
| 697 |
oci_env = NULL; |
| 698 |
|
| 699 |
udb_query_free(queries, queries_num); |
| 700 |
queries = NULL; |
| 701 |
queries_num = 0; |
| 702 |
|
| 703 |
return 0; |
| 704 |
} /* }}} int o_shutdown */ |
| 705 |
|
| 706 |
void module_register(void) /* {{{ */ |
| 707 |
{ |
| 708 |
plugin_register_complex_config("oracle", o_config); |
| 709 |
plugin_register_init("oracle", o_init); |
| 710 |
plugin_register_read("oracle", o_read); |
| 711 |
plugin_register_shutdown("oracle", o_shutdown); |
| 712 |
} /* }}} void module_register */ |