ERROR(PLUGIN_NAME " plugin: %s failed: %s", (s), err->message); \
} while (0)
-static char *metadata_get_hostname(virDomainPtr dom) {
+enum metadata_set_type_e { META_APPEND_HOST, META_APPEND_PLUGIN_INSTANCE };
+
+static void set_field_from_metadata(value_list_t *vl, virDomainPtr dom,
+ enum metadata_set_type_e field) {
const char *xpath_str = NULL;
if (hm_xpath == NULL)
xpath_str = "/instance/name/text()";
char *metadata_str = virDomainGetMetadata(
dom, VIR_DOMAIN_METADATA_ELEMENT, namespace, VIR_DOMAIN_AFFECT_CURRENT);
if (metadata_str == NULL) {
- return NULL;
+ return;
}
- char *hostname = NULL;
+ const char *value = NULL;
xmlXPathContextPtr xpath_ctx = NULL;
xmlXPathObjectPtr xpath_obj = NULL;
xmlNodePtr xml_node = NULL;
xml_node = xpath_obj->nodesetval->nodeTab[0];
if (xml_node->type == XML_TEXT_NODE) {
- hostname = strdup((const char *)xml_node->content);
+ value = (const char *)xml_node->content;
} else if (xml_node->type == XML_ATTRIBUTE_NODE) {
- hostname = strdup((const char *)xml_node->children->content);
+ value = (const char *)xml_node->children->content;
} else {
ERROR(PLUGIN_NAME " plugin: xmlXPathEval(%s) unsupported node type %d",
xpath_str, xml_node->type);
goto metadata_end;
}
- if (hostname == NULL) {
- ERROR(PLUGIN_NAME " plugin: strdup(%s) hostname failed", xpath_str);
+ if (value == NULL)
goto metadata_end;
+
+ switch (field) {
+ case META_APPEND_HOST:
+ SSTRNCAT(vl->host, value, sizeof(vl->host));
+ break;
+ case META_APPEND_PLUGIN_INSTANCE:
+ SSTRNCAT(vl->plugin_instance, value, sizeof(vl->plugin_instance));
+ break;
}
metadata_end:
if (xml_doc)
xmlFreeDoc(xml_doc);
sfree(metadata_str);
- return hostname;
}
static void init_value_list(value_list_t *vl, virDomainPtr dom) {
const char *name;
- char *metaname;
char uuid[VIR_UUID_STRING_BUFLEN];
sstrncpy(vl->plugin, PLUGIN_NAME, sizeof(vl->plugin));
SSTRNCAT(vl->host, uuid, sizeof(vl->host));
break;
case hf_metadata:
- metaname = metadata_get_hostname(dom);
- if (metaname)
- SSTRNCAT(vl->host, metaname, sizeof(vl->host));
- sfree(metaname);
+ set_field_from_metadata(vl, dom, META_APPEND_HOST);
break;
}
}
SSTRNCAT(vl->plugin_instance, uuid, sizeof(vl->plugin_instance));
break;
case plginst_metadata:
- metaname = metadata_get_hostname(dom);
- if (metaname)
- SSTRNCAT(vl->plugin_instance, metaname, sizeof(vl->plugin_instance));
- sfree(metaname);
+ set_field_from_metadata(vl, dom, META_APPEND_PLUGIN_INSTANCE);
break;
}
}
-
} /* void init_value_list */
static int init_notif(notification_t *notif, const virDomainPtr domain,