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>
51 #include "utils/common/common.h"
52 #include "utils/db_query/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 = calloc(db->queries_num, sizeof(*db->q_prep_areas));
251 if (db->q_prep_areas == NULL) {
252 WARNING("oracle plugin: calloc failed");
257 for (int i = 0; i < db->queries_num; ++i) {
258 db->q_prep_areas[i] = udb_query_allocate_preparation_area(db->queries[i]);
260 if (db->q_prep_areas[i] == NULL) {
261 WARNING("oracle plugin: udb_query_allocate_preparation_area failed");
270 /* If all went well, add this query to the list of queries within the
271 * database structure. */
275 temp = realloc(databases, sizeof(*databases) * (databases_num + 1));
277 ERROR("oracle plugin: realloc failed");
281 databases[databases_num] = db;
292 } /* }}} int o_config_add_database */
294 static int o_config(oconfig_item_t *ci) /* {{{ */
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);
304 WARNING("oracle plugin: Ignoring unknown config option `%s'.",
308 if (queries_num > 0) {
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]));
314 } /* for (ci->children) */
317 } /* }}} int o_config */
319 /* }}} End of configuration handling functions */
321 static int o_init(void) /* {{{ */
328 status = OCIEnvCreate(&oci_env,
329 /* mode = */ OCI_THREADED,
330 /* context = */ NULL,
332 /* realloc = */ NULL,
334 /* user_data_size = */ 0,
335 /* user_data_ptr = */ NULL);
337 ERROR("oracle plugin: OCIEnvCreate failed with status %i.", status);
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 "
351 } /* }}} int o_init */
353 static int o_read_database_query(o_database_t *db, /* {{{ */
355 udb_query_preparation_area_t *prep_area) {
357 char **column_values;
360 OCIStmt *oci_statement;
362 /* List of `OCIDefine' pointers. These defines map columns to the buffer
363 * space declared above. */
364 OCIDefine **oci_defines;
368 oci_statement = udb_query_get_user_data(q);
370 /* Prepare the statement */
371 if (oci_statement == NULL) /* {{{ */
373 const char *statement;
375 statement = udb_query_get_statement(q);
376 assert(statement != NULL);
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;
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;
398 udb_query_set_user_data(q, oci_statement);
400 DEBUG("oracle plugin: o_read_database_query (%s, %s): "
401 "Successfully allocated statement handle.",
402 db->name, udb_query_get_name(q));
405 assert(oci_statement != NULL);
407 /* Execute the statement */
408 status = OCIStmtExecute(db->oci_service_context, /* {{{ */
409 oci_statement, oci_error,
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);
420 /* Acquire the number of columns returned. */
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);
433 column_num = (size_t)param_counter;
434 } while (0); /* }}} */
436 /* Allocate the following buffers:
438 * +---------------+-----------------------------------+
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 * +---------------+-----------------------------------+
447 #define NUMBER_BUFFER_SIZE 64
450 if (column_names != NULL) { \
451 sfree(column_names[0]); \
452 sfree(column_names); \
454 if (column_values != NULL) { \
455 sfree(column_values[0]); \
456 sfree(column_values); \
460 #define ALLOC_OR_FAIL(ptr, ptr_size) \
462 size_t alloc_size = (size_t)((ptr_size)); \
463 (ptr) = calloc(1, alloc_size); \
464 if ((ptr) == NULL) { \
466 ERROR("oracle plugin: o_read_database_query: calloc failed."); \
471 /* Initialize everything to NULL so the above works. */
473 column_values = NULL;
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;
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;
486 ALLOC_OR_FAIL(oci_defines, column_num * sizeof(OCIDefine *));
487 /* }}} End of buffer allocations. */
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++) /* {{{ */
494 ub4 column_name_length;
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);",
505 o_report_error("o_read_database_query", db->name, udb_query_get_name(q),
506 "OCIParamGet", oci_error);
507 status = OCI_SUCCESS;
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);
522 OCIDescriptorFree(oci_param, OCI_DTYPE_PARAM);
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;
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);
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);
545 } /* for (j = 1; j <= param_counter; j++) */
546 /* }}} End of the ``define'' stuff. */
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);
553 ERROR("oracle plugin: o_read_database_query (%s, %s): "
554 "udb_query_prepare_result failed.",
555 db->name, udb_query_get_name(q));
560 /* Fetch and handle all the rows that matched the query. */
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;
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);
575 status = udb_query_handle_result(q, prep_area, column_values);
577 WARNING("oracle plugin: o_read_database_query (%s, %s): "
578 "udb_query_handle_result failed.",
579 db->name, udb_query_get_name(q));
581 } /* }}} while (42) */
583 udb_query_finish_result(q, prep_area);
585 /* DEBUG ("oracle plugin: o_read_database_query: This statement succeeded:
586 * %s", q->statement); */
592 } /* }}} int o_read_database_query */
594 static int o_read_database(o_database_t *db) /* {{{ */
598 if (db->oci_service_context != NULL) {
599 OCIServer *server_handle;
600 ub4 connection_status;
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",
612 if (server_handle == NULL) {
613 connection_status = OCI_SERVER_NOT_CONNECTED;
614 } else /* if (server_handle != NULL) */
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",
627 if (connection_status != OCI_SERVER_NORMAL) {
628 INFO("oracle plugin: Connection to %s lost. Trying to reconnect.",
630 OCIHandleFree(db->oci_service_context, OCI_HTYPE_SVCCTX);
631 db->oci_service_context = NULL;
633 } /* if (db->oci_service_context != NULL) */
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)) {
643 snprintf(errfunc, sizeof(errfunc), "OCILogon(\"%s\")", db->connect_id);
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;
650 } else if (status == OCI_SUCCESS_WITH_INFO) {
651 /* TODO: Print NOTIFY message. */
653 assert(db->oci_service_context != NULL);
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);
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]);
664 } /* }}} int o_read_database */
666 static int o_read(void) /* {{{ */
670 for (i = 0; i < databases_num; i++)
671 o_read_database(databases[i]);
674 } /* }}} int o_read */
676 static int o_shutdown(void) /* {{{ */
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;
686 for (i = 0; i < queries_num; i++) {
687 OCIStmt *oci_statement;
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);
696 OCIHandleFree(oci_env, OCI_HTYPE_ENV);
699 udb_query_free(queries, queries_num);
704 } /* }}} int o_shutdown */
706 void module_register(void) /* {{{ */
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 */