+static int persistent_domains_state_notification(void) {
+ int status = 0;
+ int n;
+#ifdef HAVE_LIST_ALL_DOMAINS
+ virDomainPtr *domains;
+ n = virConnectListAllDomains(conn, &domains,
+ VIR_CONNECT_GET_ALL_DOMAINS_STATS_PERSISTENT);
+ if (n < 0) {
+ VIRT_ERROR(conn, "reading list of persistent domains");
+ status = -1;
+ }
+ else {
+ DEBUG(PLUGIN_NAME " plugin: getting state of %i persistent domains", n);
+ /* Fetch each persistent domain's state and notify it */
+ int n_notified = n;
+ for (int i = 0; i < n; ++i) {
+ status = get_domain_state_notify(domains[i]);
+ if (status != 0) {
+ n_notified--;
+ ERROR(PLUGIN_NAME " plugin: could not notify state of domain %s",
+ virDomainGetName(domains[i]));
+ }
+ }
+
+ sfree(domains);
+ DEBUG(PLUGIN_NAME " plugin: notified state of %i persistent domains",
+ n_notified);
+ }
+#else
+ n = virConnectNumOfDomains(conn);
+ if (n > 0) {
+ int *domids;
+ /* Get list of domains. */
+ domids = malloc(sizeof(*domids) * n);
+ if (domids == NULL) {
+ ERROR(PLUGIN_NAME " plugin: malloc failed.");
+ return -1;
+ }
+ n = virConnectListDomains(conn, domids, n);
+ if (n < 0) {
+ VIRT_ERROR(conn, "reading list of domains");
+ sfree(domids);
+ return -1;
+ }
+ /* Fetch info of each active domain and notify it */
+ for (int i = 0; i < n; ++i) {
+ virDomainInfo info;
+ virDomainPtr dom = NULL;
+ dom = virDomainLookupByID(conn, domids[i]);
+ if (dom == NULL) {
+ VIRT_ERROR(conn, "virDomainLookupByID");
+ /* Could be that the domain went away -- ignore it anyway. */
+ continue;
+ }
+ status = virDomainGetInfo(dom, &info);
+ if (status != 0) {
+ ERROR(PLUGIN_NAME " plugin: virDomainGetInfo failed with status %i.",
+ status);
+ continue;
+ }
+ /* virDomainGetState is not available. Submit 0, which corresponds to
+ * unknown reason. */
+ domain_state_submit_notif(domain->ptr, info.di.state, 0);
+ }
+ sfree(domids);
+ }
+#endif
+
+ return status;
+}
+