Example #1
0
    def clear_tables(self, tables=()):
        request = openstatisticapi_pb2.Request()

        request.sequence = self._sequence

        self._sequence += 1

        # set the Request type
        request.type = openstatisticapi_pb2.Request.TYPE_REQUEST_UPDATE

        # set the Request Update type
        request.update.type = openstatisticapi_pb2.TYPE_UPDATE_STATISTICTABLECLEAR

        if tables:
            for name in tables:
                request.update.statisticTableClear.names.append(name)
        else:
            request.update.statisticTableClear.SetInParent()

        response = self._transaction(request)

        if response.type == openstatisticapi_pb2.Response.TYPE_RESPONSE_UPDATE:
            if response.update.type == openstatisticapi_pb2.TYPE_UPDATE_STATISTICTABLECLEAR:
                return
            else:
                raise ClientException(
                    'malformed statistic table update response')
        else:
            raise ClientException('malformed update response')
Example #2
0
    def statistics(self, statistics=()):
        request = openstatisticapi_pb2.Request()

        request.sequence = self._sequence

        self._sequence += 1

        # set the Request type
        request.type = openstatisticapi_pb2.Request.TYPE_REQUEST_QUERY

        # set the Request Query type
        request.query.type = openstatisticapi_pb2.TYPE_QUERY_STATISTIC

        if statistics:
            for name in statistics:
                request.query.statistic.names.append(name)
        else:
            request.query.statistic.SetInParent()

        response = self._transaction(request)

        if response.type == openstatisticapi_pb2.Response.TYPE_RESPONSE_QUERY:
            if response.query.type == openstatisticapi_pb2.TYPE_QUERY_STATISTIC:
                statistics = {}
                for element in response.query.statistic.elements:
                    statistics[element.name] = fromAny(element.value)

                return statistics

            else:
                raise ClientException('malformed statistic query response')
        else:
            raise ClientException('malformed query response')
Example #3
0
    def tables(self, tables=()):
        request = openstatisticapi_pb2.Request()

        request.sequence = self._sequence

        self._sequence += 1

        # set the Request type
        request.type = openstatisticapi_pb2.Request.TYPE_REQUEST_QUERY

        # set the Request Query type
        request.query.type = openstatisticapi_pb2.TYPE_QUERY_STATISTICTABLE

        if tables:
            for name in tables:
                request.query.statisticTable.names.append(name)
        else:
            request.query.statisticTable.SetInParent()

        response = self._transaction(request)

        if response.type == openstatisticapi_pb2.Response.TYPE_RESPONSE_QUERY:
            if response.query.type == openstatisticapi_pb2.TYPE_QUERY_STATISTICTABLE:
                tables = {}

                for table in response.query.statisticTable.tables:
                    tableData = []
                    for row in table.rows:
                        rowData = []
                        for value in row.values:
                            rowData.append(fromAny(value))
                        tableData.append(tuple(rowData))
                    tables[table.name] = (tuple(table.labels),
                                          tuple(tableData))

                return tables

            else:
                raise ClientException(
                    'malformed statistic table query response')
        else:
            raise ClientException('malformed query response')
Example #4
0
    def probe(self):
        self._logger.log(Logger.DEBUG_LEVEL, "/%s probe" % self._name)
        request = openstatisticapi_pb2.Request()
        request.type = openstatisticapi_pb2.Request.TYPE_REQUEST_QUERY
        request.query.type = openstatisticapi_pb2.TYPE_QUERY_STATISTIC
        request.query.statistic.SetInParent()
        self._logger.log(Logger.DEBUG_LEVEL,
                         "/%s probe getting statistics" % self._name)
        response = self._transaction(request)

        stats = {}

        if response.type == openstatisticapi_pb2.Response.TYPE_RESPONSE_QUERY:
            if response.query.type == openstatisticapi_pb2.TYPE_QUERY_STATISTIC:
                for element in response.query.statistic.elements:
                    stats[element.name] = fromAny(element.value)[0]

        request = openstatisticapi_pb2.Request()
        request.type = openstatisticapi_pb2.Request.TYPE_REQUEST_QUERY
        request.query.type = openstatisticapi_pb2.TYPE_QUERY_STATISTICTABLE
        request.query.statisticTable.SetInParent()
        self._logger.log(Logger.DEBUG_LEVEL,
                         "/%s probe getting tables" % self._name)
        response = self._transaction(request)

        tables = {}

        if response.type == openstatisticapi_pb2.Response.TYPE_RESPONSE_QUERY:
            if response.query.type == openstatisticapi_pb2.TYPE_QUERY_STATISTICTABLE:
                for table in response.query.statisticTable.tables:
                    tableData = []
                    for row in table.rows:
                        rowData = []
                        for value in row.values:
                            rowData.append(fromAny(value))
                        tableData.append(tuple(rowData))
                    tables[table.name] = (tuple(table.labels),
                                          tuple(tableData))

        self._logger.log(Logger.DEBUG_LEVEL,
                         "/%s probe processing" % self._name)
        lstats = {}

        for name, entry in list(stats.items()):
            lstats[name.lower()] = entry

        ltables = {}

        for name, entry in list(tables.items()):
            ltables[name.lower()] = entry

        probeData = []

        for name, probe in self._probes:
            probe.Clear()
            for member in sorted(probe.DESCRIPTOR.fields_by_name.keys()):
                if member != 'description':
                    setattr(probe, member, lstats[member.replace('_', '.')])

            probeData.append(
                (name, probe.SerializeToString(), probe.description.name,
                 probe.description.module, probe.description.version))

        for name, probe in self._tables:
            probe.Clear()

            for member in sorted(probe.DESCRIPTOR.fields_by_name.keys()):
                if member != 'description':
                    table = getattr(probe, member)

                    labels, rows = ltables[member.replace('_', '.')]

                    if not self.loadTable(member, table, rows):
                        for label in labels:
                            table.labels.append(label)

                        for row in rows:
                            pass
                            entries = []
                            r = table.rows.add()
                            for entry in row:
                                e = r.values.add()
                                loadMeasurement(e, *entry)

            probeData.append(
                (name, probe.SerializeToString(), probe.description.name,
                 probe.description.module, probe.description.version))

        return probeData