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 "configfile.h"
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)
96 unsigned int record_number;
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 (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) /* {{{ */
153 sfree (db->connect_id);
154 sfree (db->username);
155 sfree (db->password);
158 if (db->q_prep_areas != NULL)
159 for (i = 0; i < db->queries_num; ++i)
160 udb_query_delete_preparation_area (db->q_prep_areas[i]);
161 free (db->q_prep_areas);
164 } /* }}} void o_database_free */
166 /* Configuration handling functions {{{
169 * <Query "plugin_instance0">
170 * Statement "SELECT name, value FROM table"
173 * InstancesFrom "name"
178 * <Database "plugin_instance1">
182 * Query "plugin_instance0"
187 static int o_config_add_database (oconfig_item_t *ci) /* {{{ */
193 if ((ci->values_num != 1)
194 || (ci->values[0].type != OCONFIG_TYPE_STRING))
196 WARNING ("oracle plugin: The `Database' block "
197 "needs exactly one string argument.");
201 db = (o_database_t *) malloc (sizeof (*db));
204 ERROR ("oracle plugin: malloc failed.");
207 memset (db, 0, sizeof (*db));
210 db->connect_id = NULL;
214 status = cf_util_get_string (ci, &db->name);
221 /* Fill the `o_database_t' structure.. */
222 for (i = 0; i < ci->children_num; i++)
224 oconfig_item_t *child = ci->children + i;
226 if (strcasecmp ("ConnectID", child->key) == 0)
227 status = cf_util_get_string (child, &db->connect_id);
228 else if (strcasecmp ("Host", child->key) == 0)
229 status = cf_util_get_string (child, &db->host);
230 else if (strcasecmp ("Username", child->key) == 0)
231 status = cf_util_get_string (child, &db->username);
232 else if (strcasecmp ("Password", child->key) == 0)
233 status = cf_util_get_string (child, &db->password);
234 else if (strcasecmp ("Query", child->key) == 0)
235 status = udb_query_pick_from_list (child, queries, queries_num,
236 &db->queries, &db->queries_num);
239 WARNING ("oracle plugin: Option `%s' not allowed here.", child->key);
247 /* Check that all necessary options have been given. */
250 if (db->connect_id == NULL)
252 WARNING ("oracle plugin: `ConnectID' not given for query `%s'", db->name);
255 if (db->username == NULL)
257 WARNING ("oracle plugin: `Username' not given for query `%s'", db->name);
260 if (db->password == NULL)
262 WARNING ("oracle plugin: `Password' not given for query `%s'", db->name);
267 } /* while (status == 0) */
269 while ((status == 0) && (db->queries_num > 0))
271 db->q_prep_areas = (udb_query_preparation_area_t **) calloc (
272 db->queries_num, sizeof (*db->q_prep_areas));
274 if (db->q_prep_areas == NULL)
276 WARNING ("oracle plugin: malloc failed");
281 for (i = 0; i < db->queries_num; ++i)
284 = udb_query_allocate_preparation_area (db->queries[i]);
286 if (db->q_prep_areas[i] == NULL)
288 WARNING ("oracle plugin: udb_query_allocate_preparation_area failed");
297 /* If all went well, add this query to the list of queries within the
298 * database structure. */
303 temp = (o_database_t **) realloc (databases,
304 sizeof (*databases) * (databases_num + 1));
307 ERROR ("oracle plugin: realloc failed");
313 databases[databases_num] = db;
320 o_database_free (db);
325 } /* }}} int o_config_add_database */
327 static int o_config (oconfig_item_t *ci) /* {{{ */
331 for (i = 0; i < ci->children_num; i++)
333 oconfig_item_t *child = ci->children + i;
334 if (strcasecmp ("Query", child->key) == 0)
335 udb_query_create (&queries, &queries_num, child,
336 /* callback = */ NULL);
337 else if (strcasecmp ("Database", child->key) == 0)
338 o_config_add_database (child);
341 WARNING ("oracle plugin: Ignoring unknown config option `%s'.", child->key);
346 DEBUG ("oracle plugin: o_config: queries_num = %zu; queries[0] = %p; udb_query_get_user_data (queries[0]) = %p;",
347 queries_num, (void *) queries[0], udb_query_get_user_data (queries[0]));
349 } /* for (ci->children) */
352 } /* }}} int o_config */
354 /* }}} End of configuration handling functions */
356 static int o_init (void) /* {{{ */
363 status = OCIEnvCreate (&oci_env,
364 /* mode = */ OCI_THREADED,
365 /* context = */ NULL,
367 /* realloc = */ NULL,
369 /* user_data_size = */ 0,
370 /* user_data_ptr = */ NULL);
373 ERROR ("oracle plugin: OCIEnvCreate failed with status %i.", status);
377 status = OCIHandleAlloc (oci_env, (void *) &oci_error, OCI_HTYPE_ERROR,
378 /* user_data_size = */ 0, /* user_data = */ NULL);
379 if (status != OCI_SUCCESS)
381 ERROR ("oracle plugin: OCIHandleAlloc (OCI_HTYPE_ERROR) failed "
382 "with status %i.", status);
387 } /* }}} int o_init */
389 static int o_read_database_query (o_database_t *db, /* {{{ */
390 udb_query_t *q, udb_query_preparation_area_t *prep_area)
393 char **column_values;
396 OCIStmt *oci_statement;
398 /* List of `OCIDefine' pointers. These defines map columns to the buffer
399 * space declared above. */
400 OCIDefine **oci_defines;
405 oci_statement = udb_query_get_user_data (q);
407 /* Prepare the statement */
408 if (oci_statement == NULL) /* {{{ */
410 const char *statement;
412 statement = udb_query_get_statement (q);
413 assert (statement != NULL);
415 status = OCIHandleAlloc (oci_env, (void *) &oci_statement,
416 OCI_HTYPE_STMT, /* user_data_size = */ 0, /* user_data = */ NULL);
417 if (status != OCI_SUCCESS)
419 o_report_error ("o_read_database_query", db->name,
420 udb_query_get_name (q), "OCIHandleAlloc", oci_error);
421 oci_statement = NULL;
425 status = OCIStmtPrepare (oci_statement, oci_error,
426 (text *) statement, (ub4) strlen (statement),
427 /* language = */ OCI_NTV_SYNTAX,
428 /* mode = */ OCI_DEFAULT);
429 if (status != OCI_SUCCESS)
431 o_report_error ("o_read_database_query", db->name,
432 udb_query_get_name (q), "OCIStmtPrepare", oci_error);
433 OCIHandleFree (oci_statement, OCI_HTYPE_STMT);
434 oci_statement = NULL;
437 udb_query_set_user_data (q, oci_statement);
439 DEBUG ("oracle plugin: o_read_database_query (%s, %s): "
440 "Successfully allocated statement handle.",
441 db->name, udb_query_get_name (q));
444 assert (oci_statement != NULL);
446 /* Execute the statement */
447 status = OCIStmtExecute (db->oci_service_context, /* {{{ */
452 /* snap_in = */ NULL, /* snap_out = */ NULL,
453 /* mode = */ OCI_DEFAULT);
454 if (status != OCI_SUCCESS)
456 o_report_error ("o_read_database_query", db->name, udb_query_get_name (q),
457 "OCIStmtExecute", oci_error);
461 /* Acquire the number of columns returned. */
464 ub4 param_counter = 0;
465 status = OCIAttrGet (oci_statement, OCI_HTYPE_STMT, /* {{{ */
466 ¶m_counter, /* size pointer = */ NULL,
467 OCI_ATTR_PARAM_COUNT, oci_error);
468 if (status != OCI_SUCCESS)
470 o_report_error ("o_read_database_query", db->name,
471 udb_query_get_name (q), "OCIAttrGet", oci_error);
475 column_num = (size_t) param_counter;
476 } while (0); /* }}} */
478 /* Allocate the following buffers:
480 * +---------------+-----------------------------------+
482 * +---------------+-----------------------------------+
483 * ! column_names ! column_num x DATA_MAX_NAME_LEN !
484 * ! column_values ! column_num x DATA_MAX_NAME_LEN !
485 * ! oci_defines ! column_num x sizeof (OCIDefine *) !
486 * +---------------+-----------------------------------+
489 #define NUMBER_BUFFER_SIZE 64
492 if (column_names != NULL) { \
493 sfree (column_names[0]); \
494 sfree (column_names); \
496 if (column_values != NULL) { \
497 sfree (column_values[0]); \
498 sfree (column_values); \
502 #define ALLOC_OR_FAIL(ptr, ptr_size) \
504 size_t alloc_size = (size_t) ((ptr_size)); \
505 (ptr) = malloc (alloc_size); \
506 if ((ptr) == NULL) { \
508 ERROR ("oracle plugin: o_read_database_query: malloc failed."); \
511 memset ((ptr), 0, alloc_size); \
514 /* Initialize everything to NULL so the above works. */
516 column_values = NULL;
519 ALLOC_OR_FAIL (column_names, column_num * sizeof (char *));
520 ALLOC_OR_FAIL (column_names[0], column_num * DATA_MAX_NAME_LEN
522 for (i = 1; i < column_num; i++)
523 column_names[i] = column_names[i - 1] + DATA_MAX_NAME_LEN;
525 ALLOC_OR_FAIL (column_values, column_num * sizeof (char *));
526 ALLOC_OR_FAIL (column_values[0], column_num * DATA_MAX_NAME_LEN
528 for (i = 1; i < column_num; i++)
529 column_values[i] = column_values[i - 1] + DATA_MAX_NAME_LEN;
531 ALLOC_OR_FAIL (oci_defines, column_num * sizeof (OCIDefine *));
532 /* }}} End of buffer allocations. */
534 /* ``Define'' the returned data, i. e. bind the columns to the buffers
535 * allocated above. */
536 for (i = 0; i < column_num; i++) /* {{{ */
539 ub4 column_name_length;
544 status = OCIParamGet (oci_statement, OCI_HTYPE_STMT, oci_error,
545 (void *) &oci_param, (ub4) (i + 1));
546 if (status != OCI_SUCCESS)
548 /* This is probably alright */
549 DEBUG ("oracle plugin: o_read_database_query: status = %#x (= %i);",
551 o_report_error ("o_read_database_query", db->name,
552 udb_query_get_name (q), "OCIParamGet", oci_error);
553 status = OCI_SUCCESS;
558 column_name_length = 0;
559 status = OCIAttrGet (oci_param, OCI_DTYPE_PARAM,
560 &column_name, &column_name_length, OCI_ATTR_NAME, oci_error);
561 if (status != OCI_SUCCESS)
563 OCIDescriptorFree (oci_param, OCI_DTYPE_PARAM);
564 o_report_error ("o_read_database_query", db->name,
565 udb_query_get_name (q), "OCIAttrGet (OCI_ATTR_NAME)", oci_error);
569 OCIDescriptorFree (oci_param, OCI_DTYPE_PARAM);
572 /* Copy the name to column_names. Warning: The ``string'' returned by OCI
573 * may not be null terminated! */
574 memset (column_names[i], 0, DATA_MAX_NAME_LEN);
575 if (column_name_length >= DATA_MAX_NAME_LEN)
576 column_name_length = DATA_MAX_NAME_LEN - 1;
577 memcpy (column_names[i], column_name, column_name_length);
578 column_names[i][column_name_length] = 0;
580 DEBUG ("oracle plugin: o_read_database_query: column_names[%zu] = %s; "
581 "column_name_length = %"PRIu32";",
582 i, column_names[i], (uint32_t) column_name_length);
584 status = OCIDefineByPos (oci_statement,
585 &oci_defines[i], oci_error, (ub4) (i + 1),
586 column_values[i], DATA_MAX_NAME_LEN, SQLT_STR,
587 NULL, NULL, NULL, OCI_DEFAULT);
588 if (status != OCI_SUCCESS)
590 o_report_error ("o_read_database_query", db->name,
591 udb_query_get_name (q), "OCIDefineByPos", oci_error);
594 } /* for (j = 1; j <= param_counter; j++) */
595 /* }}} End of the ``define'' stuff. */
597 status = udb_query_prepare_result (q, prep_area,
598 (db->host != NULL) ? db->host : hostname_g,
599 /* plugin = */ "oracle", db->name, column_names, column_num,
603 ERROR ("oracle plugin: o_read_database_query (%s, %s): "
604 "udb_query_prepare_result failed.",
605 db->name, udb_query_get_name (q));
610 /* Fetch and handle all the rows that matched the query. */
613 status = OCIStmtFetch2 (oci_statement, oci_error,
614 /* nrows = */ 1, /* orientation = */ OCI_FETCH_NEXT,
615 /* fetch offset = */ 0, /* mode = */ OCI_DEFAULT);
616 if (status == OCI_NO_DATA)
618 status = OCI_SUCCESS;
621 else if ((status != OCI_SUCCESS) && (status != OCI_SUCCESS_WITH_INFO))
623 o_report_error ("o_read_database_query", db->name,
624 udb_query_get_name (q), "OCIStmtFetch2", oci_error);
628 status = udb_query_handle_result (q, prep_area, column_values);
631 WARNING ("oracle plugin: o_read_database_query (%s, %s): "
632 "udb_query_handle_result failed.",
633 db->name, udb_query_get_name (q));
635 } /* }}} while (42) */
637 /* DEBUG ("oracle plugin: o_read_database_query: This statement succeeded: %s", q->statement); */
643 } /* }}} int o_read_database_query */
645 static int o_read_database (o_database_t *db) /* {{{ */
650 if (db->oci_service_context != NULL)
652 OCIServer *server_handle;
653 ub4 connection_status;
655 server_handle = NULL;
656 status = OCIAttrGet ((void *) db->oci_service_context, OCI_HTYPE_SVCCTX,
657 (void *) &server_handle, /* size pointer = */ NULL,
658 OCI_ATTR_SERVER, oci_error);
659 if (status != OCI_SUCCESS)
661 o_report_error ("o_read_database", db->name, NULL, "OCIAttrGet",
666 if (server_handle == NULL)
668 connection_status = OCI_SERVER_NOT_CONNECTED;
670 else /* if (server_handle != NULL) */
672 connection_status = 0;
673 status = OCIAttrGet ((void *) server_handle, OCI_HTYPE_SERVER,
674 (void *) &connection_status, /* size pointer = */ NULL,
675 OCI_ATTR_SERVER_STATUS, oci_error);
676 if (status != OCI_SUCCESS)
678 o_report_error ("o_read_database", db->name, NULL, "OCIAttrGet",
684 if (connection_status != OCI_SERVER_NORMAL)
686 INFO ("oracle plugin: Connection to %s lost. Trying to reconnect.",
688 OCIHandleFree (db->oci_service_context, OCI_HTYPE_SVCCTX);
689 db->oci_service_context = NULL;
691 } /* if (db->oci_service_context != NULL) */
693 if (db->oci_service_context == NULL)
695 status = OCILogon (oci_env, oci_error,
696 &db->oci_service_context,
697 (OraText *) db->username, (ub4) strlen (db->username),
698 (OraText *) db->password, (ub4) strlen (db->password),
699 (OraText *) db->connect_id, (ub4) strlen (db->connect_id));
700 if ((status != OCI_SUCCESS) && (status != OCI_SUCCESS_WITH_INFO))
704 ssnprintf (errfunc, sizeof (errfunc), "OCILogon(\"%s\")", db->connect_id);
706 o_report_error ("o_read_database", db->name, NULL, errfunc, oci_error);
707 DEBUG ("oracle plugin: OCILogon (%s): db->oci_service_context = %p;",
708 db->connect_id, db->oci_service_context);
709 db->oci_service_context = NULL;
712 else if (status == OCI_SUCCESS_WITH_INFO)
714 /* TODO: Print NOTIFY message. */
716 assert (db->oci_service_context != NULL);
719 DEBUG ("oracle plugin: o_read_database: db->connect_id = %s; db->oci_service_context = %p;",
720 db->connect_id, db->oci_service_context);
722 for (i = 0; i < db->queries_num; i++)
723 o_read_database_query (db, db->queries[i], db->q_prep_areas[i]);
726 } /* }}} int o_read_database */
728 static int o_read (void) /* {{{ */
732 for (i = 0; i < databases_num; i++)
733 o_read_database (databases[i]);
736 } /* }}} int o_read */
738 static int o_shutdown (void) /* {{{ */
742 for (i = 0; i < databases_num; i++)
743 if (databases[i]->oci_service_context != NULL)
745 OCIHandleFree (databases[i]->oci_service_context, OCI_HTYPE_SVCCTX);
746 databases[i]->oci_service_context = NULL;
749 for (i = 0; i < queries_num; i++)
751 OCIStmt *oci_statement;
753 oci_statement = udb_query_get_user_data (queries[i]);
754 if (oci_statement != NULL)
756 OCIHandleFree (oci_statement, OCI_HTYPE_STMT);
757 udb_query_set_user_data (queries[i], NULL);
761 OCIHandleFree (oci_env, OCI_HTYPE_ENV);
764 udb_query_free (queries, queries_num);
769 } /* }}} int o_shutdown */
771 void module_register (void) /* {{{ */
773 plugin_register_complex_config ("oracle", o_config);
774 plugin_register_init ("oracle", o_init);
775 plugin_register_read ("oracle", o_read);
776 plugin_register_shutdown ("oracle", o_shutdown);
777 } /* }}} void module_register */
780 * vim: shiftwidth=2 softtabstop=2 et fdm=marker