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)
97 unsigned int record_number;
101 if (query_name == NULL)
102 query_name = "(none)";
104 /* An operation may cause / return multiple errors. Loop until we have
105 * handled all errors available (with a fail-save limit of 16). */
106 for (record_number = 1; record_number <= 16; record_number++)
108 memset (buffer, 0, sizeof (buffer));
111 status = OCIErrorGet (eh, (ub4) record_number,
112 /* sqlstate = */ NULL,
115 (ub4) sizeof (buffer),
117 buffer[sizeof (buffer) - 1] = 0;
119 if (status == OCI_NO_DATA)
122 if (status == OCI_SUCCESS)
124 size_t buffer_length;
126 buffer_length = strlen (buffer);
127 while ((buffer_length > 0) && (buffer[buffer_length - 1] < 32))
130 buffer[buffer_length] = 0;
133 ERROR ("oracle plugin: %s (db = %s, query = %s): %s failed: %s",
134 where, db_name, query_name, what, buffer);
138 ERROR ("oracle plugin: %s (db = %s, query = %s): %s failed. "
139 "Additionally, OCIErrorGet failed with status %i.",
140 where, db_name, query_name, what, status);
144 } /* }}} void o_report_error */
146 static void o_database_free (o_database_t *db) /* {{{ */
154 sfree (db->connect_id);
155 sfree (db->username);
156 sfree (db->password);
159 if (db->q_prep_areas != NULL)
160 for (i = 0; i < db->queries_num; ++i)
161 udb_query_delete_preparation_area (db->q_prep_areas[i]);
162 free (db->q_prep_areas);
165 } /* }}} void o_database_free */
167 /* Configuration handling functions {{{
170 * <Query "plugin_instance0">
171 * Statement "SELECT name, value FROM table"
174 * InstancesFrom "name"
179 * <Database "plugin_instance1">
183 * Query "plugin_instance0"
188 static int o_config_add_database (oconfig_item_t *ci) /* {{{ */
194 if ((ci->values_num != 1)
195 || (ci->values[0].type != OCONFIG_TYPE_STRING))
197 WARNING ("oracle plugin: The `Database' block "
198 "needs exactly one string argument.");
202 db = calloc (1, sizeof (*db));
205 ERROR ("oracle plugin: calloc failed.");
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: calloc 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 = 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) = calloc (1, alloc_size); \
506 if ((ptr) == NULL) { \
508 ERROR ("oracle plugin: o_read_database_query: calloc failed."); \
513 /* Initialize everything to NULL so the above works. */
515 column_values = NULL;
518 ALLOC_OR_FAIL (column_names, column_num * sizeof (char *));
519 ALLOC_OR_FAIL (column_names[0], column_num * DATA_MAX_NAME_LEN
521 for (i = 1; i < column_num; i++)
522 column_names[i] = column_names[i - 1] + DATA_MAX_NAME_LEN;
524 ALLOC_OR_FAIL (column_values, column_num * sizeof (char *));
525 ALLOC_OR_FAIL (column_values[0], column_num * DATA_MAX_NAME_LEN
527 for (i = 1; i < column_num; i++)
528 column_values[i] = column_values[i - 1] + DATA_MAX_NAME_LEN;
530 ALLOC_OR_FAIL (oci_defines, column_num * sizeof (OCIDefine *));
531 /* }}} End of buffer allocations. */
533 /* ``Define'' the returned data, i. e. bind the columns to the buffers
534 * allocated above. */
535 for (i = 0; i < column_num; i++) /* {{{ */
538 ub4 column_name_length;
543 status = OCIParamGet (oci_statement, OCI_HTYPE_STMT, oci_error,
544 (void *) &oci_param, (ub4) (i + 1));
545 if (status != OCI_SUCCESS)
547 /* This is probably alright */
548 DEBUG ("oracle plugin: o_read_database_query: status = %#x (= %i);",
550 o_report_error ("o_read_database_query", db->name,
551 udb_query_get_name (q), "OCIParamGet", oci_error);
552 status = OCI_SUCCESS;
557 column_name_length = 0;
558 status = OCIAttrGet (oci_param, OCI_DTYPE_PARAM,
559 &column_name, &column_name_length, OCI_ATTR_NAME, oci_error);
560 if (status != OCI_SUCCESS)
562 OCIDescriptorFree (oci_param, OCI_DTYPE_PARAM);
563 o_report_error ("o_read_database_query", db->name,
564 udb_query_get_name (q), "OCIAttrGet (OCI_ATTR_NAME)", oci_error);
568 OCIDescriptorFree (oci_param, OCI_DTYPE_PARAM);
571 /* Copy the name to column_names. Warning: The ``string'' returned by OCI
572 * may not be null terminated! */
573 memset (column_names[i], 0, DATA_MAX_NAME_LEN);
574 if (column_name_length >= DATA_MAX_NAME_LEN)
575 column_name_length = DATA_MAX_NAME_LEN - 1;
576 memcpy (column_names[i], column_name, column_name_length);
577 column_names[i][column_name_length] = 0;
579 DEBUG ("oracle plugin: o_read_database_query: column_names[%zu] = %s; "
580 "column_name_length = %"PRIu32";",
581 i, column_names[i], (uint32_t) column_name_length);
583 status = OCIDefineByPos (oci_statement,
584 &oci_defines[i], oci_error, (ub4) (i + 1),
585 column_values[i], DATA_MAX_NAME_LEN, SQLT_STR,
586 NULL, NULL, NULL, OCI_DEFAULT);
587 if (status != OCI_SUCCESS)
589 o_report_error ("o_read_database_query", db->name,
590 udb_query_get_name (q), "OCIDefineByPos", oci_error);
593 } /* for (j = 1; j <= param_counter; j++) */
594 /* }}} End of the ``define'' stuff. */
596 status = udb_query_prepare_result (q, prep_area,
597 (db->host != NULL) ? db->host : hostname_g,
598 /* plugin = */ "oracle", db->name, column_names, column_num,
602 ERROR ("oracle plugin: o_read_database_query (%s, %s): "
603 "udb_query_prepare_result failed.",
604 db->name, udb_query_get_name (q));
609 /* Fetch and handle all the rows that matched the query. */
612 status = OCIStmtFetch2 (oci_statement, oci_error,
613 /* nrows = */ 1, /* orientation = */ OCI_FETCH_NEXT,
614 /* fetch offset = */ 0, /* mode = */ OCI_DEFAULT);
615 if (status == OCI_NO_DATA)
617 status = OCI_SUCCESS;
620 else if ((status != OCI_SUCCESS) && (status != OCI_SUCCESS_WITH_INFO))
622 o_report_error ("o_read_database_query", db->name,
623 udb_query_get_name (q), "OCIStmtFetch2", oci_error);
627 status = udb_query_handle_result (q, prep_area, column_values);
630 WARNING ("oracle plugin: o_read_database_query (%s, %s): "
631 "udb_query_handle_result failed.",
632 db->name, udb_query_get_name (q));
634 } /* }}} while (42) */
636 /* DEBUG ("oracle plugin: o_read_database_query: This statement succeeded: %s", q->statement); */
642 } /* }}} int o_read_database_query */
644 static int o_read_database (o_database_t *db) /* {{{ */
649 if (db->oci_service_context != NULL)
651 OCIServer *server_handle;
652 ub4 connection_status;
654 server_handle = NULL;
655 status = OCIAttrGet ((void *) db->oci_service_context, OCI_HTYPE_SVCCTX,
656 (void *) &server_handle, /* size pointer = */ NULL,
657 OCI_ATTR_SERVER, oci_error);
658 if (status != OCI_SUCCESS)
660 o_report_error ("o_read_database", db->name, NULL, "OCIAttrGet",
665 if (server_handle == NULL)
667 connection_status = OCI_SERVER_NOT_CONNECTED;
669 else /* if (server_handle != NULL) */
671 connection_status = 0;
672 status = OCIAttrGet ((void *) server_handle, OCI_HTYPE_SERVER,
673 (void *) &connection_status, /* size pointer = */ NULL,
674 OCI_ATTR_SERVER_STATUS, oci_error);
675 if (status != OCI_SUCCESS)
677 o_report_error ("o_read_database", db->name, NULL, "OCIAttrGet",
683 if (connection_status != OCI_SERVER_NORMAL)
685 INFO ("oracle plugin: Connection to %s lost. Trying to reconnect.",
687 OCIHandleFree (db->oci_service_context, OCI_HTYPE_SVCCTX);
688 db->oci_service_context = NULL;
690 } /* if (db->oci_service_context != NULL) */
692 if (db->oci_service_context == NULL)
694 status = OCILogon (oci_env, oci_error,
695 &db->oci_service_context,
696 (OraText *) db->username, (ub4) strlen (db->username),
697 (OraText *) db->password, (ub4) strlen (db->password),
698 (OraText *) db->connect_id, (ub4) strlen (db->connect_id));
699 if ((status != OCI_SUCCESS) && (status != OCI_SUCCESS_WITH_INFO))
703 ssnprintf (errfunc, sizeof (errfunc), "OCILogon(\"%s\")", db->connect_id);
705 o_report_error ("o_read_database", db->name, NULL, errfunc, oci_error);
706 DEBUG ("oracle plugin: OCILogon (%s): db->oci_service_context = %p;",
707 db->connect_id, db->oci_service_context);
708 db->oci_service_context = NULL;
711 else if (status == OCI_SUCCESS_WITH_INFO)
713 /* TODO: Print NOTIFY message. */
715 assert (db->oci_service_context != NULL);
718 DEBUG ("oracle plugin: o_read_database: db->connect_id = %s; db->oci_service_context = %p;",
719 db->connect_id, db->oci_service_context);
721 for (i = 0; i < db->queries_num; i++)
722 o_read_database_query (db, db->queries[i], db->q_prep_areas[i]);
725 } /* }}} int o_read_database */
727 static int o_read (void) /* {{{ */
731 for (i = 0; i < databases_num; i++)
732 o_read_database (databases[i]);
735 } /* }}} int o_read */
737 static int o_shutdown (void) /* {{{ */
741 for (i = 0; i < databases_num; i++)
742 if (databases[i]->oci_service_context != NULL)
744 OCIHandleFree (databases[i]->oci_service_context, OCI_HTYPE_SVCCTX);
745 databases[i]->oci_service_context = NULL;
748 for (i = 0; i < queries_num; i++)
750 OCIStmt *oci_statement;
752 oci_statement = udb_query_get_user_data (queries[i]);
753 if (oci_statement != NULL)
755 OCIHandleFree (oci_statement, OCI_HTYPE_STMT);
756 udb_query_set_user_data (queries[i], NULL);
760 OCIHandleFree (oci_env, OCI_HTYPE_ENV);
763 udb_query_free (queries, queries_num);
768 } /* }}} int o_shutdown */
770 void module_register (void) /* {{{ */
772 plugin_register_complex_config ("oracle", o_config);
773 plugin_register_init ("oracle", o_init);
774 plugin_register_read ("oracle", o_read);
775 plugin_register_shutdown ("oracle", o_shutdown);
776 } /* }}} void module_register */
779 * vim: shiftwidth=2 softtabstop=2 et fdm=marker