Merge branch 'collectd-5.7' into collectd-5.8
[collectd.git] / src / dbi.c
index 6043fe6..62ef1dc 100644 (file)
--- a/src/dbi.c
+++ b/src/dbi.c
@@ -62,6 +62,7 @@ struct cdbi_database_s /* {{{ */
 {
   char *name;
   char *select_db;
+  char *plugin_name;
 
   cdtime_t interval;
 
@@ -101,18 +102,18 @@ static const char *cdbi_strerror(dbi_conn conn, /* {{{ */
 
   if (conn == NULL) {
     sstrncpy(buffer, "connection is NULL", buffer_size);
-    return (buffer);
+    return buffer;
   }
 
   msg = NULL;
   status = dbi_conn_error(conn, &msg);
   if ((status >= 0) && (msg != NULL))
-    ssnprintf(buffer, buffer_size, "%s (status %i)", msg, status);
+    snprintf(buffer, buffer_size, "%s (status %i)", msg, status);
   else
-    ssnprintf(buffer, buffer_size, "dbi_conn_error failed with status %i",
-              status);
+    snprintf(buffer, buffer_size, "dbi_conn_error failed with status %i",
+             status);
 
-  return (buffer);
+  return buffer;
 } /* }}} const char *cdbi_conn_error */
 
 static int cdbi_result_get_field(dbi_result res, /* {{{ */
@@ -124,19 +125,19 @@ static int cdbi_result_get_field(dbi_result res, /* {{{ */
   if (src_type == DBI_TYPE_ERROR) {
     ERROR("dbi plugin: cdbi_result_get: "
           "dbi_result_get_field_type_idx failed.");
-    return (-1);
+    return -1;
   }
 
   if (src_type == DBI_TYPE_INTEGER) {
     long long value;
 
     value = dbi_result_get_longlong_idx(res, index);
-    ssnprintf(buffer, buffer_size, "%lli", value);
+    snprintf(buffer, buffer_size, "%lli", value);
   } else if (src_type == DBI_TYPE_DECIMAL) {
     double value;
 
     value = dbi_result_get_double_idx(res, index);
-    ssnprintf(buffer, buffer_size, "%63.15g", value);
+    snprintf(buffer, buffer_size, "%63.15g", value);
   } else if (src_type == DBI_TYPE_STRING) {
     const char *value;
 
@@ -144,7 +145,7 @@ static int cdbi_result_get_field(dbi_result res, /* {{{ */
     if (value == NULL)
       sstrncpy(buffer, "", buffer_size);
     else if (strcmp("ERROR", value) == 0)
-      return (-1);
+      return -1;
     else
       sstrncpy(buffer, value, buffer_size);
   }
@@ -160,10 +161,10 @@ static int cdbi_result_get_field(dbi_result res, /* {{{ */
     ERROR("dbi plugin: Column `%s': Don't know how to handle "
           "source type %hu.",
           field_name, src_type);
-    return (-1);
+    return -1;
   }
 
-  return (0);
+  return 0;
 } /* }}} int cdbi_result_get_field */
 
 static void cdbi_database_free(cdbi_database_t *db) /* {{{ */
@@ -173,6 +174,7 @@ static void cdbi_database_free(cdbi_database_t *db) /* {{{ */
 
   sfree(db->name);
   sfree(db->select_db);
+  sfree(db->plugin_name);
   sfree(db->driver);
   sfree(db->host);
 
@@ -226,14 +228,14 @@ static int cdbi_config_add_database_driver_option(cdbi_database_t *db, /* {{{ */
        (ci->values[1].type != OCONFIG_TYPE_NUMBER))) {
     WARNING("dbi plugin: The `DriverOption' config option "
             "needs exactly two arguments.");
-    return (-1);
+    return -1;
   }
 
   option = realloc(db->driver_options,
                    sizeof(*option) * (db->driver_options_num + 1));
   if (option == NULL) {
     ERROR("dbi plugin: realloc failed");
-    return (-1);
+    return -1;
   }
 
   db->driver_options = option;
@@ -243,7 +245,7 @@ static int cdbi_config_add_database_driver_option(cdbi_database_t *db, /* {{{ */
   option->key = strdup(ci->values[0].value.string);
   if (option->key == NULL) {
     ERROR("dbi plugin: strdup failed.");
-    return (-1);
+    return -1;
   }
 
   if (ci->values[1].type == OCONFIG_TYPE_STRING) {
@@ -251,7 +253,7 @@ static int cdbi_config_add_database_driver_option(cdbi_database_t *db, /* {{{ */
     if (option->value.string == NULL) {
       ERROR("dbi plugin: strdup failed.");
       sfree(option->key);
-      return (-1);
+      return -1;
     }
   } else {
     assert(ci->values[1].type == OCONFIG_TYPE_NUMBER);
@@ -260,7 +262,7 @@ static int cdbi_config_add_database_driver_option(cdbi_database_t *db, /* {{{ */
   }
 
   db->driver_options_num++;
-  return (0);
+  return 0;
 } /* }}} int cdbi_config_add_database_driver_option */
 
 static int cdbi_config_add_database(oconfig_item_t *ci) /* {{{ */
@@ -271,19 +273,19 @@ static int cdbi_config_add_database(oconfig_item_t *ci) /* {{{ */
   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
     WARNING("dbi plugin: The `Database' block "
             "needs exactly one string argument.");
-    return (-1);
+    return -1;
   }
 
   db = calloc(1, sizeof(*db));
   if (db == NULL) {
     ERROR("dbi plugin: calloc failed.");
-    return (-1);
+    return -1;
   }
 
   status = cf_util_get_string(ci, &db->name);
   if (status != 0) {
     sfree(db);
-    return (status);
+    return status;
   }
 
   /* Fill the `cdbi_database_t' structure.. */
@@ -303,6 +305,8 @@ static int cdbi_config_add_database(oconfig_item_t *ci) /* {{{ */
       status = cf_util_get_string(child, &db->host);
     else if (strcasecmp("Interval", child->key) == 0)
       status = cf_util_get_cdtime(child, &db->interval);
+    else if (strcasecmp("Plugin", child->key) == 0)
+      status = cf_util_get_string(child, &db->plugin_name);
     else {
       WARNING("dbi plugin: Option `%s' not allowed here.", child->key);
       status = -1;
@@ -357,32 +361,29 @@ static int cdbi_config_add_database(oconfig_item_t *ci) /* {{{ */
       ERROR("dbi plugin: realloc failed");
       status = -1;
     } else {
-      char *name = NULL;
-
       databases = temp;
       databases[databases_num] = db;
       databases_num++;
 
-      name = ssnprintf_alloc("dbi:%s", db->name);
-
-      user_data_t ud = {.data = db};
-
+      char *name = ssnprintf_alloc("dbi:%s", db->name);
       plugin_register_complex_read(
           /* group = */ NULL,
           /* name = */ name ? name : db->name,
           /* callback = */ cdbi_read_database,
           /* interval = */ (db->interval > 0) ? db->interval : 0,
-          /* user_data = */ &ud);
-      free(name);
+          &(user_data_t){
+              .data = db,
+          });
+      sfree(name);
     }
   }
 
   if (status != 0) {
     cdbi_database_free(db);
-    return (-1);
+    return -1;
   }
 
-  return (0);
+  return 0;
 } /* }}} int cdbi_config_add_database */
 
 static int cdbi_config(oconfig_item_t *ci) /* {{{ */
@@ -399,7 +400,7 @@ static int cdbi_config(oconfig_item_t *ci) /* {{{ */
     }
   } /* for (ci->children) */
 
-  return (0);
+  return 0;
 } /* }}} int cdbi_config */
 
 /* }}} End of configuration handling functions */
@@ -410,34 +411,34 @@ static int cdbi_init(void) /* {{{ */
   int status;
 
   if (did_init != 0)
-    return (0);
+    return 0;
 
   if (queries_num == 0) {
     ERROR("dbi plugin: No <Query> blocks have been found. Without them, "
-          "this plugin can't do anything useful, so we will returns an error.");
-    return (-1);
+          "this plugin can't do anything useful, so we will return an error.");
+    return -1;
   }
 
   if (databases_num == 0) {
     ERROR("dbi plugin: No <Database> blocks have been found. Without them, "
-          "this plugin can't do anything useful, so we will returns an error.");
-    return (-1);
+          "this plugin can't do anything useful, so we will return an error.");
+    return -1;
   }
 
   status = dbi_initialize_r(/* driverdir = */ NULL, &dbi_instance);
   if (status < 0) {
     ERROR("dbi plugin: cdbi_init: dbi_initialize_r failed with status %i.",
           status);
-    return (-1);
+    return -1;
   } else if (status == 0) {
     ERROR("dbi plugin: `dbi_initialize_r' could not load any drivers. Please "
           "install at least one `DBD' or check your installation.");
-    return (-1);
+    return -1;
   }
   DEBUG("dbi plugin: cdbi_init: dbi_initialize_r reports %i driver%s.", status,
         (status == 1) ? "" : "s");
 
-  return (0);
+  return 0;
 } /* }}} int cdbi_init */
 
 static int cdbi_read_database_query(cdbi_database_t *db, /* {{{ */
@@ -548,7 +549,8 @@ static int cdbi_read_database_query(cdbi_database_t *db, /* {{{ */
 
   udb_query_prepare_result(
       q, prep_area, (db->host ? db->host : hostname_g),
-      /* plugin = */ "dbi", db->name, column_names, column_num,
+      /* plugin = */ (db->plugin_name != NULL) ? db->plugin_name : "dbi",
+      db->name, column_names, column_num,
       /* interval = */ (db->interval > 0) ? db->interval : 0);
 
   /* 0 = error; 1 = success; */
@@ -627,7 +629,7 @@ static int cdbi_connect_database(cdbi_database_t *db) /* {{{ */
   if (db->connection != NULL) {
     status = dbi_conn_ping(db->connection);
     if (status != 0) /* connection is alive */
-      return (0);
+      return 0;
 
     dbi_conn_close(db->connection);
     db->connection = NULL;
@@ -643,14 +645,14 @@ static int cdbi_connect_database(cdbi_database_t *db) /* {{{ */
          driver = dbi_driver_list_r(driver, dbi_instance)) {
       INFO("dbi plugin: * %s", dbi_driver_get_name(driver));
     }
-    return (-1);
+    return -1;
   }
 
   connection = dbi_conn_open(driver);
   if (connection == NULL) {
     ERROR("dbi plugin: cdbi_connect_database: dbi_conn_open (%s) failed.",
           db->driver);
-    return (-1);
+    return -1;
   }
 
   /* Set all the driver options. Because this is a very very very generic
@@ -694,7 +696,7 @@ static int cdbi_connect_database(cdbi_database_t *db) /* {{{ */
       }
 
       dbi_conn_close(connection);
-      return (-1);
+      return -1;
     }
   } /* for (i = 0; i < db->driver_options_num; i++) */
 
@@ -705,7 +707,7 @@ static int cdbi_connect_database(cdbi_database_t *db) /* {{{ */
           "dbi_conn_connect failed: %s",
           db->name, cdbi_strerror(connection, errbuf, sizeof(errbuf)));
     dbi_conn_close(connection);
-    return (-1);
+    return -1;
   }
 
   if (db->select_db != NULL) {
@@ -718,12 +720,12 @@ static int cdbi_connect_database(cdbi_database_t *db) /* {{{ */
           db->name, db->select_db,
           cdbi_strerror(connection, errbuf, sizeof(errbuf)));
       dbi_conn_close(connection);
-      return (-1);
+      return -1;
     }
   }
 
   db->connection = connection;
-  return (0);
+  return 0;
 } /* }}} int cdbi_connect_database */
 
 static int cdbi_read_database(user_data_t *ud) /* {{{ */
@@ -736,7 +738,7 @@ static int cdbi_read_database(user_data_t *ud) /* {{{ */
 
   status = cdbi_connect_database(db);
   if (status != 0)
-    return (status);
+    return status;
   assert(db->connection != NULL);
 
   db_version = dbi_conn_get_engine_version(db->connection);
@@ -757,10 +759,10 @@ static int cdbi_read_database(user_data_t *ud) /* {{{ */
 
   if (success == 0) {
     ERROR("dbi plugin: All queries failed for database `%s'.", db->name);
-    return (-1);
+    return -1;
   }
 
-  return (0);
+  return 0;
 } /* }}} int cdbi_read_database */
 
 static int cdbi_shutdown(void) /* {{{ */
@@ -779,7 +781,7 @@ static int cdbi_shutdown(void) /* {{{ */
   queries = NULL;
   queries_num = 0;
 
-  return (0);
+  return 0;
 } /* }}} int cdbi_shutdown */
 
 void module_register(void) /* {{{ */
@@ -788,7 +790,3 @@ void module_register(void) /* {{{ */
   plugin_register_init("dbi", cdbi_init);
   plugin_register_shutdown("dbi", cdbi_shutdown);
 } /* }}} void module_register */
-
-/*
- * vim: shiftwidth=2 softtabstop=2 et fdm=marker
- */