'dbi', 'oracle' and 'postgresql' plugins: Use 'Plugin' as option name
[collectd.git] / src / oracle.c
1 /**
2  * collectd - src/oracle.c
3  * Copyright (C) 2008,2009  noris network AG
4  * Copyright (C) 2012       Florian octo Forster
5  *
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.
9  *
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.
14  *
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
18  *
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
22  * combination.
23  *
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.
32  *
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.
40  *
41  * Oracle® is a registered trademark of Oracle Corporation and/or its
42  * affiliates. Other names may be trademarks of their respective owners.
43  *
44  * Authors:
45  *   Florian octo Forster <octo at collectd.org>
46  **/
47
48 #include "collectd.h"
49
50 #include "common.h"
51 #include "plugin.h"
52 #include "utils_db_query.h"
53
54 #include <oci.h>
55
56 /*
57  * Data types
58  */
59 struct o_database_s {
60   char *name;
61   char *host;
62   char *connect_id;
63   char *username;
64   char *password;
65   char *plugin_name;
66
67   udb_query_preparation_area_t **q_prep_areas;
68   udb_query_t **queries;
69   size_t queries_num;
70
71   OCISvcCtx *oci_service_context;
72 };
73 typedef struct o_database_s o_database_t;
74
75 /*
76  * Global variables
77  */
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;
82
83 OCIEnv *oci_env = NULL;
84 OCIError *oci_error = NULL;
85
86 /*
87  * Functions
88  */
89 static void o_report_error(const char *where, /* {{{ */
90                            const char *db_name, const char *query_name,
91                            const char *what, OCIError *eh) {
92   char buffer[2048];
93   sb4 error_code;
94   int status;
95
96   if (db_name == NULL)
97     db_name = "(none)";
98   if (query_name == NULL)
99     query_name = "(none)";
100
101   /* An operation may cause / return multiple errors. Loop until we have
102    * handled all errors available (with a fail-save limit of 16). */
103   for (unsigned int record_number = 1; record_number <= 16; record_number++) {
104     memset(buffer, 0, sizeof(buffer));
105     error_code = -1;
106
107     status = OCIErrorGet(eh, (ub4)record_number,
108                          /* sqlstate = */ NULL, &error_code, (text *)&buffer[0],
109                          (ub4)sizeof(buffer), OCI_HTYPE_ERROR);
110     buffer[sizeof(buffer) - 1] = 0;
111
112     if (status == OCI_NO_DATA)
113       return;
114
115     if (status == OCI_SUCCESS) {
116       size_t buffer_length;
117
118       buffer_length = strlen(buffer);
119       while ((buffer_length > 0) && (buffer[buffer_length - 1] < 32)) {
120         buffer_length--;
121         buffer[buffer_length] = 0;
122       }
123
124       ERROR("oracle plugin: %s (db = %s, query = %s): %s failed: %s", where,
125             db_name, query_name, what, buffer);
126     } else {
127       ERROR("oracle plugin: %s (db = %s, query = %s): %s failed. "
128             "Additionally, OCIErrorGet failed with status %i.",
129             where, db_name, query_name, what, status);
130       return;
131     }
132   }
133 } /* }}} void o_report_error */
134
135 static void o_database_free(o_database_t *db) /* {{{ */
136 {
137   if (db == NULL)
138     return;
139
140   sfree(db->name);
141   sfree(db->connect_id);
142   sfree(db->username);
143   sfree(db->password);
144   sfree(db->queries);
145   sfree(db->plugin_name);
146
147   if (db->q_prep_areas != NULL)
148     for (size_t i = 0; i < db->queries_num; ++i)
149       udb_query_delete_preparation_area(db->q_prep_areas[i]);
150   free(db->q_prep_areas);
151
152   sfree(db);
153 } /* }}} void o_database_free */
154
155 /* Configuration handling functions {{{
156  *
157  * <Plugin oracle>
158  *   <Query "plugin_instance0">
159  *     Statement "SELECT name, value FROM table"
160  *     <Result>
161  *       Type "gauge"
162  *       InstancesFrom "name"
163  *       ValuesFrom "value"
164  *     </Result>
165  *   </Query>
166  *
167  *   <Database "plugin_instance1">
168  *     ConnectID "db01"
169  *     Username "oracle"
170  *     Password "secret"
171  *     Query "plugin_instance0"
172  *   </Database>
173  * </Plugin>
174  */
175
176 static int o_config_add_database(oconfig_item_t *ci) /* {{{ */
177 {
178   o_database_t *db;
179   int status;
180
181   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
182     WARNING("oracle plugin: The `Database' block "
183             "needs exactly one string argument.");
184     return -1;
185   }
186
187   db = calloc(1, sizeof(*db));
188   if (db == NULL) {
189     ERROR("oracle plugin: calloc failed.");
190     return -1;
191   }
192   db->name = NULL;
193   db->host = NULL;
194   db->connect_id = NULL;
195   db->username = NULL;
196   db->password = NULL;
197   db->plugin_name = NULL;
198
199   status = cf_util_get_string(ci, &db->name);
200   if (status != 0) {
201     sfree(db);
202     return status;
203   }
204
205   /* Fill the `o_database_t' structure.. */
206   for (int i = 0; i < ci->children_num; i++) {
207     oconfig_item_t *child = ci->children + i;
208
209     if (strcasecmp("ConnectID", child->key) == 0)
210       status = cf_util_get_string(child, &db->connect_id);
211     else if (strcasecmp("Host", child->key) == 0)
212       status = cf_util_get_string(child, &db->host);
213     else if (strcasecmp("Username", child->key) == 0)
214       status = cf_util_get_string(child, &db->username);
215     else if (strcasecmp("Password", child->key) == 0)
216       status = cf_util_get_string(child, &db->password);
217     else if (strcasecmp("Plugin", child->key) == 0)
218       status = cf_util_get_string(child, &db->plugin_name);
219     else if (strcasecmp("Query", child->key) == 0)
220       status = udb_query_pick_from_list(child, queries, queries_num,
221                                         &db->queries, &db->queries_num);
222     else {
223       WARNING("oracle plugin: Option `%s' not allowed here.", child->key);
224       status = -1;
225     }
226
227     if (status != 0)
228       break;
229   }
230
231   /* Check that all necessary options have been given. */
232   while (status == 0) {
233     if (db->connect_id == NULL) {
234       WARNING("oracle plugin: `ConnectID' not given for query `%s'", db->name);
235       status = -1;
236     }
237     if (db->username == NULL) {
238       WARNING("oracle plugin: `Username' not given for query `%s'", db->name);
239       status = -1;
240     }
241     if (db->password == NULL) {
242       WARNING("oracle plugin: `Password' not given for query `%s'", db->name);
243       status = -1;
244     }
245
246     break;
247   } /* while (status == 0) */
248
249   while ((status == 0) && (db->queries_num > 0)) {
250     db->q_prep_areas = (udb_query_preparation_area_t **)calloc(
251         db->queries_num, sizeof(*db->q_prep_areas));
252
253     if (db->q_prep_areas == NULL) {
254       WARNING("oracle plugin: calloc failed");
255       status = -1;
256       break;
257     }
258
259     for (int i = 0; i < db->queries_num; ++i) {
260       db->q_prep_areas[i] = udb_query_allocate_preparation_area(db->queries[i]);
261
262       if (db->q_prep_areas[i] == NULL) {
263         WARNING("oracle plugin: udb_query_allocate_preparation_area failed");
264         status = -1;
265         break;
266       }
267     }
268
269     break;
270   }
271
272   /* If all went well, add this query to the list of queries within the
273    * database structure. */
274   if (status == 0) {
275     o_database_t **temp;
276
277     temp = realloc(databases, sizeof(*databases) * (databases_num + 1));
278     if (temp == NULL) {
279       ERROR("oracle plugin: realloc failed");
280       status = -1;
281     } else {
282       databases = temp;
283       databases[databases_num] = db;
284       databases_num++;
285     }
286   }
287
288   if (status != 0) {
289     o_database_free(db);
290     return -1;
291   }
292
293   return 0;
294 } /* }}} int o_config_add_database */
295
296 static int o_config(oconfig_item_t *ci) /* {{{ */
297 {
298   for (int i = 0; i < ci->children_num; i++) {
299     oconfig_item_t *child = ci->children + i;
300     if (strcasecmp("Query", child->key) == 0)
301       udb_query_create(&queries, &queries_num, child,
302                        /* callback = */ NULL);
303     else if (strcasecmp("Database", child->key) == 0)
304       o_config_add_database(child);
305     else {
306       WARNING("oracle plugin: Ignoring unknown config option `%s'.",
307               child->key);
308     }
309
310     if (queries_num > 0) {
311       DEBUG("oracle plugin: o_config: queries_num = %zu; queries[0] = %p; "
312             "udb_query_get_user_data (queries[0]) = %p;",
313             queries_num, (void *)queries[0],
314             udb_query_get_user_data(queries[0]));
315     }
316   } /* for (ci->children) */
317
318   return 0;
319 } /* }}} int o_config */
320
321 /* }}} End of configuration handling functions */
322
323 static int o_init(void) /* {{{ */
324 {
325   int status;
326
327   if (oci_env != NULL)
328     return 0;
329
330   status = OCIEnvCreate(&oci_env,
331                         /* mode = */ OCI_THREADED,
332                         /* context        = */ NULL,
333                         /* malloc         = */ NULL,
334                         /* realloc        = */ NULL,
335                         /* free           = */ NULL,
336                         /* user_data_size = */ 0,
337                         /* user_data_ptr  = */ NULL);
338   if (status != 0) {
339     ERROR("oracle plugin: OCIEnvCreate failed with status %i.", status);
340     return -1;
341   }
342
343   status = OCIHandleAlloc(oci_env, (void *)&oci_error, OCI_HTYPE_ERROR,
344                           /* user_data_size = */ 0, /* user_data = */ NULL);
345   if (status != OCI_SUCCESS) {
346     ERROR("oracle plugin: OCIHandleAlloc (OCI_HTYPE_ERROR) failed "
347           "with status %i.",
348           status);
349     return -1;
350   }
351
352   return 0;
353 } /* }}} int o_init */
354
355 static int o_read_database_query(o_database_t *db, /* {{{ */
356                                  udb_query_t *q,
357                                  udb_query_preparation_area_t *prep_area) {
358   char **column_names;
359   char **column_values;
360   size_t column_num;
361
362   OCIStmt *oci_statement;
363
364   /* List of `OCIDefine' pointers. These defines map columns to the buffer
365    * space declared above. */
366   OCIDefine **oci_defines;
367
368   int status;
369
370   oci_statement = udb_query_get_user_data(q);
371
372   /* Prepare the statement */
373   if (oci_statement == NULL) /* {{{ */
374   {
375     const char *statement;
376
377     statement = udb_query_get_statement(q);
378     assert(statement != NULL);
379
380     status = OCIHandleAlloc(oci_env, (void *)&oci_statement, OCI_HTYPE_STMT,
381                             /* user_data_size = */ 0, /* user_data = */ NULL);
382     if (status != OCI_SUCCESS) {
383       o_report_error("o_read_database_query", db->name, udb_query_get_name(q),
384                      "OCIHandleAlloc", oci_error);
385       oci_statement = NULL;
386       return -1;
387     }
388
389     status = OCIStmtPrepare(oci_statement, oci_error, (text *)statement,
390                             (ub4)strlen(statement),
391                             /* language = */ OCI_NTV_SYNTAX,
392                             /* mode     = */ OCI_DEFAULT);
393     if (status != OCI_SUCCESS) {
394       o_report_error("o_read_database_query", db->name, udb_query_get_name(q),
395                      "OCIStmtPrepare", oci_error);
396       OCIHandleFree(oci_statement, OCI_HTYPE_STMT);
397       oci_statement = NULL;
398       return -1;
399     }
400     udb_query_set_user_data(q, oci_statement);
401
402     DEBUG("oracle plugin: o_read_database_query (%s, %s): "
403           "Successfully allocated statement handle.",
404           db->name, udb_query_get_name(q));
405   } /* }}} */
406
407   assert(oci_statement != NULL);
408
409   /* Execute the statement */
410   status = OCIStmtExecute(db->oci_service_context, /* {{{ */
411                           oci_statement, oci_error,
412                           /* iters = */ 0,
413                           /* rowoff = */ 0,
414                           /* snap_in = */ NULL, /* snap_out = */ NULL,
415                           /* mode = */ OCI_DEFAULT);
416   if (status != OCI_SUCCESS) {
417     o_report_error("o_read_database_query", db->name, udb_query_get_name(q),
418                    "OCIStmtExecute", oci_error);
419     return -1;
420   } /* }}} */
421
422   /* Acquire the number of columns returned. */
423   do /* {{{ */
424   {
425     ub4 param_counter = 0;
426     status = OCIAttrGet(oci_statement, OCI_HTYPE_STMT, /* {{{ */
427                         &param_counter, /* size pointer = */ NULL,
428                         OCI_ATTR_PARAM_COUNT, oci_error);
429     if (status != OCI_SUCCESS) {
430       o_report_error("o_read_database_query", db->name, udb_query_get_name(q),
431                      "OCIAttrGet", oci_error);
432       return -1;
433     } /* }}} */
434
435     column_num = (size_t)param_counter;
436   } while (0); /* }}} */
437
438 /* Allocate the following buffers:
439  *
440  *  +---------------+-----------------------------------+
441  *  ! Name          ! Size                              !
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  *  +---------------+-----------------------------------+
447  *
448  * {{{ */
449 #define NUMBER_BUFFER_SIZE 64
450
451 #define FREE_ALL                                                               \
452   if (column_names != NULL) {                                                  \
453     sfree(column_names[0]);                                                    \
454     sfree(column_names);                                                       \
455   }                                                                            \
456   if (column_values != NULL) {                                                 \
457     sfree(column_values[0]);                                                   \
458     sfree(column_values);                                                      \
459   }                                                                            \
460   sfree(oci_defines)
461
462 #define ALLOC_OR_FAIL(ptr, ptr_size)                                           \
463   do {                                                                         \
464     size_t alloc_size = (size_t)((ptr_size));                                  \
465     (ptr) = calloc(1, alloc_size);                                             \
466     if ((ptr) == NULL) {                                                       \
467       FREE_ALL;                                                                \
468       ERROR("oracle plugin: o_read_database_query: calloc failed.");           \
469       return -1;                                                               \
470     }                                                                          \
471   } while (0)
472
473   /* Initialize everything to NULL so the above works. */
474   column_names = NULL;
475   column_values = NULL;
476   oci_defines = NULL;
477
478   ALLOC_OR_FAIL(column_names, column_num * sizeof(char *));
479   ALLOC_OR_FAIL(column_names[0], column_num * DATA_MAX_NAME_LEN);
480   for (size_t i = 1; i < column_num; i++)
481     column_names[i] = column_names[i - 1] + DATA_MAX_NAME_LEN;
482
483   ALLOC_OR_FAIL(column_values, column_num * sizeof(char *));
484   ALLOC_OR_FAIL(column_values[0],
485                 column_num * DATA_MAX_NAME_LEN);
486   for (size_t i = 1; i < column_num; i++)
487     column_values[i] = column_values[i - 1] + DATA_MAX_NAME_LEN;
488
489   ALLOC_OR_FAIL(oci_defines, column_num * sizeof(OCIDefine *));
490   /* }}} End of buffer allocations. */
491
492   /* ``Define'' the returned data, i. e. bind the columns to the buffers
493    * allocated above. */
494   for (size_t i = 0; i < column_num; i++) /* {{{ */
495   {
496     char *column_name;
497     ub4 column_name_length;
498     OCIParam *oci_param;
499
500     oci_param = NULL;
501
502     status = OCIParamGet(oci_statement, OCI_HTYPE_STMT, oci_error,
503                          (void *)&oci_param, (ub4)(i + 1));
504     if (status != OCI_SUCCESS) {
505       /* This is probably alright */
506       DEBUG("oracle plugin: o_read_database_query: status = %#x (= %i);",
507             status, status);
508       o_report_error("o_read_database_query", db->name, udb_query_get_name(q),
509                      "OCIParamGet", oci_error);
510       status = OCI_SUCCESS;
511       break;
512     }
513
514     column_name = NULL;
515     column_name_length = 0;
516     status = OCIAttrGet(oci_param, OCI_DTYPE_PARAM, &column_name,
517                         &column_name_length, OCI_ATTR_NAME, oci_error);
518     if (status != OCI_SUCCESS) {
519       OCIDescriptorFree(oci_param, OCI_DTYPE_PARAM);
520       o_report_error("o_read_database_query", db->name, udb_query_get_name(q),
521                      "OCIAttrGet (OCI_ATTR_NAME)", oci_error);
522       continue;
523     }
524
525     OCIDescriptorFree(oci_param, OCI_DTYPE_PARAM);
526     oci_param = NULL;
527
528     /* Copy the name to column_names. Warning: The ``string'' returned by OCI
529      * may not be null terminated! */
530     memset(column_names[i], 0, DATA_MAX_NAME_LEN);
531     if (column_name_length >= DATA_MAX_NAME_LEN)
532       column_name_length = DATA_MAX_NAME_LEN - 1;
533     memcpy(column_names[i], column_name, column_name_length);
534     column_names[i][column_name_length] = 0;
535
536     DEBUG("oracle plugin: o_read_database_query: column_names[%zu] = %s; "
537           "column_name_length = %" PRIu32 ";",
538           i, column_names[i], (uint32_t)column_name_length);
539
540     status = OCIDefineByPos(oci_statement, &oci_defines[i], oci_error,
541                             (ub4)(i + 1), column_values[i], DATA_MAX_NAME_LEN,
542                             SQLT_STR, NULL, NULL, NULL, OCI_DEFAULT);
543     if (status != OCI_SUCCESS) {
544       o_report_error("o_read_database_query", db->name, udb_query_get_name(q),
545                      "OCIDefineByPos", oci_error);
546       continue;
547     }
548   } /* for (j = 1; j <= param_counter; j++) */
549   /* }}} End of the ``define'' stuff. */
550
551   status = udb_query_prepare_result(
552       q, prep_area, (db->host != NULL) ? db->host : hostname_g,
553       /* plugin = */ (db->plugin_name != NULL) ? db->plugin_name : "oracle",
554       db->name, column_names, column_num,
555       /* interval = */ 0);
556   if (status != 0) {
557     ERROR("oracle plugin: o_read_database_query (%s, %s): "
558           "udb_query_prepare_result failed.",
559           db->name, udb_query_get_name(q));
560     FREE_ALL;
561     return -1;
562   }
563
564   /* Fetch and handle all the rows that matched the query. */
565   while (42) /* {{{ */
566   {
567     status = OCIStmtFetch2(oci_statement, oci_error,
568                            /* nrows = */ 1, /* orientation = */ OCI_FETCH_NEXT,
569                            /* fetch offset = */ 0, /* mode = */ OCI_DEFAULT);
570     if (status == OCI_NO_DATA) {
571       status = OCI_SUCCESS;
572       break;
573     } else if ((status != OCI_SUCCESS) && (status != OCI_SUCCESS_WITH_INFO)) {
574       o_report_error("o_read_database_query", db->name, udb_query_get_name(q),
575                      "OCIStmtFetch2", oci_error);
576       break;
577     }
578
579     status = udb_query_handle_result(q, prep_area, column_values);
580     if (status != 0) {
581       WARNING("oracle plugin: o_read_database_query (%s, %s): "
582               "udb_query_handle_result failed.",
583               db->name, udb_query_get_name(q));
584     }
585   } /* }}} while (42) */
586
587   /* DEBUG ("oracle plugin: o_read_database_query: This statement succeeded:
588    * %s", q->statement); */
589   FREE_ALL;
590
591   return 0;
592 #undef FREE_ALL
593 #undef ALLOC_OR_FAIL
594 } /* }}} int o_read_database_query */
595
596 static int o_read_database(o_database_t *db) /* {{{ */
597 {
598   int status;
599
600   if (db->oci_service_context != NULL) {
601     OCIServer *server_handle;
602     ub4 connection_status;
603
604     server_handle = NULL;
605     status = OCIAttrGet((void *)db->oci_service_context, OCI_HTYPE_SVCCTX,
606                         (void *)&server_handle, /* size pointer = */ NULL,
607                         OCI_ATTR_SERVER, oci_error);
608     if (status != OCI_SUCCESS) {
609       o_report_error("o_read_database", db->name, NULL, "OCIAttrGet",
610                      oci_error);
611       return -1;
612     }
613
614     if (server_handle == NULL) {
615       connection_status = OCI_SERVER_NOT_CONNECTED;
616     } else /* if (server_handle != NULL) */
617     {
618       connection_status = 0;
619       status = OCIAttrGet((void *)server_handle, OCI_HTYPE_SERVER,
620                           (void *)&connection_status, /* size pointer = */ NULL,
621                           OCI_ATTR_SERVER_STATUS, oci_error);
622       if (status != OCI_SUCCESS) {
623         o_report_error("o_read_database", db->name, NULL, "OCIAttrGet",
624                        oci_error);
625         return -1;
626       }
627     }
628
629     if (connection_status != OCI_SERVER_NORMAL) {
630       INFO("oracle plugin: Connection to %s lost. Trying to reconnect.",
631            db->name);
632       OCIHandleFree(db->oci_service_context, OCI_HTYPE_SVCCTX);
633       db->oci_service_context = NULL;
634     }
635   } /* if (db->oci_service_context != NULL) */
636
637   if (db->oci_service_context == NULL) {
638     status = OCILogon(oci_env, oci_error, &db->oci_service_context,
639                       (OraText *)db->username, (ub4)strlen(db->username),
640                       (OraText *)db->password, (ub4)strlen(db->password),
641                       (OraText *)db->connect_id, (ub4)strlen(db->connect_id));
642     if ((status != OCI_SUCCESS) && (status != OCI_SUCCESS_WITH_INFO)) {
643       char errfunc[256];
644
645       ssnprintf(errfunc, sizeof(errfunc), "OCILogon(\"%s\")", db->connect_id);
646
647       o_report_error("o_read_database", db->name, NULL, errfunc, oci_error);
648       DEBUG("oracle plugin: OCILogon (%s): db->oci_service_context = %p;",
649             db->connect_id, db->oci_service_context);
650       db->oci_service_context = NULL;
651       return -1;
652     } else if (status == OCI_SUCCESS_WITH_INFO) {
653       /* TODO: Print NOTIFY message. */
654     }
655     assert(db->oci_service_context != NULL);
656   }
657
658   DEBUG("oracle plugin: o_read_database: db->connect_id = %s; "
659         "db->oci_service_context = %p;",
660         db->connect_id, db->oci_service_context);
661
662   for (size_t i = 0; i < db->queries_num; i++)
663     o_read_database_query(db, db->queries[i], db->q_prep_areas[i]);
664
665   return 0;
666 } /* }}} int o_read_database */
667
668 static int o_read(void) /* {{{ */
669 {
670   size_t i;
671
672   for (i = 0; i < databases_num; i++)
673     o_read_database(databases[i]);
674
675   return 0;
676 } /* }}} int o_read */
677
678 static int o_shutdown(void) /* {{{ */
679 {
680   size_t i;
681
682   for (i = 0; i < databases_num; i++)
683     if (databases[i]->oci_service_context != NULL) {
684       OCIHandleFree(databases[i]->oci_service_context, OCI_HTYPE_SVCCTX);
685       databases[i]->oci_service_context = NULL;
686     }
687
688   for (i = 0; i < queries_num; i++) {
689     OCIStmt *oci_statement;
690
691     oci_statement = udb_query_get_user_data(queries[i]);
692     if (oci_statement != NULL) {
693       OCIHandleFree(oci_statement, OCI_HTYPE_STMT);
694       udb_query_set_user_data(queries[i], NULL);
695     }
696   }
697
698   OCIHandleFree(oci_env, OCI_HTYPE_ENV);
699   oci_env = NULL;
700
701   udb_query_free(queries, queries_num);
702   queries = NULL;
703   queries_num = 0;
704
705   return 0;
706 } /* }}} int o_shutdown */
707
708 void module_register(void) /* {{{ */
709 {
710   plugin_register_complex_config("oracle", o_config);
711   plugin_register_init("oracle", o_init);
712   plugin_register_read("oracle", o_read);
713   plugin_register_shutdown("oracle", o_shutdown);
714 } /* }}} void module_register */