]> git.ipfire.org Git - collecty.git/commitdiff
Add plugin to collect traffic data from interfaces.
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 25 Aug 2012 16:02:50 +0000 (16:02 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 25 Aug 2012 16:02:50 +0000 (16:02 +0000)
collecty/plugins/__init__.py
collecty/plugins/interface.py [new file with mode: 0644]

index 3e4e349e1d576c482f63b6077d1bcfee2e5e9f76..6c904b9e13f700cc81b327c27249025af89c4ef7 100644 (file)
@@ -23,12 +23,14 @@ from base import Timer
 
 import cpu
 import entropy
+import interface
 import loadavg
 import memory
 
 registered_plugins = [
        cpu.PluginCPU,
        entropy.PluginEntropy,
+       interface.PluginInterface,
        loadavg.PluginLoadAvg,
        memory.PluginMemory,
 ]
diff --git a/collecty/plugins/interface.py b/collecty/plugins/interface.py
new file mode 100644 (file)
index 0000000..1dde801
--- /dev/null
@@ -0,0 +1,95 @@
+#!/usr/bin/python
+###############################################################################
+#                                                                             #
+# collecty - A system statistics collection daemon for IPFire                 #
+# Copyright (C) 2012 IPFire development team                                  #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+###############################################################################
+
+from __future__ import division
+
+import os
+
+import base
+
+from ..i18n import _
+
+SYS_CLASS_NET = "/sys/class/net"
+
+class PluginInterface(base.Plugin):
+       name = "interface"
+       description = "Interface Statistics"
+
+       rrd_schema = [
+               "DS:bytes_rx:DERIVE:0:U",
+               "DS:bytes_tx:DERIVE:0:U",
+               "DS:collisions:DERIVE:0:U",
+               "DS:dropped_rx:DERIVE:0:U",
+               "DS:dropped_tx:DERIVE:0:U",
+               "DS:errors_rx:DERIVE:0:U",
+               "DS:errors_tx:DERIVE:0:U",
+               "DS:multicast:DERIVE:0:U",
+               "DS:packets_rx:DERIVE:0:U",
+               "DS:packets_tx:DERIVE:0:U",
+       ]
+
+       @classmethod
+       def autocreate(cls, collecty, **kwargs):
+               if not os.path.exists(SYS_CLASS_NET):
+                       return
+
+               instances = []
+               for interface in os.listdir(SYS_CLASS_NET):
+                       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")
+
+       @property
+       def id(self):
+               return "-".join((self.name, self.interface))
+
+       def read(self):
+               files = (
+                       "rx_bytes", "tx_bytes",
+                       "collisions",
+                       "rx_dropped", "tx_dropped",
+                       "rx_errors", "tx_errors",
+                       "multicast",
+                       "rx_packets", "tx_packets",
+               )
+               ret = ["%s" % self.now,]
+
+               for file in files:
+                       path = os.path.join(SYS_CLASS_NET, self.interface, "statistics", file)
+
+                       # Open file and read it's content.
+                       f = open(path)
+
+                       line = f.readline()
+                       line = line.strip()
+                       ret.append(line)
+
+                       f.close()
+
+               self.data.append(":".join(ret))