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