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 = NULL;
79 static size_t queries_num = 0;
80 static o_database_t **databases = NULL;
81 static size_t databases_num = 0;
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)
99 if (query_name == NULL)
100 query_name = "(none)";
102 /* An operation may cause / return multiple errors. Loop until we have
103 * handled all errors available (with a fail-save limit of 16). */
104 for (unsigned int record_number = 1; record_number <= 16; record_number++)
106 memset (buffer, 0, sizeof (buffer));
109 status = OCIErrorGet (eh, (ub4) record_number,
110 /* sqlstate = */ NULL,
113 (ub4) sizeof (buffer),
115 buffer[sizeof (buffer) - 1] = 0;
117 if (status == OCI_NO_DATA)
120 if (status == OCI_SUCCESS)
122 size_t buffer_length;
124 buffer_length = strlen (buffer);
125 while ((buffer_length > 0) && (buffer[buffer_length - 1] < 32))
128 buffer[buffer_length] = 0;
131 ERROR ("oracle plugin: %s (db = %s, query = %s): %s failed: %s",
132 where, db_name, query_name, what, buffer);
136 ERROR ("oracle plugin: %s (db = %s, query = %s): %s failed. "
137 "Additionally, OCIErrorGet failed with status %i.",
138 where, db_name, query_name, what, status);
142 } /* }}} void o_report_error */
144 static void o_database_free (o_database_t *db) /* {{{ */
150 sfree (db->connect_id);
151 sfree (db->username);
152 sfree (db->password);
155 if (db->q_prep_areas != NULL)
156 for (size_t i = 0; i < db->queries_num; ++i)
157 udb_query_delete_preparation_area (db->q_prep_areas[i]);
158 free (db->q_prep_areas);
161 } /* }}} void o_database_free */
163 /* Configuration handling functions {{{
166 * <Query "plugin_instance0">
167 * Statement "SELECT name, value FROM table"
170 * InstancesFrom "name"
175 * <Database "plugin_instance1">
179 * Query "plugin_instance0"
184 static int o_config_add_database (oconfig_item_t *ci) /* {{{ */
189 if ((ci->values_num != 1)
190 || (ci->values[0].type != OCONFIG_TYPE_STRING))
192 WARNING ("oracle plugin: The `Database' block "
193 "needs exactly one string argument.");
197 db = calloc (1, sizeof (*db));
200 ERROR ("oracle plugin: calloc failed.");
205 db->connect_id = NULL;
209 status = cf_util_get_string (ci, &db->name);
216 /* Fill the `o_database_t' structure.. */
217 for (int i = 0; i < ci->children_num; i++)
219 oconfig_item_t *child = ci->children + i;
221 if (strcasecmp ("ConnectID", child->key) == 0)
222 status = cf_util_get_string (child, &db->connect_id);
223 else if (strcasecmp ("Host", child->key) == 0)
224 status = cf_util_get_string (child, &db->host);
225 else if (strcasecmp ("Username", child->key) == 0)
226 status = cf_util_get_string (child, &db->username);
227 else if (strcasecmp ("Password", child->key) == 0)
228 status = cf_util_get_string (child, &db->password);
229 else if (strcasecmp ("Query", child->key) == 0)
230 status = udb_query_pick_from_list (child, queries, queries_num,
231 &db->queries, &db->queries_num);
234 WARNING ("oracle plugin: Option `%s' not allowed here.", child->key);
242 /* Check that all necessary options have been given. */
245 if (db->connect_id == NULL)
247 WARNING ("oracle plugin: `ConnectID' not given for query `%s'", db->name);
250 if (db->username == NULL)
252 WARNING ("oracle plugin: `Username' not given for query `%s'", db->name);
255 if (db->password == NULL)
257 WARNING ("oracle plugin: `Password' not given for query `%s'", db->name);
262 } /* while (status == 0) */
264 while ((status == 0) && (db->queries_num > 0))
266 db->q_prep_areas = (udb_query_preparation_area_t **) calloc (
267 db->queries_num, sizeof (*db->q_prep_areas));
269 if (db->q_prep_areas == NULL)
271 WARNING ("oracle plugin: calloc failed");
276 for (int i = 0; i < db->queries_num; ++i)
279 = udb_query_allocate_preparation_area (db->queries[i]);
281 if (db->q_prep_areas[i] == NULL)
283 WARNING ("oracle plugin: udb_query_allocate_preparation_area failed");
292 /* If all went well, add this query to the list of queries within the
293 * database structure. */
298 temp = realloc (databases,
299 sizeof (*databases) * (databases_num + 1));
302 ERROR ("oracle plugin: realloc failed");
308 databases[databases_num] = db;
315 o_database_free (db);
320 } /* }}} int o_config_add_database */
322 static int o_config (oconfig_item_t *ci) /* {{{ */
324 for (int i = 0; i < ci->children_num; i++)
326 oconfig_item_t *child = ci->children + i;
327 if (strcasecmp ("Query", child->key) == 0)
328 udb_query_create (&queries, &queries_num, child,
329 /* callback = */ NULL);
330 else if (strcasecmp ("Database", child->key) == 0)
331 o_config_add_database (child);
334 WARNING ("oracle plugin: Ignoring unknown config option `%s'.", child->key);
339 DEBUG ("oracle plugin: o_config: queries_num = %zu; queries[0] = %p; udb_query_get_user_data (queries[0]) = %p;",
340 queries_num, (void *) queries[0], udb_query_get_user_data (queries[0]));
342 } /* for (ci->children) */
345 } /* }}} int o_config */
347 /* }}} End of configuration handling functions */
349 static int o_init (void) /* {{{ */
356 status = OCIEnvCreate (&oci_env,
357 /* mode = */ OCI_THREADED,
358 /* context = */ NULL,
360 /* realloc = */ NULL,
362 /* user_data_size = */ 0,
363 /* user_data_ptr = */ NULL);
366 ERROR ("oracle plugin: OCIEnvCreate failed with status %i.", status);
370 status = OCIHandleAlloc (oci_env, (void *) &oci_error, OCI_HTYPE_ERROR,
371 /* user_data_size = */ 0, /* user_data = */ NULL);
372 if (status != OCI_SUCCESS)
374 ERROR ("oracle plugin: OCIHandleAlloc (OCI_HTYPE_ERROR) failed "
375 "with status %i.", status);
380 } /* }}} int o_init */
382 static int o_read_database_query (o_database_t *db, /* {{{ */
383 udb_query_t *q, udb_query_preparation_area_t *prep_area)
386 char **column_values;
389 OCIStmt *oci_statement;
391 /* List of `OCIDefine' pointers. These defines map columns to the buffer
392 * space declared above. */
393 OCIDefine **oci_defines;
397 oci_statement = udb_query_get_user_data (q);
399 /* Prepare the statement */
400 if (oci_statement == NULL) /* {{{ */
402 const char *statement;
404 statement = udb_query_get_statement (q);
405 assert (statement != NULL);
407 status = OCIHandleAlloc (oci_env, (void *) &oci_statement,
408 OCI_HTYPE_STMT, /* user_data_size = */ 0, /* user_data = */ NULL);
409 if (status != OCI_SUCCESS)
411 o_report_error ("o_read_database_query", db->name,
412 udb_query_get_name (q), "OCIHandleAlloc", oci_error);
413 oci_statement = NULL;
417 status = OCIStmtPrepare (oci_statement, oci_error,
418 (text *) statement, (ub4) strlen (statement),
419 /* language = */ OCI_NTV_SYNTAX,
420 /* mode = */ OCI_DEFAULT);
421 if (status != OCI_SUCCESS)
423 o_report_error ("o_read_database_query", db->name,
424 udb_query_get_name (q), "OCIStmtPrepare", oci_error);
425 OCIHandleFree (oci_statement, OCI_HTYPE_STMT);
426 oci_statement = NULL;
429 udb_query_set_user_data (q, oci_statement);
431 DEBUG ("oracle plugin: o_read_database_query (%s, %s): "
432 "Successfully allocated statement handle.",
433 db->name, udb_query_get_name (q));
436 assert (oci_statement != NULL);
438 /* Execute the statement */
439 status = OCIStmtExecute (db->oci_service_context, /* {{{ */
444 /* snap_in = */ NULL, /* snap_out = */ NULL,
445 /* mode = */ OCI_DEFAULT);
446 if (status != OCI_SUCCESS)
448 o_report_error ("o_read_database_query", db->name, udb_query_get_name (q),
449 "OCIStmtExecute", oci_error);
453 /* Acquire the number of columns returned. */
456 ub4 param_counter = 0;
457 status = OCIAttrGet (oci_statement, OCI_HTYPE_STMT, /* {{{ */
458 ¶m_counter, /* size pointer = */ NULL,
459 OCI_ATTR_PARAM_COUNT, oci_error);
460 if (status != OCI_SUCCESS)
462 o_report_error ("o_read_database_query", db->name,
463 udb_query_get_name (q), "OCIAttrGet", oci_error);
467 column_num = (size_t) param_counter;
468 } while (0); /* }}} */
470 /* Allocate the following buffers:
472 * +---------------+-----------------------------------+
474 * +---------------+-----------------------------------+
475 * ! column_names ! column_num x DATA_MAX_NAME_LEN !
476 * ! column_values ! column_num x DATA_MAX_NAME_LEN !
477 * ! oci_defines ! column_num x sizeof (OCIDefine *) !
478 * +---------------+-----------------------------------+
481 #define NUMBER_BUFFER_SIZE 64
484 if (column_names != NULL) { \
485 sfree (column_names[0]); \
486 sfree (column_names); \
488 if (column_values != NULL) { \
489 sfree (column_values[0]); \
490 sfree (column_values); \
494 #define ALLOC_OR_FAIL(ptr, ptr_size) \
496 size_t alloc_size = (size_t) ((ptr_size)); \
497 (ptr) = calloc (1, alloc_size); \
498 if ((ptr) == NULL) { \
500 ERROR ("oracle plugin: o_read_database_query: calloc failed."); \
505 /* Initialize everything to NULL so the above works. */
507 column_values = NULL;
510 ALLOC_OR_FAIL (column_names, column_num * sizeof (char *));
511 ALLOC_OR_FAIL (column_names[0], column_num * DATA_MAX_NAME_LEN
513 for (size_t i = 1; i < column_num; i++)
514 column_names[i] = column_names[i - 1] + DATA_MAX_NAME_LEN;
516 ALLOC_OR_FAIL (column_values, column_num * sizeof (char *));
517 ALLOC_OR_FAIL (column_values[0], column_num * DATA_MAX_NAME_LEN
519 for (size_t i = 1; i < column_num; i++)
520 column_values[i] = column_values[i - 1] + DATA_MAX_NAME_LEN;
522 ALLOC_OR_FAIL (oci_defines, column_num * sizeof (OCIDefine *));
523 /* }}} End of buffer allocations. */
525 /* ``Define'' the returned data, i. e. bind the columns to the buffers
526 * allocated above. */
527 for (size_t i = 0; i < column_num; i++) /* {{{ */
530 ub4 column_name_length;
535 status = OCIParamGet (oci_statement, OCI_HTYPE_STMT, oci_error,
536 (void *) &oci_param, (ub4) (i + 1));
537 if (status != OCI_SUCCESS)
539 /* This is probably alright */
540 DEBUG ("oracle plugin: o_read_database_query: status = %#x (= %i);",
542 o_report_error ("o_read_database_query", db->name,
543 udb_query_get_name (q), "OCIParamGet", oci_error);
544 status = OCI_SUCCESS;
549 column_name_length = 0;
550 status = OCIAttrGet (oci_param, OCI_DTYPE_PARAM,
551 &column_name, &column_name_length, OCI_ATTR_NAME, oci_error);
552 if (status != OCI_SUCCESS)
554 OCIDescriptorFree (oci_param, OCI_DTYPE_PARAM);
555 o_report_error ("o_read_database_query", db->name,
556 udb_query_get_name (q), "OCIAttrGet (OCI_ATTR_NAME)", oci_error);
560 OCIDescriptorFree (oci_param, OCI_DTYPE_PARAM);
563 /* Copy the name to column_names. Warning: The ``string'' returned by OCI
564 * may not be null terminated! */
565 memset (column_names[i], 0, DATA_MAX_NAME_LEN);
566 if (column_name_length >= DATA_MAX_NAME_LEN)
567 column_name_length = DATA_MAX_NAME_LEN - 1;
568 memcpy (column_names[i], column_name, column_name_length);
569 column_names[i][column_name_length] = 0;
571 DEBUG ("oracle plugin: o_read_database_query: column_names[%zu] = %s; "
572 "column_name_length = %"PRIu32";",
573 i, column_names[i], (uint32_t) column_name_length);
575 status = OCIDefineByPos (oci_statement,
576 &oci_defines[i], oci_error, (ub4) (i + 1),
577 column_values[i], DATA_MAX_NAME_LEN, SQLT_STR,
578 NULL, NULL, NULL, OCI_DEFAULT);
579 if (status != OCI_SUCCESS)
581 o_report_error ("o_read_database_query", db->name,
582 udb_query_get_name (q), "OCIDefineByPos", oci_error);
585 } /* for (j = 1; j <= param_counter; j++) */
586 /* }}} End of the ``define'' stuff. */
588 status = udb_query_prepare_result (q, prep_area,
589 (db->host != NULL) ? db->host : hostname_g,
590 /* plugin = */ "oracle", db->name, column_names, column_num,
594 ERROR ("oracle plugin: o_read_database_query (%s, %s): "
595 "udb_query_prepare_result failed.",
596 db->name, udb_query_get_name (q));
601 /* Fetch and handle all the rows that matched the query. */
604 status = OCIStmtFetch2 (oci_statement, oci_error,
605 /* nrows = */ 1, /* orientation = */ OCI_FETCH_NEXT,
606 /* fetch offset = */ 0, /* mode = */ OCI_DEFAULT);
607 if (status == OCI_NO_DATA)
609 status = OCI_SUCCESS;
612 else if ((status != OCI_SUCCESS) && (status != OCI_SUCCESS_WITH_INFO))
614 o_report_error ("o_read_database_query", db->name,
615 udb_query_get_name (q), "OCIStmtFetch2", oci_error);
619 status = udb_query_handle_result (q, prep_area, column_values);
622 WARNING ("oracle plugin: o_read_database_query (%s, %s): "
623 "udb_query_handle_result failed.",
624 db->name, udb_query_get_name (q));
626 } /* }}} while (42) */
628 /* DEBUG ("oracle plugin: o_read_database_query: This statement succeeded: %s", q->statement); */
634 } /* }}} int o_read_database_query */
636 static int o_read_database (o_database_t *db) /* {{{ */
640 if (db->oci_service_context != NULL)
642 OCIServer *server_handle;
643 ub4 connection_status;
645 server_handle = NULL;
646 status = OCIAttrGet ((void *) db->oci_service_context, OCI_HTYPE_SVCCTX,
647 (void *) &server_handle, /* size pointer = */ NULL,
648 OCI_ATTR_SERVER, oci_error);
649 if (status != OCI_SUCCESS)
651 o_report_error ("o_read_database", db->name, NULL, "OCIAttrGet",
656 if (server_handle == NULL)
658 connection_status = OCI_SERVER_NOT_CONNECTED;
660 else /* if (server_handle != NULL) */
662 connection_status = 0;
663 status = OCIAttrGet ((void *) server_handle, OCI_HTYPE_SERVER,
664 (void *) &connection_status, /* size pointer = */ NULL,
665 OCI_ATTR_SERVER_STATUS, oci_error);
666 if (status != OCI_SUCCESS)
668 o_report_error ("o_read_database", db->name, NULL, "OCIAttrGet",
674 if (connection_status != OCI_SERVER_NORMAL)
676 INFO ("oracle plugin: Connection to %s lost. Trying to reconnect.",
678 OCIHandleFree (db->oci_service_context, OCI_HTYPE_SVCCTX);
679 db->oci_service_context = NULL;
681 } /* if (db->oci_service_context != NULL) */
683 if (db->oci_service_context == NULL)
685 status = OCILogon (oci_env, oci_error,
686 &db->oci_service_context,
687 (OraText *) db->username, (ub4) strlen (db->username),
688 (OraText *) db->password, (ub4) strlen (db->password),
689 (OraText *) db->connect_id, (ub4) strlen (db->connect_id));
690 if ((status != OCI_SUCCESS) && (status != OCI_SUCCESS_WITH_INFO))
694 ssnprintf (errfunc, sizeof (errfunc), "OCILogon(\"%s\")", db->connect_id);
696 o_report_error ("o_read_database", db->name, NULL, errfunc, oci_error);
697 DEBUG ("oracle plugin: OCILogon (%s): db->oci_service_context = %p;",
698 db->connect_id, db->oci_service_context);
699 db->oci_service_context = NULL;
702 else if (status == OCI_SUCCESS_WITH_INFO)
704 /* TODO: Print NOTIFY message. */
706 assert (db->oci_service_context != NULL);
709 DEBUG ("oracle plugin: o_read_database: db->connect_id = %s; db->oci_service_context = %p;",
710 db->connect_id, db->oci_service_context);
712 for (size_t i = 0; i < db->queries_num; i++)
713 o_read_database_query (db, db->queries[i], db->q_prep_areas[i]);
716 } /* }}} int o_read_database */
718 static int o_read (void) /* {{{ */
722 for (i = 0; i < databases_num; i++)
723 o_read_database (databases[i]);
726 } /* }}} int o_read */
728 static int o_shutdown (void) /* {{{ */
732 for (i = 0; i < databases_num; i++)
733 if (databases[i]->oci_service_context != NULL)
735 OCIHandleFree (databases[i]->oci_service_context, OCI_HTYPE_SVCCTX);
736 databases[i]->oci_service_context = NULL;
739 for (i = 0; i < queries_num; i++)
741 OCIStmt *oci_statement;
743 oci_statement = udb_query_get_user_data (queries[i]);
744 if (oci_statement != NULL)
746 OCIHandleFree (oci_statement, OCI_HTYPE_STMT);
747 udb_query_set_user_data (queries[i], NULL);
751 OCIHandleFree (oci_env, OCI_HTYPE_ENV);
754 udb_query_free (queries, queries_num);
759 } /* }}} int o_shutdown */
761 void module_register (void) /* {{{ */
763 plugin_register_complex_config ("oracle", o_config);
764 plugin_register_init ("oracle", o_init);
765 plugin_register_read ("oracle", o_read);
766 plugin_register_shutdown ("oracle", o_shutdown);
767 } /* }}} void module_register */
770 * vim: shiftwidth=2 softtabstop=2 et fdm=marker