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"
67 udb_query_preparation_area_t **q_prep_areas;
68 udb_query_t **queries;
71 OCISvcCtx *oci_service_context;
73 typedef struct o_database_s o_database_t;
78 static udb_query_t **queries;
79 static size_t queries_num;
80 static o_database_t **databases;
81 static size_t databases_num;
83 OCIEnv *oci_env = NULL;
84 OCIError *oci_error = NULL;
89 static void o_report_error(const char *where, /* {{{ */
90 const char *db_name, const char *query_name,
91 const char *what, OCIError *eh) {
98 if (query_name == NULL)
99 query_name = "(none)";
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));
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;
112 if (status == OCI_NO_DATA)
115 if (status == OCI_SUCCESS) {
116 size_t buffer_length;
118 buffer_length = strlen(buffer);
119 while ((buffer_length > 0) && (buffer[buffer_length - 1] < 32)) {
121 buffer[buffer_length] = 0;
124 ERROR("oracle plugin: %s (db = %s, query = %s): %s failed: %s", where,
125 db_name, query_name, what, buffer);
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);
133 } /* }}} void o_report_error */
135 static void o_database_free(o_database_t *db) /* {{{ */
141 sfree(db->connect_id);
145 sfree(db->plugin_name);
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);
153 } /* }}} void o_database_free */
155 /* Configuration handling functions {{{
158 * <Query "plugin_instance0">
159 * Statement "SELECT name, value FROM table"
162 * InstancesFrom "name"
167 * <Database "plugin_instance1">
171 * Query "plugin_instance0"
176 static int o_config_add_database(oconfig_item_t *ci) /* {{{ */
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.");
187 db = calloc(1, sizeof(*db));
189 ERROR("oracle plugin: calloc failed.");
194 db->connect_id = NULL;
197 db->plugin_name = NULL;
199 status = cf_util_get_string(ci, &db->name);
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;
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);
223 WARNING("oracle plugin: Option `%s' not allowed here.", child->key);
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);
237 if (db->username == NULL) {
238 WARNING("oracle plugin: `Username' not given for query `%s'", db->name);
241 if (db->password == NULL) {
242 WARNING("oracle plugin: `Password' not given for query `%s'", db->name);
247 } /* while (status == 0) */
249 while ((status == 0) && (db->queries_num > 0)) {
250 db->q_prep_areas = (udb_query_preparation_area_t **)calloc(
251 db->queries_num, sizeof(*db->q_prep_areas));
253 if (db->q_prep_areas == NULL) {
254 WARNING("oracle plugin: calloc failed");
259 for (int i = 0; i < db->queries_num; ++i) {
260 db->q_prep_areas[i] = udb_query_allocate_preparation_area(db->queries[i]);
262 if (db->q_prep_areas[i] == NULL) {
263 WARNING("oracle plugin: udb_query_allocate_preparation_area failed");
272 /* If all went well, add this query to the list of queries within the
273 * database structure. */
277 temp = realloc(databases, sizeof(*databases) * (databases_num + 1));
279 ERROR("oracle plugin: realloc failed");
283 databases[databases_num] = db;
294 } /* }}} int o_config_add_database */
296 static int o_config(oconfig_item_t *ci) /* {{{ */
298 for (int i = 0; i < ci->children_num; i++) {
299 oconfig_item_t *child = ci->children + i;
300 if (strcasecmp("Query", child->key) == 0)
301 udb_query_create(&queries, &queries_num, child,
302 /* callback = */ NULL);
303 else if (strcasecmp("Database", child->key) == 0)
304 o_config_add_database(child);
306 WARNING("oracle plugin: Ignoring unknown config option `%s'.",
310 if (queries_num > 0) {
312 "oracle plugin: o_config: queries_num = %" PRIsz "; queries[0] = %p; "
313 "udb_query_get_user_data (queries[0]) = %p;",
314 queries_num, (void *)queries[0], udb_query_get_user_data(queries[0]));
316 } /* for (ci->children) */
319 } /* }}} int o_config */
321 /* }}} End of configuration handling functions */
323 static int o_init(void) /* {{{ */
330 status = OCIEnvCreate(&oci_env,
331 /* mode = */ OCI_THREADED,
332 /* context = */ NULL,
334 /* realloc = */ NULL,
336 /* user_data_size = */ 0,
337 /* user_data_ptr = */ NULL);
339 ERROR("oracle plugin: OCIEnvCreate failed with status %i.", status);
343 status = OCIHandleAlloc(oci_env, (void *)&oci_error, OCI_HTYPE_ERROR,
344 /* user_data_size = */ 0, /* user_data = */ NULL);
345 if (status != OCI_SUCCESS) {
346 ERROR("oracle plugin: OCIHandleAlloc (OCI_HTYPE_ERROR) failed "
353 } /* }}} int o_init */
355 static int o_read_database_query(o_database_t *db, /* {{{ */
357 udb_query_preparation_area_t *prep_area) {
359 char **column_values;
362 OCIStmt *oci_statement;
364 /* List of `OCIDefine' pointers. These defines map columns to the buffer
365 * space declared above. */
366 OCIDefine **oci_defines;
370 oci_statement = udb_query_get_user_data(q);
372 /* Prepare the statement */
373 if (oci_statement == NULL) /* {{{ */
375 const char *statement;
377 statement = udb_query_get_statement(q);
378 assert(statement != NULL);
380 status = OCIHandleAlloc(oci_env, (void *)&oci_statement, OCI_HTYPE_STMT,
381 /* user_data_size = */ 0, /* user_data = */ NULL);
382 if (status != OCI_SUCCESS) {
383 o_report_error("o_read_database_query", db->name, udb_query_get_name(q),
384 "OCIHandleAlloc", oci_error);
385 oci_statement = NULL;
389 status = OCIStmtPrepare(oci_statement, oci_error, (text *)statement,
390 (ub4)strlen(statement),
391 /* language = */ OCI_NTV_SYNTAX,
392 /* mode = */ OCI_DEFAULT);
393 if (status != OCI_SUCCESS) {
394 o_report_error("o_read_database_query", db->name, udb_query_get_name(q),
395 "OCIStmtPrepare", oci_error);
396 OCIHandleFree(oci_statement, OCI_HTYPE_STMT);
397 oci_statement = NULL;
400 udb_query_set_user_data(q, oci_statement);
402 DEBUG("oracle plugin: o_read_database_query (%s, %s): "
403 "Successfully allocated statement handle.",
404 db->name, udb_query_get_name(q));
407 assert(oci_statement != NULL);
409 /* Execute the statement */
410 status = OCIStmtExecute(db->oci_service_context, /* {{{ */
411 oci_statement, oci_error,
414 /* snap_in = */ NULL, /* snap_out = */ NULL,
415 /* mode = */ OCI_DEFAULT);
416 if (status != OCI_SUCCESS) {
417 o_report_error("o_read_database_query", db->name, udb_query_get_name(q),
418 "OCIStmtExecute", oci_error);
422 /* Acquire the number of columns returned. */
425 ub4 param_counter = 0;
426 status = OCIAttrGet(oci_statement, OCI_HTYPE_STMT, /* {{{ */
427 ¶m_counter, /* size pointer = */ NULL,
428 OCI_ATTR_PARAM_COUNT, oci_error);
429 if (status != OCI_SUCCESS) {
430 o_report_error("o_read_database_query", db->name, udb_query_get_name(q),
431 "OCIAttrGet", oci_error);
435 column_num = (size_t)param_counter;
436 } while (0); /* }}} */
438 /* Allocate the following buffers:
440 * +---------------+-----------------------------------+
442 * +---------------+-----------------------------------+
443 * ! column_names ! column_num x DATA_MAX_NAME_LEN !
444 * ! column_values ! column_num x DATA_MAX_NAME_LEN !
445 * ! oci_defines ! column_num x sizeof (OCIDefine *) !
446 * +---------------+-----------------------------------+
449 #define NUMBER_BUFFER_SIZE 64
452 if (column_names != NULL) { \
453 sfree(column_names[0]); \
454 sfree(column_names); \
456 if (column_values != NULL) { \
457 sfree(column_values[0]); \
458 sfree(column_values); \
462 #define ALLOC_OR_FAIL(ptr, ptr_size) \
464 size_t alloc_size = (size_t)((ptr_size)); \
465 (ptr) = calloc(1, alloc_size); \
466 if ((ptr) == NULL) { \
468 ERROR("oracle plugin: o_read_database_query: calloc failed."); \
473 /* Initialize everything to NULL so the above works. */
475 column_values = NULL;
478 ALLOC_OR_FAIL(column_names, column_num * sizeof(char *));
479 ALLOC_OR_FAIL(column_names[0], column_num * DATA_MAX_NAME_LEN);
480 for (size_t i = 1; i < column_num; i++)
481 column_names[i] = column_names[i - 1] + DATA_MAX_NAME_LEN;
483 ALLOC_OR_FAIL(column_values, column_num * sizeof(char *));
484 ALLOC_OR_FAIL(column_values[0], column_num * DATA_MAX_NAME_LEN);
485 for (size_t i = 1; i < column_num; i++)
486 column_values[i] = column_values[i - 1] + DATA_MAX_NAME_LEN;
488 ALLOC_OR_FAIL(oci_defines, column_num * sizeof(OCIDefine *));
489 /* }}} End of buffer allocations. */
491 /* ``Define'' the returned data, i. e. bind the columns to the buffers
492 * allocated above. */
493 for (size_t i = 0; i < column_num; i++) /* {{{ */
496 ub4 column_name_length;
501 status = OCIParamGet(oci_statement, OCI_HTYPE_STMT, oci_error,
502 (void *)&oci_param, (ub4)(i + 1));
503 if (status != OCI_SUCCESS) {
504 /* This is probably alright */
505 DEBUG("oracle plugin: o_read_database_query: status = %#x (= %i);",
507 o_report_error("o_read_database_query", db->name, udb_query_get_name(q),
508 "OCIParamGet", oci_error);
509 status = OCI_SUCCESS;
514 column_name_length = 0;
515 status = OCIAttrGet(oci_param, OCI_DTYPE_PARAM, &column_name,
516 &column_name_length, OCI_ATTR_NAME, oci_error);
517 if (status != OCI_SUCCESS) {
518 OCIDescriptorFree(oci_param, OCI_DTYPE_PARAM);
519 o_report_error("o_read_database_query", db->name, udb_query_get_name(q),
520 "OCIAttrGet (OCI_ATTR_NAME)", oci_error);
524 OCIDescriptorFree(oci_param, OCI_DTYPE_PARAM);
527 /* Copy the name to column_names. Warning: The ``string'' returned by OCI
528 * may not be null terminated! */
529 memset(column_names[i], 0, DATA_MAX_NAME_LEN);
530 if (column_name_length >= DATA_MAX_NAME_LEN)
531 column_name_length = DATA_MAX_NAME_LEN - 1;
532 memcpy(column_names[i], column_name, column_name_length);
533 column_names[i][column_name_length] = 0;
535 DEBUG("oracle plugin: o_read_database_query: column_names[%" PRIsz "] = %s;"
536 " column_name_length = %" PRIu32 ";",
537 i, column_names[i], (uint32_t)column_name_length);
539 status = OCIDefineByPos(oci_statement, &oci_defines[i], oci_error,
540 (ub4)(i + 1), column_values[i], DATA_MAX_NAME_LEN,
541 SQLT_STR, NULL, NULL, NULL, OCI_DEFAULT);
542 if (status != OCI_SUCCESS) {
543 o_report_error("o_read_database_query", db->name, udb_query_get_name(q),
544 "OCIDefineByPos", oci_error);
547 } /* for (j = 1; j <= param_counter; j++) */
548 /* }}} End of the ``define'' stuff. */
550 status = udb_query_prepare_result(
551 q, prep_area, (db->host != NULL) ? db->host : hostname_g,
552 /* plugin = */ (db->plugin_name != NULL) ? db->plugin_name : "oracle",
553 db->name, column_names, column_num,
556 ERROR("oracle plugin: o_read_database_query (%s, %s): "
557 "udb_query_prepare_result failed.",
558 db->name, udb_query_get_name(q));
563 /* Fetch and handle all the rows that matched the query. */
566 status = OCIStmtFetch2(oci_statement, oci_error,
567 /* nrows = */ 1, /* orientation = */ OCI_FETCH_NEXT,
568 /* fetch offset = */ 0, /* mode = */ OCI_DEFAULT);
569 if (status == OCI_NO_DATA) {
570 status = OCI_SUCCESS;
572 } else if ((status != OCI_SUCCESS) && (status != OCI_SUCCESS_WITH_INFO)) {
573 o_report_error("o_read_database_query", db->name, udb_query_get_name(q),
574 "OCIStmtFetch2", oci_error);
578 status = udb_query_handle_result(q, prep_area, column_values);
580 WARNING("oracle plugin: o_read_database_query (%s, %s): "
581 "udb_query_handle_result failed.",
582 db->name, udb_query_get_name(q));
584 } /* }}} while (42) */
586 udb_query_finish_result(q, prep_area);
588 /* DEBUG ("oracle plugin: o_read_database_query: This statement succeeded:
589 * %s", q->statement); */
595 } /* }}} int o_read_database_query */
597 static int o_read_database(o_database_t *db) /* {{{ */
601 if (db->oci_service_context != NULL) {
602 OCIServer *server_handle;
603 ub4 connection_status;
605 server_handle = NULL;
606 status = OCIAttrGet((void *)db->oci_service_context, OCI_HTYPE_SVCCTX,
607 (void *)&server_handle, /* size pointer = */ NULL,
608 OCI_ATTR_SERVER, oci_error);
609 if (status != OCI_SUCCESS) {
610 o_report_error("o_read_database", db->name, NULL, "OCIAttrGet",
615 if (server_handle == NULL) {
616 connection_status = OCI_SERVER_NOT_CONNECTED;
617 } else /* if (server_handle != NULL) */
619 connection_status = 0;
620 status = OCIAttrGet((void *)server_handle, OCI_HTYPE_SERVER,
621 (void *)&connection_status, /* size pointer = */ NULL,
622 OCI_ATTR_SERVER_STATUS, oci_error);
623 if (status != OCI_SUCCESS) {
624 o_report_error("o_read_database", db->name, NULL, "OCIAttrGet",
630 if (connection_status != OCI_SERVER_NORMAL) {
631 INFO("oracle plugin: Connection to %s lost. Trying to reconnect.",
633 OCIHandleFree(db->oci_service_context, OCI_HTYPE_SVCCTX);
634 db->oci_service_context = NULL;
636 } /* if (db->oci_service_context != NULL) */
638 if (db->oci_service_context == NULL) {
639 status = OCILogon(oci_env, oci_error, &db->oci_service_context,
640 (OraText *)db->username, (ub4)strlen(db->username),
641 (OraText *)db->password, (ub4)strlen(db->password),
642 (OraText *)db->connect_id, (ub4)strlen(db->connect_id));
643 if ((status != OCI_SUCCESS) && (status != OCI_SUCCESS_WITH_INFO)) {
646 snprintf(errfunc, sizeof(errfunc), "OCILogon(\"%s\")", db->connect_id);
648 o_report_error("o_read_database", db->name, NULL, errfunc, oci_error);
649 DEBUG("oracle plugin: OCILogon (%s): db->oci_service_context = %p;",
650 db->connect_id, db->oci_service_context);
651 db->oci_service_context = NULL;
653 } else if (status == OCI_SUCCESS_WITH_INFO) {
654 /* TODO: Print NOTIFY message. */
656 assert(db->oci_service_context != NULL);
659 DEBUG("oracle plugin: o_read_database: db->connect_id = %s; "
660 "db->oci_service_context = %p;",
661 db->connect_id, db->oci_service_context);
663 for (size_t i = 0; i < db->queries_num; i++)
664 o_read_database_query(db, db->queries[i], db->q_prep_areas[i]);
667 } /* }}} int o_read_database */
669 static int o_read(void) /* {{{ */
673 for (i = 0; i < databases_num; i++)
674 o_read_database(databases[i]);
677 } /* }}} int o_read */
679 static int o_shutdown(void) /* {{{ */
683 for (i = 0; i < databases_num; i++)
684 if (databases[i]->oci_service_context != NULL) {
685 OCIHandleFree(databases[i]->oci_service_context, OCI_HTYPE_SVCCTX);
686 databases[i]->oci_service_context = NULL;
689 for (i = 0; i < queries_num; i++) {
690 OCIStmt *oci_statement;
692 oci_statement = udb_query_get_user_data(queries[i]);
693 if (oci_statement != NULL) {
694 OCIHandleFree(oci_statement, OCI_HTYPE_STMT);
695 udb_query_set_user_data(queries[i], NULL);
699 OCIHandleFree(oci_env, OCI_HTYPE_ENV);
702 udb_query_free(queries, queries_num);
707 } /* }}} int o_shutdown */
709 void module_register(void) /* {{{ */
711 plugin_register_complex_config("oracle", o_config);
712 plugin_register_init("oracle", o_init);
713 plugin_register_read("oracle", o_read);
714 plugin_register_shutdown("oracle", o_shutdown);
715 } /* }}} void module_register */