Beispiel #1
0
    def export(self, name, columns, points):
        """Export the stats to the JSON file."""

        # Check for completion of loop for all exports
        if name == self.plugins_to_export()[0] and self.buffer != {}:
            # One whole loop has been completed
            # Flush stats to file
            logger.debug("Exporting stats ({}) to JSON file ({})".format(
                listkeys(self.buffer), self.json_filename))

            # Export stats to JSON file
            if PY3:
                with open(self.json_filename, "w") as self.json_file:
                    self.json_file.write("{}\n".format(json.dumps(
                        self.buffer)))
            else:
                with open(self.json_filename, "wb") as self.json_file:
                    self.json_file.write("{}\n".format(json.dumps(
                        self.buffer)))

            # Reset buffer
            self.buffer = {}

        # Add current stat to the buffer
        self.buffer[name] = dict(zip(columns, points))
Beispiel #2
0
    def export(self, name, columns, points):
        """Write the points to the Prometheus exporter using Gauge."""
        logger.debug("Export {} stats to Prometheus exporter".format(name))

        # Remove non number stats and convert all to float (for Boolean)
        data = {k: float(v) for (k, v) in iteritems(dict(zip(columns, points))) if isinstance(v, Number)}

        # Write metrics to the Prometheus exporter
        for k, v in iteritems(data):
            # Prometheus metric name: prefix_<glances stats name>
            metric_name = self.prefix + self.METRIC_SEPARATOR + name + self.METRIC_SEPARATOR + k
            # Prometheus is very sensible to the metric name
            # See: https://prometheus.io/docs/practices/naming/
            for c in ['.', '-', '/', ' ']:
                metric_name = metric_name.replace(c, self.METRIC_SEPARATOR)
            # Get the labels
            labels = self.parse_tags(self.labels)
            # Manage an internal dict between metric name and Gauge
            if metric_name not in self._metric_dict:
                self._metric_dict[metric_name] = Gauge(metric_name, k,
                                                       labelnames=listkeys(labels))
            # Write the value
            if hasattr(self._metric_dict[metric_name], 'labels'):
                # Add the labels (see issue #1255)
                self._metric_dict[metric_name].labels(**labels).set(v)
            else:
                self._metric_dict[metric_name].set(v)
Beispiel #3
0
    def update_views(self):
        """Update the stats views.

        The V of MVC
        A dict of dict with the needed information to display the stats.
        Example for the stat xxx:
        'xxx': {'decoration': 'DEFAULT',
                'optional': False,
                'additional': False,
                'splittable': False}
        """
        ret = {}

        if (isinstance(self.get_raw(), list) and self.get_raw() is not None
                and self.get_key() is not None):
            # Stats are stored in a list of dict (ex: NETWORK, FS...)
            for i in self.get_raw():
                ret[i[self.get_key()]] = {}
                for key in listkeys(i):
                    value = {
                        'decoration': 'DEFAULT',
                        'optional': False,
                        'additional': False,
                        'splittable': False
                    }
                    ret[i[self.get_key()]][key] = value
        elif isinstance(self.get_raw(), dict) and self.get_raw() is not None:
            # Stats are stored in a dict (ex: CPU, LOAD...)
            for key in listkeys(self.get_raw()):
                value = {
                    'decoration': 'DEFAULT',
                    'optional': False,
                    'additional': False,
                    'splittable': False
                }
                ret[key] = value

        self.views = ret

        return self.views
Beispiel #4
0
    def export(self, name, columns, points):
        """Export the stats to the Statsd server."""
        if name == self.plugins_to_export()[0] and self.buffer != {}:
            # One complete loop have been done
            logger.debug("Export stats ({}) to Restful endpoint ({})".format(listkeys(self.buffer),
                                                                             self.client))
            # Export stats
            post(self.client, json=self.buffer, allow_redirects=True)
            # Reset buffer
            self.buffer = {}

        # Add current stat to the buffer
        self.buffer[name] = dict(zip(columns, points))
Beispiel #5
0
    def export(self, name, columns, points):
        """Export the stats to the Statsd server."""
        if name == self.plugins_to_export()[0] and self.buffer != {}:
            # One complete loop have been done
            logger.debug("Export stats ({}) to RESTful endpoint ({})".format(
                listkeys(self.buffer), self.client))
            # Export stats
            post(self.client, json=self.buffer, allow_redirects=True)
            # Reset buffer
            self.buffer = {}

        # Add current stat to the buffer
        self.buffer[name] = dict(zip(columns, points))
Beispiel #6
0
    def update_views(self):
        """Default builder fo the stats views.

        The V of MVC
        A dict of dict with the needed information to display the stats.
        Example for the stat xxx:
        'xxx': {'decoration': 'DEFAULT',
                'optional': False,
                'additional': False,
                'splittable': False}
        """
        ret = {}

        if (isinstance(self.get_raw(), list) and
                self.get_raw() is not None and
                self.get_key() is not None):
            # Stats are stored in a list of dict (ex: NETWORK, FS...)
            for i in self.get_raw():
                ret[i[self.get_key()]] = {}
                for key in listkeys(i):
                    value = {'decoration': 'DEFAULT',
                             'optional': False,
                             'additional': False,
                             'splittable': False}
                    ret[i[self.get_key()]][key] = value
        elif isinstance(self.get_raw(), dict) and self.get_raw() is not None:
            # Stats are stored in a dict (ex: CPU, LOAD...)
            for key in listkeys(self.get_raw()):
                value = {'decoration': 'DEFAULT',
                         'optional': False,
                         'additional': False,
                         'splittable': False}
                ret[key] = value

        self.views = ret

        return self.views
Beispiel #7
0
    def update_views_hidden(self):
        """If the self.hide_zero is set then update the hidden field of the view
        It will check if all fields values are already be different from 0
        In this case, the hidden field is set to True

        Note: This function should be called by plugin (in the update_views method)

        Example (for network plugin):
        __Init__
            self.hide_zero_fields = ['rx', 'tx']
        Update views
            ...
            self.update_views_hidden()
        """
        if not self.hide_zero:
            return False
        if (isinstance(self.get_raw(), list) and self.get_raw() is not None
                and self.get_key() is not None):
            # Stats are stored in a list of dict (ex: NETWORK, FS...)
            for i in self.get_raw():
                if any([i[f] for f in self.hide_zero_fields]):
                    for f in self.hide_zero_fields:
                        self.views[i[self.get_key()]][f]['_zero'] = self.views[
                            i[self.get_key()]][f]['hidden']
                for f in self.hide_zero_fields:
                    self.views[i[self.get_key()]][f]['hidden'] = self.views[i[
                        self.get_key()]][f]['_zero'] and i[f] == 0
        elif isinstance(self.get_raw(), dict) and self.get_raw() is not None:
            #
            # Warning: This code has never been tested because
            # no plugin with dict instance use the hidden function...
            #                       vvvv
            #
            # Stats are stored in a dict (ex: CPU, LOAD...)
            for key in listkeys(self.get_raw()):
                if any([self.get_raw()[f] for f in self.hide_zero_fields]):
                    for f in self.hide_zero_fields:
                        self.views[f]['_zero'] = self.views[f]['hidden']
                for f in self.hide_zero_fields:
                    self.views[f][
                        'hidden'] = self.views['_zero'] and self.views[f] == 0
        return True
Beispiel #8
0
    def export(self, name, columns, points):
        """Export the stats to the JSON file."""

        # Check for completion of loop for all exports
        if name == self.plugins_to_export()[0] and self.buffer != {}:
            # One whole loop has been completed
            # Flush stats to file
            logger.debug("Exporting stats ({}) to JSON file ({})".format(
                listkeys(self.buffer),
                self.json_filename)
            )

            # Export stats to JSON file
            data_json = json.dumps(self.buffer)
            self.json_file.write("{}\n".format(data_json))

            # Reset buffer
            self.buffer = {}

        # Add current stat to the buffer
        self.buffer[name] = dict(zip(columns, points))
Beispiel #9
0
    def export(self, name, columns, points):
        """Export the stats to the kdb+ host/port."""

        # Remove non number stats and convert all numbers to float like prometheus
        data = {
            k: v
            for (k, v) in iteritems(dict(zip(columns, points)))
            if isinstance(v, Number)
        }

        # Append all tables name with self.prefix
        try:
            self.client.sendAsync(
                "{if[count z; x insert flip (`time, y)! .z.t, (),/: z]}",
                np.string_(f"{self.prefix}{name.capitalize()}"),
                qlist(listkeys(data), qtype=QSYMBOL_LIST),
                qlist(listvalues(data), qtype=QDOUBLE_LIST),
            )
        except Exception as e:
            logger.error(f"Cannot export stats <{name}> to kdb+ ({e})")

        logger.debug(f"Exported <{name}> stats to kdb+")
Beispiel #10
0
 def getList(self):
     """Return the AMPs list."""
     return listkeys(self.__amps_dict)
Beispiel #11
0
 def getList(self):
     """Return the AMPs list."""
     return listkeys(self.__amps_dict)
Beispiel #12
0
    def update_views(self):
        """Update the stats views.

        The V of MVC
        A dict of dict with the needed information to display the stats.
        Example for the stat xxx:
        'xxx': {'decoration': 'DEFAULT',  >>> The decoration of the stats
                'optional': False,        >>> Is the stat optional
                'additional': False,      >>> Is the stat provide additional information
                'splittable': False,      >>> Is the stat can be cut (like process lon name)
                'hidden': False,          >>> Is the stats should be hidden in the UI 
                '_zero': True}            >>> For internal purpose only
        """
        ret = {}

        if (isinstance(self.get_raw(), list) and self.get_raw() is not None
                and self.get_key() is not None):
            # Stats are stored in a list of dict (ex: NETWORK, FS...)
            for i in self.get_raw():
                # i[self.get_key()] is the interface name (example for NETWORK)
                ret[i[self.get_key()]] = {}
                for key in listkeys(i):
                    value = {
                        'decoration':
                        'DEFAULT',
                        'optional':
                        False,
                        'additional':
                        False,
                        'splittable':
                        False,
                        'hidden':
                        False,
                        '_zero':
                        self.views[i[self.get_key()]][key]['_zero']
                        if i[self.get_key()] in self.views
                        and key in self.views[i[self.get_key()]] else True
                    }
                    ret[i[self.get_key()]][key] = value
        elif isinstance(self.get_raw(), dict) and self.get_raw() is not None:
            # Stats are stored in a dict (ex: CPU, LOAD...)
            for key in listkeys(self.get_raw()):
                value = {
                    'decoration':
                    'DEFAULT',
                    'optional':
                    False,
                    'additional':
                    False,
                    'splittable':
                    False,
                    'hidden':
                    False,
                    '_zero':
                    self.views[key]['_zero'] if key in self.views
                    and '_zero' in self.views[key] else True
                }
                ret[key] = value

        self.views = ret

        return self.views