[python PATCH 3/5] API: Implement bindings for virConnectGetAllDomainStats

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Implement the function by returning a list of tuples instead the array
of virDomainStatsRecords and store the typed parameters as dict.
---
 generator.py                   |  1 +
 libvirt-override-virConnect.py | 53 ++++++++++++++++++++++++
 libvirt-override.c             | 94 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 148 insertions(+)

diff --git a/generator.py b/generator.py
index cfc016e..9addb89 100755
--- a/generator.py
+++ b/generator.py
@@ -507,6 +507,7 @@ skip_function = (
     'virConnectListAllNodeDevices', # overridden in virConnect.py
     'virConnectListAllNWFilters', # overridden in virConnect.py
     'virConnectListAllSecrets', # overridden in virConnect.py
+    'virConnectGetAllDomainStats', # overridden in virConnect.py

     'virStreamRecvAll', # Pure python libvirt-override-virStream.py
     'virStreamSendAll', # Pure python libvirt-override-virStream.py
diff --git a/libvirt-override-virConnect.py b/libvirt-override-virConnect.py
index 31d71a3..c4c400a 100644
--- a/libvirt-override-virConnect.py
+++ b/libvirt-override-virConnect.py
@@ -383,3 +383,56 @@
         if ret is None:raise libvirtError('virDomainCreateXMLWithFiles() failed', conn=self)
         __tmp = virDomain(self,_obj=ret)
         return __tmp
+
+    def getAllDomainStats(self, stats = 0, flags=0):
+        """Query statistics for all domains on a given connection.
+
+        Report statistics of various parameters for a running VM according to @stats
+        field. The statistics are returned as an array of structures for each queried
+        domain. The structure contains an array of typed parameters containing the
+        individual statistics. The typed parameter name for each statistic field
+        consists of a dot-separated string containing name of the requested group
+        followed by a group specific description of the statistic value.
+
+        The statistic groups are enabled using the @stats parameter which is a
+        binary-OR of enum virDomainStatsTypes. The following groups are available
+        (although not necessarily implemented for each hypervisor):
+
+        VIR_DOMAIN_STATS_STATE: Return domain state and reason for entering that
+        state. The typed parameter keys are in this format:
+        "state.state" - state of the VM, returned as int from virDomainState enum
+        "state.reason" - reason for entering given state, returned as int from
+                         virDomain*Reason enum corresponding to given state.
+
+        Using 0 for @stats returns all stats groups supported by the given
+        hypervisor.
+
+        Specifying VIR_CONNECT_GET_ALL_DOMAINS_STATS_ENFORCE_STATS as @flags makes
+        the function return error in case some of the stat types in @stats were
+        not recognized by the daemon.
+
+        Similarly to virConnectListAllDomains, @flags can contain various flags to
+        filter the list of domains to provide stats for.
+
+        VIR_CONNECT_GET_ALL_DOMAINS_STATS_ACTIVE selects online domains while
+        VIR_CONNECT_GET_ALL_DOMAINS_STATS_INACTIVE selects offline ones.
+
+        VIR_CONNECT_GET_ALL_DOMAINS_STATS_PERSISTENT and
+        VIR_CONNECT_GET_ALL_DOMAINS_STATS_TRANSIENT allow to filter the list
+        according to their persistence.
+
+        To filter the list of VMs by domain state @flags can contain
+        VIR_CONNECT_GET_ALL_DOMAINS_STATS_RUNNING,
+        VIR_CONNECT_GET_ALL_DOMAINS_STATS_PAUSED,
+        VIR_CONNECT_GET_ALL_DOMAINS_STATS_SHUTOFF and/or
+        VIR_CONNECT_GET_ALL_DOMAINS_STATS_OTHER for all other states. """
+        ret = libvirtmod.virConnectGetAllDomainStats(self._o, stats, flags)
+        if ret is None:
+            raise libvirtError("virConnectGetAllDomainStats() failed", conn=self)
+
+        retlist = list()
+        for elem in ret:
+            record = (virDomain(self, _obj=elem[0]) , elem[1])
+            retlist.append(record)
+
+        return retlist
diff --git a/libvirt-override.c b/libvirt-override.c
index b2271ae..df4f15b 100644
--- a/libvirt-override.c
+++ b/libvirt-override.c
@@ -4964,6 +4964,97 @@ cleanup:
     return py_retval;
 }

+#if LIBVIR_CHECK_VERSION(1, 2, 8)
+static PyObject *
+convertDomainStatsRecord(virDomainStatsRecordPtr *records,
+                         int nrecords)
+{
+    PyObject *py_retval;
+    PyObject *py_record;
+    PyObject *py_record_domain;
+    PyObject *py_record_stats;
+    size_t i;
+
+    if (!(py_retval = PyList_New(nrecords)))
+        return NULL;
+
+    for (i = 0; i < nrecords; i++) {
+        if (!(py_record = PyTuple_New(2)))
+            goto error;
+
+        /* libvirt_virDomainPtrWrap steals the object */
+        virDomainRef(records[i]->dom);
+        if (!(py_record_domain = libvirt_virDomainPtrWrap(records[i]->dom))) {
+            virDomainFree(records[i]->dom);
+            goto error;
+        }
+
+        if (!(py_record_stats = getPyVirTypedParameter(records[i]->params,
+                                                       records[i]->nparams)))
+            goto error;
+
+        if (PyTuple_SetItem(py_record, 0, py_record_domain) < 0)
+            goto error;
+
+        py_record_domain = NULL;
+
+        if (PyTuple_SetItem(py_record, 1, py_record_stats) < 0)
+            goto error;
+
+        py_record_stats = NULL;
+
+        if (PyList_SetItem(py_retval, i, py_record) < 0)
+            goto error;
+
+        py_record = NULL;
+    }
+
+    return py_retval;
+
+ error:
+    Py_XDECREF(py_retval);
+    Py_XDECREF(py_record);
+    Py_XDECREF(py_record_domain);
+    Py_XDECREF(py_record_stats);
+    return NULL;
+}
+
+
+static PyObject *
+libvirt_virConnectGetAllDomainStats(PyObject *self ATTRIBUTE_UNUSED,
+                                    PyObject *args)
+{
+    PyObject *pyobj_conn;
+    PyObject *py_retval;
+    virConnectPtr conn;
+    virDomainStatsRecordPtr *records;
+    int nrecords;
+    unsigned int flags;
+    unsigned int stats;
+
+    if (!PyArg_ParseTuple(args, (char *)"Oii:virConnectGetAllDomainStats",
+                          &pyobj_conn, &stats, &flags))
+        return NULL;
+    conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
+
+    LIBVIRT_BEGIN_ALLOW_THREADS;
+    nrecords = virConnectGetAllDomainStats(conn, stats, &records, flags);
+    LIBVIRT_END_ALLOW_THREADS;
+
+    if (nrecords < 0)
+        return VIR_PY_NONE;
+
+    if (!(py_retval = convertDomainStatsRecord(records, nrecords)))
+        py_retval = VIR_PY_NONE;
+
+ cleanup:
+    virDomainStatsRecordListFree(records);
+
+    return py_retval;
+}
+#endif
+
+
 /*******************************************
  * Helper functions to avoid importing modules
  * for every callback
@@ -8140,6 +8231,9 @@ static PyMethodDef libvirtMethods[] = {
     {(char *) "virNodeGetFreePages", libvirt_virNodeGetFreePages, METH_VARARGS, NULL},
     {(char *) "virNetworkGetDHCPLeases", libvirt_virNetworkGetDHCPLeases, METH_VARARGS, NULL},
 #endif /* LIBVIR_CHECK_VERSION(1, 2, 6) */
+#if LIBVIR_CHECK_VERSION(1, 2, 8)
+    {(char *) "virConnectGetAllDomainStats", libvirt_virConnectGetAllDomainStats, METH_VARARGS, NULL},
+#endif /* LIBVIR_CHECK_VERSION(1, 2, 8) */
     {NULL, NULL, 0, NULL}
 };

-- 
2.0.2

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]