-#!/usr/bin/python
+#!/usr/bin/python3
###############################################################################
# #
# collecty - A system statistics collection daemon for IPFire #
# #
###############################################################################
-from __future__ import division
-
import datetime
import logging
import math
import tempfile
import threading
import time
+import unicodedata
from ..constants import *
from ..i18n import _
-_plugins = {}
-
-def get():
- """
- Returns a list with all automatically registered plugins.
- """
- return _plugins.values()
-
class Timer(object):
def __init__(self, timeout, heartbeat=1):
self.timeout = timeout
return self.elapsed > self.timeout
-class Plugin(object):
+class PluginRegistration(type):
+ plugins = {}
+
+ def __init__(plugin, name, bases, dict):
+ type.__init__(plugin, name, bases, dict)
+
+ # The main class from which is inherited is not registered
+ # as a plugin.
+ if name == "Plugin":
+ return
+
+ if not all((plugin.name, plugin.description)):
+ raise RuntimeError(_("Plugin is not properly configured: %s") % plugin)
+
+ PluginRegistration.plugins[plugin.name] = plugin
+
+
+def get():
+ """
+ Returns a list with all automatically registered plugins.
+ """
+ return PluginRegistration.plugins.values()
+
+class Plugin(object, metaclass=PluginRegistration):
# The name of this plugin.
name = None
# The default interval for all plugins
interval = 60
- # Automatically register all providers.
- class __metaclass__(type):
- def __init__(plugin, name, bases, dict):
- type.__init__(plugin, name, bases, dict)
-
- # The main class from which is inherited is not registered
- # as a plugin.
- if name == "Plugin":
- return
-
- if not all((plugin.name, plugin.description)):
- raise RuntimeError(_("Plugin is not properly configured: %s") \
- % plugin)
-
- _plugins[plugin.name] = plugin
-
def __init__(self, collecty, **kwargs):
self.collecty = collecty
rrd_schema = None
# RRA properties.
- rra_types = ["AVERAGE", "MIN", "MAX"]
- rra_timespans = [3600, 86400, 604800, 2678400, 31622400]
- rra_rows = 2880
+ rra_types = ("AVERAGE", "MIN", "MAX")
+ rra_timespans = (
+ ("1m", "10d"),
+ ("1h", "18M"),
+ ("1d", "5y"),
+ )
def __init__(self, plugin, *args, **kwargs):
self.plugin = plugin
@staticmethod
def _normalise_filename(filename):
# Convert the filename into ASCII characters only
- filename = filename.encode("ascii", "ignore")
+ filename = unicodedata.normalize("NFKC", filename)
# Replace any spaces by dashes
filename = filename.replace(" ", "-")
xff = 0.1
- cdp_length = 0
- for rra_timespan in self.rra_timespans:
- if (rra_timespan / self.stepsize) < self.rra_rows:
- rra_timespan = self.stepsize * self.rra_rows
-
- if cdp_length == 0:
- cdp_length = 1
- else:
- cdp_length = rra_timespan // (self.rra_rows * self.stepsize)
-
- cdp_number = math.ceil(rra_timespan / (cdp_length * self.stepsize))
-
- for rra_type in self.rra_types:
- schema.append("RRA:%s:%.10f:%d:%d" % \
- (rra_type, xff, cdp_length, cdp_number))
+ for steps, rows in self.rra_timespans:
+ for type in self.rra_types:
+ schema.append("RRA:%s:%s:%s:%s" % (type, xff, steps, rows))
return schema
self.log.debug(" %s" % args[-1])
- return self.write_graph(*args)
-
- def write_graph(self, *args):
- # Convert all arguments to string
+ # Convert arguments to string
args = [str(e) for e in args]
- with tempfile.NamedTemporaryFile() as f:
- rrdtool.graph(f.name, *args)
-
- # Get back to the beginning of the file
- f.seek(0)
+ graph = rrdtool.graphv("-", *args)
- # Return all the content
- return f.read()
+ return graph.get("image")