X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fvirt.c;h=7df51c1309a39817d3d4ff55eaf202dc03ee3b6e;hb=43954e07f30d05b2da8319749400c9bf4d01c23c;hp=c9355c4a1cd961417ce8611befb2c1454c488bfe;hpb=092e0c8763ef268918fb8117be9579d375ac8cfc;p=collectd.git diff --git a/src/virt.c b/src/virt.c index c9355c4a..7df51c13 100644 --- a/src/virt.c +++ b/src/virt.c @@ -20,6 +20,7 @@ **/ #include "collectd.h" + #include "common.h" #include "plugin.h" #include "configfile.h" @@ -128,7 +129,7 @@ enum plginst_field { }; static enum plginst_field plugin_instance_format[PLGINST_MAX_FIELDS] = - { plginst_name }; + { plginst_none }; /* InterfaceFormat. */ enum if_field { @@ -154,7 +155,7 @@ static int refresh_lists (void); static void init_value_list (value_list_t *vl, virDomainPtr dom) { - int i, n; + int n; const char *name; char uuid[VIR_UUID_STRING_BUFLEN]; @@ -163,7 +164,7 @@ init_value_list (value_list_t *vl, virDomainPtr dom) vl->host[0] = '\0'; /* Construct the hostname field according to HostnameFormat. */ - for (i = 0; i < HF_MAX_FIELDS; ++i) { + for (int i = 0; i < HF_MAX_FIELDS; ++i) { if (hostname_format[i] == hf_none) continue; @@ -194,7 +195,7 @@ init_value_list (value_list_t *vl, virDomainPtr dom) vl->host[sizeof (vl->host) - 1] = '\0'; /* Construct the plugin instance field according to PluginInstanceFormat. */ - for (i = 0; i < PLGINST_MAX_FIELDS; ++i) { + for (int i = 0; i < PLGINST_MAX_FIELDS; ++i) { if (plugin_instance_format[i] == plginst_none) continue; @@ -394,7 +395,7 @@ lv_config (const char *key, const char *value) if (strcasecmp (key, "HostnameFormat") == 0) { char *value_copy; char *fields[HF_MAX_FIELDS]; - int i, n; + int n; value_copy = strdup (value); if (value_copy == NULL) { @@ -409,7 +410,7 @@ lv_config (const char *key, const char *value) return -1; } - for (i = 0; i < n; ++i) { + for (int i = 0; i < n; ++i) { if (strcasecmp (fields[i], "hostname") == 0) hostname_format[i] = hf_hostname; else if (strcasecmp (fields[i], "name") == 0) @@ -424,7 +425,7 @@ lv_config (const char *key, const char *value) } sfree (value_copy); - for (i = n; i < HF_MAX_FIELDS; ++i) + for (int i = n; i < HF_MAX_FIELDS; ++i) hostname_format[i] = hf_none; return 0; @@ -433,7 +434,7 @@ lv_config (const char *key, const char *value) if (strcasecmp (key, "PluginInstanceFormat") == 0) { char *value_copy; char *fields[PLGINST_MAX_FIELDS]; - int i, n; + int n; value_copy = strdup (value); if (value_copy == NULL) { @@ -448,8 +449,11 @@ lv_config (const char *key, const char *value) return -1; } - for (i = 0; i < n; ++i) { - if (strcasecmp (fields[i], "name") == 0) + for (int i = 0; i < n; ++i) { + if (strcasecmp (fields[i], "none") == 0) { + plugin_instance_format[i] = plginst_none; + break; + } else if (strcasecmp (fields[i], "name") == 0) plugin_instance_format[i] = plginst_name; else if (strcasecmp (fields[i], "uuid") == 0) plugin_instance_format[i] = plginst_uuid; @@ -461,7 +465,7 @@ lv_config (const char *key, const char *value) } sfree (value_copy); - for (i = n; i < PLGINST_MAX_FIELDS; ++i) + for (int i = n; i < PLGINST_MAX_FIELDS; ++i) plugin_instance_format[i] = plginst_none; return 0; @@ -489,7 +493,6 @@ static int lv_read (void) { time_t t; - int i; if (conn == NULL) { /* `conn_string == NULL' is acceptable. */ @@ -519,25 +522,24 @@ lv_read (void) } #if 0 - for (i = 0; i < nr_domains; ++i) + for (int i = 0; i < nr_domains; ++i) fprintf (stderr, "domain %s\n", virDomainGetName (domains[i])); - for (i = 0; i < nr_block_devices; ++i) + for (int i = 0; i < nr_block_devices; ++i) fprintf (stderr, "block device %d %s:%s\n", i, virDomainGetName (block_devices[i].dom), block_devices[i].path); - for (i = 0; i < nr_interface_devices; ++i) + for (int i = 0; i < nr_interface_devices; ++i) fprintf (stderr, "interface device %d %s:%s\n", i, virDomainGetName (interface_devices[i].dom), interface_devices[i].path); #endif /* Get CPU usage, memory, VCPU usage for each domain. */ - for (i = 0; i < nr_domains; ++i) { + for (int i = 0; i < nr_domains; ++i) { virDomainInfo info; virVcpuInfoPtr vinfo = NULL; virDomainMemoryStatPtr minfo = NULL; int status; - int j; status = virDomainGetInfo (domains[i], &info); if (status != 0) @@ -572,7 +574,7 @@ lv_read (void) continue; } - for (j = 0; j < info.nrVirtCpu; ++j) + for (int j = 0; j < info.nrVirtCpu; ++j) vcpu_submit (vinfo[j].cpuTime, domains[i], vinfo[j].number, "virt_vcpu"); @@ -584,7 +586,7 @@ lv_read (void) continue; } - status = virDomainMemoryStats (domains[i], minfo, VIR_DOMAIN_MEMORY_STAT_NR, 0); + status = virDomainMemoryStats (domains[i], minfo, VIR_DOMAIN_MEMORY_STAT_NR, 0); if (status < 0) { ERROR ("virt plugin: virDomainMemoryStats failed with status %i.", @@ -593,7 +595,7 @@ lv_read (void) continue; } - for (j = 0; j < status; j++) { + for (int j = 0; j < status; j++) { memory_stats_submit ((gauge_t) minfo[j].val * 1024, domains[i], minfo[j].tag); } @@ -602,7 +604,7 @@ lv_read (void) /* Get block device stats for each domain. */ - for (i = 0; i < nr_block_devices; ++i) { + for (int i = 0; i < nr_block_devices; ++i) { struct _virDomainBlockStats stats; if (virDomainBlockStats (block_devices[i].dom, block_devices[i].path, @@ -621,7 +623,7 @@ lv_read (void) } /* for (nr_block_devices) */ /* Get interface stats for each domain. */ - for (i = 0; i < nr_interface_devices; ++i) { + for (int i = 0; i < nr_interface_devices; ++i) { struct _virDomainInterfaceStats stats; char *display_name = NULL; @@ -679,12 +681,11 @@ refresh_lists (void) } if (n > 0) { - int i; int *domids; /* Get list of domains. */ - domids = malloc (sizeof (int) * n); - if (domids == 0) { + domids = malloc (sizeof (*domids) * n); + if (domids == NULL) { ERROR (PLUGIN_NAME " plugin: malloc failed."); return -1; } @@ -701,14 +702,13 @@ refresh_lists (void) free_domains (); /* Fetch each domain and add it to the list, unless ignore. */ - for (i = 0; i < n; ++i) { + for (int i = 0; i < n; ++i) { virDomainPtr dom = NULL; const char *name; char *xml = NULL; xmlDocPtr xml_doc = NULL; xmlXPathContextPtr xpath_ctx = NULL; xmlXPathObjectPtr xpath_obj = NULL; - int j; dom = virDomainLookupByID (conn, domids[i]); if (dom == NULL) { @@ -755,7 +755,7 @@ refresh_lists (void) xpath_obj->nodesetval == NULL) goto cont; - for (j = 0; j < xpath_obj->nodesetval->nodeNr; ++j) { + for (int j = 0; j < xpath_obj->nodesetval->nodeNr; ++j) { xmlNodePtr node; char *path = NULL; @@ -784,16 +784,15 @@ refresh_lists (void) xmlNodeSetPtr xml_interfaces = xpath_obj->nodesetval; - for (j = 0; j < xml_interfaces->nodeNr; ++j) { + for (int j = 0; j < xml_interfaces->nodeNr; ++j) { char *path = NULL; char *address = NULL; xmlNodePtr xml_interface; xml_interface = xml_interfaces->nodeTab[j]; if (!xml_interface) continue; - xmlNodePtr child = NULL; - for (child = xml_interface->children; child; child = child->next) { + for (xmlNodePtr child = xml_interface->children; child; child = child->next) { if (child->type != XML_ELEMENT_NODE) continue; if (xmlStrEqual(child->name, (const xmlChar *) "target")) { @@ -830,12 +829,10 @@ refresh_lists (void) } static void -free_domains () +free_domains (void) { - int i; - if (domains) { - for (i = 0; i < nr_domains; ++i) + for (int i = 0; i < nr_domains; ++i) virDomainFree (domains[i]); sfree (domains); } @@ -863,12 +860,10 @@ add_domain (virDomainPtr dom) } static void -free_block_devices () +free_block_devices (void) { - int i; - if (block_devices) { - for (i = 0; i < nr_block_devices; ++i) + for (int i = 0; i < nr_block_devices; ++i) sfree (block_devices[i].path); sfree (block_devices); } @@ -903,12 +898,10 @@ add_block_device (virDomainPtr dom, const char *path) } static void -free_interface_devices () +free_interface_devices (void) { - int i; - if (interface_devices) { - for (i = 0; i < nr_interface_devices; ++i) { + for (int i = 0; i < nr_interface_devices; ++i) { sfree (interface_devices[i].path); sfree (interface_devices[i].address); sfree (interface_devices[i].number);