2 * collectd - src/oracle.c
3 * Copyright (C) 2008,2009 noris network AG
4 * Copyright (C) 2012 Florian octo Forster
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.
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.
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
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
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.
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.
41 * Oracle® is a registered trademark of Oracle Corporation and/or its
42 * affiliates. Other names may be trademarks of their respective owners.
45 * Florian octo Forster <octo at collectd.org>
52 #include "utils_db_query.h"
66 udb_query_preparation_area_t **q_prep_areas;
67 udb_query_t **queries;
70 OCISvcCtx *oci_service_context;
72 typedef struct o_database_s o_database_t;
77 static udb_query_t **queries = NULL;
78 static size_t queries_num = 0;
79 static o_database_t **databases = NULL;
80 static size_t databases_num = 0;
82 OCIEnv *oci_env = NULL;
83 OCIError *oci_error = NULL;
88 static void o_report_error(const char *where, /* {{{ */
89 const char *db_name, const char *query_name,
90 const char *what, OCIError *eh) {
97 if (query_name == NULL)
98 query_name = "(none)";
100 /* An operation may cause / return multiple errors. Loop until we have
101 * handled all errors available (with a fail-save limit of 16). */
102 for (unsigned int record_number = 1; record_number <= 16; record_number++) {
103 memset(buffer, 0, sizeof(buffer));
106 status = OCIErrorGet(eh, (ub4)record_number,
107 /* sqlstate = */ NULL, &error_code, (text *)&buffer[0],
108 (ub4)sizeof(buffer), OCI_HTYPE_ERROR);
109 buffer[sizeof(buffer) - 1] = 0;
111 if (status == OCI_NO_DATA)
114 if (status == OCI_SUCCESS) {
115 size_t buffer_length;
117 buffer_length = strlen(buffer);
118 while ((buffer_length > 0) && (buffer[buffer_length - 1] < 32)) {
120 buffer[buffer_length] = 0;
123 ERROR("oracle plugin: %s (db = %s, query = %s): %s failed: %s", where,
124 db_name, query_name, what, buffer);
126 ERROR("oracle plugin: %s (db = %s, query = %s): %s failed. "
127 "Additionally, OCIErrorGet failed with status %i.",
128 where, db_name, query_name, what, status);
132 } /* }}} void o_report_error */
134 static void o_database_free(o_database_t *db) /* {{{ */
140 sfree(db->connect_id);
145 if (db->q_prep_areas != NULL)
146 for (size_t i = 0; i < db->queries_num; ++i)
147 udb_query_delete_preparation_area(db->q_prep_areas[i]);
148 free(db->q_prep_areas);
151 } /* }}} void o_database_free */
153 /* Configuration handling functions {{{
156 * <Query "plugin_instance0">
157 * Statement "SELECT name, value FROM table"
160 * InstancesFrom "name"
165 * <Database "plugin_instance1">
169 * Query "plugin_instance0"
174 static int o_config_add_database(oconfig_item_t *ci) /* {{{ */
179 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
180 WARNING("oracle plugin: The `Database' block "
181 "needs exactly one string argument.");
185 db = calloc(1, sizeof(*db));
187 ERROR("oracle plugin: calloc failed.");
192 db->connect_id = NULL;
196 status = cf_util_get_string(ci, &db->name);
202 /* Fill the `o_database_t' structure.. */
203 for (int i = 0; i < ci->children_num; i++) {
204 oconfig_item_t *child = ci->children + i;
206 if (strcasecmp("ConnectID", child->key) == 0)
207 status = cf_util_get_string(child, &db->connect_id);
208 else if (strcasecmp("Host", child->key) == 0)
209 status = cf_util_get_string(child, &db->host);
210 else if (strcasecmp("Username", child->key) == 0)
211 status = cf_util_get_string(child, &db->username);
212 else if (strcasecmp("Password", child->key) == 0)
213 status = cf_util_get_string(child, &db->password);
214 else if (strcasecmp("Query", child->key) == 0)
215 status = udb_query_pick_from_list(child, queries, queries_num,
216 &db->queries, &db->queries_num);
218 WARNING("oracle plugin: Option `%s' not allowed here.", child->key);
226 /* Check that all necessary options have been given. */
227 while (status == 0) {
228 if (db->connect_id == NULL) {
229 WARNING("oracle plugin: `ConnectID' not given for query `%s'", db->name);
232 if (db->username == NULL) {
233 WARNING("oracle plugin: `Username' not given for query `%s'", db->name);
236 if (db->password == NULL) {
237 WARNING("oracle plugin: `Password' not given for query `%s'", db->name);
242 } /* while (status == 0) */
244 while ((status == 0) && (db->queries_num > 0)) {
245 db->q_prep_areas = (udb_query_preparation_area_t **)calloc(
246 db->queries_num, sizeof(*db->q_prep_areas));
248 if (db->q_prep_areas == NULL) {
249 WARNING("oracle plugin: calloc failed");
254 for (int i = 0; i < db->queries_num; ++i) {
255 db->q_prep_areas[i] = udb_query_allocate_preparation_area(db->queries[i]);
257 if (db->q_prep_areas[i] == NULL) {
258 WARNING("oracle plugin: udb_query_allocate_preparation_area failed");
267 /* If all went well, add this query to the list of queries within the
268 * database structure. */
272 temp = realloc(databases, sizeof(*databases) * (databases_num + 1));
274 ERROR("oracle plugin: realloc failed");
278 databases[databases_num] = db;
289 } /* }}} int o_config_add_database */
291 static int o_config(oconfig_item_t *ci) /* {{{ */
293 for (int i = 0; i < ci->children_num; i++) {
294 oconfig_item_t *child = ci->children + i;
295 if (strcasecmp("Query", child->key) == 0)
296 udb_query_create(&queries, &queries_num, child,
297 /* callback = */ NULL);
298 else if (strcasecmp("Database", child->key) == 0)
299 o_config_add_database(child);
301 WARNING("oracle plugin: Ignoring unknown config option `%s'.",
305 if (queries_num > 0) {
306 DEBUG("oracle plugin: o_config: queries_num = %zu; queries[0] = %p; "
307 "udb_query_get_user_data (queries[0]) = %p;",
308 queries_num, (void *)queries[0],
309 udb_query_get_user_data(queries[0]));
311 } /* for (ci->children) */
314 } /* }}} int o_config */
316 /* }}} End of configuration handling functions */
318 static int o_init(void) /* {{{ */
325 status = OCIEnvCreate(&oci_env,
326 /* mode = */ OCI_THREADED,
327 /* context = */ NULL,
329 /* realloc = */ NULL,
331 /* user_data_size = */ 0,
332 /* user_data_ptr = */ NULL);
334 ERROR("oracle plugin: OCIEnvCreate failed with status %i.", status);
338 status = OCIHandleAlloc(oci_env, (void *)&oci_error, OCI_HTYPE_ERROR,
339 /* user_data_size = */ 0, /* user_data = */ NULL);
340 if (status != OCI_SUCCESS) {
341 ERROR("oracle plugin: OCIHandleAlloc (OCI_HTYPE_ERROR) failed "
348 } /* }}} int o_init */
350 static int o_read_database_query(o_database_t *db, /* {{{ */
352 udb_query_preparation_area_t *prep_area) {
354 char **column_values;
357 OCIStmt *oci_statement;
359 /* List of `OCIDefine' pointers. These defines map columns to the buffer
360 * space declared above. */
361 OCIDefine **oci_defines;
365 oci_statement = udb_query_get_user_data(q);
367 /* Prepare the statement */
368 if (oci_statement == NULL) /* {{{ */
370 const char *statement;
372 statement = udb_query_get_statement(q);
373 assert(statement != NULL);
375 status = OCIHandleAlloc(oci_env, (void *)&oci_statement, OCI_HTYPE_STMT,
376 /* user_data_size = */ 0, /* user_data = */ NULL);
377 if (status != OCI_SUCCESS) {
378 o_report_error("o_read_database_query", db->name, udb_query_get_name(q),
379 "OCIHandleAlloc", oci_error);
380 oci_statement = NULL;
384 status = OCIStmtPrepare(oci_statement, oci_error, (text *)statement,
385 (ub4)strlen(statement),
386 /* language = */ OCI_NTV_SYNTAX,
387 /* mode = */ OCI_DEFAULT);
388 if (status != OCI_SUCCESS) {
389 o_report_error("o_read_database_query", db->name, udb_query_get_name(q),
390 "OCIStmtPrepare", oci_error);
391 OCIHandleFree(oci_statement, OCI_HTYPE_STMT);
392 oci_statement = NULL;
395 udb_query_set_user_data(q, oci_statement);
397 DEBUG("oracle plugin: o_read_database_query (%s, %s): "
398 "Successfully allocated statement handle.",
399 db->name, udb_query_get_name(q));
402 assert(oci_statement != NULL);
404 /* Execute the statement */
405 status = OCIStmtExecute(db->oci_service_context, /* {{{ */
406 oci_statement, oci_error,
409 /* snap_in = */ NULL, /* snap_out = */ NULL,
410 /* mode = */ OCI_DEFAULT);
411 if (status != OCI_SUCCESS) {
412 o_report_error("o_read_database_query", db->name, udb_query_get_name(q),
413 "OCIStmtExecute", oci_error);
417 /* Acquire the number of columns returned. */
420 ub4 param_counter = 0;
421 status = OCIAttrGet(oci_statement, OCI_HTYPE_STMT, /* {{{ */
422 ¶m_counter, /* size pointer = */ NULL,
423 OCI_ATTR_PARAM_COUNT, oci_error);
424 if (status != OCI_SUCCESS) {
425 o_report_error("o_read_database_query", db->name, udb_query_get_name(q),
426 "OCIAttrGet", oci_error);
430 column_num = (size_t)param_counter;
431 } while (0); /* }}} */
433 /* Allocate the following buffers:
435 * +---------------+-----------------------------------+
437 * +---------------+-----------------------------------+
438 * ! column_names ! column_num x DATA_MAX_NAME_LEN !
439 * ! column_values ! column_num x DATA_MAX_NAME_LEN !
440 * ! oci_defines ! column_num x sizeof (OCIDefine *) !
441 * +---------------+-----------------------------------+
444 #define NUMBER_BUFFER_SIZE 64
447 if (column_names != NULL) { \
448 sfree(column_names[0]); \
449 sfree(column_names); \
451 if (column_values != NULL) { \
452 sfree(column_values[0]); \
453 sfree(column_values); \
457 #define ALLOC_OR_FAIL(ptr, ptr_size) \
459 size_t alloc_size = (size_t)((ptr_size)); \
460 (ptr) = calloc(1, alloc_size); \
461 if ((ptr) == NULL) { \
463 ERROR("oracle plugin: o_read_database_query: calloc failed."); \
468 /* Initialize everything to NULL so the above works. */
470 column_values = NULL;
473 ALLOC_OR_FAIL(column_names, column_num * sizeof(char *));
474 ALLOC_OR_FAIL(column_names[0], column_num * DATA_MAX_NAME_LEN * sizeof(char));
475 for (size_t i = 1; i < column_num; i++)
476 column_names[i] = column_names[i - 1] + DATA_MAX_NAME_LEN;
478 ALLOC_OR_FAIL(column_values, column_num * sizeof(char *));
479 ALLOC_OR_FAIL(column_values[0],
480 column_num * DATA_MAX_NAME_LEN * sizeof(char));
481 for (size_t i = 1; i < column_num; i++)
482 column_values[i] = column_values[i - 1] + DATA_MAX_NAME_LEN;
484 ALLOC_OR_FAIL(oci_defines, column_num * sizeof(OCIDefine *));
485 /* }}} End of buffer allocations. */
487 /* ``Define'' the returned data, i. e. bind the columns to the buffers
488 * allocated above. */
489 for (size_t i = 0; i < column_num; i++) /* {{{ */
492 ub4 column_name_length;
497 status = OCIParamGet(oci_statement, OCI_HTYPE_STMT, oci_error,
498 (void *)&oci_param, (ub4)(i + 1));
499 if (status != OCI_SUCCESS) {
500 /* This is probably alright */
501 DEBUG("oracle plugin: o_read_database_query: status = %#x (= %i);",
503 o_report_error("o_read_database_query", db->name, udb_query_get_name(q),
504 "OCIParamGet", oci_error);
505 status = OCI_SUCCESS;
510 column_name_length = 0;
511 status = OCIAttrGet(oci_param, OCI_DTYPE_PARAM, &column_name,
512 &column_name_length, OCI_ATTR_NAME, oci_error);
513 if (status != OCI_SUCCESS) {
514 OCIDescriptorFree(oci_param, OCI_DTYPE_PARAM);
515 o_report_error("o_read_database_query", db->name, udb_query_get_name(q),
516 "OCIAttrGet (OCI_ATTR_NAME)", oci_error);
520 OCIDescriptorFree(oci_param, OCI_DTYPE_PARAM);
523 /* Copy the name to column_names. Warning: The ``string'' returned by OCI
524 * may not be null terminated! */
525 memset(column_names[i], 0, DATA_MAX_NAME_LEN);
526 if (column_name_length >= DATA_MAX_NAME_LEN)
527 column_name_length = DATA_MAX_NAME_LEN - 1;
528 memcpy(column_names[i], column_name, column_name_length);
529 column_names[i][column_name_length] = 0;
531 DEBUG("oracle plugin: o_read_database_query: column_names[%zu] = %s; "
532 "column_name_length = %" PRIu32 ";",
533 i, column_names[i], (uint32_t)column_name_length);
535 status = OCIDefineByPos(oci_statement, &oci_defines[i], oci_error,
536 (ub4)(i + 1), column_values[i], DATA_MAX_NAME_LEN,
537 SQLT_STR, NULL, NULL, NULL, OCI_DEFAULT);
538 if (status != OCI_SUCCESS) {
539 o_report_error("o_read_database_query", db->name, udb_query_get_name(q),
540 "OCIDefineByPos", oci_error);
543 } /* for (j = 1; j <= param_counter; j++) */
544 /* }}} End of the ``define'' stuff. */
546 status = udb_query_prepare_result(
547 q, prep_area, (db->host != NULL) ? db->host : hostname_g,
548 /* plugin = */ "oracle", db->name, column_names, column_num,
551 ERROR("oracle plugin: o_read_database_query (%s, %s): "
552 "udb_query_prepare_result failed.",
553 db->name, udb_query_get_name(q));
558 /* Fetch and handle all the rows that matched the query. */
561 status = OCIStmtFetch2(oci_statement, oci_error,
562 /* nrows = */ 1, /* orientation = */ OCI_FETCH_NEXT,
563 /* fetch offset = */ 0, /* mode = */ OCI_DEFAULT);
564 if (status == OCI_NO_DATA) {
565 status = OCI_SUCCESS;
567 } else if ((status != OCI_SUCCESS) && (status != OCI_SUCCESS_WITH_INFO)) {
568 o_report_error("o_read_database_query", db->name, udb_query_get_name(q),
569 "OCIStmtFetch2", oci_error);
573 status = udb_query_handle_result(q, prep_area, column_values);
575 WARNING("oracle plugin: o_read_database_query (%s, %s): "
576 "udb_query_handle_result failed.",
577 db->name, udb_query_get_name(q));
579 } /* }}} while (42) */
581 /* DEBUG ("oracle plugin: o_read_database_query: This statement succeeded:
582 * %s", q->statement); */
588 } /* }}} int o_read_database_query */
590 static int o_read_database(o_database_t *db) /* {{{ */
594 if (db->oci_service_context != NULL) {
595 OCIServer *server_handle;
596 ub4 connection_status;
598 server_handle = NULL;
599 status = OCIAttrGet((void *)db->oci_service_context, OCI_HTYPE_SVCCTX,
600 (void *)&server_handle, /* size pointer = */ NULL,
601 OCI_ATTR_SERVER, oci_error);
602 if (status != OCI_SUCCESS) {
603 o_report_error("o_read_database", db->name, NULL, "OCIAttrGet",
608 if (server_handle == NULL) {
609 connection_status = OCI_SERVER_NOT_CONNECTED;
610 } else /* if (server_handle != NULL) */
612 connection_status = 0;
613 status = OCIAttrGet((void *)server_handle, OCI_HTYPE_SERVER,
614 (void *)&connection_status, /* size pointer = */ NULL,
615 OCI_ATTR_SERVER_STATUS, oci_error);
616 if (status != OCI_SUCCESS) {
617 o_report_error("o_read_database", db->name, NULL, "OCIAttrGet",
623 if (connection_status != OCI_SERVER_NORMAL) {
624 INFO("oracle plugin: Connection to %s lost. Trying to reconnect.",
626 OCIHandleFree(db->oci_service_context, OCI_HTYPE_SVCCTX);
627 db->oci_service_context = NULL;
629 } /* if (db->oci_service_context != NULL) */
631 if (db->oci_service_context == NULL) {
632 status = OCILogon(oci_env, oci_error, &db->oci_service_context,
633 (OraText *)db->username, (ub4)strlen(db->username),
634 (OraText *)db->password, (ub4)strlen(db->password),
635 (OraText *)db->connect_id, (ub4)strlen(db->connect_id));
636 if ((status != OCI_SUCCESS) && (status != OCI_SUCCESS_WITH_INFO)) {
639 ssnprintf(errfunc, sizeof(errfunc), "OCILogon(\"%s\")", db->connect_id);
641 o_report_error("o_read_database", db->name, NULL, errfunc, oci_error);
642 DEBUG("oracle plugin: OCILogon (%s): db->oci_service_context = %p;",
643 db->connect_id, db->oci_service_context);
644 db->oci_service_context = NULL;
646 } else if (status == OCI_SUCCESS_WITH_INFO) {
647 /* TODO: Print NOTIFY message. */
649 assert(db->oci_service_context != NULL);
652 DEBUG("oracle plugin: o_read_database: db->connect_id = %s; "
653 "db->oci_service_context = %p;",
654 db->connect_id, db->oci_service_context);
656 for (size_t i = 0; i < db->queries_num; i++)
657 o_read_database_query(db, db->queries[i], db->q_prep_areas[i]);
660 } /* }}} int o_read_database */
662 static int o_read(void) /* {{{ */
666 for (i = 0; i < databases_num; i++)
667 o_read_database(databases[i]);
670 } /* }}} int o_read */
672 static int o_shutdown(void) /* {{{ */
676 for (i = 0; i < databases_num; i++)
677 if (databases[i]->oci_service_context != NULL) {
678 OCIHandleFree(databases[i]->oci_service_context, OCI_HTYPE_SVCCTX);
679 databases[i]->oci_service_context = NULL;
682 for (i = 0; i < queries_num; i++) {
683 OCIStmt *oci_statement;
685 oci_statement = udb_query_get_user_data(queries[i]);
686 if (oci_statement != NULL) {
687 OCIHandleFree(oci_statement, OCI_HTYPE_STMT);
688 udb_query_set_user_data(queries[i], NULL);
692 OCIHandleFree(oci_env, OCI_HTYPE_ENV);
695 udb_query_free(queries, queries_num);
700 } /* }}} int o_shutdown */
702 void module_register(void) /* {{{ */
704 plugin_register_complex_config("oracle", o_config);
705 plugin_register_init("oracle", o_init);
706 plugin_register_read("oracle", o_read);
707 plugin_register_shutdown("oracle", o_shutdown);
708 } /* }}} void module_register */