099013e3692757e3609660b2595751a54582800b
[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], column_num * DATA_MAX_NAME_LEN);
485   for (size_t i = 1; i < column_num; i++)
486     column_values[i] = column_values[i - 1] + DATA_MAX_NAME_LEN;
487
488   ALLOC_OR_FAIL(oci_defines, column_num * sizeof(OCIDefine *));
489   /* }}} End of buffer allocations. */
490
491   /* ``Define'' the returned data, i. e. bind the columns to the buffers
492    * allocated above. */
493   for (size_t i = 0; i < column_num; i++) /* {{{ */
494   {
495     char *column_name;
496     ub4 column_name_length;
497     OCIParam *oci_param;
498
499     oci_param = NULL;
500
501     status = OCIParamGet(oci_statement, OCI_HTYPE_STMT, oci_error,
502                          (void *)&oci_param, (ub4)(i + 1));
503     if (status != OCI_SUCCESS) {
504       /* This is probably alright */
505       DEBUG("oracle plugin: o_read_database_query: status = %#x (= %i);",
506             status, status);
507       o_report_error("o_read_database_query", db->name, udb_query_get_name(q),
508                      "OCIParamGet", oci_error);
509       status = OCI_SUCCESS;
510       break;
511     }
512
513     column_name = NULL;
514     column_name_length = 0;
515     status = OCIAttrGet(oci_param, OCI_DTYPE_PARAM, &column_name,
516                         &column_name_length, OCI_ATTR_NAME, oci_error);
517     if (status != OCI_SUCCESS) {
518       OCIDescriptorFree(oci_param, OCI_DTYPE_PARAM);
519       o_report_error("o_read_database_query", db->name, udb_query_get_name(q),
520                      "OCIAttrGet (OCI_ATTR_NAME)", oci_error);
521       continue;
522     }
523
524     OCIDescriptorFree(oci_param, OCI_DTYPE_PARAM);
525     oci_param = NULL;
526
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;
534
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);
538
539     status = OCIDefineByPos(oci_statement, &oci_defines[i], oci_error,
540                             (ub4)(i + 1), column_values[i], DATA_MAX_NAME_LEN,
541                             SQLT_STR, NULL, NULL, NULL, OCI_DEFAULT);
542     if (status != OCI_SUCCESS) {
543       o_report_error("o_read_database_query", db->name, udb_query_get_name(q),
544                      "OCIDefineByPos", oci_error);
545       continue;
546     }
547   } /* for (j = 1; j <= param_counter; j++) */
548   /* }}} End of the ``define'' stuff. */
549
550   status = udb_query_prepare_result(
551       q, prep_area, (db->host != NULL) ? db->host : hostname_g,
552       /* plugin = */ (db->plugin_name != NULL) ? db->plugin_name : "oracle",
553       db->name, column_names, column_num,
554       /* interval = */ 0);
555   if (status != 0) {
556     ERROR("oracle plugin: o_read_database_query (%s, %s): "
557           "udb_query_prepare_result failed.",
558           db->name, udb_query_get_name(q));
559     FREE_ALL;
560     return -1;
561   }
562
563   /* Fetch and handle all the rows that matched the query. */
564   while (42) /* {{{ */
565   {
566     status = OCIStmtFetch2(oci_statement, oci_error,
567                            /* nrows = */ 1, /* orientation = */ OCI_FETCH_NEXT,
568                            /* fetch offset = */ 0, /* mode = */ OCI_DEFAULT);
569     if (status == OCI_NO_DATA) {
570       status = OCI_SUCCESS;
571       break;
572     } else if ((status != OCI_SUCCESS) && (status != OCI_SUCCESS_WITH_INFO)) {
573       o_report_error("o_read_database_query", db->name, udb_query_get_name(q),
574                      "OCIStmtFetch2", oci_error);
575       break;
576     }
577
578     status = udb_query_handle_result(q, prep_area, column_values);
579     if (status != 0) {
580       WARNING("oracle plugin: o_read_database_query (%s, %s): "
581               "udb_query_handle_result failed.",
582               db->name, udb_query_get_name(q));
583     }
584   } /* }}} while (42) */
585
586   /* DEBUG ("oracle plugin: o_read_database_query: This statement succeeded:
587    * %s", q->statement); */
588   FREE_ALL;
589
590   return 0;
591 #undef FREE_ALL
592 #undef ALLOC_OR_FAIL
593 } /* }}} int o_read_database_query */
594
595 static int o_read_database(o_database_t *db) /* {{{ */
596 {
597   int status;
598
599   if (db->oci_service_context != NULL) {
600     OCIServer *server_handle;
601     ub4 connection_status;
602
603     server_handle = NULL;
604     status = OCIAttrGet((void *)db->oci_service_context, OCI_HTYPE_SVCCTX,
605                         (void *)&server_handle, /* size pointer = */ NULL,
606                         OCI_ATTR_SERVER, oci_error);
607     if (status != OCI_SUCCESS) {
608       o_report_error("o_read_database", db->name, NULL, "OCIAttrGet",
609                      oci_error);
610       return -1;
611     }
612
613     if (server_handle == NULL) {
614       connection_status = OCI_SERVER_NOT_CONNECTED;
615     } else /* if (server_handle != NULL) */
616     {
617       connection_status = 0;
618       status = OCIAttrGet((void *)server_handle, OCI_HTYPE_SERVER,
619                           (void *)&connection_status, /* size pointer = */ NULL,
620                           OCI_ATTR_SERVER_STATUS, oci_error);
621       if (status != OCI_SUCCESS) {
622         o_report_error("o_read_database", db->name, NULL, "OCIAttrGet",
623                        oci_error);
624         return -1;
625       }
626     }
627
628     if (connection_status != OCI_SERVER_NORMAL) {
629       INFO("oracle plugin: Connection to %s lost. Trying to reconnect.",
630            db->name);
631       OCIHandleFree(db->oci_service_context, OCI_HTYPE_SVCCTX);
632       db->oci_service_context = NULL;
633     }
634   } /* if (db->oci_service_context != NULL) */
635
636   if (db->oci_service_context == NULL) {
637     status = OCILogon(oci_env, oci_error, &db->oci_service_context,
638                       (OraText *)db->username, (ub4)strlen(db->username),
639                       (OraText *)db->password, (ub4)strlen(db->password),
640                       (OraText *)db->connect_id, (ub4)strlen(db->connect_id));
641     if ((status != OCI_SUCCESS) && (status != OCI_SUCCESS_WITH_INFO)) {
642       char errfunc[256];
643
644       snprintf(errfunc, sizeof(errfunc), "OCILogon(\"%s\")", db->connect_id);
645
646       o_report_error("o_read_database", db->name, NULL, errfunc, oci_error);
647       DEBUG("oracle plugin: OCILogon (%s): db->oci_service_context = %p;",
648             db->connect_id, db->oci_service_context);
649       db->oci_service_context = NULL;
650       return -1;
651     } else if (status == OCI_SUCCESS_WITH_INFO) {
652       /* TODO: Print NOTIFY message. */
653     }
654     assert(db->oci_service_context != NULL);
655   }
656
657   DEBUG("oracle plugin: o_read_database: db->connect_id = %s; "
658         "db->oci_service_context = %p;",
659         db->connect_id, db->oci_service_context);
660
661   for (size_t i = 0; i < db->queries_num; i++)
662     o_read_database_query(db, db->queries[i], db->q_prep_areas[i]);
663
664   return 0;
665 } /* }}} int o_read_database */
666
667 static int o_read(void) /* {{{ */
668 {
669   size_t i;
670
671   for (i = 0; i < databases_num; i++)
672     o_read_database(databases[i]);
673
674   return 0;
675 } /* }}} int o_read */
676
677 static int o_shutdown(void) /* {{{ */
678 {
679   size_t i;
680
681   for (i = 0; i < databases_num; i++)
682     if (databases[i]->oci_service_context != NULL) {
683       OCIHandleFree(databases[i]->oci_service_context, OCI_HTYPE_SVCCTX);
684       databases[i]->oci_service_context = NULL;
685     }
686
687   for (i = 0; i < queries_num; i++) {
688     OCIStmt *oci_statement;
689
690     oci_statement = udb_query_get_user_data(queries[i]);
691     if (oci_statement != NULL) {
692       OCIHandleFree(oci_statement, OCI_HTYPE_STMT);
693       udb_query_set_user_data(queries[i], NULL);
694     }
695   }
696
697   OCIHandleFree(oci_env, OCI_HTYPE_ENV);
698   oci_env = NULL;
699
700   udb_query_free(queries, queries_num);
701   queries = NULL;
702   queries_num = 0;
703
704   return 0;
705 } /* }}} int o_shutdown */
706
707 void module_register(void) /* {{{ */
708 {
709   plugin_register_complex_config("oracle", o_config);
710   plugin_register_init("oracle", o_init);
711   plugin_register_read("oracle", o_read);
712   plugin_register_shutdown("oracle", o_shutdown);
713 } /* }}} void module_register */