]> git.ipfire.org Git - collecty.git/blame - collecty/plugins/interface.py
Add plugin to collect traffic data from interfaces.
[collecty.git] / collecty / plugins / interface.py
CommitLineData
89b70961
MT
1#!/usr/bin/python
2###############################################################################
3# #
4# collecty - A system statistics collection daemon for IPFire #
5# Copyright (C) 2012 IPFire development team #
6# #
7# This program is free software: you can redistribute it and/or modify #
8# it under the terms of the GNU General Public License as published by #
9# the Free Software Foundation, either version 3 of the License, or #
10# (at your option) any later version. #
11# #
12# This program is distributed in the hope that it will be useful, #
13# but WITHOUT ANY WARRANTY; without even the implied warranty of #
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15# GNU General Public License for more details. #
16# #
17# You should have received a copy of the GNU General Public License #
18# along with this program. If not, see <http://www.gnu.org/licenses/>. #
19# #
20###############################################################################
21
22from __future__ import division
23
24import os
25
26import base
27
28from ..i18n import _
29
30SYS_CLASS_NET = "/sys/class/net"
31
32class PluginInterface(base.Plugin):
33 name = "interface"
34 description = "Interface Statistics"
35
36 rrd_schema = [
37 "DS:bytes_rx:DERIVE:0:U",
38 "DS:bytes_tx:DERIVE:0:U",
39 "DS:collisions:DERIVE:0:U",
40 "DS:dropped_rx:DERIVE:0:U",
41 "DS:dropped_tx:DERIVE:0:U",
42 "DS:errors_rx:DERIVE:0:U",
43 "DS:errors_tx:DERIVE:0:U",
44 "DS:multicast:DERIVE:0:U",
45 "DS:packets_rx:DERIVE:0:U",
46 "DS:packets_tx:DERIVE:0:U",
47 ]
48
49 @classmethod
50 def autocreate(cls, collecty, **kwargs):
51 if not os.path.exists(SYS_CLASS_NET):
52 return
53
54 instances = []
55 for interface in os.listdir(SYS_CLASS_NET):
56 path = os.path.join(SYS_CLASS_NET, interface)
57 if not os.path.isdir(path):
58 continue
59
60 instance = cls(collecty, interface=interface)
61 instances.append(instance)
62
63 return instances
64
65 def init(self, **kwargs):
66 self.interface = kwargs.get("interface")
67
68 @property
69 def id(self):
70 return "-".join((self.name, self.interface))
71
72 def read(self):
73 files = (
74 "rx_bytes", "tx_bytes",
75 "collisions",
76 "rx_dropped", "tx_dropped",
77 "rx_errors", "tx_errors",
78 "multicast",
79 "rx_packets", "tx_packets",
80 )
81 ret = ["%s" % self.now,]
82
83 for file in files:
84 path = os.path.join(SYS_CLASS_NET, self.interface, "statistics", file)
85
86 # Open file and read it's content.
87 f = open(path)
88
89 line = f.readline()
90 line = line.strip()
91 ret.append(line)
92
93 f.close()
94
95 self.data.append(":".join(ret))