]> git.ipfire.org Git - oddments/collecty.git/blobdiff - src/collecty/plugins/interface.py
interface: Localise plugin
[oddments/collecty.git] / src / collecty / plugins / interface.py
index fbf715ab150dea49b1cf2749211bdadd42a0a685..135a59c2348fef98ee32503c391fe11c4b7071ff 100644 (file)
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/python3
 ###############################################################################
 #                                                                             #
 # collecty - A system statistics collection daemon for IPFire                 #
 #                                                                             #
 ###############################################################################
 
-from __future__ import division
+
 
 import os
 
-import base
+from . import base
 
 from ..i18n import _
 
@@ -34,11 +34,19 @@ COLOUR_RX_AREA = "%sAA" % COLOUR_RX
 COLOUR_TX = "228B22"
 COLOUR_TX_AREA = "%sAA" % COLOUR_TX
 
-class GraphTemplateInterfaceBits(base.GraphTemplate):
+class GraphTemplateInterfaceBase(base.GraphTemplate):
+       @property
+       def interface(self):
+               return self.object.interface
+
+
+class GraphTemplateInterfaceBits(GraphTemplateInterfaceBase):
        name = "interface-bits"
 
        @property
        def rrd_graph(self):
+               _ = self.locale.translate
+
                return [
                        "DEF:bytes_rx=%(file)s:bytes_rx:AVERAGE",
                        "DEF:bytes_tx=%(file)s:bytes_tx:AVERAGE",
@@ -82,18 +90,23 @@ class GraphTemplateInterfaceBits(base.GraphTemplate):
                ]
 
        @property
-       def rrd_graph_args(self):
-               return [
-                       "--title", _("Bandwidth usage on %(interface)s"),
-                       "--vertical-label", _("Bit/s"),
-               ]
+       def graph_title(self):
+               _ = self.locale.translate
+               return _("Bandwidth usage on %s") % self.interface
+
+       @property
+       def graph_vertical_label(self):
+               _ = self.locale.translate
+               return _("Bit/s")
 
 
-class GraphTemplateInterfacePackets(base.GraphTemplate):
+class GraphTemplateInterfacePackets(GraphTemplateInterfaceBase):
        name = "interface-packets"
 
        @property
        def rrd_graph(self):
+               _ = self.locale.translate
+
                return [
                        "DEF:packets_rx=%(file)s:packets_rx:AVERAGE",
                        "DEF:packets_tx=%(file)s:packets_tx:AVERAGE",
@@ -122,18 +135,23 @@ class GraphTemplateInterfacePackets(base.GraphTemplate):
                ]
 
        @property
-       def rrd_graph_args(self):
-               return [
-                       "--title", _("Transferred packets on %(interface)s"),
-                       "--vertical-label", _("Packets/s"),
-               ]
+       def graph_title(self):
+               _ = self.locale.translate
+               return _("Transferred packets on %s") % self.interface
+
+       @property
+       def graph_vertical_label(self):
+               _ = self.locale.translate
+               return _("Packets/s")
 
 
-class GraphTemplateInterfaceErrors(base.GraphTemplate):
+class GraphTemplateInterfaceErrors(GraphTemplateInterfaceBase):
        name = "interface-errors"
 
        @property
        def rrd_graph(self):
+               _ = self.locale.translate
+
                return [
                        "DEF:errors_rx=%(file)s:errors_rx:AVERAGE",
                        "DEF:errors_tx=%(file)s:errors_tx:AVERAGE",
@@ -190,23 +208,17 @@ class GraphTemplateInterfaceErrors(base.GraphTemplate):
                ]
 
        @property
-       def rrd_graph_args(self):
-               return [
-                       "--title", _("Errors/dropped packets on %(interface)s"),
-                       "--vertical-label", _("Packets/s"),
-               ]
+       def graph_title(self):
+               _ = self.locale.translate
+               return _("Errors/dropped packets on %s") % self.interface
 
+       @property
+       def graph_vertical_label(self):
+               _ = self.locale.translate
+               return _("Packets/s")
 
-class InterfacePlugin(base.Plugin):
-       name = "interface"
-       description = "Interface Statistics Data Source"
-
-       templates = [
-               GraphTemplateInterfaceBits,
-               GraphTemplateInterfacePackets,
-               GraphTemplateInterfaceErrors,
-       ]
 
+class InterfaceObject(base.Object):
        rrd_schema = [
                "DS:bytes_rx:DERIVE:0:U",
                "DS:bytes_tx:DERIVE:0:U",
@@ -220,34 +232,17 @@ class InterfacePlugin(base.Plugin):
                "DS:packets_tx:DERIVE:0:U",
        ]
 
-       @classmethod
-       def autocreate(cls, collecty, **kwargs):
-               if not os.path.exists(SYS_CLASS_NET):
-                       return
+       def __repr__(self):
+               return "<%s %s>" % (self.__class__.__name__, self.interface)
 
-               instances = []
-               for interface in os.listdir(SYS_CLASS_NET):
-                       # Skip some unwanted interfaces.
-                       if interface == "lo" or interface.startswith("mon."):
-                               continue
-
-                       path = os.path.join(SYS_CLASS_NET, interface)
-                       if not os.path.isdir(path):
-                               continue
-
-                       instance = cls(collecty, interface=interface)
-                       instances.append(instance)
-
-               return instances
-
-       def init(self, **kwargs):
-               self.interface = kwargs.get("interface")
+       def init(self, interface):
+               self.interface = interface
 
        @property
        def id(self):
-               return "-".join((self.name, self.interface))
+               return self.interface
 
-       def read(self):
+       def collect(self):
                interface_path = os.path.join(SYS_CLASS_NET, self.interface)
 
                # Check if the interface exists.
@@ -285,4 +280,34 @@ class InterfacePlugin(base.Plugin):
                                if f:
                                        f.close()
 
-               return ":".join(ret)
+               return ret
+
+
+class InterfacePlugin(base.Plugin):
+       name = "interface"
+       description = "Interface Statistics Plugin"
+
+       templates = [
+               GraphTemplateInterfaceBits,
+               GraphTemplateInterfacePackets,
+               GraphTemplateInterfaceErrors,
+       ]
+
+       interval = 30
+
+       def get_interfaces(self):
+               for interface in os.listdir(SYS_CLASS_NET):
+                       # Skip some unwanted interfaces.
+                       if interface == "lo" or interface.startswith("mon."):
+                               continue
+
+                       path = os.path.join(SYS_CLASS_NET, interface)
+                       if not os.path.isdir(path):
+                               continue
+
+                       yield interface
+
+       @property
+       def objects(self):
+               for interface in self.get_interfaces():
+                       yield InterfaceObject(self, interface=interface)