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 "configfile.h"
53 #include "utils_db_query.h"
68 udb_query_preparation_area_t **q_prep_areas;
69 udb_query_t **queries;
72 OCISvcCtx *oci_service_context;
74 typedef struct o_database_s o_database_t;
79 static udb_query_t **queries = NULL;
80 static size_t queries_num = 0;
81 static o_database_t **databases = NULL;
82 static size_t databases_num = 0;
84 OCIEnv *oci_env = NULL;
85 OCIError *oci_error = NULL;
90 static void o_report_error (const char *where, /* {{{ */
91 const char *db_name, const char *query_name,
92 const char *what, OCIError *eh)
100 if (query_name == NULL)
101 query_name = "(none)";
103 /* An operation may cause / return multiple errors. Loop until we have
104 * handled all errors available (with a fail-save limit of 16). */
105 for (unsigned int record_number = 1; record_number <= 16; record_number++)
107 memset (buffer, 0, sizeof (buffer));
110 status = OCIErrorGet (eh, (ub4) record_number,
111 /* sqlstate = */ NULL,
114 (ub4) sizeof (buffer),
116 buffer[sizeof (buffer) - 1] = 0;
118 if (status == OCI_NO_DATA)
121 if (status == OCI_SUCCESS)
123 size_t buffer_length;
125 buffer_length = strlen (buffer);
126 while ((buffer_length > 0) && (buffer[buffer_length - 1] < 32))
129 buffer[buffer_length] = 0;
132 ERROR ("oracle plugin: %s (db = %s, query = %s): %s failed: %s",
133 where, db_name, query_name, what, buffer);
137 ERROR ("oracle plugin: %s (db = %s, query = %s): %s failed. "
138 "Additionally, OCIErrorGet failed with status %i.",
139 where, db_name, query_name, what, status);
143 } /* }}} void o_report_error */
145 static void o_database_free (o_database_t *db) /* {{{ */
151 sfree (db->connect_id);
152 sfree (db->username);
153 sfree (db->password);
156 if (db->q_prep_areas != NULL)
157 for (size_t i = 0; i < db->queries_num; ++i)
158 udb_query_delete_preparation_area (db->q_prep_areas[i]);
159 free (db->q_prep_areas);
162 } /* }}} void o_database_free */
164 /* Configuration handling functions {{{
167 * <Query "plugin_instance0">
168 * Statement "SELECT name, value FROM table"
171 * InstancesFrom "name"
176 * <Database "plugin_instance1">
180 * Query "plugin_instance0"
185 static int o_config_add_database (oconfig_item_t *ci) /* {{{ */
190 if ((ci->values_num != 1)
191 || (ci->values[0].type != OCONFIG_TYPE_STRING))
193 WARNING ("oracle plugin: The `Database' block "
194 "needs exactly one string argument.");
198 db = calloc (1, sizeof (*db));
201 ERROR ("oracle plugin: calloc failed.");
206 db->connect_id = NULL;
210 status = cf_util_get_string (ci, &db->name);
217 /* Fill the `o_database_t' structure.. */
218 for (int i = 0; i < ci->children_num; i++)
220 oconfig_item_t *child = ci->children + i;
222 if (strcasecmp ("ConnectID", child->key) == 0)
223 status = cf_util_get_string (child, &db->connect_id);
224 else if (strcasecmp ("Host", child->key) == 0)
225 status = cf_util_get_string (child, &db->host);
226 else if (strcasecmp ("Username", child->key) == 0)
227 status = cf_util_get_string (child, &db->username);
228 else if (strcasecmp ("Password", child->key) == 0)
229 status = cf_util_get_string (child, &db->password);
230 else if (strcasecmp ("Query", child->key) == 0)
231 status = udb_query_pick_from_list (child, queries, queries_num,
232 &db->queries, &db->queries_num);
235 WARNING ("oracle plugin: Option `%s' not allowed here.", child->key);
243 /* Check that all necessary options have been given. */
246 if (db->connect_id == NULL)
248 WARNING ("oracle plugin: `ConnectID' not given for query `%s'", db->name);
251 if (db->username == NULL)
253 WARNING ("oracle plugin: `Username' not given for query `%s'", db->name);
256 if (db->password == NULL)
258 WARNING ("oracle plugin: `Password' not given for query `%s'", db->name);
263 } /* while (status == 0) */
265 while ((status == 0) && (db->queries_num > 0))
267 db->q_prep_areas = (udb_query_preparation_area_t **) calloc (
268 db->queries_num, sizeof (*db->q_prep_areas));
270 if (db->q_prep_areas == NULL)
272 WARNING ("oracle plugin: calloc failed");
277 for (int i = 0; i < db->queries_num; ++i)
280 = udb_query_allocate_preparation_area (db->queries[i]);
282 if (db->q_prep_areas[i] == NULL)
284 WARNING ("oracle plugin: udb_query_allocate_preparation_area failed");
293 /* If all went well, add this query to the list of queries within the
294 * database structure. */
299 temp = realloc (databases,
300 sizeof (*databases) * (databases_num + 1));
303 ERROR ("oracle plugin: realloc failed");
309 databases[databases_num] = db;
316 o_database_free (db);
321 } /* }}} int o_config_add_database */
323 static int o_config (oconfig_item_t *ci) /* {{{ */
325 for (int i = 0; i < ci->children_num; i++)
327 oconfig_item_t *child = ci->children + i;
328 if (strcasecmp ("Query", child->key) == 0)
329 udb_query_create (&queries, &queries_num, child,
330 /* callback = */ NULL);
331 else if (strcasecmp ("Database", child->key) == 0)
332 o_config_add_database (child);
335 WARNING ("oracle plugin: Ignoring unknown config option `%s'.", child->key);
340 DEBUG ("oracle plugin: o_config: queries_num = %zu; queries[0] = %p; udb_query_get_user_data (queries[0]) = %p;",
341 queries_num, (void *) queries[0], udb_query_get_user_data (queries[0]));
343 } /* for (ci->children) */
346 } /* }}} int o_config */
348 /* }}} End of configuration handling functions */
350 static int o_init (void) /* {{{ */
357 status = OCIEnvCreate (&oci_env,
358 /* mode = */ OCI_THREADED,
359 /* context = */ NULL,
361 /* realloc = */ NULL,
363 /* user_data_size = */ 0,
364 /* user_data_ptr = */ NULL);
367 ERROR ("oracle plugin: OCIEnvCreate failed with status %i.", status);
371 status = OCIHandleAlloc (oci_env, (void *) &oci_error, OCI_HTYPE_ERROR,
372 /* user_data_size = */ 0, /* user_data = */ NULL);
373 if (status != OCI_SUCCESS)
375 ERROR ("oracle plugin: OCIHandleAlloc (OCI_HTYPE_ERROR) failed "
376 "with status %i.", status);
381 } /* }}} int o_init */
383 static int o_read_database_query (o_database_t *db, /* {{{ */
384 udb_query_t *q, udb_query_preparation_area_t *prep_area)
387 char **column_values;
390 OCIStmt *oci_statement;
392 /* List of `OCIDefine' pointers. These defines map columns to the buffer
393 * space declared above. */
394 OCIDefine **oci_defines;
398 oci_statement = udb_query_get_user_data (q);
400 /* Prepare the statement */
401 if (oci_statement == NULL) /* {{{ */
403 const char *statement;
405 statement = udb_query_get_statement (q);
406 assert (statement != NULL);
408 status = OCIHandleAlloc (oci_env, (void *) &oci_statement,
409 OCI_HTYPE_STMT, /* user_data_size = */ 0, /* user_data = */ NULL);
410 if (status != OCI_SUCCESS)
412 o_report_error ("o_read_database_query", db->name,
413 udb_query_get_name (q), "OCIHandleAlloc", oci_error);
414 oci_statement = NULL;
418 status = OCIStmtPrepare (oci_statement, oci_error,
419 (text *) statement, (ub4) strlen (statement),
420 /* language = */ OCI_NTV_SYNTAX,
421 /* mode = */ OCI_DEFAULT);
422 if (status != OCI_SUCCESS)
424 o_report_error ("o_read_database_query", db->name,
425 udb_query_get_name (q), "OCIStmtPrepare", oci_error);
426 OCIHandleFree (oci_statement, OCI_HTYPE_STMT);
427 oci_statement = NULL;
430 udb_query_set_user_data (q, oci_statement);
432 DEBUG ("oracle plugin: o_read_database_query (%s, %s): "
433 "Successfully allocated statement handle.",
434 db->name, udb_query_get_name (q));
437 assert (oci_statement != NULL);
439 /* Execute the statement */
440 status = OCIStmtExecute (db->oci_service_context, /* {{{ */
445 /* snap_in = */ NULL, /* snap_out = */ NULL,
446 /* mode = */ OCI_DEFAULT);
447 if (status != OCI_SUCCESS)
449 o_report_error ("o_read_database_query", db->name, udb_query_get_name (q),
450 "OCIStmtExecute", oci_error);
454 /* Acquire the number of columns returned. */
457 ub4 param_counter = 0;
458 status = OCIAttrGet (oci_statement, OCI_HTYPE_STMT, /* {{{ */
459 ¶m_counter, /* size pointer = */ NULL,
460 OCI_ATTR_PARAM_COUNT, oci_error);
461 if (status != OCI_SUCCESS)
463 o_report_error ("o_read_database_query", db->name,
464 udb_query_get_name (q), "OCIAttrGet", oci_error);
468 column_num = (size_t) param_counter;
469 } while (0); /* }}} */
471 /* Allocate the following buffers:
473 * +---------------+-----------------------------------+
475 * +---------------+-----------------------------------+
476 * ! column_names ! column_num x DATA_MAX_NAME_LEN !
477 * ! column_values ! column_num x DATA_MAX_NAME_LEN !
478 * ! oci_defines ! column_num x sizeof (OCIDefine *) !
479 * +---------------+-----------------------------------+
482 #define NUMBER_BUFFER_SIZE 64
485 if (column_names != NULL) { \
486 sfree (column_names[0]); \
487 sfree (column_names); \
489 if (column_values != NULL) { \
490 sfree (column_values[0]); \
491 sfree (column_values); \
495 #define ALLOC_OR_FAIL(ptr, ptr_size) \
497 size_t alloc_size = (size_t) ((ptr_size)); \
498 (ptr) = calloc (1, alloc_size); \
499 if ((ptr) == NULL) { \
501 ERROR ("oracle plugin: o_read_database_query: calloc failed."); \
506 /* Initialize everything to NULL so the above works. */
508 column_values = NULL;
511 ALLOC_OR_FAIL (column_names, column_num * sizeof (char *));
512 ALLOC_OR_FAIL (column_names[0], column_num * DATA_MAX_NAME_LEN
514 for (size_t i = 1; i < column_num; i++)
515 column_names[i] = column_names[i - 1] + DATA_MAX_NAME_LEN;
517 ALLOC_OR_FAIL (column_values, column_num * sizeof (char *));
518 ALLOC_OR_FAIL (column_values[0], column_num * DATA_MAX_NAME_LEN
520 for (size_t i = 1; i < column_num; i++)
521 column_values[i] = column_values[i - 1] + DATA_MAX_NAME_LEN;
523 ALLOC_OR_FAIL (oci_defines, column_num * sizeof (OCIDefine *));
524 /* }}} End of buffer allocations. */
526 /* ``Define'' the returned data, i. e. bind the columns to the buffers
527 * allocated above. */
528 for (size_t i = 0; i < column_num; i++) /* {{{ */
531 ub4 column_name_length;
536 status = OCIParamGet (oci_statement, OCI_HTYPE_STMT, oci_error,
537 (void *) &oci_param, (ub4) (i + 1));
538 if (status != OCI_SUCCESS)
540 /* This is probably alright */
541 DEBUG ("oracle plugin: o_read_database_query: status = %#x (= %i);",
543 o_report_error ("o_read_database_query", db->name,
544 udb_query_get_name (q), "OCIParamGet", oci_error);
545 status = OCI_SUCCESS;
550 column_name_length = 0;
551 status = OCIAttrGet (oci_param, OCI_DTYPE_PARAM,
552 &column_name, &column_name_length, OCI_ATTR_NAME, oci_error);
553 if (status != OCI_SUCCESS)
555 OCIDescriptorFree (oci_param, OCI_DTYPE_PARAM);
556 o_report_error ("o_read_database_query", db->name,
557 udb_query_get_name (q), "OCIAttrGet (OCI_ATTR_NAME)", oci_error);
561 OCIDescriptorFree (oci_param, OCI_DTYPE_PARAM);
564 /* Copy the name to column_names. Warning: The ``string'' returned by OCI
565 * may not be null terminated! */
566 memset (column_names[i], 0, DATA_MAX_NAME_LEN);
567 if (column_name_length >= DATA_MAX_NAME_LEN)
568 column_name_length = DATA_MAX_NAME_LEN - 1;
569 memcpy (column_names[i], column_name, column_name_length);
570 column_names[i][column_name_length] = 0;
572 DEBUG ("oracle plugin: o_read_database_query: column_names[%zu] = %s; "
573 "column_name_length = %"PRIu32";",
574 i, column_names[i], (uint32_t) column_name_length);
576 status = OCIDefineByPos (oci_statement,
577 &oci_defines[i], oci_error, (ub4) (i + 1),
578 column_values[i], DATA_MAX_NAME_LEN, SQLT_STR,
579 NULL, NULL, NULL, OCI_DEFAULT);
580 if (status != OCI_SUCCESS)
582 o_report_error ("o_read_database_query", db->name,
583 udb_query_get_name (q), "OCIDefineByPos", oci_error);
586 } /* for (j = 1; j <= param_counter; j++) */
587 /* }}} End of the ``define'' stuff. */
589 status = udb_query_prepare_result (q, prep_area,
590 (db->host != NULL) ? db->host : hostname_g,
591 /* plugin = */ "oracle", db->name, column_names, column_num,
595 ERROR ("oracle plugin: o_read_database_query (%s, %s): "
596 "udb_query_prepare_result failed.",
597 db->name, udb_query_get_name (q));
602 /* Fetch and handle all the rows that matched the query. */
605 status = OCIStmtFetch2 (oci_statement, oci_error,
606 /* nrows = */ 1, /* orientation = */ OCI_FETCH_NEXT,
607 /* fetch offset = */ 0, /* mode = */ OCI_DEFAULT);
608 if (status == OCI_NO_DATA)
610 status = OCI_SUCCESS;
613 else if ((status != OCI_SUCCESS) && (status != OCI_SUCCESS_WITH_INFO))
615 o_report_error ("o_read_database_query", db->name,
616 udb_query_get_name (q), "OCIStmtFetch2", oci_error);
620 status = udb_query_handle_result (q, prep_area, column_values);
623 WARNING ("oracle plugin: o_read_database_query (%s, %s): "
624 "udb_query_handle_result failed.",
625 db->name, udb_query_get_name (q));
627 } /* }}} while (42) */
629 /* DEBUG ("oracle plugin: o_read_database_query: This statement succeeded: %s", q->statement); */
635 } /* }}} int o_read_database_query */
637 static int o_read_database (o_database_t *db) /* {{{ */
641 if (db->oci_service_context != NULL)
643 OCIServer *server_handle;
644 ub4 connection_status;
646 server_handle = NULL;
647 status = OCIAttrGet ((void *) db->oci_service_context, OCI_HTYPE_SVCCTX,
648 (void *) &server_handle, /* size pointer = */ NULL,
649 OCI_ATTR_SERVER, oci_error);
650 if (status != OCI_SUCCESS)
652 o_report_error ("o_read_database", db->name, NULL, "OCIAttrGet",
657 if (server_handle == NULL)
659 connection_status = OCI_SERVER_NOT_CONNECTED;
661 else /* if (server_handle != NULL) */
663 connection_status = 0;
664 status = OCIAttrGet ((void *) server_handle, OCI_HTYPE_SERVER,
665 (void *) &connection_status, /* size pointer = */ NULL,
666 OCI_ATTR_SERVER_STATUS, oci_error);
667 if (status != OCI_SUCCESS)
669 o_report_error ("o_read_database", db->name, NULL, "OCIAttrGet",
675 if (connection_status != OCI_SERVER_NORMAL)
677 INFO ("oracle plugin: Connection to %s lost. Trying to reconnect.",
679 OCIHandleFree (db->oci_service_context, OCI_HTYPE_SVCCTX);
680 db->oci_service_context = NULL;
682 } /* if (db->oci_service_context != NULL) */
684 if (db->oci_service_context == NULL)
686 status = OCILogon (oci_env, oci_error,
687 &db->oci_service_context,
688 (OraText *) db->username, (ub4) strlen (db->username),
689 (OraText *) db->password, (ub4) strlen (db->password),
690 (OraText *) db->connect_id, (ub4) strlen (db->connect_id));
691 if ((status != OCI_SUCCESS) && (status != OCI_SUCCESS_WITH_INFO))
695 ssnprintf (errfunc, sizeof (errfunc), "OCILogon(\"%s\")", db->connect_id);
697 o_report_error ("o_read_database", db->name, NULL, errfunc, oci_error);
698 DEBUG ("oracle plugin: OCILogon (%s): db->oci_service_context = %p;",
699 db->connect_id, db->oci_service_context);
700 db->oci_service_context = NULL;
703 else if (status == OCI_SUCCESS_WITH_INFO)
705 /* TODO: Print NOTIFY message. */
707 assert (db->oci_service_context != NULL);
710 DEBUG ("oracle plugin: o_read_database: db->connect_id = %s; db->oci_service_context = %p;",
711 db->connect_id, db->oci_service_context);
713 for (size_t i = 0; i < db->queries_num; i++)
714 o_read_database_query (db, db->queries[i], db->q_prep_areas[i]);
717 } /* }}} int o_read_database */
719 static int o_read (void) /* {{{ */
723 for (i = 0; i < databases_num; i++)
724 o_read_database (databases[i]);
727 } /* }}} int o_read */
729 static int o_shutdown (void) /* {{{ */
733 for (i = 0; i < databases_num; i++)
734 if (databases[i]->oci_service_context != NULL)
736 OCIHandleFree (databases[i]->oci_service_context, OCI_HTYPE_SVCCTX);
737 databases[i]->oci_service_context = NULL;
740 for (i = 0; i < queries_num; i++)
742 OCIStmt *oci_statement;
744 oci_statement = udb_query_get_user_data (queries[i]);
745 if (oci_statement != NULL)
747 OCIHandleFree (oci_statement, OCI_HTYPE_STMT);
748 udb_query_set_user_data (queries[i], NULL);
752 OCIHandleFree (oci_env, OCI_HTYPE_ENV);
755 udb_query_free (queries, queries_num);
760 } /* }}} int o_shutdown */
762 void module_register (void) /* {{{ */
764 plugin_register_complex_config ("oracle", o_config);
765 plugin_register_init ("oracle", o_init);
766 plugin_register_read ("oracle", o_read);
767 plugin_register_shutdown ("oracle", o_shutdown);
768 } /* }}} void module_register */
771 * vim: shiftwidth=2 softtabstop=2 et fdm=marker