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 = calloc (1, sizeof (*db));
204 ERROR ("oracle plugin: calloc failed.");
209 db->connect_id = NULL;
213 status = cf_util_get_string (ci, &db->name);
220 /* Fill the `o_database_t' structure.. */
221 for (i = 0; i < ci->children_num; i++)
223 oconfig_item_t *child = ci->children + i;
225 if (strcasecmp ("ConnectID", child->key) == 0)
226 status = cf_util_get_string (child, &db->connect_id);
227 else if (strcasecmp ("Host", child->key) == 0)
228 status = cf_util_get_string (child, &db->host);
229 else if (strcasecmp ("Username", child->key) == 0)
230 status = cf_util_get_string (child, &db->username);
231 else if (strcasecmp ("Password", child->key) == 0)
232 status = cf_util_get_string (child, &db->password);
233 else if (strcasecmp ("Query", child->key) == 0)
234 status = udb_query_pick_from_list (child, queries, queries_num,
235 &db->queries, &db->queries_num);
238 WARNING ("oracle plugin: Option `%s' not allowed here.", child->key);
246 /* Check that all necessary options have been given. */
249 if (db->connect_id == NULL)
251 WARNING ("oracle plugin: `ConnectID' not given for query `%s'", db->name);
254 if (db->username == NULL)
256 WARNING ("oracle plugin: `Username' not given for query `%s'", db->name);
259 if (db->password == NULL)
261 WARNING ("oracle plugin: `Password' not given for query `%s'", db->name);
266 } /* while (status == 0) */
268 while ((status == 0) && (db->queries_num > 0))
270 db->q_prep_areas = (udb_query_preparation_area_t **) calloc (
271 db->queries_num, sizeof (*db->q_prep_areas));
273 if (db->q_prep_areas == NULL)
275 WARNING ("oracle plugin: calloc failed");
280 for (i = 0; i < db->queries_num; ++i)
283 = udb_query_allocate_preparation_area (db->queries[i]);
285 if (db->q_prep_areas[i] == NULL)
287 WARNING ("oracle plugin: udb_query_allocate_preparation_area failed");
296 /* If all went well, add this query to the list of queries within the
297 * database structure. */
302 temp = realloc (databases,
303 sizeof (*databases) * (databases_num + 1));
306 ERROR ("oracle plugin: realloc failed");
312 databases[databases_num] = db;
319 o_database_free (db);
324 } /* }}} int o_config_add_database */
326 static int o_config (oconfig_item_t *ci) /* {{{ */
330 for (i = 0; i < ci->children_num; i++)
332 oconfig_item_t *child = ci->children + i;
333 if (strcasecmp ("Query", child->key) == 0)
334 udb_query_create (&queries, &queries_num, child,
335 /* callback = */ NULL);
336 else if (strcasecmp ("Database", child->key) == 0)
337 o_config_add_database (child);
340 WARNING ("oracle plugin: Ignoring unknown config option `%s'.", child->key);
345 DEBUG ("oracle plugin: o_config: queries_num = %zu; queries[0] = %p; udb_query_get_user_data (queries[0]) = %p;",
346 queries_num, (void *) queries[0], udb_query_get_user_data (queries[0]));
348 } /* for (ci->children) */
351 } /* }}} int o_config */
353 /* }}} End of configuration handling functions */
355 static int o_init (void) /* {{{ */
362 status = OCIEnvCreate (&oci_env,
363 /* mode = */ OCI_THREADED,
364 /* context = */ NULL,
366 /* realloc = */ NULL,
368 /* user_data_size = */ 0,
369 /* user_data_ptr = */ NULL);
372 ERROR ("oracle plugin: OCIEnvCreate failed with status %i.", status);
376 status = OCIHandleAlloc (oci_env, (void *) &oci_error, OCI_HTYPE_ERROR,
377 /* user_data_size = */ 0, /* user_data = */ NULL);
378 if (status != OCI_SUCCESS)
380 ERROR ("oracle plugin: OCIHandleAlloc (OCI_HTYPE_ERROR) failed "
381 "with status %i.", status);
386 } /* }}} int o_init */
388 static int o_read_database_query (o_database_t *db, /* {{{ */
389 udb_query_t *q, udb_query_preparation_area_t *prep_area)
392 char **column_values;
395 OCIStmt *oci_statement;
397 /* List of `OCIDefine' pointers. These defines map columns to the buffer
398 * space declared above. */
399 OCIDefine **oci_defines;
404 oci_statement = udb_query_get_user_data (q);
406 /* Prepare the statement */
407 if (oci_statement == NULL) /* {{{ */
409 const char *statement;
411 statement = udb_query_get_statement (q);
412 assert (statement != NULL);
414 status = OCIHandleAlloc (oci_env, (void *) &oci_statement,
415 OCI_HTYPE_STMT, /* user_data_size = */ 0, /* user_data = */ NULL);
416 if (status != OCI_SUCCESS)
418 o_report_error ("o_read_database_query", db->name,
419 udb_query_get_name (q), "OCIHandleAlloc", oci_error);
420 oci_statement = NULL;
424 status = OCIStmtPrepare (oci_statement, oci_error,
425 (text *) statement, (ub4) strlen (statement),
426 /* language = */ OCI_NTV_SYNTAX,
427 /* mode = */ OCI_DEFAULT);
428 if (status != OCI_SUCCESS)
430 o_report_error ("o_read_database_query", db->name,
431 udb_query_get_name (q), "OCIStmtPrepare", oci_error);
432 OCIHandleFree (oci_statement, OCI_HTYPE_STMT);
433 oci_statement = NULL;
436 udb_query_set_user_data (q, oci_statement);
438 DEBUG ("oracle plugin: o_read_database_query (%s, %s): "
439 "Successfully allocated statement handle.",
440 db->name, udb_query_get_name (q));
443 assert (oci_statement != NULL);
445 /* Execute the statement */
446 status = OCIStmtExecute (db->oci_service_context, /* {{{ */
451 /* snap_in = */ NULL, /* snap_out = */ NULL,
452 /* mode = */ OCI_DEFAULT);
453 if (status != OCI_SUCCESS)
455 o_report_error ("o_read_database_query", db->name, udb_query_get_name (q),
456 "OCIStmtExecute", oci_error);
460 /* Acquire the number of columns returned. */
463 ub4 param_counter = 0;
464 status = OCIAttrGet (oci_statement, OCI_HTYPE_STMT, /* {{{ */
465 ¶m_counter, /* size pointer = */ NULL,
466 OCI_ATTR_PARAM_COUNT, oci_error);
467 if (status != OCI_SUCCESS)
469 o_report_error ("o_read_database_query", db->name,
470 udb_query_get_name (q), "OCIAttrGet", oci_error);
474 column_num = (size_t) param_counter;
475 } while (0); /* }}} */
477 /* Allocate the following buffers:
479 * +---------------+-----------------------------------+
481 * +---------------+-----------------------------------+
482 * ! column_names ! column_num x DATA_MAX_NAME_LEN !
483 * ! column_values ! column_num x DATA_MAX_NAME_LEN !
484 * ! oci_defines ! column_num x sizeof (OCIDefine *) !
485 * +---------------+-----------------------------------+
488 #define NUMBER_BUFFER_SIZE 64
491 if (column_names != NULL) { \
492 sfree (column_names[0]); \
493 sfree (column_names); \
495 if (column_values != NULL) { \
496 sfree (column_values[0]); \
497 sfree (column_values); \
501 #define ALLOC_OR_FAIL(ptr, ptr_size) \
503 size_t alloc_size = (size_t) ((ptr_size)); \
504 (ptr) = calloc (1, alloc_size); \
505 if ((ptr) == NULL) { \
507 ERROR ("oracle plugin: o_read_database_query: calloc failed."); \
512 /* Initialize everything to NULL so the above works. */
514 column_values = NULL;
517 ALLOC_OR_FAIL (column_names, column_num * sizeof (char *));
518 ALLOC_OR_FAIL (column_names[0], column_num * DATA_MAX_NAME_LEN
520 for (i = 1; i < column_num; i++)
521 column_names[i] = column_names[i - 1] + DATA_MAX_NAME_LEN;
523 ALLOC_OR_FAIL (column_values, column_num * sizeof (char *));
524 ALLOC_OR_FAIL (column_values[0], column_num * DATA_MAX_NAME_LEN
526 for (i = 1; i < column_num; i++)
527 column_values[i] = column_values[i - 1] + DATA_MAX_NAME_LEN;
529 ALLOC_OR_FAIL (oci_defines, column_num * sizeof (OCIDefine *));
530 /* }}} End of buffer allocations. */
532 /* ``Define'' the returned data, i. e. bind the columns to the buffers
533 * allocated above. */
534 for (i = 0; i < column_num; i++) /* {{{ */
537 ub4 column_name_length;
542 status = OCIParamGet (oci_statement, OCI_HTYPE_STMT, oci_error,
543 (void *) &oci_param, (ub4) (i + 1));
544 if (status != OCI_SUCCESS)
546 /* This is probably alright */
547 DEBUG ("oracle plugin: o_read_database_query: status = %#x (= %i);",
549 o_report_error ("o_read_database_query", db->name,
550 udb_query_get_name (q), "OCIParamGet", oci_error);
551 status = OCI_SUCCESS;
556 column_name_length = 0;
557 status = OCIAttrGet (oci_param, OCI_DTYPE_PARAM,
558 &column_name, &column_name_length, OCI_ATTR_NAME, oci_error);
559 if (status != OCI_SUCCESS)
561 OCIDescriptorFree (oci_param, OCI_DTYPE_PARAM);
562 o_report_error ("o_read_database_query", db->name,
563 udb_query_get_name (q), "OCIAttrGet (OCI_ATTR_NAME)", oci_error);
567 OCIDescriptorFree (oci_param, OCI_DTYPE_PARAM);
570 /* Copy the name to column_names. Warning: The ``string'' returned by OCI
571 * may not be null terminated! */
572 memset (column_names[i], 0, DATA_MAX_NAME_LEN);
573 if (column_name_length >= DATA_MAX_NAME_LEN)
574 column_name_length = DATA_MAX_NAME_LEN - 1;
575 memcpy (column_names[i], column_name, column_name_length);
576 column_names[i][column_name_length] = 0;
578 DEBUG ("oracle plugin: o_read_database_query: column_names[%zu] = %s; "
579 "column_name_length = %"PRIu32";",
580 i, column_names[i], (uint32_t) column_name_length);
582 status = OCIDefineByPos (oci_statement,
583 &oci_defines[i], oci_error, (ub4) (i + 1),
584 column_values[i], DATA_MAX_NAME_LEN, SQLT_STR,
585 NULL, NULL, NULL, OCI_DEFAULT);
586 if (status != OCI_SUCCESS)
588 o_report_error ("o_read_database_query", db->name,
589 udb_query_get_name (q), "OCIDefineByPos", oci_error);
592 } /* for (j = 1; j <= param_counter; j++) */
593 /* }}} End of the ``define'' stuff. */
595 status = udb_query_prepare_result (q, prep_area,
596 (db->host != NULL) ? db->host : hostname_g,
597 /* plugin = */ "oracle", db->name, column_names, column_num,
601 ERROR ("oracle plugin: o_read_database_query (%s, %s): "
602 "udb_query_prepare_result failed.",
603 db->name, udb_query_get_name (q));
608 /* Fetch and handle all the rows that matched the query. */
611 status = OCIStmtFetch2 (oci_statement, oci_error,
612 /* nrows = */ 1, /* orientation = */ OCI_FETCH_NEXT,
613 /* fetch offset = */ 0, /* mode = */ OCI_DEFAULT);
614 if (status == OCI_NO_DATA)
616 status = OCI_SUCCESS;
619 else if ((status != OCI_SUCCESS) && (status != OCI_SUCCESS_WITH_INFO))
621 o_report_error ("o_read_database_query", db->name,
622 udb_query_get_name (q), "OCIStmtFetch2", oci_error);
626 status = udb_query_handle_result (q, prep_area, column_values);
629 WARNING ("oracle plugin: o_read_database_query (%s, %s): "
630 "udb_query_handle_result failed.",
631 db->name, udb_query_get_name (q));
633 } /* }}} while (42) */
635 /* DEBUG ("oracle plugin: o_read_database_query: This statement succeeded: %s", q->statement); */
641 } /* }}} int o_read_database_query */
643 static int o_read_database (o_database_t *db) /* {{{ */
648 if (db->oci_service_context != NULL)
650 OCIServer *server_handle;
651 ub4 connection_status;
653 server_handle = NULL;
654 status = OCIAttrGet ((void *) db->oci_service_context, OCI_HTYPE_SVCCTX,
655 (void *) &server_handle, /* size pointer = */ NULL,
656 OCI_ATTR_SERVER, oci_error);
657 if (status != OCI_SUCCESS)
659 o_report_error ("o_read_database", db->name, NULL, "OCIAttrGet",
664 if (server_handle == NULL)
666 connection_status = OCI_SERVER_NOT_CONNECTED;
668 else /* if (server_handle != NULL) */
670 connection_status = 0;
671 status = OCIAttrGet ((void *) server_handle, OCI_HTYPE_SERVER,
672 (void *) &connection_status, /* size pointer = */ NULL,
673 OCI_ATTR_SERVER_STATUS, oci_error);
674 if (status != OCI_SUCCESS)
676 o_report_error ("o_read_database", db->name, NULL, "OCIAttrGet",
682 if (connection_status != OCI_SERVER_NORMAL)
684 INFO ("oracle plugin: Connection to %s lost. Trying to reconnect.",
686 OCIHandleFree (db->oci_service_context, OCI_HTYPE_SVCCTX);
687 db->oci_service_context = NULL;
689 } /* if (db->oci_service_context != NULL) */
691 if (db->oci_service_context == NULL)
693 status = OCILogon (oci_env, oci_error,
694 &db->oci_service_context,
695 (OraText *) db->username, (ub4) strlen (db->username),
696 (OraText *) db->password, (ub4) strlen (db->password),
697 (OraText *) db->connect_id, (ub4) strlen (db->connect_id));
698 if ((status != OCI_SUCCESS) && (status != OCI_SUCCESS_WITH_INFO))
702 ssnprintf (errfunc, sizeof (errfunc), "OCILogon(\"%s\")", db->connect_id);
704 o_report_error ("o_read_database", db->name, NULL, errfunc, oci_error);
705 DEBUG ("oracle plugin: OCILogon (%s): db->oci_service_context = %p;",
706 db->connect_id, db->oci_service_context);
707 db->oci_service_context = NULL;
710 else if (status == OCI_SUCCESS_WITH_INFO)
712 /* TODO: Print NOTIFY message. */
714 assert (db->oci_service_context != NULL);
717 DEBUG ("oracle plugin: o_read_database: db->connect_id = %s; db->oci_service_context = %p;",
718 db->connect_id, db->oci_service_context);
720 for (i = 0; i < db->queries_num; i++)
721 o_read_database_query (db, db->queries[i], db->q_prep_areas[i]);
724 } /* }}} int o_read_database */
726 static int o_read (void) /* {{{ */
730 for (i = 0; i < databases_num; i++)
731 o_read_database (databases[i]);
734 } /* }}} int o_read */
736 static int o_shutdown (void) /* {{{ */
740 for (i = 0; i < databases_num; i++)
741 if (databases[i]->oci_service_context != NULL)
743 OCIHandleFree (databases[i]->oci_service_context, OCI_HTYPE_SVCCTX);
744 databases[i]->oci_service_context = NULL;
747 for (i = 0; i < queries_num; i++)
749 OCIStmt *oci_statement;
751 oci_statement = udb_query_get_user_data (queries[i]);
752 if (oci_statement != NULL)
754 OCIHandleFree (oci_statement, OCI_HTYPE_STMT);
755 udb_query_set_user_data (queries[i], NULL);
759 OCIHandleFree (oci_env, OCI_HTYPE_ENV);
762 udb_query_free (queries, queries_num);
767 } /* }}} int o_shutdown */
769 void module_register (void) /* {{{ */
771 plugin_register_complex_config ("oracle", o_config);
772 plugin_register_init ("oracle", o_init);
773 plugin_register_read ("oracle", o_read);
774 plugin_register_shutdown ("oracle", o_shutdown);
775 } /* }}} void module_register */
778 * vim: shiftwidth=2 softtabstop=2 et fdm=marker