summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 1d33324)
raw | patch | inline | side by side (parent: 1d33324)
author | Florian Forster <octo@huhu.verplant.org> | |
Mon, 11 Jan 2010 14:06:57 +0000 (15:06 +0100) | ||
committer | Florian Forster <octo@huhu.verplant.org> | |
Mon, 11 Jan 2010 14:06:57 +0000 (15:06 +0100) |
src/curl_xml.c | patch | blob | history |
diff --git a/src/curl_xml.c b/src/curl_xml.c
index 219c5349d4cd46953340d18ecbc94033b132567d..53c6e620dd881a34d4e9c2d86ab5468bd8ae942f 100644 (file)
--- a/src/curl_xml.c
+++ b/src/curl_xml.c
#define CX_KEY_MAGIC 0x43484b59UL /* CHKY */
#define CX_IS_KEY(key) (key)->magic == CX_KEY_MAGIC
+/*
+ * Private data structures
+ */
struct cx_values_s /* {{{ */
{
char path[DATA_MAX_NAME_LEN];
};
typedef struct cx_s cx_t; /* }}} */
-static int cx_read (user_data_t *ud);
-static int cx_curl_perform (cx_t *db, CURL *curl);
-static int cx_parse_stats_xml (xmlChar *ptr, cx_t *db);
-static int cx_submit_statistics(xmlDocPtr doc, xmlXPathContextPtr xpath_ctx,
- cx_t *db);
-static int cx_submit_xpath_values (char *plugin_instance, xmlXPathContextPtr xpath_ctx,
- char *base_xpath, cx_xpath_t *xpath);
-static int cx_if_not_text_node (xmlNodePtr node);
-static xmlXPathObjectPtr cx_evaluate_xpath (xmlXPathContextPtr xpath_ctx,
- xmlChar *expr);
-static int cx_check_type (cx_xpath_t *xpath);
-
+/*
+ * Private functions
+ */
static size_t cx_curl_callback (void *buf, /* {{{ */
size_t size, size_t nmemb, void *user_data)
{
sfree (db);
} /* }}} void cx_free */
-/* Configuration handling functions {{{ */
-
-static int cx_config_add_values (const char *name, cx_xpath_t *xpath, /* {{{ */
- oconfig_item_t *ci)
+static int cx_check_type (cx_xpath_t *xpath) /* {{{ */
{
- int i;
-
- if (ci->values_num < 1)
+ const data_set_t *ds;
+
+ ds = plugin_get_ds (xpath->type);
+ if (!ds)
{
- WARNING ("curl_xml plugin: `Values' needs at least one argument.");
- return (-1);
- }
-
- for (i = 0; i < ci->values_num; i++)
- if (ci->values[i].type != OCONFIG_TYPE_STRING)
- {
- WARNING ("curl_xml plugin: `Values' needs only string argument.");
- return (-1);
- }
-
- sfree (xpath->values);
-
- xpath->values_len = 0;
- xpath->values = (cx_values_t *) malloc (sizeof (cx_values_t) * ci->values_num);
- if (xpath->values == NULL)
+ WARNING ("curl_xml plugin: DataSet `%s' not defined.", xpath->type);
return (-1);
- xpath->values_len = ci->values_num;
-
- /* populate cx_values_t structure */
- for (i = 0; i < ci->values_num; i++)
- {
- xpath->values[i].path_len = sizeof (ci->values[i].value.string);
- sstrncpy (xpath->values[i].path, ci->values[i].value.string, sizeof (xpath->values[i].path));
}
- return (0);
-} /* }}} cx_config_add_values */
-
-static int cx_config_add_string (const char *name, char **dest, /* {{{ */
- oconfig_item_t *ci)
-{
- if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
+ if (ds->ds_num != xpath->values_len)
{
- WARNING ("curl_xml plugin: `%s' needs exactly one string argument.", name);
+ WARNING ("curl_xml plugin: DataSet `%s' requires %i values, but config talks about %i",
+ xpath->type, ds->ds_num, xpath->values_len);
return (-1);
}
- sfree (*dest);
- *dest = strdup (ci->values[0].value.string);
- if (*dest == NULL)
- return (-1);
-
return (0);
-} /* }}} int cx_config_add_string */
+} /* }}} cx_check_type */
-static int cx_config_set_boolean (const char *name, int *dest, /* {{{ */
- oconfig_item_t *ci)
+static xmlXPathObjectPtr cx_evaluate_xpath (xmlXPathContextPtr xpath_ctx, /* {{{ */
+ xmlChar *expr)
{
- if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
+ xmlXPathObjectPtr xpath_obj;
+
+ // XXX: When to free this?
+ xpath_obj = xmlXPathEvalExpression(BAD_CAST expr, xpath_ctx);
+ if (xpath_obj == NULL)
{
- WARNING ("curl_xml plugin: `%s' needs exactly one boolean argument.", name);
- return (-1);
+ WARNING ("curl_xml plugin: "
+ "Error unable to evaluate xpath expression \"%s\". Skipping...", expr);
+ return NULL;
}
- *dest = ci->values[0].value.boolean ? 1 : 0;
-
- return (0);
-} /* }}} int cx_config_set_boolean */
+ return xpath_obj;
+} /* }}} cx_evaluate_xpath */
-static c_avl_tree_t *cx_avl_create(void) /* {{{ */
+static int cx_if_not_text_node (xmlNodePtr node) /* {{{ */
{
- return c_avl_create ((int (*) (const void *, const void *)) strcmp);
-} /* }}} cx_avl_create */
+ if (node->type == XML_TEXT_NODE || node->type == XML_ATTRIBUTE_NODE)
+ return (0);
-static int cx_config_add_xpath (cx_t *db, /* {{{ */
- oconfig_item_t *ci)
+ WARNING ("curl_xml plugin: "
+ "Node \"%s\" doesn't seem to be a text node. Skipping...", node->name);
+ return -1;
+} /* }}} cx_if_not_text_node */
+
+static int cx_submit_xpath_values (char *plugin_instance, /* {{{ */
+ xmlXPathContextPtr xpath_ctx,
+ char *base_xpath, cx_xpath_t *xpath)
{
- cx_xpath_t *xpath;
- int status;
int i;
+ int j;
+ int total_nodes;
+ int tmp_size;
+ int status=-1;
+ char *node_value;
- if ((ci->values_num != 1)
- || (ci->values[0].type != OCONFIG_TYPE_STRING))
- {
- WARNING ("curl_xml plugin: The `xpath' block "
- "needs exactly one string argument.");
- return (-1);
- }
-
- xpath = (cx_xpath_t *) malloc (sizeof (*xpath));
- if (xpath == NULL)
- {
- ERROR ("curl_xml plugin: malloc failed.");
- return (-1);
- }
- memset (xpath, 0, sizeof (*xpath));
- xpath->magic = CX_KEY_MAGIC;
-
- if (strcasecmp ("xpath", ci->key) == 0)
- {
- status = cx_config_add_string ("xpath", &xpath->path, ci);
- if (status != 0)
- {
- sfree (xpath);
- return (status);
- }
- }
- else
- {
- ERROR ("curl_xml plugin: cx_config: "
- "Invalid key: %s", ci->key);
- return (-1);
- }
-
- status = 0;
- for (i = 0; i < ci->children_num; i++)
- {
- oconfig_item_t *child = ci->children + i;
-
- if (strcasecmp ("Type", child->key) == 0)
- status = cx_config_add_string ("Type", &xpath->type, child);
- else if (strcasecmp ("InstancePrefix", child->key) == 0)
- status = cx_config_add_string ("InstancePrefix", &xpath->instance_prefix, child);
- else if (strcasecmp ("Instance", child->key) == 0)
- status = cx_config_add_string ("Instance", &xpath->instance, child);
- else if (strcasecmp ("Values", child->key) == 0)
- status = cx_config_add_values ("Values", xpath, child);
- else
- {
- WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
- status = -1;
- }
+ xmlXPathObjectPtr base_node_obj = NULL;
+ xmlXPathObjectPtr instance_node_obj = NULL;
+ xmlXPathObjectPtr values_node_obj = NULL;
+ xmlNodeSetPtr base_nodes = NULL;
+ xmlNodeSetPtr instance_node = NULL;
+ xmlNodeSetPtr values_node = NULL;
- if (status != 0)
- break;
- } /* for (i = 0; i < ci->children_num; i++) */
+ value_list_t vl = VALUE_LIST_INIT;
+ const data_set_t *ds;
- while (status == 0)
- {
- if (xpath->type == NULL)
- {
- WARNING ("curl_xml plugin: `Type' missing in `xpath' block.");
- status = -1;
- }
+ base_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST base_xpath);
+ if (base_node_obj == NULL)
+ return -1; /* error is logged already */
- break;
- } /* while (status == 0) */
+ base_nodes = base_node_obj->nodesetval;
+ total_nodes = (base_nodes) ? base_nodes->nodeNr : 0;
- if (status == 0)
+ if (total_nodes == 0)
{
- char *name;
- c_avl_tree_t *tree;
-
- if (db->tree == NULL)
- db->tree = cx_avl_create();
-
- tree = db->tree;
- name = xpath->path;
-
- if (*name)
- c_avl_insert (tree, strdup(name), xpath);
- else
- {
- ERROR ("curl_xml plugin: invalid key: %s", xpath->path);
- status = -1;
- }
+ ERROR ("curl_xml plugin: "
+ "xpath expression \"%s\" doesn't match any of the node. Skipping...", base_xpath);
+ xmlXPathFreeObject (base_node_obj);
+ return -1;
}
- return (status);
-} /* }}} int cx_config_add_xpath */
-
-static int cx_init_curl (cx_t *db) /* {{{ */
-{
- db->curl = curl_easy_init ();
- if (db->curl == NULL)
+ /* If base_xpath returned multiple results, then */
+ /* Instance in the xpath block is required */
+ if (total_nodes > 1 && xpath->instance == NULL)
{
- ERROR ("curl_xml plugin: curl_easy_init failed.");
- return (-1);
+ ERROR ("curl_xml plugin: "
+ "Instance is must in xpath block since the base xpath expression \"%s\" "
+ "returned multiple results. Skipping the xpath block...", base_xpath);
+ return -1;
}
- curl_easy_setopt (db->curl, CURLOPT_WRITEFUNCTION, cx_curl_callback);
- curl_easy_setopt (db->curl, CURLOPT_WRITEDATA, db);
- curl_easy_setopt (db->curl, CURLOPT_USERAGENT,
- PACKAGE_NAME"/"PACKAGE_VERSION);
- curl_easy_setopt (db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf);
- curl_easy_setopt (db->curl, CURLOPT_URL, db->url);
+ /* set the values for the value_list */
+ ds = plugin_get_ds (xpath->type);
+ 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, hostname_g, sizeof (vl.host));
+ if (plugin_instance != NULL)
+ sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
- if (db->user != NULL)
+ for (i = 0; i < total_nodes; i++)
{
- size_t credentials_size;
+ xpath_ctx->node = base_nodes->nodeTab[i];
- credentials_size = strlen (db->user) + 2;
- if (db->pass != NULL)
- credentials_size += strlen (db->pass);
+ /* instance has to be an xpath expression */
+ if (xpath->instance != NULL)
+ {
+ instance_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST xpath->instance);
+ if (instance_node_obj == NULL)
+ continue; /* error is logged already */
- db->credentials = (char *) malloc (credentials_size);
- if (db->credentials == NULL)
- {
- ERROR ("curl_xml plugin: malloc failed.");
- return (-1);
- }
+ instance_node = instance_node_obj->nodesetval;
+ tmp_size = (instance_node) ? instance_node->nodeNr : 0;
- ssnprintf (db->credentials, credentials_size, "%s:%s",
- db->user, (db->pass == NULL) ? "" : db->pass);
- curl_easy_setopt (db->curl, CURLOPT_USERPWD, db->credentials);
- }
+ if ( (tmp_size == 0) && (total_nodes > 1) )
+ {
+ WARNING ("curl_xml plugin: "
+ "relative xpath expression for 'Instance' \"%s\" doesn't match "
+ "any of the nodes. Skipping the node - %s",
+ xpath->instance, base_nodes->nodeTab[i]->name);
+ xmlXPathFreeObject (instance_node_obj);
+ continue;
+ }
- curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYPEER, db->verify_peer);
- curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYHOST,
- db->verify_host ? 2 : 0);
- if (db->cacert != NULL)
- curl_easy_setopt (db->curl, CURLOPT_CAINFO, db->cacert);
+ if (tmp_size > 1)
+ {
+ WARNING ("curl_xml plugin: "
+ "relative xpath expression for 'Instance' \"%s\" is expected "
+ "to return only one text node. Skipping the node - %s",
+ xpath->instance, base_nodes->nodeTab[i]->name);
+ xmlXPathFreeObject (instance_node_obj);
+ continue;
+ }
- return (0);
-} /* }}} int cx_init_curl */
+ // ignoring the element if other than textnode/attribute
+ if (cx_if_not_text_node(instance_node->nodeTab[0]))
+ {
+ WARNING ("curl_xml plugin: "
+ "relative xpath expression \"%s\" is expected to return only text node "
+ "which is not the case. Skipping the node - %s",
+ xpath->instance, base_nodes->nodeTab[i]->name);
+ xmlXPathFreeObject (instance_node_obj);
+ continue;
+ }
+ }
-static int cx_config_add_url (oconfig_item_t *ci) /* {{{ */
-{
- cx_t *db;
- int status = 0;
- int i;
+ for (j = 0; j < xpath->values_len; j++)
+ {
+ values_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST xpath->values[j].path);
+ values_node = values_node_obj->nodesetval;
+ tmp_size = (values_node) ? values_node->nodeNr : 0;
- if ((ci->values_num != 1)
- || (ci->values[0].type != OCONFIG_TYPE_STRING))
- {
- WARNING ("curl_xml plugin: The `URL' block "
- "needs exactly one string argument.");
- return (-1);
+ if (tmp_size == 0)
+ {
+ WARNING ("curl_xml plugin: "
+ "relative xpath expression \"%s\" doesn't match any of the nodes. "
+ "Skipping...", xpath->values[j].path);
+ xmlXPathFreeObject (values_node_obj);
+ continue;
+ }
+
+ if (tmp_size > 1)
+ {
+ WARNING ("curl_xml plugin: "
+ "relative xpath expression \"%s\" is expected to return "
+ "only one node. Skipping...", xpath->values[j].path);
+ xmlXPathFreeObject (values_node_obj);
+ continue;
+ }
+
+ /* ignoring the element if other than textnode/attribute*/
+ if (cx_if_not_text_node(values_node->nodeTab[0]))
+ {
+ WARNING ("curl_xml plugin: "
+ "relative xpath expression \"%s\" is expected to return "
+ "only text/attribute node which is not the case. Skipping...",
+ xpath->values[j].path);
+ xmlXPathFreeObject (values_node_obj);
+ continue;
+ }
+
+ vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
+ if (vl.values == NULL)
+ {
+ ERROR ("curl_xml plugin: malloc failed.");
+ xmlXPathFreeObject (base_node_obj);
+ xmlXPathFreeObject (instance_node_obj);
+ xmlXPathFreeObject (values_node_obj);
+ return (-1);
+ }
+
+ node_value = (char *) xmlNodeGetContent(values_node->nodeTab[0]);
+ switch (ds->ds[j].type)
+ {
+ case DS_TYPE_COUNTER:
+ vl.values[j].counter = atoi(node_value);
+ break;
+ case DS_TYPE_DERIVE:
+ vl.values[j].derive = atoi(node_value);
+ break;
+ case DS_TYPE_ABSOLUTE:
+ vl.values[j].absolute = atoi(node_value);
+ break;
+ case DS_TYPE_GAUGE:
+ vl.values[j].absolute = atoi(node_value);
+ }
+
+ if (xpath->instance_prefix != NULL)
+ {
+ if (instance_node != NULL)
+ ssnprintf (vl.type_instance, sizeof (vl.type_instance),"%s-%s",
+ xpath->instance_prefix, (char *) xmlNodeGetContent(instance_node->nodeTab[0]));
+ else
+ sstrncpy (vl.type_instance, xpath->instance_prefix,
+ sizeof (vl.type_instance));
+ }
+ else
+ {
+ /* If instance_prefix and instance_node are NULL , then */
+ /* don't set the type_instance */
+ if (instance_node != NULL)
+ sstrncpy (vl.type_instance, (char *) xmlNodeGetContent(instance_node->nodeTab[0]),
+ sizeof (vl.type_instance));
+ }
+
+ /* free up object */
+ xmlXPathFreeObject (values_node_obj);
+
+ /* We have reached here which means that */
+ /* we have got something to work */
+ status = 0;
+ }
+
+ /* submit the values */
+ if (vl.values)
+ plugin_dispatch_values (&vl);
+
+ sfree(vl.values);
+ if (instance_node_obj != NULL)
+ xmlXPathFreeObject (instance_node_obj);
}
- db = (cx_t *) malloc (sizeof (*db));
- if (db == NULL)
+ /* free up the allocated memory */
+ xmlXPathFreeObject (base_node_obj);
+
+ return status;
+} /* }}} cx_submit_xpath_values */
+
+static int cx_submit_statistics(xmlDocPtr doc, /* {{{ */
+ xmlXPathContextPtr xpath_ctx, cx_t *db)
+{
+ c_avl_iterator_t *iter;
+ char *key;
+ cx_xpath_t *value;
+ int status=-1;
+
+ iter = c_avl_get_iterator (db->tree);
+ while (c_avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
{
- ERROR ("curl_xml plugin: malloc failed.");
- return (-1);
- }
- memset (db, 0, sizeof (*db));
+ if (cx_check_type(value) == -1)
+ continue;
- if (strcasecmp ("URL", ci->key) == 0)
+ if (cx_submit_xpath_values(db->instance, xpath_ctx, key, value) == 0)
+ status = 0; /* we got atleast one success */
+ } /* while (c_avl_iterator_next) */
+
+ return status;
+} /* }}} cx_submit_statistics */
+
+static int cx_parse_stats_xml(xmlChar* xml, cx_t *db) /* {{{ */
+{
+ int status;
+ xmlDocPtr doc;
+ xmlXPathContextPtr xpath_ctx;
+
+ /* Load the XML */
+ doc = xmlParseDoc(xml);
+ if (doc == NULL)
{
- status = cx_config_add_string ("URL", &db->url, ci);
- if (status != 0)
- {
- sfree (db);
- return (status);
- }
+ ERROR ("curl_xml plugin: Failed to parse the xml document - %s", xml);
+ return (-1);
}
- else
+
+ xpath_ctx = xmlXPathNewContext(doc);
+ if(xpath_ctx == NULL)
{
- ERROR ("curl_xml plugin: cx_config: "
- "Invalid key: %s", ci->key);
+ ERROR ("curl_xml plugin: Failed to create the xml context");
+ xmlFreeDoc(doc);
return (-1);
}
- /* Fill the `cx_t' structure.. */
- for (i = 0; i < ci->children_num; i++)
- {
- oconfig_item_t *child = ci->children + i;
+ status = cx_submit_statistics (doc, xpath_ctx, db);
+ /* Cleanup */
+ xmlXPathFreeContext(xpath_ctx);
+ xmlFreeDoc(doc);
+ return status;
+} /* }}} cx_parse_stats_xml */
- if (strcasecmp ("Instance", child->key) == 0)
- status = cx_config_add_string ("Instance", &db->instance, child);
- else if (strcasecmp ("Host", child->key) == 0)
- status = cx_config_add_string ("Host", &db->host, child);
- else if (strcasecmp ("User", child->key) == 0)
- status = cx_config_add_string ("User", &db->user, child);
- else if (strcasecmp ("Password", child->key) == 0)
- status = cx_config_add_string ("Password", &db->pass, child);
- else if (strcasecmp ("VerifyPeer", child->key) == 0)
- status = cx_config_set_boolean ("VerifyPeer", &db->verify_peer, child);
- else if (strcasecmp ("VerifyHost", child->key) == 0)
- status = cx_config_set_boolean ("VerifyHost", &db->verify_host, child);
- else if (strcasecmp ("CACert", child->key) == 0)
- status = cx_config_add_string ("CACert", &db->cacert, child);
- else if (strcasecmp ("xpath", child->key) == 0)
- status = cx_config_add_xpath (db, child);
- else
- {
- WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
- status = -1;
- }
+static int cx_curl_perform (cx_t *db, CURL *curl) /* {{{ */
+{
+ int status;
+ long rc;
+ char *ptr;
+ char *url;
- if (status != 0)
- break;
- }
+ db->buffer_fill = 0;
+ status = curl_easy_perform (curl);
- if (status == 0)
+ curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
+ curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &rc);
+
+ if (rc != 200)
{
- if (db->tree == NULL)
- {
- WARNING ("curl_xml plugin: No (valid) `Key' block "
- "within `URL' block `%s'.", db->url);
- status = -1;
- }
- if (status == 0)
- status = cx_init_curl (db);
+ ERROR ("curl_xml plugin: curl_easy_perform failed with response code %ld (%s)",
+ rc, url);
+ return (-1);
}
- /* If all went well, register this database for reading */
- if (status == 0)
+ if (status != 0)
{
- user_data_t ud;
- char cb_name[DATA_MAX_NAME_LEN];
+ ERROR ("curl_xml plugin: curl_easy_perform failed with status %i: %s (%s)",
+ status, db->curl_errbuf, url);
+ return (-1);
+ }
- if (db->instance == NULL)
- db->instance = strdup("default");
+ ptr = db->buffer;
- DEBUG ("curl_xml plugin: Registering new read callback: %s",
- db->instance);
+ status = cx_parse_stats_xml(BAD_CAST ptr, db);
+ db->buffer_fill = 0;
- memset (&ud, 0, sizeof (ud));
- ud.data = (void *) db;
- ud.free_func = cx_free;
+ return status;
+} /* }}} int cx_curl_perform */
- ssnprintf (cb_name, sizeof (cb_name), "curl_xml-%s-%s",
- db->instance, db->url);
+static int cx_read (user_data_t *ud) /* {{{ */
+{
+ cx_t *db;
- plugin_register_complex_read (cb_name, cx_read,
- /* interval = */ NULL, &ud);
- }
- else
+ if ((ud == NULL) || (ud->data == NULL))
{
- cx_free (db);
+ ERROR ("curl_xml plugin: cx_read: Invalid user data.");
return (-1);
}
- return (0);
-} /* }}} int cx_config_add_url */
+ db = (cx_t *) ud->data;
-static int cx_config (oconfig_item_t *ci) /* {{{ */
+ return cx_curl_perform (db, db->curl);
+} /* }}} int cx_read */
+
+/* Configuration handling functions {{{ */
+
+static int cx_config_add_values (const char *name, cx_xpath_t *xpath, /* {{{ */
+ oconfig_item_t *ci)
{
- int success;
- int errors;
- int status;
int i;
- success = 0;
- errors = 0;
-
- for (i = 0; i < ci->children_num; i++)
+ if (ci->values_num < 1)
{
- oconfig_item_t *child = ci->children + i;
+ WARNING ("curl_xml plugin: `Values' needs at least one argument.");
+ return (-1);
+ }
- if (strcasecmp ("URL", child->key) == 0)
- {
- status = cx_config_add_url (child);
- if (status == 0)
- success++;
- else
- errors++;
- }
- else
+ for (i = 0; i < ci->values_num; i++)
+ if (ci->values[i].type != OCONFIG_TYPE_STRING)
{
- WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
- errors++;
+ WARNING ("curl_xml plugin: `Values' needs only string argument.");
+ return (-1);
}
+
+ sfree (xpath->values);
+
+ xpath->values_len = 0;
+ xpath->values = (cx_values_t *) malloc (sizeof (cx_values_t) * ci->values_num);
+ if (xpath->values == NULL)
+ return (-1);
+ xpath->values_len = ci->values_num;
+
+ /* populate cx_values_t structure */
+ for (i = 0; i < ci->values_num; i++)
+ {
+ xpath->values[i].path_len = sizeof (ci->values[i].value.string);
+ sstrncpy (xpath->values[i].path, ci->values[i].value.string, sizeof (xpath->values[i].path));
}
- if ((success == 0) && (errors > 0))
+ return (0);
+} /* }}} cx_config_add_values */
+
+static int cx_config_add_string (const char *name, char **dest, /* {{{ */
+ oconfig_item_t *ci)
+{
+ if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
{
- ERROR ("curl_xml plugin: All statements failed.");
+ WARNING ("curl_xml plugin: `%s' needs exactly one string argument.", name);
return (-1);
}
- return (0);
-} /* }}} int cx_config */
-
-/* }}} End of configuration handling functions */
+ sfree (*dest);
+ *dest = strdup (ci->values[0].value.string);
+ if (*dest == NULL)
+ return (-1);
-static int cx_read (user_data_t *ud) /* {{{ */
-{
- cx_t *db;
+ return (0);
+} /* }}} int cx_config_add_string */
- if ((ud == NULL) || (ud->data == NULL))
+static int cx_config_set_boolean (const char *name, int *dest, /* {{{ */
+ oconfig_item_t *ci)
+{
+ if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
{
- ERROR ("curl_xml plugin: cx_read: Invalid user data.");
+ WARNING ("curl_xml plugin: `%s' needs exactly one boolean argument.", name);
return (-1);
}
- db = (cx_t *) ud->data;
+ *dest = ci->values[0].value.boolean ? 1 : 0;
- return cx_curl_perform (db, db->curl);
-} /* }}} int cx_read */
+ return (0);
+} /* }}} int cx_config_set_boolean */
-static int cx_curl_perform (cx_t *db, CURL *curl) /* {{{ */
+static c_avl_tree_t *cx_avl_create(void) /* {{{ */
{
- int status;
- long rc;
- char *ptr;
- char *url;
-
- db->buffer_fill = 0;
- status = curl_easy_perform (curl);
+ return c_avl_create ((int (*) (const void *, const void *)) strcmp);
+} /* }}} cx_avl_create */
- curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
- curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &rc);
+static int cx_config_add_xpath (cx_t *db, /* {{{ */
+ oconfig_item_t *ci)
+{
+ cx_xpath_t *xpath;
+ int status;
+ int i;
- if (rc != 200)
+ if ((ci->values_num != 1)
+ || (ci->values[0].type != OCONFIG_TYPE_STRING))
{
- ERROR ("curl_xml plugin: curl_easy_perform failed with response code %ld (%s)",
- rc, url);
+ WARNING ("curl_xml plugin: The `xpath' block "
+ "needs exactly one string argument.");
return (-1);
}
- if (status != 0)
+ xpath = (cx_xpath_t *) malloc (sizeof (*xpath));
+ if (xpath == NULL)
{
- ERROR ("curl_xml plugin: curl_easy_perform failed with status %i: %s (%s)",
- status, db->curl_errbuf, url);
+ ERROR ("curl_xml plugin: malloc failed.");
return (-1);
}
+ memset (xpath, 0, sizeof (*xpath));
+ xpath->magic = CX_KEY_MAGIC;
- ptr = db->buffer;
+ if (strcasecmp ("xpath", ci->key) == 0)
+ {
+ status = cx_config_add_string ("xpath", &xpath->path, ci);
+ if (status != 0)
+ {
+ sfree (xpath);
+ return (status);
+ }
+ }
+ else
+ {
+ ERROR ("curl_xml plugin: cx_config: "
+ "Invalid key: %s", ci->key);
+ return (-1);
+ }
- status = cx_parse_stats_xml(BAD_CAST ptr, db);
- db->buffer_fill = 0;
+ status = 0;
+ for (i = 0; i < ci->children_num; i++)
+ {
+ oconfig_item_t *child = ci->children + i;
- return status;
-} /* }}} int cx_curl_perform */
+ if (strcasecmp ("Type", child->key) == 0)
+ status = cx_config_add_string ("Type", &xpath->type, child);
+ else if (strcasecmp ("InstancePrefix", child->key) == 0)
+ status = cx_config_add_string ("InstancePrefix", &xpath->instance_prefix, child);
+ else if (strcasecmp ("Instance", child->key) == 0)
+ status = cx_config_add_string ("Instance", &xpath->instance, child);
+ else if (strcasecmp ("Values", child->key) == 0)
+ status = cx_config_add_values ("Values", xpath, child);
+ else
+ {
+ WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
+ status = -1;
+ }
-static int cx_parse_stats_xml(xmlChar* xml, cx_t *db) /* {{{ */
-{
- int status;
- xmlDocPtr doc;
- xmlXPathContextPtr xpath_ctx;
+ if (status != 0)
+ break;
+ } /* for (i = 0; i < ci->children_num; i++) */
- /* Load the XML */
- doc = xmlParseDoc(xml);
- if (doc == NULL)
+ while (status == 0)
{
- ERROR ("curl_xml plugin: Failed to parse the xml document - %s", xml);
- return (-1);
- }
+ if (xpath->type == NULL)
+ {
+ WARNING ("curl_xml plugin: `Type' missing in `xpath' block.");
+ status = -1;
+ }
- xpath_ctx = xmlXPathNewContext(doc);
- if(xpath_ctx == NULL)
+ break;
+ } /* while (status == 0) */
+
+ if (status == 0)
{
- ERROR ("curl_xml plugin: Failed to create the xml context");
- xmlFreeDoc(doc);
- return (-1);
- }
+ char *name;
+ c_avl_tree_t *tree;
- status = cx_submit_statistics (doc, xpath_ctx, db);
- /* Cleanup */
- xmlXPathFreeContext(xpath_ctx);
- xmlFreeDoc(doc);
- return status;
-} /* }}} cx_parse_stats_xml */
+ if (db->tree == NULL)
+ db->tree = cx_avl_create();
-static int cx_submit_statistics(xmlDocPtr doc, /* {{{ */
- xmlXPathContextPtr xpath_ctx, cx_t *db)
-{
- c_avl_iterator_t *iter;
- char *key;
- cx_xpath_t *value;
- int status=-1;
-
- iter = c_avl_get_iterator (db->tree);
- while (c_avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
- {
- if (cx_check_type(value) == -1)
- continue;
+ tree = db->tree;
+ name = xpath->path;
- if (cx_submit_xpath_values(db->instance, xpath_ctx, key, value) == 0)
- status = 0; /* we got atleast one success */
- } /* while (c_avl_iterator_next) */
+ if (*name)
+ c_avl_insert (tree, strdup(name), xpath);
+ else
+ {
+ ERROR ("curl_xml plugin: invalid key: %s", xpath->path);
+ status = -1;
+ }
+ }
- return status;
-} /* }}} cx_submit_statistics */
+ return (status);
+} /* }}} int cx_config_add_xpath */
-static int cx_check_type (cx_xpath_t *xpath) /* {{{ */
+static int cx_init_curl (cx_t *db) /* {{{ */
{
- const data_set_t *ds;
-
- ds = plugin_get_ds (xpath->type);
- if (!ds)
+ db->curl = curl_easy_init ();
+ if (db->curl == NULL)
{
- WARNING ("curl_xml plugin: DataSet `%s' not defined.", xpath->type);
+ ERROR ("curl_xml plugin: curl_easy_init failed.");
return (-1);
}
- if (ds->ds_num != xpath->values_len)
+ curl_easy_setopt (db->curl, CURLOPT_WRITEFUNCTION, cx_curl_callback);
+ curl_easy_setopt (db->curl, CURLOPT_WRITEDATA, db);
+ curl_easy_setopt (db->curl, CURLOPT_USERAGENT,
+ PACKAGE_NAME"/"PACKAGE_VERSION);
+ curl_easy_setopt (db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf);
+ curl_easy_setopt (db->curl, CURLOPT_URL, db->url);
+
+ if (db->user != NULL)
{
- WARNING ("curl_xml plugin: DataSet `%s' requires %i values, but config talks about %i",
- xpath->type, ds->ds_num, xpath->values_len);
- return (-1);
- }
+ size_t credentials_size;
- return (0);
-} /* }}} cx_check_type */
+ credentials_size = strlen (db->user) + 2;
+ if (db->pass != NULL)
+ credentials_size += strlen (db->pass);
-static xmlXPathObjectPtr /* {{{ */
- cx_evaluate_xpath (xmlXPathContextPtr xpath_ctx, xmlChar *expr)
-{
- xmlXPathObjectPtr xpath_obj;
+ db->credentials = (char *) malloc (credentials_size);
+ if (db->credentials == NULL)
+ {
+ ERROR ("curl_xml plugin: malloc failed.");
+ return (-1);
+ }
- // XXX: When to free this?
- xpath_obj = xmlXPathEvalExpression(BAD_CAST expr, xpath_ctx);
- if (xpath_obj == NULL)
- {
- WARNING ("curl_xml plugin: "
- "Error unable to evaluate xpath expression \"%s\". Skipping...", expr);
- return NULL;
+ ssnprintf (db->credentials, credentials_size, "%s:%s",
+ db->user, (db->pass == NULL) ? "" : db->pass);
+ curl_easy_setopt (db->curl, CURLOPT_USERPWD, db->credentials);
}
- return xpath_obj;
-} /* }}} cx_evaluate_xpath */
-
-static int cx_if_not_text_node (xmlNodePtr node) /* {{{ */
-{
- if (node->type == XML_TEXT_NODE || node->type == XML_ATTRIBUTE_NODE)
- return (0);
+ curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYPEER, db->verify_peer);
+ curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYHOST,
+ db->verify_host ? 2 : 0);
+ if (db->cacert != NULL)
+ curl_easy_setopt (db->curl, CURLOPT_CAINFO, db->cacert);
- WARNING ("curl_xml plugin: "
- "Node \"%s\" doesn't seem to be a text node. Skipping...", node->name);
- return -1;
-} /* }}} cx_if_not_text_node */
+ return (0);
+} /* }}} int cx_init_curl */
-static int /* {{{ */
-cx_submit_xpath_values (char *plugin_instance,
- xmlXPathContextPtr xpath_ctx,
- char *base_xpath, cx_xpath_t *xpath)
+static int cx_config_add_url (oconfig_item_t *ci) /* {{{ */
{
+ cx_t *db;
+ int status = 0;
int i;
- int j;
- int total_nodes;
- int tmp_size;
- int status=-1;
- char *node_value;
-
- xmlXPathObjectPtr base_node_obj = NULL;
- xmlXPathObjectPtr instance_node_obj = NULL;
- xmlXPathObjectPtr values_node_obj = NULL;
- xmlNodeSetPtr base_nodes = NULL;
- xmlNodeSetPtr instance_node = NULL;
- xmlNodeSetPtr values_node = NULL;
-
- value_list_t vl = VALUE_LIST_INIT;
- const data_set_t *ds;
- base_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST base_xpath);
- if (base_node_obj == NULL)
- return -1; /* error is logged already */
+ if ((ci->values_num != 1)
+ || (ci->values[0].type != OCONFIG_TYPE_STRING))
+ {
+ WARNING ("curl_xml plugin: The `URL' block "
+ "needs exactly one string argument.");
+ return (-1);
+ }
- base_nodes = base_node_obj->nodesetval;
- total_nodes = (base_nodes) ? base_nodes->nodeNr : 0;
+ db = (cx_t *) malloc (sizeof (*db));
+ if (db == NULL)
+ {
+ ERROR ("curl_xml plugin: malloc failed.");
+ return (-1);
+ }
+ memset (db, 0, sizeof (*db));
- if (total_nodes == 0)
+ if (strcasecmp ("URL", ci->key) == 0)
{
- ERROR ("curl_xml plugin: "
- "xpath expression \"%s\" doesn't match any of the node. Skipping...", base_xpath);
- xmlXPathFreeObject (base_node_obj);
- return -1;
+ status = cx_config_add_string ("URL", &db->url, ci);
+ if (status != 0)
+ {
+ sfree (db);
+ return (status);
+ }
}
-
- /* If base_xpath returned multiple results, then */
- /* Instance in the xpath block is required */
- if (total_nodes > 1 && xpath->instance == NULL)
+ else
{
- ERROR ("curl_xml plugin: "
- "Instance is must in xpath block since the base xpath expression \"%s\" "
- "returned multiple results. Skipping the xpath block...", base_xpath);
- return -1;
+ ERROR ("curl_xml plugin: cx_config: "
+ "Invalid key: %s", ci->key);
+ return (-1);
}
- /* set the values for the value_list */
- ds = plugin_get_ds (xpath->type);
- 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, hostname_g, sizeof (vl.host));
- if (plugin_instance != NULL)
- sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
-
- for (i = 0; i < total_nodes; i++)
+ /* Fill the `cx_t' structure.. */
+ for (i = 0; i < ci->children_num; i++)
{
- xpath_ctx->node = base_nodes->nodeTab[i];
+ oconfig_item_t *child = ci->children + i;
- /* instance has to be an xpath expression */
- if (xpath->instance != NULL)
- {
- instance_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST xpath->instance);
- if (instance_node_obj == NULL)
- continue; /* error is logged already */
+ if (strcasecmp ("Instance", child->key) == 0)
+ status = cx_config_add_string ("Instance", &db->instance, child);
+ else if (strcasecmp ("Host", child->key) == 0)
+ status = cx_config_add_string ("Host", &db->host, child);
+ else if (strcasecmp ("User", child->key) == 0)
+ status = cx_config_add_string ("User", &db->user, child);
+ else if (strcasecmp ("Password", child->key) == 0)
+ status = cx_config_add_string ("Password", &db->pass, child);
+ else if (strcasecmp ("VerifyPeer", child->key) == 0)
+ status = cx_config_set_boolean ("VerifyPeer", &db->verify_peer, child);
+ else if (strcasecmp ("VerifyHost", child->key) == 0)
+ status = cx_config_set_boolean ("VerifyHost", &db->verify_host, child);
+ else if (strcasecmp ("CACert", child->key) == 0)
+ status = cx_config_add_string ("CACert", &db->cacert, child);
+ else if (strcasecmp ("xpath", child->key) == 0)
+ status = cx_config_add_xpath (db, child);
+ else
+ {
+ WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
+ status = -1;
+ }
- instance_node = instance_node_obj->nodesetval;
- tmp_size = (instance_node) ? instance_node->nodeNr : 0;
+ if (status != 0)
+ break;
+ }
- if ( (tmp_size == 0) && (total_nodes > 1) )
- {
- WARNING ("curl_xml plugin: "
- "relative xpath expression for 'Instance' \"%s\" doesn't match "
- "any of the nodes. Skipping the node - %s",
- xpath->instance, base_nodes->nodeTab[i]->name);
- xmlXPathFreeObject (instance_node_obj);
- continue;
- }
+ if (status == 0)
+ {
+ if (db->tree == NULL)
+ {
+ WARNING ("curl_xml plugin: No (valid) `Key' block "
+ "within `URL' block `%s'.", db->url);
+ status = -1;
+ }
+ if (status == 0)
+ status = cx_init_curl (db);
+ }
- if (tmp_size > 1)
- {
- WARNING ("curl_xml plugin: "
- "relative xpath expression for 'Instance' \"%s\" is expected "
- "to return only one text node. Skipping the node - %s",
- xpath->instance, base_nodes->nodeTab[i]->name);
- xmlXPathFreeObject (instance_node_obj);
- continue;
- }
+ /* If all went well, register this database for reading */
+ if (status == 0)
+ {
+ user_data_t ud;
+ char cb_name[DATA_MAX_NAME_LEN];
- // ignoring the element if other than textnode/attribute
- if (cx_if_not_text_node(instance_node->nodeTab[0]))
- {
- WARNING ("curl_xml plugin: "
- "relative xpath expression \"%s\" is expected to return only text node "
- "which is not the case. Skipping the node - %s",
- xpath->instance, base_nodes->nodeTab[i]->name);
- xmlXPathFreeObject (instance_node_obj);
- continue;
- }
- }
+ if (db->instance == NULL)
+ db->instance = strdup("default");
- for (j = 0; j < xpath->values_len; j++)
- {
- values_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST xpath->values[j].path);
- values_node = values_node_obj->nodesetval;
- tmp_size = (values_node) ? values_node->nodeNr : 0;
+ DEBUG ("curl_xml plugin: Registering new read callback: %s",
+ db->instance);
- if (tmp_size == 0)
- {
- WARNING ("curl_xml plugin: "
- "relative xpath expression \"%s\" doesn't match any of the nodes. "
- "Skipping...", xpath->values[j].path);
- xmlXPathFreeObject (values_node_obj);
- continue;
- }
+ memset (&ud, 0, sizeof (ud));
+ ud.data = (void *) db;
+ ud.free_func = cx_free;
- if (tmp_size > 1)
- {
- WARNING ("curl_xml plugin: "
- "relative xpath expression \"%s\" is expected to return "
- "only one node. Skipping...", xpath->values[j].path);
- xmlXPathFreeObject (values_node_obj);
- continue;
- }
+ ssnprintf (cb_name, sizeof (cb_name), "curl_xml-%s-%s",
+ db->instance, db->url);
- /* ignoring the element if other than textnode/attribute*/
- if (cx_if_not_text_node(values_node->nodeTab[0]))
- {
- WARNING ("curl_xml plugin: "
- "relative xpath expression \"%s\" is expected to return "
- "only text/attribute node which is not the case. Skipping...",
- xpath->values[j].path);
- xmlXPathFreeObject (values_node_obj);
- continue;
- }
+ plugin_register_complex_read (cb_name, cx_read,
+ /* interval = */ NULL, &ud);
+ }
+ else
+ {
+ cx_free (db);
+ return (-1);
+ }
- vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
- if (vl.values == NULL)
- {
- ERROR ("curl_xml plugin: malloc failed.");
- xmlXPathFreeObject (base_node_obj);
- xmlXPathFreeObject (instance_node_obj);
- xmlXPathFreeObject (values_node_obj);
- return (-1);
- }
+ return (0);
+} /* }}} int cx_config_add_url */
- node_value = (char *) xmlNodeGetContent(values_node->nodeTab[0]);
- switch (ds->ds[j].type)
- {
- case DS_TYPE_COUNTER:
- vl.values[j].counter = atoi(node_value);
- break;
- case DS_TYPE_DERIVE:
- vl.values[j].derive = atoi(node_value);
- break;
- case DS_TYPE_ABSOLUTE:
- vl.values[j].absolute = atoi(node_value);
- break;
- case DS_TYPE_GAUGE:
- vl.values[j].absolute = atoi(node_value);
- }
-
- if (xpath->instance_prefix != NULL)
- {
- if (instance_node != NULL)
- ssnprintf (vl.type_instance, sizeof (vl.type_instance),"%s-%s",
- xpath->instance_prefix, (char *) xmlNodeGetContent(instance_node->nodeTab[0]));
- else
- sstrncpy (vl.type_instance, xpath->instance_prefix,
- sizeof (vl.type_instance));
- }
- else
- {
- /* If instance_prefix and instance_node are NULL , then */
- /* don't set the type_instance */
- if (instance_node != NULL)
- sstrncpy (vl.type_instance, (char *) xmlNodeGetContent(instance_node->nodeTab[0]),
- sizeof (vl.type_instance));
- }
+/* }}} End of configuration handling functions */
- /* free up object */
- xmlXPathFreeObject (values_node_obj);
+static int cx_config (oconfig_item_t *ci) /* {{{ */
+{
+ int success;
+ int errors;
+ int status;
+ int i;
- /* We have reached here which means that */
- /* we have got something to work */
- status = 0;
- }
+ success = 0;
+ errors = 0;
- /* submit the values */
- if (vl.values)
- plugin_dispatch_values (&vl);
+ for (i = 0; i < ci->children_num; i++)
+ {
+ oconfig_item_t *child = ci->children + i;
- sfree(vl.values);
- if (instance_node_obj != NULL)
- xmlXPathFreeObject (instance_node_obj);
+ if (strcasecmp ("URL", child->key) == 0)
+ {
+ status = cx_config_add_url (child);
+ if (status == 0)
+ success++;
+ else
+ errors++;
+ }
+ else
+ {
+ WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
+ errors++;
+ }
}
- /* free up the allocated memory */
- xmlXPathFreeObject (base_node_obj);
+ if ((success == 0) && (errors > 0))
+ {
+ ERROR ("curl_xml plugin: All statements failed.");
+ return (-1);
+ }
- return status;
-} /* }}} cx_submit_xpath_values */
+ return (0);
+} /* }}} int cx_config */
void module_register (void)
{