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 else if (strcasecmp ("Database", child->key) == 0)
299 o_config_add_database (child);
302 WARNING ("oracle plugin: Ignoring unknown config option `%s'.", child->key);
307 DEBUG ("oracle plugin: o_config: queries_num = %zu; queries[0] = %p; udb_query_get_user_data (queries[0]) = %p;",
308 queries_num, (void *) queries[0], udb_query_get_user_data (queries[0]));
310 } /* for (ci->children) */
313 } /* }}} int o_config */
315 /* }}} End of configuration handling functions */
317 static int o_init (void) /* {{{ */
324 status = OCIEnvCreate (&oci_env,
325 /* mode = */ OCI_THREADED,
326 /* context = */ NULL,
328 /* realloc = */ NULL,
330 /* user_data_size = */ 0,
331 /* user_data_ptr = */ NULL);
334 ERROR ("oracle plugin: OCIEnvCreate failed with status %i.", status);
338 status = OCIHandleAlloc (oci_env, (void *) &oci_error, OCI_HTYPE_ERROR,
339 /* user_data_size = */ 0, /* user_data = */ NULL);
340 if (status != OCI_SUCCESS)
342 ERROR ("oracle plugin: OCIHandleAlloc (OCI_HTYPE_ERROR) failed "
343 "with status %i.", status);
348 } /* }}} int o_init */
350 static int o_read_database_query (o_database_t *db, /* {{{ */
354 char **column_values;
357 OCIStmt *oci_statement;
359 /* List of `OCIDefine' pointers. These defines map columns to the buffer
360 * space declared above. */
361 OCIDefine **oci_defines;
366 oci_statement = udb_query_get_user_data (q);
368 /* Prepare the statement */
369 if (oci_statement == NULL) /* {{{ */
371 const char *statement;
373 statement = udb_query_get_statement (q);
374 assert (statement != NULL);
376 status = OCIHandleAlloc (oci_env, (void *) &oci_statement,
377 OCI_HTYPE_STMT, /* user_data_size = */ 0, /* user_data = */ NULL);
378 if (status != OCI_SUCCESS)
380 o_report_error ("o_read_database_query", "OCIHandleAlloc", oci_error);
381 oci_statement = NULL;
385 status = OCIStmtPrepare (oci_statement, oci_error,
386 (text *) statement, (ub4) strlen (statement),
387 /* language = */ OCI_NTV_SYNTAX,
388 /* mode = */ OCI_DEFAULT);
389 if (status != OCI_SUCCESS)
391 o_report_error ("o_read_database_query", "OCIStmtPrepare", oci_error);
392 OCIHandleFree (oci_statement, OCI_HTYPE_STMT);
393 oci_statement = NULL;
396 udb_query_set_user_data (q, oci_statement);
398 DEBUG ("oracle plugin: o_read_database_query (%s, %s): "
399 "Successfully allocated statement handle.",
400 db->name, udb_query_get_name (q));
403 assert (oci_statement != NULL);
405 /* Execute the statement */
406 status = OCIStmtExecute (db->oci_service_context, /* {{{ */
411 /* snap_in = */ NULL, /* snap_out = */ NULL,
412 /* mode = */ OCI_DEFAULT);
413 if (status != OCI_SUCCESS)
415 DEBUG ("oracle plugin: o_read_database_query: status = %i (%#x)", status, status);
416 o_report_error ("o_read_database_query", "OCIStmtExecute", oci_error);
417 ERROR ("oracle plugin: o_read_database_query: "
418 "Failing statement was: %s", udb_query_get_statement (q));
422 /* Acquire the number of columns returned. */
425 ub4 param_counter = 0;
426 status = OCIAttrGet (oci_statement, OCI_HTYPE_STMT, /* {{{ */
427 ¶m_counter, /* size pointer = */ NULL,
428 OCI_ATTR_PARAM_COUNT, oci_error);
429 if (status != OCI_SUCCESS)
431 o_report_error ("o_read_database_query", "OCIAttrGet", oci_error);
435 column_num = (size_t) param_counter;
436 } while (0); /* }}} */
438 /* Allocate the following buffers:
440 * +---------------+-----------------------------------+
442 * +---------------+-----------------------------------+
443 * ! column_names ! column_num x DATA_MAX_NAME_LEN !
444 * ! column_values ! column_num x DATA_MAX_NAME_LEN !
445 * ! oci_defines ! column_num x sizeof (OCIDefine *) !
446 * +---------------+-----------------------------------+
449 #define NUMBER_BUFFER_SIZE 64
452 if (column_names != NULL) { \
453 sfree (column_names[0]); \
454 sfree (column_names); \
456 if (column_values != NULL) { \
457 sfree (column_values[0]); \
458 sfree (column_values); \
462 #define ALLOC_OR_FAIL(ptr, ptr_size) \
464 size_t alloc_size = (size_t) ((ptr_size)); \
465 (ptr) = malloc (alloc_size); \
466 if ((ptr) == NULL) { \
468 ERROR ("oracle plugin: o_read_database_query: malloc failed."); \
471 memset ((ptr), 0, alloc_size); \
474 /* Initialize everything to NULL so the above works. */
476 column_values = NULL;
479 ALLOC_OR_FAIL (column_names, column_num * sizeof (char *));
480 ALLOC_OR_FAIL (column_names[0], column_num * DATA_MAX_NAME_LEN
482 for (i = 1; i < column_num; i++)
483 column_names[i] = column_names[i - 1] + DATA_MAX_NAME_LEN;
485 ALLOC_OR_FAIL (column_values, column_num * sizeof (char *));
486 ALLOC_OR_FAIL (column_values[0], column_num * DATA_MAX_NAME_LEN
488 for (i = 1; i < column_num; i++)
489 column_values[i] = column_values[i - 1] + DATA_MAX_NAME_LEN;
491 ALLOC_OR_FAIL (oci_defines, column_num * sizeof (OCIDefine *));
492 /* }}} End of buffer allocations. */
494 /* ``Define'' the returned data, i. e. bind the columns to the buffers
495 * allocated above. */
496 for (i = 0; i < column_num; i++) /* {{{ */
499 size_t column_name_length;
504 status = OCIParamGet (oci_statement, OCI_HTYPE_STMT, oci_error,
505 (void *) &oci_param, (ub4) (i + 1));
506 if (status != OCI_SUCCESS)
508 /* This is probably alright */
509 DEBUG ("oracle plugin: o_read_database_query: status = %#x (= %i);", status, status);
510 o_report_error ("o_read_database_query", "OCIParamGet", oci_error);
511 status = OCI_SUCCESS;
516 column_name_length = 0;
517 status = OCIAttrGet (oci_param, OCI_DTYPE_PARAM,
518 &column_name, &column_name_length, OCI_ATTR_NAME, oci_error);
519 if (status != OCI_SUCCESS)
521 o_report_error ("o_read_database_query", "OCIAttrGet (OCI_ATTR_NAME)",
526 /* Copy the name to column_names. Warning: The ``string'' returned by OCI
527 * may not be null terminated! */
528 memset (column_names[i], 0, DATA_MAX_NAME_LEN);
529 if (column_name_length >= DATA_MAX_NAME_LEN)
530 column_name_length = DATA_MAX_NAME_LEN - 1;
531 memcpy (column_names[i], column_name, column_name_length);
532 column_names[i][column_name_length] = 0;
534 DEBUG ("oracle plugin: o_read_database_query: column_names[%zu] = %s; "
535 "column_name_length = %zu;",
536 i, column_names[i], column_name_length);
538 status = OCIDefineByPos (oci_statement,
539 &oci_defines[i], oci_error, (ub4) (i + 1),
540 column_values[i], DATA_MAX_NAME_LEN, SQLT_STR,
541 NULL, NULL, NULL, OCI_DEFAULT);
542 if (status != OCI_SUCCESS)
544 o_report_error ("o_read_database_query", "OCIDefineByPos", oci_error);
547 } /* for (j = 1; j <= param_counter; j++) */
548 /* }}} End of the ``define'' stuff. */
550 status = udb_query_prepare_result (q, hostname_g, /* plugin = */ "oracle",
551 db->name, column_names, column_num);
554 ERROR ("oracle plugin: o_read_database_query (%s, %s): "
555 "udb_query_prepare_result failed.",
556 db->name, udb_query_get_name (q));
561 /* Fetch and handle all the rows that matched the query. */
564 status = OCIStmtFetch2 (oci_statement, oci_error,
565 /* nrows = */ 1, /* orientation = */ OCI_FETCH_NEXT,
566 /* fetch offset = */ 0, /* mode = */ OCI_DEFAULT);
567 if (status == OCI_NO_DATA)
569 status = OCI_SUCCESS;
572 else if ((status != OCI_SUCCESS) && (status != OCI_SUCCESS_WITH_INFO))
574 o_report_error ("o_read_database_query", "OCIStmtFetch2", oci_error);
578 status = udb_query_handle_result (q, column_values);
581 WARNING ("oracle plugin: o_read_database_query (%s, %s): "
582 "udb_query_handle_result failed.",
583 db->name, udb_query_get_name (q));
585 } /* }}} while (42) */
587 /* DEBUG ("oracle plugin: o_read_database_query: This statement succeeded: %s", q->statement); */
593 } /* }}} int o_read_database_query */
595 static int o_read_database (o_database_t *db) /* {{{ */
600 if (db->oci_service_context == NULL)
602 status = OCILogon (oci_env, oci_error,
603 &db->oci_service_context,
604 (OraText *) db->username, (ub4) strlen (db->username),
605 (OraText *) db->password, (ub4) strlen (db->password),
606 (OraText *) db->connect_id, (ub4) strlen (db->connect_id));
607 if (status != OCI_SUCCESS)
609 o_report_error ("o_read_database", "OCILogon", oci_error);
610 DEBUG ("oracle plugin: OCILogon (%s): db->oci_service_context = %p;",
611 db->connect_id, db->oci_service_context);
612 db->oci_service_context = NULL;
615 assert (db->oci_service_context != NULL);
618 DEBUG ("oracle plugin: o_read_database: db->connect_id = %s; db->oci_service_context = %p;",
619 db->connect_id, db->oci_service_context);
621 for (i = 0; i < db->queries_num; i++)
622 o_read_database_query (db, db->queries[i]);
625 } /* }}} int o_read_database */
627 static int o_read (void) /* {{{ */
631 for (i = 0; i < databases_num; i++)
632 o_read_database (databases[i]);
635 } /* }}} int o_read */
637 static int o_shutdown (void) /* {{{ */
641 for (i = 0; i < databases_num; i++)
642 if (databases[i]->oci_service_context != NULL)
644 OCIHandleFree (databases[i]->oci_service_context, OCI_HTYPE_SVCCTX);
645 databases[i]->oci_service_context = NULL;
648 for (i = 0; i < queries_num; i++)
650 OCIStmt *oci_statement;
652 oci_statement = udb_query_get_user_data (queries[i]);
653 if (oci_statement != NULL)
655 OCIHandleFree (oci_statement, OCI_HTYPE_STMT);
656 udb_query_set_user_data (queries[i], NULL);
660 OCIHandleFree (oci_env, OCI_HTYPE_ENV);
662 udb_query_free (queries, queries_num);
667 } /* }}} int o_shutdown */
669 void module_register (void) /* {{{ */
671 plugin_register_complex_config ("oracle", o_config);
672 plugin_register_init ("oracle", o_init);
673 plugin_register_read ("oracle", o_read);
674 plugin_register_shutdown ("oracle", o_shutdown);
675 } /* }}} void module_register */
678 * vim: shiftwidth=2 softtabstop=2 et fdm=marker