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