]> git.ipfire.org Git - collecty.git/blame - collecty/plugins/interface.py
Split plugins into data sources and graph templates.
[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
b1ea4956 32class DataSourceInterface(base.DataSource):
89b70961 33 name = "interface"
b1ea4956
MT
34 description = "Interface Statistics Data Source"
35
36 templates = []
89b70961
MT
37
38 rrd_schema = [
39 "DS:bytes_rx:DERIVE:0:U",
40 "DS:bytes_tx:DERIVE:0:U",
41 "DS:collisions:DERIVE:0:U",
42 "DS:dropped_rx:DERIVE:0:U",
43 "DS:dropped_tx:DERIVE:0:U",
44 "DS:errors_rx:DERIVE:0:U",
45 "DS:errors_tx:DERIVE:0:U",
46 "DS:multicast:DERIVE:0:U",
47 "DS:packets_rx:DERIVE:0:U",
48 "DS:packets_tx:DERIVE:0:U",
49 ]
50
51 @classmethod
52 def autocreate(cls, collecty, **kwargs):
53 if not os.path.exists(SYS_CLASS_NET):
54 return
55
56 instances = []
57 for interface in os.listdir(SYS_CLASS_NET):
58 path = os.path.join(SYS_CLASS_NET, interface)
59 if not os.path.isdir(path):
60 continue
61
62 instance = cls(collecty, interface=interface)
63 instances.append(instance)
64
65 return instances
66
67 def init(self, **kwargs):
68 self.interface = kwargs.get("interface")
69
70 @property
71 def id(self):
72 return "-".join((self.name, self.interface))
73
74 def read(self):
75 files = (
76 "rx_bytes", "tx_bytes",
77 "collisions",
78 "rx_dropped", "tx_dropped",
79 "rx_errors", "tx_errors",
80 "multicast",
81 "rx_packets", "tx_packets",
82 )
83 ret = ["%s" % self.now,]
84
85 for file in files:
86 path = os.path.join(SYS_CLASS_NET, self.interface, "statistics", file)
87
88 # Open file and read it's content.
89 f = open(path)
90
91 line = f.readline()
92 line = line.strip()
93 ret.append(line)
94
95 f.close()
96
97 self.data.append(":".join(ret))