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_t **queries;
68 OCISvcCtx *oci_service_context;
70 typedef struct o_database_s o_database_t;
75 static udb_query_t **queries = NULL;
76 static size_t queries_num = 0;
77 static o_database_t **databases = NULL;
78 static size_t databases_num = 0;
80 OCIEnv *oci_env = NULL;
81 OCIError *oci_error = NULL;
86 static void o_report_error (const char *where, /* {{{ */
87 const char *what, OCIError *eh)
93 status = OCIErrorGet (eh, /* record number = */ 1,
94 /* sqlstate = */ NULL,
97 (ub4) sizeof (buffer),
99 buffer[sizeof (buffer) - 1] = 0;
101 if (status == OCI_SUCCESS)
103 size_t buffer_length;
105 buffer_length = strlen (buffer);
106 while ((buffer_length > 0) && (buffer[buffer_length - 1] < 32))
109 buffer[buffer_length] = 0;
112 ERROR ("oracle plugin: %s: %s failed: %s",
113 where, what, buffer);
117 ERROR ("oracle plugin: %s: %s failed. Additionally, OCIErrorGet failed with status %i.",
118 where, what, status);
120 } /* }}} void o_report_error */
122 static void o_database_free (o_database_t *db) /* {{{ */
128 sfree (db->connect_id);
129 sfree (db->username);
130 sfree (db->password);
134 } /* }}} void o_database_free */
136 /* Configuration handling functions {{{
139 * <Query "plugin_instance0">
140 * Statement "SELECT name, value FROM table"
143 * InstancesFrom "name"
148 * <Database "plugin_instance1">
152 * Query "plugin_instance0"
157 static int o_config_set_string (char **ret_string, /* {{{ */
162 if ((ci->values_num != 1)
163 || (ci->values[0].type != OCONFIG_TYPE_STRING))
165 WARNING ("oracle plugin: The `%s' config option "
166 "needs exactly one string argument.", ci->key);
170 string = strdup (ci->values[0].value.string);
173 ERROR ("oracle plugin: strdup failed.");
177 if (*ret_string != NULL)
179 *ret_string = string;
182 } /* }}} int o_config_set_string */
184 static int o_config_add_database (oconfig_item_t *ci) /* {{{ */
190 if ((ci->values_num != 1)
191 || (ci->values[0].type != OCONFIG_TYPE_STRING))
193 WARNING ("oracle plugin: The `Database' block "
194 "needs exactly one string argument.");
198 db = (o_database_t *) malloc (sizeof (*db));
201 ERROR ("oracle plugin: malloc failed.");
204 memset (db, 0, sizeof (*db));
206 status = o_config_set_string (&db->name, ci);
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 = o_config_set_string (&db->connect_id, child);
220 else if (strcasecmp ("Username", child->key) == 0)
221 status = o_config_set_string (&db->username, child);
222 else if (strcasecmp ("Password", child->key) == 0)
223 status = o_config_set_string (&db->password, child);
224 else if (strcasecmp ("Query", child->key) == 0)
225 status = udb_query_pick_from_list (child, queries, queries_num,
226 &db->queries, &db->queries_num);
229 WARNING ("oracle plugin: Option `%s' not allowed here.", child->key);
237 /* Check that all necessary options have been given. */
240 if (db->connect_id == NULL)
242 WARNING ("oracle plugin: `ConnectID' not given for query `%s'", db->name);
245 if (db->username == NULL)
247 WARNING ("oracle plugin: `Username' not given for query `%s'", db->name);
250 if (db->password == NULL)
252 WARNING ("oracle plugin: `Password' not given for query `%s'", db->name);
257 } /* while (status == 0) */
259 /* If all went well, add this query to the list of queries within the
260 * database structure. */
265 temp = (o_database_t **) realloc (databases,
266 sizeof (*databases) * (databases_num + 1));
269 ERROR ("oracle plugin: realloc failed");
275 databases[databases_num] = db;
282 o_database_free (db);
287 } /* }}} int o_config_add_database */
289 static int o_config (oconfig_item_t *ci) /* {{{ */
293 for (i = 0; i < ci->children_num; i++)
295 oconfig_item_t *child = ci->children + i;
296 if (strcasecmp ("Query", child->key) == 0)
297 udb_query_create (&queries, &queries_num, child,
298 /* callback = */ NULL, /* legacy mode = */ 0);
299 else if (strcasecmp ("Database", child->key) == 0)
300 o_config_add_database (child);
303 WARNING ("oracle plugin: Ignoring unknown config option `%s'.", child->key);
308 DEBUG ("oracle plugin: o_config: queries_num = %zu; queries[0] = %p; udb_query_get_user_data (queries[0]) = %p;",
309 queries_num, (void *) queries[0], udb_query_get_user_data (queries[0]));
311 } /* for (ci->children) */
314 } /* }}} int o_config */
316 /* }}} End of configuration handling functions */
318 static int o_init (void) /* {{{ */
325 status = OCIEnvCreate (&oci_env,
326 /* mode = */ OCI_THREADED,
327 /* context = */ NULL,
329 /* realloc = */ NULL,
331 /* user_data_size = */ 0,
332 /* user_data_ptr = */ NULL);
335 ERROR ("oracle plugin: OCIEnvCreate failed with status %i.", status);
339 status = OCIHandleAlloc (oci_env, (void *) &oci_error, OCI_HTYPE_ERROR,
340 /* user_data_size = */ 0, /* user_data = */ NULL);
341 if (status != OCI_SUCCESS)
343 ERROR ("oracle plugin: OCIHandleAlloc (OCI_HTYPE_ERROR) failed "
344 "with status %i.", status);
349 } /* }}} int o_init */
351 static int o_read_database_query (o_database_t *db, /* {{{ */
355 char **column_values;
358 OCIStmt *oci_statement;
360 /* List of `OCIDefine' pointers. These defines map columns to the buffer
361 * space declared above. */
362 OCIDefine **oci_defines;
367 oci_statement = udb_query_get_user_data (q);
369 /* Prepare the statement */
370 if (oci_statement == NULL) /* {{{ */
372 const char *statement;
374 statement = udb_query_get_statement (q);
375 assert (statement != NULL);
377 status = OCIHandleAlloc (oci_env, (void *) &oci_statement,
378 OCI_HTYPE_STMT, /* user_data_size = */ 0, /* user_data = */ NULL);
379 if (status != OCI_SUCCESS)
381 o_report_error ("o_read_database_query", "OCIHandleAlloc", oci_error);
382 oci_statement = NULL;
386 status = OCIStmtPrepare (oci_statement, oci_error,
387 (text *) statement, (ub4) strlen (statement),
388 /* language = */ OCI_NTV_SYNTAX,
389 /* mode = */ OCI_DEFAULT);
390 if (status != OCI_SUCCESS)
392 o_report_error ("o_read_database_query", "OCIStmtPrepare", oci_error);
393 OCIHandleFree (oci_statement, OCI_HTYPE_STMT);
394 oci_statement = NULL;
397 udb_query_set_user_data (q, oci_statement);
399 DEBUG ("oracle plugin: o_read_database_query (%s, %s): "
400 "Successfully allocated statement handle.",
401 db->name, udb_query_get_name (q));
404 assert (oci_statement != NULL);
406 /* Execute the statement */
407 status = OCIStmtExecute (db->oci_service_context, /* {{{ */
412 /* snap_in = */ NULL, /* snap_out = */ NULL,
413 /* mode = */ OCI_DEFAULT);
414 if (status != OCI_SUCCESS)
416 DEBUG ("oracle plugin: o_read_database_query: status = %i (%#x)", status, status);
417 o_report_error ("o_read_database_query", "OCIStmtExecute", oci_error);
418 ERROR ("oracle plugin: o_read_database_query: "
419 "Failing statement was: %s", udb_query_get_statement (q));
423 /* Acquire the number of columns returned. */
426 ub4 param_counter = 0;
427 status = OCIAttrGet (oci_statement, OCI_HTYPE_STMT, /* {{{ */
428 ¶m_counter, /* size pointer = */ NULL,
429 OCI_ATTR_PARAM_COUNT, oci_error);
430 if (status != OCI_SUCCESS)
432 o_report_error ("o_read_database_query", "OCIAttrGet", oci_error);
436 column_num = (size_t) param_counter;
437 } while (0); /* }}} */
439 /* Allocate the following buffers:
441 * +---------------+-----------------------------------+
443 * +---------------+-----------------------------------+
444 * ! column_names ! column_num x DATA_MAX_NAME_LEN !
445 * ! column_values ! column_num x DATA_MAX_NAME_LEN !
446 * ! oci_defines ! column_num x sizeof (OCIDefine *) !
447 * +---------------+-----------------------------------+
450 #define NUMBER_BUFFER_SIZE 64
453 if (column_names != NULL) { \
454 sfree (column_names[0]); \
455 sfree (column_names); \
457 if (column_values != NULL) { \
458 sfree (column_values[0]); \
459 sfree (column_values); \
463 #define ALLOC_OR_FAIL(ptr, ptr_size) \
465 size_t alloc_size = (size_t) ((ptr_size)); \
466 (ptr) = malloc (alloc_size); \
467 if ((ptr) == NULL) { \
469 ERROR ("oracle plugin: o_read_database_query: malloc failed."); \
472 memset ((ptr), 0, alloc_size); \
475 /* Initialize everything to NULL so the above works. */
477 column_values = NULL;
480 ALLOC_OR_FAIL (column_names, column_num * sizeof (char *));
481 ALLOC_OR_FAIL (column_names[0], column_num * DATA_MAX_NAME_LEN
483 for (i = 1; i < column_num; i++)
484 column_names[i] = column_names[i - 1] + DATA_MAX_NAME_LEN;
486 ALLOC_OR_FAIL (column_values, column_num * sizeof (char *));
487 ALLOC_OR_FAIL (column_values[0], column_num * DATA_MAX_NAME_LEN
489 for (i = 1; i < column_num; i++)
490 column_values[i] = column_values[i - 1] + DATA_MAX_NAME_LEN;
492 ALLOC_OR_FAIL (oci_defines, column_num * sizeof (OCIDefine *));
493 /* }}} End of buffer allocations. */
495 /* ``Define'' the returned data, i. e. bind the columns to the buffers
496 * allocated above. */
497 for (i = 0; i < column_num; i++) /* {{{ */
500 ub4 column_name_length;
505 status = OCIParamGet (oci_statement, OCI_HTYPE_STMT, oci_error,
506 (void *) &oci_param, (ub4) (i + 1));
507 if (status != OCI_SUCCESS)
509 /* This is probably alright */
510 DEBUG ("oracle plugin: o_read_database_query: status = %#x (= %i);", status, status);
511 o_report_error ("o_read_database_query", "OCIParamGet", oci_error);
512 status = OCI_SUCCESS;
517 column_name_length = 0;
518 status = OCIAttrGet (oci_param, OCI_DTYPE_PARAM,
519 &column_name, &column_name_length, OCI_ATTR_NAME, oci_error);
520 if (status != OCI_SUCCESS)
522 o_report_error ("o_read_database_query", "OCIAttrGet (OCI_ATTR_NAME)",
527 /* Copy the name to column_names. Warning: The ``string'' returned by OCI
528 * may not be null terminated! */
529 memset (column_names[i], 0, DATA_MAX_NAME_LEN);
530 if (column_name_length >= DATA_MAX_NAME_LEN)
531 column_name_length = DATA_MAX_NAME_LEN - 1;
532 memcpy (column_names[i], column_name, column_name_length);
533 column_names[i][column_name_length] = 0;
535 DEBUG ("oracle plugin: o_read_database_query: column_names[%zu] = %s; "
536 "column_name_length = %"PRIu32";",
537 i, column_names[i], (uint32_t) column_name_length);
539 status = OCIDefineByPos (oci_statement,
540 &oci_defines[i], oci_error, (ub4) (i + 1),
541 column_values[i], DATA_MAX_NAME_LEN, SQLT_STR,
542 NULL, NULL, NULL, OCI_DEFAULT);
543 if (status != OCI_SUCCESS)
545 o_report_error ("o_read_database_query", "OCIDefineByPos", oci_error);
548 } /* for (j = 1; j <= param_counter; j++) */
549 /* }}} End of the ``define'' stuff. */
551 status = udb_query_prepare_result (q, hostname_g, /* plugin = */ "oracle",
552 db->name, column_names, column_num);
555 ERROR ("oracle plugin: o_read_database_query (%s, %s): "
556 "udb_query_prepare_result failed.",
557 db->name, udb_query_get_name (q));
562 /* Fetch and handle all the rows that matched the query. */
565 status = OCIStmtFetch2 (oci_statement, oci_error,
566 /* nrows = */ 1, /* orientation = */ OCI_FETCH_NEXT,
567 /* fetch offset = */ 0, /* mode = */ OCI_DEFAULT);
568 if (status == OCI_NO_DATA)
570 status = OCI_SUCCESS;
573 else if ((status != OCI_SUCCESS) && (status != OCI_SUCCESS_WITH_INFO))
575 o_report_error ("o_read_database_query", "OCIStmtFetch2", oci_error);
579 status = udb_query_handle_result (q, column_values);
582 WARNING ("oracle plugin: o_read_database_query (%s, %s): "
583 "udb_query_handle_result failed.",
584 db->name, udb_query_get_name (q));
586 } /* }}} while (42) */
588 /* DEBUG ("oracle plugin: o_read_database_query: This statement succeeded: %s", q->statement); */
594 } /* }}} int o_read_database_query */
596 static int o_read_database (o_database_t *db) /* {{{ */
601 if (db->oci_service_context != NULL)
603 OCIServer *server_handle;
604 ub4 connection_status;
606 server_handle = NULL;
607 status = OCIAttrGet ((void *) db->oci_service_context, OCI_HTYPE_SVCCTX,
608 (void *) &server_handle, /* size pointer = */ NULL,
609 OCI_ATTR_SERVER, oci_error);
610 if (status != OCI_SUCCESS)
612 o_report_error ("o_read_database", "OCIAttrGet", oci_error);
616 if (server_handle == NULL)
618 connection_status = OCI_SERVER_NOT_CONNECTED;
620 else /* if (server_handle != NULL) */
622 connection_status = 0;
623 status = OCIAttrGet ((void *) server_handle, OCI_HTYPE_SERVER,
624 (void *) &connection_status, /* size pointer = */ NULL,
625 OCI_ATTR_SERVER_STATUS, oci_error);
626 if (status != OCI_SUCCESS)
628 o_report_error ("o_read_database", "OCIAttrGet", oci_error);
633 if (connection_status != OCI_SERVER_NORMAL)
635 INFO ("oracle plugin: Connection to %s lost. Trying to reconnect.",
637 OCIHandleFree (db->oci_service_context, OCI_HTYPE_SVCCTX);
638 db->oci_service_context = NULL;
640 } /* if (db->oci_service_context != NULL) */
642 if (db->oci_service_context == NULL)
644 status = OCILogon (oci_env, oci_error,
645 &db->oci_service_context,
646 (OraText *) db->username, (ub4) strlen (db->username),
647 (OraText *) db->password, (ub4) strlen (db->password),
648 (OraText *) db->connect_id, (ub4) strlen (db->connect_id));
649 if (status != OCI_SUCCESS)
651 o_report_error ("o_read_database", "OCILogon", oci_error);
652 DEBUG ("oracle plugin: OCILogon (%s): db->oci_service_context = %p;",
653 db->connect_id, db->oci_service_context);
654 db->oci_service_context = NULL;
657 assert (db->oci_service_context != NULL);
660 DEBUG ("oracle plugin: o_read_database: db->connect_id = %s; db->oci_service_context = %p;",
661 db->connect_id, db->oci_service_context);
663 for (i = 0; i < db->queries_num; i++)
664 o_read_database_query (db, db->queries[i]);
667 } /* }}} int o_read_database */
669 static int o_read (void) /* {{{ */
673 for (i = 0; i < databases_num; i++)
674 o_read_database (databases[i]);
677 } /* }}} int o_read */
679 static int o_shutdown (void) /* {{{ */
683 for (i = 0; i < databases_num; i++)
684 if (databases[i]->oci_service_context != NULL)
686 OCIHandleFree (databases[i]->oci_service_context, OCI_HTYPE_SVCCTX);
687 databases[i]->oci_service_context = NULL;
690 for (i = 0; i < queries_num; i++)
692 OCIStmt *oci_statement;
694 oci_statement = udb_query_get_user_data (queries[i]);
695 if (oci_statement != NULL)
697 OCIHandleFree (oci_statement, OCI_HTYPE_STMT);
698 udb_query_set_user_data (queries[i], NULL);
702 OCIHandleFree (oci_env, OCI_HTYPE_ENV);
704 udb_query_free (queries, queries_num);
709 } /* }}} int o_shutdown */
711 void module_register (void) /* {{{ */
713 plugin_register_complex_config ("oracle", o_config);
714 plugin_register_init ("oracle", o_init);
715 plugin_register_read ("oracle", o_read);
716 plugin_register_shutdown ("oracle", o_shutdown);
717 } /* }}} void module_register */
720 * vim: shiftwidth=2 softtabstop=2 et fdm=marker