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