2 * collectd - src/oracle.c
3 * Copyright (C) 2008,2009 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 * Linking src/oracle.c ("the oracle plugin") statically or dynamically with
19 * other modules is making a combined work based on the oracle plugin. Thus,
20 * the terms and conditions of the GNU General Public License cover the whole
23 * In addition, as a special exception, the copyright holders of the oracle
24 * plugin give you permission to combine the oracle plugin with free software
25 * programs or libraries that are released under the GNU LGPL and with code
26 * included in the standard release of the Oracle® Call Interface (OCI) under
27 * the Oracle® Technology Network (OTN) License (or modified versions of such
28 * code, with unchanged license). You may copy and distribute such a system
29 * following the terms of the GNU GPL for the oracle plugin and the licenses of
30 * the other code concerned.
32 * Note that people who make modified versions of the oracle plugin are not
33 * obligated to grant this special exception for their modified versions; it is
34 * their choice whether to do so. The GNU General Public License gives
35 * permission to release a modified version without this exception; this
36 * exception also makes it possible to release a modified version which carries
37 * forward this exception. However, without this exception the OTN License does
38 * not allow linking with code licensed under the GNU General Public License.
40 * Oracle® is a registered trademark of Oracle Corporation and/or its
41 * affiliates. Other names may be trademarks of their respective owners.
44 * Florian octo Forster <octo at noris.net>
50 #include "configfile.h"
51 #include "utils_db_query.h"
65 udb_query_preparation_area_t **q_prep_areas;
66 udb_query_t **queries;
69 OCISvcCtx *oci_service_context;
71 typedef struct o_database_s o_database_t;
76 static udb_query_t **queries = NULL;
77 static size_t queries_num = 0;
78 static o_database_t **databases = NULL;
79 static size_t databases_num = 0;
81 OCIEnv *oci_env = NULL;
82 OCIError *oci_error = NULL;
87 static void o_report_error (const char *where, /* {{{ */
88 const char *what, OCIError *eh)
94 status = OCIErrorGet (eh, /* record number = */ 1,
95 /* sqlstate = */ NULL,
98 (ub4) sizeof (buffer),
100 buffer[sizeof (buffer) - 1] = 0;
102 if (status == OCI_SUCCESS)
104 size_t buffer_length;
106 buffer_length = strlen (buffer);
107 while ((buffer_length > 0) && (buffer[buffer_length - 1] < 32))
110 buffer[buffer_length] = 0;
113 ERROR ("oracle plugin: %s: %s failed: %s",
114 where, what, buffer);
118 ERROR ("oracle plugin: %s: %s failed. Additionally, OCIErrorGet failed with status %i.",
119 where, what, status);
121 } /* }}} void o_report_error */
123 static void o_database_free (o_database_t *db) /* {{{ */
131 sfree (db->connect_id);
132 sfree (db->username);
133 sfree (db->password);
136 if (db->q_prep_areas != NULL)
137 for (i = 0; i < db->queries_num; ++i)
138 udb_query_delete_preparation_area (db->q_prep_areas[i]);
139 free (db->q_prep_areas);
142 } /* }}} void o_database_free */
144 /* Configuration handling functions {{{
147 * <Query "plugin_instance0">
148 * Statement "SELECT name, value FROM table"
151 * InstancesFrom "name"
156 * <Database "plugin_instance1">
160 * Query "plugin_instance0"
165 static int o_config_set_string (char **ret_string, /* {{{ */
170 if ((ci->values_num != 1)
171 || (ci->values[0].type != OCONFIG_TYPE_STRING))
173 WARNING ("oracle plugin: The `%s' config option "
174 "needs exactly one string argument.", ci->key);
178 string = strdup (ci->values[0].value.string);
181 ERROR ("oracle plugin: strdup failed.");
185 if (*ret_string != NULL)
187 *ret_string = string;
190 } /* }}} int o_config_set_string */
192 static int o_config_add_database (oconfig_item_t *ci) /* {{{ */
198 if ((ci->values_num != 1)
199 || (ci->values[0].type != OCONFIG_TYPE_STRING))
201 WARNING ("oracle plugin: The `Database' block "
202 "needs exactly one string argument.");
206 db = (o_database_t *) malloc (sizeof (*db));
209 ERROR ("oracle plugin: malloc failed.");
212 memset (db, 0, sizeof (*db));
214 status = o_config_set_string (&db->name, ci);
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 = o_config_set_string (&db->connect_id, child);
228 else if (strcasecmp ("Username", child->key) == 0)
229 status = o_config_set_string (&db->username, child);
230 else if (strcasecmp ("Password", child->key) == 0)
231 status = o_config_set_string (&db->password, child);
232 else if (strcasecmp ("Query", child->key) == 0)
233 status = udb_query_pick_from_list (child, queries, queries_num,
234 &db->queries, &db->queries_num);
237 WARNING ("oracle plugin: Option `%s' not allowed here.", child->key);
245 /* Check that all necessary options have been given. */
248 if (db->connect_id == NULL)
250 WARNING ("oracle plugin: `ConnectID' not given for query `%s'", db->name);
253 if (db->username == NULL)
255 WARNING ("oracle plugin: `Username' not given for query `%s'", db->name);
258 if (db->password == NULL)
260 WARNING ("oracle plugin: `Password' not given for query `%s'", db->name);
265 } /* while (status == 0) */
267 while ((status == 0) && (db->queries_num > 0))
269 db->q_prep_areas = (udb_query_preparation_area_t **) calloc (
270 db->queries_num, sizeof (*db->q_prep_areas));
272 if (db->q_prep_areas == NULL)
274 WARNING ("oracle plugin: malloc failed");
279 for (i = 0; i < db->queries_num; ++i)
282 = udb_query_allocate_preparation_area (db->queries[i]);
284 if (db->q_prep_areas[i] == NULL)
286 WARNING ("oracle plugin: udb_query_allocate_preparation_area failed");
295 /* If all went well, add this query to the list of queries within the
296 * database structure. */
301 temp = (o_database_t **) realloc (databases,
302 sizeof (*databases) * (databases_num + 1));
305 ERROR ("oracle plugin: realloc failed");
311 databases[databases_num] = db;
318 o_database_free (db);
323 } /* }}} int o_config_add_database */
325 static int o_config (oconfig_item_t *ci) /* {{{ */
329 for (i = 0; i < ci->children_num; i++)
331 oconfig_item_t *child = ci->children + i;
332 if (strcasecmp ("Query", child->key) == 0)
333 udb_query_create (&queries, &queries_num, child,
334 /* callback = */ NULL, /* legacy mode = */ 0);
335 else if (strcasecmp ("Database", child->key) == 0)
336 o_config_add_database (child);
339 WARNING ("oracle plugin: Ignoring unknown config option `%s'.", child->key);
344 DEBUG ("oracle plugin: o_config: queries_num = %zu; queries[0] = %p; udb_query_get_user_data (queries[0]) = %p;",
345 queries_num, (void *) queries[0], udb_query_get_user_data (queries[0]));
347 } /* for (ci->children) */
350 } /* }}} int o_config */
352 /* }}} End of configuration handling functions */
354 static int o_init (void) /* {{{ */
361 status = OCIEnvCreate (&oci_env,
362 /* mode = */ OCI_THREADED,
363 /* context = */ NULL,
365 /* realloc = */ NULL,
367 /* user_data_size = */ 0,
368 /* user_data_ptr = */ NULL);
371 ERROR ("oracle plugin: OCIEnvCreate failed with status %i.", status);
375 status = OCIHandleAlloc (oci_env, (void *) &oci_error, OCI_HTYPE_ERROR,
376 /* user_data_size = */ 0, /* user_data = */ NULL);
377 if (status != OCI_SUCCESS)
379 ERROR ("oracle plugin: OCIHandleAlloc (OCI_HTYPE_ERROR) failed "
380 "with status %i.", status);
385 } /* }}} int o_init */
387 static int o_read_database_query (o_database_t *db, /* {{{ */
388 udb_query_t *q, udb_query_preparation_area_t *prep_area)
391 char **column_values;
394 OCIStmt *oci_statement;
396 /* List of `OCIDefine' pointers. These defines map columns to the buffer
397 * space declared above. */
398 OCIDefine **oci_defines;
403 oci_statement = udb_query_get_user_data (q);
405 /* Prepare the statement */
406 if (oci_statement == NULL) /* {{{ */
408 const char *statement;
410 statement = udb_query_get_statement (q);
411 assert (statement != NULL);
413 status = OCIHandleAlloc (oci_env, (void *) &oci_statement,
414 OCI_HTYPE_STMT, /* user_data_size = */ 0, /* user_data = */ NULL);
415 if (status != OCI_SUCCESS)
417 o_report_error ("o_read_database_query", "OCIHandleAlloc", oci_error);
418 oci_statement = NULL;
422 status = OCIStmtPrepare (oci_statement, oci_error,
423 (text *) statement, (ub4) strlen (statement),
424 /* language = */ OCI_NTV_SYNTAX,
425 /* mode = */ OCI_DEFAULT);
426 if (status != OCI_SUCCESS)
428 o_report_error ("o_read_database_query", "OCIStmtPrepare", oci_error);
429 OCIHandleFree (oci_statement, OCI_HTYPE_STMT);
430 oci_statement = NULL;
433 udb_query_set_user_data (q, oci_statement);
435 DEBUG ("oracle plugin: o_read_database_query (%s, %s): "
436 "Successfully allocated statement handle.",
437 db->name, udb_query_get_name (q));
440 assert (oci_statement != NULL);
442 /* Execute the statement */
443 status = OCIStmtExecute (db->oci_service_context, /* {{{ */
448 /* snap_in = */ NULL, /* snap_out = */ NULL,
449 /* mode = */ OCI_DEFAULT);
450 if (status != OCI_SUCCESS)
452 DEBUG ("oracle plugin: o_read_database_query: status = %i (%#x)", status, status);
453 o_report_error ("o_read_database_query", "OCIStmtExecute", oci_error);
454 ERROR ("oracle plugin: o_read_database_query: "
455 "Failing statement was: %s", udb_query_get_statement (q));
459 /* Acquire the number of columns returned. */
462 ub4 param_counter = 0;
463 status = OCIAttrGet (oci_statement, OCI_HTYPE_STMT, /* {{{ */
464 ¶m_counter, /* size pointer = */ NULL,
465 OCI_ATTR_PARAM_COUNT, oci_error);
466 if (status != OCI_SUCCESS)
468 o_report_error ("o_read_database_query", "OCIAttrGet", oci_error);
472 column_num = (size_t) param_counter;
473 } while (0); /* }}} */
475 /* Allocate the following buffers:
477 * +---------------+-----------------------------------+
479 * +---------------+-----------------------------------+
480 * ! column_names ! column_num x DATA_MAX_NAME_LEN !
481 * ! column_values ! column_num x DATA_MAX_NAME_LEN !
482 * ! oci_defines ! column_num x sizeof (OCIDefine *) !
483 * +---------------+-----------------------------------+
486 #define NUMBER_BUFFER_SIZE 64
489 if (column_names != NULL) { \
490 sfree (column_names[0]); \
491 sfree (column_names); \
493 if (column_values != NULL) { \
494 sfree (column_values[0]); \
495 sfree (column_values); \
499 #define ALLOC_OR_FAIL(ptr, ptr_size) \
501 size_t alloc_size = (size_t) ((ptr_size)); \
502 (ptr) = malloc (alloc_size); \
503 if ((ptr) == NULL) { \
505 ERROR ("oracle plugin: o_read_database_query: malloc failed."); \
508 memset ((ptr), 0, alloc_size); \
511 /* Initialize everything to NULL so the above works. */
513 column_values = NULL;
516 ALLOC_OR_FAIL (column_names, column_num * sizeof (char *));
517 ALLOC_OR_FAIL (column_names[0], column_num * DATA_MAX_NAME_LEN
519 for (i = 1; i < column_num; i++)
520 column_names[i] = column_names[i - 1] + DATA_MAX_NAME_LEN;
522 ALLOC_OR_FAIL (column_values, column_num * sizeof (char *));
523 ALLOC_OR_FAIL (column_values[0], column_num * DATA_MAX_NAME_LEN
525 for (i = 1; i < column_num; i++)
526 column_values[i] = column_values[i - 1] + DATA_MAX_NAME_LEN;
528 ALLOC_OR_FAIL (oci_defines, column_num * sizeof (OCIDefine *));
529 /* }}} End of buffer allocations. */
531 /* ``Define'' the returned data, i. e. bind the columns to the buffers
532 * allocated above. */
533 for (i = 0; i < column_num; i++) /* {{{ */
536 ub4 column_name_length;
541 status = OCIParamGet (oci_statement, OCI_HTYPE_STMT, oci_error,
542 (void *) &oci_param, (ub4) (i + 1));
543 if (status != OCI_SUCCESS)
545 /* This is probably alright */
546 DEBUG ("oracle plugin: o_read_database_query: status = %#x (= %i);", status, status);
547 o_report_error ("o_read_database_query", "OCIParamGet", oci_error);
548 status = OCI_SUCCESS;
553 column_name_length = 0;
554 status = OCIAttrGet (oci_param, OCI_DTYPE_PARAM,
555 &column_name, &column_name_length, OCI_ATTR_NAME, oci_error);
556 if (status != OCI_SUCCESS)
558 o_report_error ("o_read_database_query", "OCIAttrGet (OCI_ATTR_NAME)",
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", "OCIDefineByPos", oci_error);
584 } /* for (j = 1; j <= param_counter; j++) */
585 /* }}} End of the ``define'' stuff. */
587 status = udb_query_prepare_result (q, prep_area, hostname_g,
588 /* plugin = */ "oracle", db->name, column_names, column_num,
589 /* interval = */ -1);
592 ERROR ("oracle plugin: o_read_database_query (%s, %s): "
593 "udb_query_prepare_result failed.",
594 db->name, udb_query_get_name (q));
599 /* Fetch and handle all the rows that matched the query. */
602 status = OCIStmtFetch2 (oci_statement, oci_error,
603 /* nrows = */ 1, /* orientation = */ OCI_FETCH_NEXT,
604 /* fetch offset = */ 0, /* mode = */ OCI_DEFAULT);
605 if (status == OCI_NO_DATA)
607 status = OCI_SUCCESS;
610 else if ((status != OCI_SUCCESS) && (status != OCI_SUCCESS_WITH_INFO))
612 o_report_error ("o_read_database_query", "OCIStmtFetch2", oci_error);
616 status = udb_query_handle_result (q, prep_area, column_values);
619 WARNING ("oracle plugin: o_read_database_query (%s, %s): "
620 "udb_query_handle_result failed.",
621 db->name, udb_query_get_name (q));
623 } /* }}} while (42) */
625 /* DEBUG ("oracle plugin: o_read_database_query: This statement succeeded: %s", q->statement); */
631 } /* }}} int o_read_database_query */
633 static int o_read_database (o_database_t *db) /* {{{ */
638 if (db->oci_service_context != NULL)
640 OCIServer *server_handle;
641 ub4 connection_status;
643 server_handle = NULL;
644 status = OCIAttrGet ((void *) db->oci_service_context, OCI_HTYPE_SVCCTX,
645 (void *) &server_handle, /* size pointer = */ NULL,
646 OCI_ATTR_SERVER, oci_error);
647 if (status != OCI_SUCCESS)
649 o_report_error ("o_read_database", "OCIAttrGet", oci_error);
653 if (server_handle == NULL)
655 connection_status = OCI_SERVER_NOT_CONNECTED;
657 else /* if (server_handle != NULL) */
659 connection_status = 0;
660 status = OCIAttrGet ((void *) server_handle, OCI_HTYPE_SERVER,
661 (void *) &connection_status, /* size pointer = */ NULL,
662 OCI_ATTR_SERVER_STATUS, oci_error);
663 if (status != OCI_SUCCESS)
665 o_report_error ("o_read_database", "OCIAttrGet", oci_error);
670 if (connection_status != OCI_SERVER_NORMAL)
672 INFO ("oracle plugin: Connection to %s lost. Trying to reconnect.",
674 OCIHandleFree (db->oci_service_context, OCI_HTYPE_SVCCTX);
675 db->oci_service_context = NULL;
677 } /* if (db->oci_service_context != NULL) */
679 if (db->oci_service_context == NULL)
681 status = OCILogon (oci_env, oci_error,
682 &db->oci_service_context,
683 (OraText *) db->username, (ub4) strlen (db->username),
684 (OraText *) db->password, (ub4) strlen (db->password),
685 (OraText *) db->connect_id, (ub4) strlen (db->connect_id));
686 if (status != OCI_SUCCESS)
688 o_report_error ("o_read_database", "OCILogon", oci_error);
689 DEBUG ("oracle plugin: OCILogon (%s): db->oci_service_context = %p;",
690 db->connect_id, db->oci_service_context);
691 db->oci_service_context = NULL;
694 assert (db->oci_service_context != NULL);
697 DEBUG ("oracle plugin: o_read_database: db->connect_id = %s; db->oci_service_context = %p;",
698 db->connect_id, db->oci_service_context);
700 for (i = 0; i < db->queries_num; i++)
701 o_read_database_query (db, db->queries[i], db->q_prep_areas[i]);
704 } /* }}} int o_read_database */
706 static int o_read (void) /* {{{ */
710 for (i = 0; i < databases_num; i++)
711 o_read_database (databases[i]);
714 } /* }}} int o_read */
716 static int o_shutdown (void) /* {{{ */
720 for (i = 0; i < databases_num; i++)
721 if (databases[i]->oci_service_context != NULL)
723 OCIHandleFree (databases[i]->oci_service_context, OCI_HTYPE_SVCCTX);
724 databases[i]->oci_service_context = NULL;
727 for (i = 0; i < queries_num; i++)
729 OCIStmt *oci_statement;
731 oci_statement = udb_query_get_user_data (queries[i]);
732 if (oci_statement != NULL)
734 OCIHandleFree (oci_statement, OCI_HTYPE_STMT);
735 udb_query_set_user_data (queries[i], NULL);
739 OCIHandleFree (oci_env, OCI_HTYPE_ENV);
741 udb_query_free (queries, queries_num);
746 } /* }}} int o_shutdown */
748 void module_register (void) /* {{{ */
750 plugin_register_complex_config ("oracle", o_config);
751 plugin_register_init ("oracle", o_init);
752 plugin_register_read ("oracle", o_read);
753 plugin_register_shutdown ("oracle", o_shutdown);
754 } /* }}} void module_register */
757 * vim: shiftwidth=2 softtabstop=2 et fdm=marker