curl_xml: Add ability to set custom plugin name in collected data.
[collectd.git] / src / curl_xml.c
index a0dca05..074a2d1 100644 (file)
@@ -71,6 +71,7 @@ typedef struct cx_namespace_s cx_namespace_t;
 struct cx_s /* {{{ */
 {
   char *instance;
+  char *plugin_name;
   char *host;
 
   char *url;
@@ -188,6 +189,7 @@ static void cx_free(void *arg) /* {{{ */
 
   sfree(db->buffer);
   sfree(db->instance);
+  sfree(db->plugin_name);
   sfree(db->host);
 
   sfree(db->url);
@@ -208,7 +210,7 @@ static void cx_free(void *arg) /* {{{ */
   sfree(db);
 } /* }}} void cx_free */
 
-static const char *cx_host(cx_t *db) /* {{{ */
+static const char *cx_host(const cx_t *db) /* {{{ */
 {
   if (db->host == NULL)
     return hostname_g;
@@ -369,7 +371,7 @@ static int cx_handle_all_value_xpaths(xmlXPathContextPtr xpath_ctx, /* {{{ */
     status = cx_handle_single_value_xpath(xpath_ctx, xpath, ds, vl, i);
     if (status != 0)
       return -1; /* An error has been printed. */
-  }                /* for (i = 0; i < xpath->values_len; i++) */
+  }              /* for (i = 0; i < xpath->values_len; i++) */
 
   plugin_dispatch_values(vl);
   vl->values = NULL;
@@ -441,8 +443,8 @@ static int cx_handle_instance_xpath(xmlXPathContextPtr xpath_ctx, /* {{{ */
   if (xpath->instance_prefix != NULL) {
     if (instance_node != NULL) {
       char *node_value = (char *)xmlNodeGetContent(instance_node->nodeTab[0]);
-      ssnprintf(vl->type_instance, sizeof(vl->type_instance), "%s%s",
-                xpath->instance_prefix, node_value);
+      snprintf(vl->type_instance, sizeof(vl->type_instance), "%s%s",
+               xpath->instance_prefix, node_value);
       sfree(node_value);
     } else
       sstrncpy(vl->type_instance, xpath->instance_prefix,
@@ -464,8 +466,8 @@ static int cx_handle_instance_xpath(xmlXPathContextPtr xpath_ctx, /* {{{ */
   return 0;
 } /* }}} int cx_handle_instance_xpath */
 
-static int cx_handle_base_xpath(char const *plugin_instance, /* {{{ */
-                                char const *host, xmlXPathContextPtr xpath_ctx,
+static int cx_handle_base_xpath(const cx_t *db, /* {{{ */
+                                xmlXPathContextPtr xpath_ctx,
                                 const data_set_t *ds, char *base_xpath,
                                 cx_xpath_t *xpath) {
   int total_nodes;
@@ -505,10 +507,11 @@ static int cx_handle_base_xpath(char const *plugin_instance, /* {{{ */
   /* set the values for the value_list */
   vl.values_len = ds->ds_num;
   sstrncpy(vl.type, xpath->type, sizeof(vl.type));
-  sstrncpy(vl.plugin, "curl_xml", sizeof(vl.plugin));
-  sstrncpy(vl.host, host, sizeof(vl.host));
-  if (plugin_instance != NULL)
-    sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
+  sstrncpy(vl.plugin, (db->plugin_name != NULL) ? db->plugin_name : "curl_xml",
+           sizeof(vl.plugin));
+  sstrncpy(vl.host, cx_host(db), sizeof(vl.host));
+  if (db->instance != NULL)
+    sstrncpy(vl.plugin_instance, db->instance, sizeof(vl.plugin_instance));
 
   for (int i = 0; i < total_nodes; i++) {
     int status;
@@ -545,8 +548,7 @@ static int cx_handle_parsed_xml(xmlDocPtr doc, /* {{{ */
     ds = plugin_get_ds(xpath->type);
 
     if ((cx_check_type(ds, xpath) == 0) &&
-        (cx_handle_base_xpath(db->instance, cx_host(db), xpath_ctx, ds, le->key,
-                              xpath) == 0))
+        (cx_handle_base_xpath(db, xpath_ctx, ds, le->key, xpath) == 0))
       status = 0; /* we got atleast one success */
 
     le = le->next;
@@ -602,13 +604,15 @@ static int cx_curl_perform(cx_t *db, CURL *curl) /* {{{ */
   long rc;
   char *ptr;
   char *url;
-  url = db->url;
 
   db->buffer_fill = 0;
+
+  curl_easy_setopt(db->curl, CURLOPT_URL, db->url);
+
   status = curl_easy_perform(curl);
   if (status != CURLE_OK) {
     ERROR("curl_xml plugin: curl_easy_perform failed with status %i: %s (%s)",
-          status, db->curl_errbuf, url);
+          status, db->curl_errbuf, db->url);
     return -1;
   }
   if (db->stats != NULL)
@@ -817,7 +821,6 @@ static int cx_init_curl(cx_t *db) /* {{{ */
   curl_easy_setopt(db->curl, CURLOPT_WRITEDATA, db);
   curl_easy_setopt(db->curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
   curl_easy_setopt(db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf);
-  curl_easy_setopt(db->curl, CURLOPT_URL, db->url);
   curl_easy_setopt(db->curl, CURLOPT_FOLLOWLOCATION, 1L);
   curl_easy_setopt(db->curl, CURLOPT_MAXREDIRS, 50L);
 
@@ -839,8 +842,8 @@ static int cx_init_curl(cx_t *db) /* {{{ */
       return -1;
     }
 
-    ssnprintf(db->credentials, credentials_size, "%s:%s", db->user,
-              (db->pass == NULL) ? "" : db->pass);
+    snprintf(db->credentials, credentials_size, "%s:%s", db->user,
+             (db->pass == NULL) ? "" : db->pass);
     curl_easy_setopt(db->curl, CURLOPT_USERPWD, db->credentials);
 #endif
 
@@ -907,6 +910,8 @@ static int cx_config_add_url(oconfig_item_t *ci) /* {{{ */
 
     if (strcasecmp("Instance", child->key) == 0)
       status = cf_util_get_string(child, &db->instance);
+    else if (strcasecmp("PluginName", child->key) == 0)
+      status = cf_util_get_string(child, &db->plugin_name);
     else if (strcasecmp("Host", child->key) == 0)
       status = cf_util_get_string(child, &db->host);
     else if (strcasecmp("User", child->key) == 0)