1 /**
2 * collectd - src/oracle.c
3 * Copyright (C) 2008,2009 Florian octo Forster
4 *
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.
8 *
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.
13 *
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
17 *
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
21 * combination.
22 *
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.
31 *
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.
39 *
40 * Oracle® is a registered trademark of Oracle Corporation and/or its
41 * affiliates. Other names may be trademarks of their respective owners.
42 *
43 * Authors:
44 * Florian octo Forster <octo at noris.net>
45 **/
47 #include "collectd.h"
48 #include "common.h"
49 #include "plugin.h"
50 #include "configfile.h"
51 #include "utils_db_query.h"
53 #include <oci.h>
55 /*
56 * Data types
57 */
58 struct o_database_s
59 {
60 char *name;
61 char *connect_id;
62 char *username;
63 char *password;
65 udb_query_t **queries;
66 size_t queries_num;
68 OCISvcCtx *oci_service_context;
69 };
70 typedef struct o_database_s o_database_t;
72 /*
73 * Global variables
74 */
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;
83 /*
84 * Functions
85 */
86 static void o_report_error (const char *where, /* {{{ */
87 const char *what, OCIError *eh)
88 {
89 char buffer[2048];
90 sb4 error_code;
91 int status;
93 status = OCIErrorGet (eh, /* record number = */ 1,
94 /* sqlstate = */ NULL,
95 &error_code,
96 (text *) &buffer[0],
97 (ub4) sizeof (buffer),
98 OCI_HTYPE_ERROR);
99 buffer[sizeof (buffer) - 1] = 0;
101 if (status == OCI_SUCCESS)
102 {
103 size_t buffer_length;
105 buffer_length = strlen (buffer);
106 while ((buffer_length > 0) && (buffer[buffer_length - 1] < 32))
107 {
108 buffer_length--;
109 buffer[buffer_length] = 0;
110 }
112 ERROR ("oracle plugin: %s: %s failed: %s",
113 where, what, buffer);
114 }
115 else
116 {
117 ERROR ("oracle plugin: %s: %s failed. Additionally, OCIErrorGet failed with status %i.",
118 where, what, status);
119 }
120 } /* }}} void o_report_error */
122 static void o_database_free (o_database_t *db) /* {{{ */
123 {
124 if (db == NULL)
125 return;
127 sfree (db->name);
128 sfree (db->connect_id);
129 sfree (db->username);
130 sfree (db->password);
131 sfree (db->queries);
133 sfree (db);
134 } /* }}} void o_database_free */
136 /* Configuration handling functions {{{
137 *
138 * <Plugin oracle>
139 * <Query "plugin_instance0">
140 * Statement "SELECT name, value FROM table"
141 * <Result>
142 * Type "gauge"
143 * InstancesFrom "name"
144 * ValuesFrom "value"
145 * </Result>
146 * </Query>
147 *
148 * <Database "plugin_instance1">
149 * ConnectID "db01"
150 * Username "oracle"
151 * Password "secret"
152 * Query "plugin_instance0"
153 * </Database>
154 * </Plugin>
155 */
157 static int o_config_set_string (char **ret_string, /* {{{ */
158 oconfig_item_t *ci)
159 {
160 char *string;
162 if ((ci->values_num != 1)
163 || (ci->values[0].type != OCONFIG_TYPE_STRING))
164 {
165 WARNING ("oracle plugin: The `%s' config option "
166 "needs exactly one string argument.", ci->key);
167 return (-1);
168 }
170 string = strdup (ci->values[0].value.string);
171 if (string == NULL)
172 {
173 ERROR ("oracle plugin: strdup failed.");
174 return (-1);
175 }
177 if (*ret_string != NULL)
178 free (*ret_string);
179 *ret_string = string;
181 return (0);
182 } /* }}} int o_config_set_string */
184 static int o_config_add_database (oconfig_item_t *ci) /* {{{ */
185 {
186 o_database_t *db;
187 int status;
188 int i;
190 if ((ci->values_num != 1)
191 || (ci->values[0].type != OCONFIG_TYPE_STRING))
192 {
193 WARNING ("oracle plugin: The `Database' block "
194 "needs exactly one string argument.");
195 return (-1);
196 }
198 db = (o_database_t *) malloc (sizeof (*db));
199 if (db == NULL)
200 {
201 ERROR ("oracle plugin: malloc failed.");
202 return (-1);
203 }
204 memset (db, 0, sizeof (*db));
206 status = o_config_set_string (&db->name, ci);
207 if (status != 0)
208 {
209 sfree (db);
210 return (status);
211 }
213 /* Fill the `o_database_t' structure.. */
214 for (i = 0; i < ci->children_num; i++)
215 {
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);
227 else
228 {
229 WARNING ("oracle plugin: Option `%s' not allowed here.", child->key);
230 status = -1;
231 }
233 if (status != 0)
234 break;
235 }
237 /* Check that all necessary options have been given. */
238 while (status == 0)
239 {
240 if (db->connect_id == NULL)
241 {
242 WARNING ("oracle plugin: `ConnectID' not given for query `%s'", db->name);
243 status = -1;
244 }
245 if (db->username == NULL)
246 {
247 WARNING ("oracle plugin: `Username' not given for query `%s'", db->name);
248 status = -1;
249 }
250 if (db->password == NULL)
251 {
252 WARNING ("oracle plugin: `Password' not given for query `%s'", db->name);
253 status = -1;
254 }
256 break;
257 } /* while (status == 0) */
259 /* If all went well, add this query to the list of queries within the
260 * database structure. */
261 if (status == 0)
262 {
263 o_database_t **temp;
265 temp = (o_database_t **) realloc (databases,
266 sizeof (*databases) * (databases_num + 1));
267 if (temp == NULL)
268 {
269 ERROR ("oracle plugin: realloc failed");
270 status = -1;
271 }
272 else
273 {
274 databases = temp;
275 databases[databases_num] = db;
276 databases_num++;
277 }
278 }
280 if (status != 0)
281 {
282 o_database_free (db);
283 return (-1);
284 }
286 return (0);
287 } /* }}} int o_config_add_database */
289 static int o_config (oconfig_item_t *ci) /* {{{ */
290 {
291 int i;
293 for (i = 0; i < ci->children_num; i++)
294 {
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);
301 else
302 {
303 WARNING ("oracle plugin: Ignoring unknown config option `%s'.", child->key);
304 }
306 if (queries_num > 0)
307 {
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]));
310 }
311 } /* for (ci->children) */
313 return (0);
314 } /* }}} int o_config */
316 /* }}} End of configuration handling functions */
318 static int o_init (void) /* {{{ */
319 {
320 int status;
322 if (oci_env != NULL)
323 return (0);
325 status = OCIEnvCreate (&oci_env,
326 /* mode = */ OCI_THREADED,
327 /* context = */ NULL,
328 /* malloc = */ NULL,
329 /* realloc = */ NULL,
330 /* free = */ NULL,
331 /* user_data_size = */ 0,
332 /* user_data_ptr = */ NULL);
333 if (status != 0)
334 {
335 ERROR ("oracle plugin: OCIEnvCreate failed with status %i.", status);
336 return (-1);
337 }
339 status = OCIHandleAlloc (oci_env, (void *) &oci_error, OCI_HTYPE_ERROR,
340 /* user_data_size = */ 0, /* user_data = */ NULL);
341 if (status != OCI_SUCCESS)
342 {
343 ERROR ("oracle plugin: OCIHandleAlloc (OCI_HTYPE_ERROR) failed "
344 "with status %i.", status);
345 return (-1);
346 }
348 return (0);
349 } /* }}} int o_init */
351 static int o_read_database_query (o_database_t *db, /* {{{ */
352 udb_query_t *q)
353 {
354 char **column_names;
355 char **column_values;
356 size_t column_num;
358 OCIStmt *oci_statement;
360 /* List of `OCIDefine' pointers. These defines map columns to the buffer
361 * space declared above. */
362 OCIDefine **oci_defines;
364 int status;
365 size_t i;
367 oci_statement = udb_query_get_user_data (q);
369 /* Prepare the statement */
370 if (oci_statement == NULL) /* {{{ */
371 {
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)
380 {
381 o_report_error ("o_read_database_query", "OCIHandleAlloc", oci_error);
382 oci_statement = NULL;
383 return (-1);
384 }
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)
391 {
392 o_report_error ("o_read_database_query", "OCIStmtPrepare", oci_error);
393 OCIHandleFree (oci_statement, OCI_HTYPE_STMT);
394 oci_statement = NULL;
395 return (-1);
396 }
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));
402 } /* }}} */
404 assert (oci_statement != NULL);
406 /* Execute the statement */
407 status = OCIStmtExecute (db->oci_service_context, /* {{{ */
408 oci_statement,
409 oci_error,
410 /* iters = */ 0,
411 /* rowoff = */ 0,
412 /* snap_in = */ NULL, /* snap_out = */ NULL,
413 /* mode = */ OCI_DEFAULT);
414 if (status != OCI_SUCCESS)
415 {
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));
420 return (-1);
421 } /* }}} */
423 /* Acquire the number of columns returned. */
424 do /* {{{ */
425 {
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)
431 {
432 o_report_error ("o_read_database_query", "OCIAttrGet", oci_error);
433 return (-1);
434 } /* }}} */
436 column_num = (size_t) param_counter;
437 } while (0); /* }}} */
439 /* Allocate the following buffers:
440 *
441 * +---------------+-----------------------------------+
442 * ! Name ! Size !
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 * +---------------+-----------------------------------+
448 *
449 * {{{ */
450 #define NUMBER_BUFFER_SIZE 64
452 #define FREE_ALL \
453 if (column_names != NULL) { \
454 sfree (column_names[0]); \
455 sfree (column_names); \
456 } \
457 if (column_values != NULL) { \
458 sfree (column_values[0]); \
459 sfree (column_values); \
460 } \
461 sfree (oci_defines)
463 #define ALLOC_OR_FAIL(ptr, ptr_size) \
464 do { \
465 size_t alloc_size = (size_t) ((ptr_size)); \
466 (ptr) = malloc (alloc_size); \
467 if ((ptr) == NULL) { \
468 FREE_ALL; \
469 ERROR ("oracle plugin: o_read_database_query: malloc failed."); \
470 return (-1); \
471 } \
472 memset ((ptr), 0, alloc_size); \
473 } while (0)
475 /* Initialize everything to NULL so the above works. */
476 column_names = NULL;
477 column_values = NULL;
478 oci_defines = NULL;
480 ALLOC_OR_FAIL (column_names, column_num * sizeof (char *));
481 ALLOC_OR_FAIL (column_names[0], column_num * DATA_MAX_NAME_LEN
482 * sizeof (char));
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
488 * sizeof (char));
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++) /* {{{ */
498 {
499 char *column_name;
500 ub4 column_name_length;
501 OCIParam *oci_param;
503 oci_param = NULL;
505 status = OCIParamGet (oci_statement, OCI_HTYPE_STMT, oci_error,
506 (void *) &oci_param, (ub4) (i + 1));
507 if (status != OCI_SUCCESS)
508 {
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;
513 break;
514 }
516 column_name = NULL;
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)
521 {
522 o_report_error ("o_read_database_query", "OCIAttrGet (OCI_ATTR_NAME)",
523 oci_error);
524 continue;
525 }
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)
544 {
545 o_report_error ("o_read_database_query", "OCIDefineByPos", oci_error);
546 continue;
547 }
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);
553 if (status != 0)
554 {
555 ERROR ("oracle plugin: o_read_database_query (%s, %s): "
556 "udb_query_prepare_result failed.",
557 db->name, udb_query_get_name (q));
558 FREE_ALL;
559 return (-1);
560 }
562 /* Fetch and handle all the rows that matched the query. */
563 while (42) /* {{{ */
564 {
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)
569 {
570 status = OCI_SUCCESS;
571 break;
572 }
573 else if ((status != OCI_SUCCESS) && (status != OCI_SUCCESS_WITH_INFO))
574 {
575 o_report_error ("o_read_database_query", "OCIStmtFetch2", oci_error);
576 break;
577 }
579 status = udb_query_handle_result (q, column_values);
580 if (status != 0)
581 {
582 WARNING ("oracle plugin: o_read_database_query (%s, %s): "
583 "udb_query_handle_result failed.",
584 db->name, udb_query_get_name (q));
585 }
586 } /* }}} while (42) */
588 /* DEBUG ("oracle plugin: o_read_database_query: This statement succeeded: %s", q->statement); */
589 FREE_ALL;
591 return (0);
592 #undef FREE_ALL
593 #undef ALLOC_OR_FAIL
594 } /* }}} int o_read_database_query */
596 static int o_read_database (o_database_t *db) /* {{{ */
597 {
598 size_t i;
599 int status;
601 if (db->oci_service_context == NULL)
602 {
603 status = OCILogon (oci_env, oci_error,
604 &db->oci_service_context,
605 (OraText *) db->username, (ub4) strlen (db->username),
606 (OraText *) db->password, (ub4) strlen (db->password),
607 (OraText *) db->connect_id, (ub4) strlen (db->connect_id));
608 if (status != OCI_SUCCESS)
609 {
610 o_report_error ("o_read_database", "OCILogon", oci_error);
611 DEBUG ("oracle plugin: OCILogon (%s): db->oci_service_context = %p;",
612 db->connect_id, db->oci_service_context);
613 db->oci_service_context = NULL;
614 return (-1);
615 }
616 assert (db->oci_service_context != NULL);
617 }
619 DEBUG ("oracle plugin: o_read_database: db->connect_id = %s; db->oci_service_context = %p;",
620 db->connect_id, db->oci_service_context);
622 for (i = 0; i < db->queries_num; i++)
623 o_read_database_query (db, db->queries[i]);
625 return (0);
626 } /* }}} int o_read_database */
628 static int o_read (void) /* {{{ */
629 {
630 size_t i;
632 for (i = 0; i < databases_num; i++)
633 o_read_database (databases[i]);
635 return (0);
636 } /* }}} int o_read */
638 static int o_shutdown (void) /* {{{ */
639 {
640 size_t i;
642 for (i = 0; i < databases_num; i++)
643 if (databases[i]->oci_service_context != NULL)
644 {
645 OCIHandleFree (databases[i]->oci_service_context, OCI_HTYPE_SVCCTX);
646 databases[i]->oci_service_context = NULL;
647 }
649 for (i = 0; i < queries_num; i++)
650 {
651 OCIStmt *oci_statement;
653 oci_statement = udb_query_get_user_data (queries[i]);
654 if (oci_statement != NULL)
655 {
656 OCIHandleFree (oci_statement, OCI_HTYPE_STMT);
657 udb_query_set_user_data (queries[i], NULL);
658 }
659 }
661 OCIHandleFree (oci_env, OCI_HTYPE_ENV);
663 udb_query_free (queries, queries_num);
664 queries = NULL;
665 queries_num = 0;
667 return (0);
668 } /* }}} int o_shutdown */
670 void module_register (void) /* {{{ */
671 {
672 plugin_register_complex_config ("oracle", o_config);
673 plugin_register_init ("oracle", o_init);
674 plugin_register_read ("oracle", o_read);
675 plugin_register_shutdown ("oracle", o_shutdown);
676 } /* }}} void module_register */
678 /*
679 * vim: shiftwidth=2 softtabstop=2 et fdm=marker
680 */