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 *what, OCIError *eh)
95 unsigned int record_number;
97 /* An operation may cause / return multiple errors. Loop until we have
98 * handled all errors available (with a fail-save limit of 16). */
99 for (record_number = 1; record_number <= 16; record_number++)
101 memset (buffer, 0, sizeof (buffer));
104 status = OCIErrorGet (eh, (ub4) record_number,
105 /* sqlstate = */ NULL,
108 (ub4) sizeof (buffer),
110 buffer[sizeof (buffer) - 1] = 0;
112 if (status == OCI_NO_DATA)
115 if (status == OCI_SUCCESS)
117 size_t buffer_length;
119 buffer_length = strlen (buffer);
120 while ((buffer_length > 0) && (buffer[buffer_length - 1] < 32))
123 buffer[buffer_length] = 0;
126 ERROR ("oracle plugin: %s: %s failed: %s", where, what, buffer);
130 ERROR ("oracle plugin: %s: %s failed. Additionally, OCIErrorGet failed with status %i.",
131 where, what, status);
135 } /* }}} void o_report_error */
137 static void o_database_free (o_database_t *db) /* {{{ */
145 sfree (db->connect_id);
146 sfree (db->username);
147 sfree (db->password);
150 if (db->q_prep_areas != NULL)
151 for (i = 0; i < db->queries_num; ++i)
152 udb_query_delete_preparation_area (db->q_prep_areas[i]);
153 free (db->q_prep_areas);
156 } /* }}} void o_database_free */
158 /* Configuration handling functions {{{
161 * <Query "plugin_instance0">
162 * Statement "SELECT name, value FROM table"
165 * InstancesFrom "name"
170 * <Database "plugin_instance1">
174 * Query "plugin_instance0"
179 static int o_config_add_database (oconfig_item_t *ci) /* {{{ */
185 if ((ci->values_num != 1)
186 || (ci->values[0].type != OCONFIG_TYPE_STRING))
188 WARNING ("oracle plugin: The `Database' block "
189 "needs exactly one string argument.");
193 db = (o_database_t *) malloc (sizeof (*db));
196 ERROR ("oracle plugin: malloc failed.");
199 memset (db, 0, sizeof (*db));
202 db->connect_id = NULL;
206 status = cf_util_get_string (ci, &db->name);
213 /* Fill the `o_database_t' structure.. */
214 for (i = 0; i < ci->children_num; i++)
216 oconfig_item_t *child = ci->children + i;
218 if (strcasecmp ("ConnectID", child->key) == 0)
219 status = cf_util_get_string (child, &db->connect_id);
220 else if (strcasecmp ("Host", child->key) == 0)
221 status = cf_util_get_string (child, &db->host);
222 else if (strcasecmp ("Username", child->key) == 0)
223 status = cf_util_get_string (child, &db->username);
224 else if (strcasecmp ("Password", child->key) == 0)
225 status = cf_util_get_string (child, &db->password);
226 else if (strcasecmp ("Query", child->key) == 0)
227 status = udb_query_pick_from_list (child, queries, queries_num,
228 &db->queries, &db->queries_num);
231 WARNING ("oracle plugin: Option `%s' not allowed here.", child->key);
239 /* Check that all necessary options have been given. */
242 if (db->connect_id == NULL)
244 WARNING ("oracle plugin: `ConnectID' not given for query `%s'", db->name);
247 if (db->username == NULL)
249 WARNING ("oracle plugin: `Username' not given for query `%s'", db->name);
252 if (db->password == NULL)
254 WARNING ("oracle plugin: `Password' not given for query `%s'", db->name);
259 } /* while (status == 0) */
261 while ((status == 0) && (db->queries_num > 0))
263 db->q_prep_areas = (udb_query_preparation_area_t **) calloc (
264 db->queries_num, sizeof (*db->q_prep_areas));
266 if (db->q_prep_areas == NULL)
268 WARNING ("oracle plugin: malloc failed");
273 for (i = 0; i < db->queries_num; ++i)
276 = udb_query_allocate_preparation_area (db->queries[i]);
278 if (db->q_prep_areas[i] == NULL)
280 WARNING ("oracle plugin: udb_query_allocate_preparation_area failed");
289 /* If all went well, add this query to the list of queries within the
290 * database structure. */
295 temp = (o_database_t **) realloc (databases,
296 sizeof (*databases) * (databases_num + 1));
299 ERROR ("oracle plugin: realloc failed");
305 databases[databases_num] = db;
312 o_database_free (db);
317 } /* }}} int o_config_add_database */
319 static int o_config (oconfig_item_t *ci) /* {{{ */
323 for (i = 0; i < ci->children_num; i++)
325 oconfig_item_t *child = ci->children + i;
326 if (strcasecmp ("Query", child->key) == 0)
327 udb_query_create (&queries, &queries_num, child,
328 /* callback = */ NULL);
329 else if (strcasecmp ("Database", child->key) == 0)
330 o_config_add_database (child);
333 WARNING ("oracle plugin: Ignoring unknown config option `%s'.", child->key);
338 DEBUG ("oracle plugin: o_config: queries_num = %zu; queries[0] = %p; udb_query_get_user_data (queries[0]) = %p;",
339 queries_num, (void *) queries[0], udb_query_get_user_data (queries[0]));
341 } /* for (ci->children) */
344 } /* }}} int o_config */
346 /* }}} End of configuration handling functions */
348 static int o_init (void) /* {{{ */
355 status = OCIEnvCreate (&oci_env,
356 /* mode = */ OCI_THREADED,
357 /* context = */ NULL,
359 /* realloc = */ NULL,
361 /* user_data_size = */ 0,
362 /* user_data_ptr = */ NULL);
365 ERROR ("oracle plugin: OCIEnvCreate failed with status %i.", status);
369 status = OCIHandleAlloc (oci_env, (void *) &oci_error, OCI_HTYPE_ERROR,
370 /* user_data_size = */ 0, /* user_data = */ NULL);
371 if (status != OCI_SUCCESS)
373 ERROR ("oracle plugin: OCIHandleAlloc (OCI_HTYPE_ERROR) failed "
374 "with status %i.", status);
379 } /* }}} int o_init */
381 static int o_read_database_query (o_database_t *db, /* {{{ */
382 udb_query_t *q, udb_query_preparation_area_t *prep_area)
385 char **column_values;
388 OCIStmt *oci_statement;
390 /* List of `OCIDefine' pointers. These defines map columns to the buffer
391 * space declared above. */
392 OCIDefine **oci_defines;
397 oci_statement = udb_query_get_user_data (q);
399 /* Prepare the statement */
400 if (oci_statement == NULL) /* {{{ */
402 const char *statement;
404 statement = udb_query_get_statement (q);
405 assert (statement != NULL);
407 status = OCIHandleAlloc (oci_env, (void *) &oci_statement,
408 OCI_HTYPE_STMT, /* user_data_size = */ 0, /* user_data = */ NULL);
409 if (status != OCI_SUCCESS)
411 o_report_error ("o_read_database_query", "OCIHandleAlloc", oci_error);
412 oci_statement = NULL;
416 status = OCIStmtPrepare (oci_statement, oci_error,
417 (text *) statement, (ub4) strlen (statement),
418 /* language = */ OCI_NTV_SYNTAX,
419 /* mode = */ OCI_DEFAULT);
420 if (status != OCI_SUCCESS)
422 o_report_error ("o_read_database_query", "OCIStmtPrepare", oci_error);
423 OCIHandleFree (oci_statement, OCI_HTYPE_STMT);
424 oci_statement = NULL;
427 udb_query_set_user_data (q, oci_statement);
429 DEBUG ("oracle plugin: o_read_database_query (%s, %s): "
430 "Successfully allocated statement handle.",
431 db->name, udb_query_get_name (q));
434 assert (oci_statement != NULL);
436 /* Execute the statement */
437 status = OCIStmtExecute (db->oci_service_context, /* {{{ */
442 /* snap_in = */ NULL, /* snap_out = */ NULL,
443 /* mode = */ OCI_DEFAULT);
444 if (status != OCI_SUCCESS)
446 DEBUG ("oracle plugin: o_read_database_query: status = %i (%#x)", status, status);
447 o_report_error ("o_read_database_query", "OCIStmtExecute", oci_error);
448 ERROR ("oracle plugin: o_read_database_query: "
449 "Failing statement was: %s", udb_query_get_statement (q));
453 /* Acquire the number of columns returned. */
456 ub4 param_counter = 0;
457 status = OCIAttrGet (oci_statement, OCI_HTYPE_STMT, /* {{{ */
458 ¶m_counter, /* size pointer = */ NULL,
459 OCI_ATTR_PARAM_COUNT, oci_error);
460 if (status != OCI_SUCCESS)
462 o_report_error ("o_read_database_query", "OCIAttrGet", oci_error);
466 column_num = (size_t) param_counter;
467 } while (0); /* }}} */
469 /* Allocate the following buffers:
471 * +---------------+-----------------------------------+
473 * +---------------+-----------------------------------+
474 * ! column_names ! column_num x DATA_MAX_NAME_LEN !
475 * ! column_values ! column_num x DATA_MAX_NAME_LEN !
476 * ! oci_defines ! column_num x sizeof (OCIDefine *) !
477 * +---------------+-----------------------------------+
480 #define NUMBER_BUFFER_SIZE 64
483 if (column_names != NULL) { \
484 sfree (column_names[0]); \
485 sfree (column_names); \
487 if (column_values != NULL) { \
488 sfree (column_values[0]); \
489 sfree (column_values); \
493 #define ALLOC_OR_FAIL(ptr, ptr_size) \
495 size_t alloc_size = (size_t) ((ptr_size)); \
496 (ptr) = malloc (alloc_size); \
497 if ((ptr) == NULL) { \
499 ERROR ("oracle plugin: o_read_database_query: malloc failed."); \
502 memset ((ptr), 0, alloc_size); \
505 /* Initialize everything to NULL so the above works. */
507 column_values = NULL;
510 ALLOC_OR_FAIL (column_names, column_num * sizeof (char *));
511 ALLOC_OR_FAIL (column_names[0], column_num * DATA_MAX_NAME_LEN
513 for (i = 1; i < column_num; i++)
514 column_names[i] = column_names[i - 1] + DATA_MAX_NAME_LEN;
516 ALLOC_OR_FAIL (column_values, column_num * sizeof (char *));
517 ALLOC_OR_FAIL (column_values[0], column_num * DATA_MAX_NAME_LEN
519 for (i = 1; i < column_num; i++)
520 column_values[i] = column_values[i - 1] + DATA_MAX_NAME_LEN;
522 ALLOC_OR_FAIL (oci_defines, column_num * sizeof (OCIDefine *));
523 /* }}} End of buffer allocations. */
525 /* ``Define'' the returned data, i. e. bind the columns to the buffers
526 * allocated above. */
527 for (i = 0; i < column_num; i++) /* {{{ */
530 ub4 column_name_length;
535 status = OCIParamGet (oci_statement, OCI_HTYPE_STMT, oci_error,
536 (void *) &oci_param, (ub4) (i + 1));
537 if (status != OCI_SUCCESS)
539 /* This is probably alright */
540 DEBUG ("oracle plugin: o_read_database_query: status = %#x (= %i);", status, status);
541 o_report_error ("o_read_database_query", "OCIParamGet", oci_error);
542 status = OCI_SUCCESS;
547 column_name_length = 0;
548 status = OCIAttrGet (oci_param, OCI_DTYPE_PARAM,
549 &column_name, &column_name_length, OCI_ATTR_NAME, oci_error);
550 if (status != OCI_SUCCESS)
552 OCIDescriptorFree (oci_param, OCI_DTYPE_PARAM);
553 o_report_error ("o_read_database_query", "OCIAttrGet (OCI_ATTR_NAME)",
558 OCIDescriptorFree (oci_param, OCI_DTYPE_PARAM);
561 /* Copy the name to column_names. Warning: The ``string'' returned by OCI
562 * may not be null terminated! */
563 memset (column_names[i], 0, DATA_MAX_NAME_LEN);
564 if (column_name_length >= DATA_MAX_NAME_LEN)
565 column_name_length = DATA_MAX_NAME_LEN - 1;
566 memcpy (column_names[i], column_name, column_name_length);
567 column_names[i][column_name_length] = 0;
569 DEBUG ("oracle plugin: o_read_database_query: column_names[%zu] = %s; "
570 "column_name_length = %"PRIu32";",
571 i, column_names[i], (uint32_t) column_name_length);
573 status = OCIDefineByPos (oci_statement,
574 &oci_defines[i], oci_error, (ub4) (i + 1),
575 column_values[i], DATA_MAX_NAME_LEN, SQLT_STR,
576 NULL, NULL, NULL, OCI_DEFAULT);
577 if (status != OCI_SUCCESS)
579 o_report_error ("o_read_database_query", "OCIDefineByPos", oci_error);
582 } /* for (j = 1; j <= param_counter; j++) */
583 /* }}} End of the ``define'' stuff. */
585 status = udb_query_prepare_result (q, prep_area,
586 (db->host != NULL) ? db->host : hostname_g,
587 /* plugin = */ "oracle", db->name, column_names, column_num,
591 ERROR ("oracle plugin: o_read_database_query (%s, %s): "
592 "udb_query_prepare_result failed.",
593 db->name, udb_query_get_name (q));
598 /* Fetch and handle all the rows that matched the query. */
601 status = OCIStmtFetch2 (oci_statement, oci_error,
602 /* nrows = */ 1, /* orientation = */ OCI_FETCH_NEXT,
603 /* fetch offset = */ 0, /* mode = */ OCI_DEFAULT);
604 if (status == OCI_NO_DATA)
606 status = OCI_SUCCESS;
609 else if ((status != OCI_SUCCESS) && (status != OCI_SUCCESS_WITH_INFO))
611 o_report_error ("o_read_database_query", "OCIStmtFetch2", oci_error);
615 status = udb_query_handle_result (q, prep_area, column_values);
618 WARNING ("oracle plugin: o_read_database_query (%s, %s): "
619 "udb_query_handle_result failed.",
620 db->name, udb_query_get_name (q));
622 } /* }}} while (42) */
624 /* DEBUG ("oracle plugin: o_read_database_query: This statement succeeded: %s", q->statement); */
630 } /* }}} int o_read_database_query */
632 static int o_read_database (o_database_t *db) /* {{{ */
637 if (db->oci_service_context != NULL)
639 OCIServer *server_handle;
640 ub4 connection_status;
642 server_handle = NULL;
643 status = OCIAttrGet ((void *) db->oci_service_context, OCI_HTYPE_SVCCTX,
644 (void *) &server_handle, /* size pointer = */ NULL,
645 OCI_ATTR_SERVER, oci_error);
646 if (status != OCI_SUCCESS)
648 o_report_error ("o_read_database", "OCIAttrGet", oci_error);
652 if (server_handle == NULL)
654 connection_status = OCI_SERVER_NOT_CONNECTED;
656 else /* if (server_handle != NULL) */
658 connection_status = 0;
659 status = OCIAttrGet ((void *) server_handle, OCI_HTYPE_SERVER,
660 (void *) &connection_status, /* size pointer = */ NULL,
661 OCI_ATTR_SERVER_STATUS, oci_error);
662 if (status != OCI_SUCCESS)
664 o_report_error ("o_read_database", "OCIAttrGet", oci_error);
669 if (connection_status != OCI_SERVER_NORMAL)
671 INFO ("oracle plugin: Connection to %s lost. Trying to reconnect.",
673 OCIHandleFree (db->oci_service_context, OCI_HTYPE_SVCCTX);
674 db->oci_service_context = NULL;
676 } /* if (db->oci_service_context != NULL) */
678 if (db->oci_service_context == NULL)
680 status = OCILogon (oci_env, oci_error,
681 &db->oci_service_context,
682 (OraText *) db->username, (ub4) strlen (db->username),
683 (OraText *) db->password, (ub4) strlen (db->password),
684 (OraText *) db->connect_id, (ub4) strlen (db->connect_id));
685 if ((status != OCI_SUCCESS) && (status != OCI_SUCCESS_WITH_INFO))
687 o_report_error ("o_read_database", "OCILogon", oci_error);
688 DEBUG ("oracle plugin: OCILogon (%s): db->oci_service_context = %p;",
689 db->connect_id, db->oci_service_context);
690 db->oci_service_context = NULL;
693 else if (status == OCI_SUCCESS_WITH_INFO)
695 /* TODO: Print NOTIFY message. */
697 assert (db->oci_service_context != NULL);
700 DEBUG ("oracle plugin: o_read_database: db->connect_id = %s; db->oci_service_context = %p;",
701 db->connect_id, db->oci_service_context);
703 for (i = 0; i < db->queries_num; i++)
704 o_read_database_query (db, db->queries[i], db->q_prep_areas[i]);
707 } /* }}} int o_read_database */
709 static int o_read (void) /* {{{ */
713 for (i = 0; i < databases_num; i++)
714 o_read_database (databases[i]);
717 } /* }}} int o_read */
719 static int o_shutdown (void) /* {{{ */
723 for (i = 0; i < databases_num; i++)
724 if (databases[i]->oci_service_context != NULL)
726 OCIHandleFree (databases[i]->oci_service_context, OCI_HTYPE_SVCCTX);
727 databases[i]->oci_service_context = NULL;
730 for (i = 0; i < queries_num; i++)
732 OCIStmt *oci_statement;
734 oci_statement = udb_query_get_user_data (queries[i]);
735 if (oci_statement != NULL)
737 OCIHandleFree (oci_statement, OCI_HTYPE_STMT);
738 udb_query_set_user_data (queries[i], NULL);
742 OCIHandleFree (oci_env, OCI_HTYPE_ENV);
745 udb_query_free (queries, queries_num);
750 } /* }}} int o_shutdown */
752 void module_register (void) /* {{{ */
754 plugin_register_complex_config ("oracle", o_config);
755 plugin_register_init ("oracle", o_init);
756 plugin_register_read ("oracle", o_read);
757 plugin_register_shutdown ("oracle", o_shutdown);
758 } /* }}} void module_register */
761 * vim: shiftwidth=2 softtabstop=2 et fdm=marker