From 89b70961640108fccc44bca6442dd59ad68a3265 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sat, 25 Aug 2012 16:02:50 +0000 Subject: [PATCH] Add plugin to collect traffic data from interfaces. --- collecty/plugins/__init__.py | 2 + collecty/plugins/interface.py | 95 +++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 collecty/plugins/interface.py diff --git a/collecty/plugins/__init__.py b/collecty/plugins/__init__.py index 3e4e349..6c904b9 100644 --- a/collecty/plugins/__init__.py +++ b/collecty/plugins/__init__.py @@ -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 index 0000000..1dde801 --- /dev/null +++ b/collecty/plugins/interface.py @@ -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 . # +# # +############################################################################### + +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)) -- 2.47.2