]> git.ipfire.org Git - oddments/collecty.git/blame - src/collecty/util.py
Move color functions into the color module
[oddments/collecty.git] / src / collecty / util.py
CommitLineData
9823dfef
MT
1#!/usr/bin/python3
2###############################################################################
3# #
4# collecty - A system statistics collection daemon for IPFire #
5# Copyright (C) 2015 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
29455c5f
MT
22import os
23
24import logging
25log = logging.getLogger("collecty.util")
26log.propagate = 1
27
28from .constants import *
29
29455c5f
MT
30def get_network_interfaces():
31 """
32 Returns all real network interfaces
33 """
34 for interface in os.listdir("/sys/class/net"):
35 # Skip some unwanted interfaces.
36 if interface == "lo" or interface.startswith("mon."):
37 continue
38
39 path = os.path.join("/sys/class/net", interface)
40 if not os.path.isdir(path):
41 continue
42
43 yield interface
44
4d9ed86e
MT
45def make_interval(interval):
46 try:
47 return INTERVALS[interval]
48 except KeyError:
49 return "end-%s" % interval
50
29455c5f
MT
51class ProcNetSnmpParser(object):
52 """
53 This class parses /proc/net/snmp{,6} and allows
54 easy access to the values.
55 """
56 def __init__(self, intf=None):
57 self.intf = intf
58 self._data = {}
59
60 if not self.intf:
61 self._data.update(self._parse())
62
63 self._data.update(self._parse6())
64
65 def _parse(self):
66 res = {}
67
68 with open("/proc/net/snmp") as f:
69 keys = {}
70
71 for line in f.readlines():
72 line = line.strip()
73
74 # Stop after an empty line
75 if not line:
76 break
77
78 type, values = line.split(": ", 1)
79
80 # Check if the keys are already known
81 if type in keys:
82 values = (int(v) for v in values.split())
83 res[type] = dict(zip(keys[type], values))
84
85 # Otherwise remember the keys
86 else:
87 keys[type] = values.split()
88
89 return res
90
91 def _parse6(self):
92 res = {}
93
94 fn = "/proc/net/snmp6"
95 if self.intf:
96 fn = os.path.join("/proc/net/dev_snmp6", self.intf)
97
98 with open(fn) as f:
99 for line in f.readlines():
100 key, val = line.split()
101
102 try:
103 type, key = key.split("6", 1)
104 except ValueError:
105 continue
106
107 type += "6"
108 val = int(val)
109
110 try:
111 res[type][key] = val
112 except KeyError:
113 res[type] = { key : val }
114
115 return res
116
117 def get(self, proto, key):
118 """
119 Retrieves a value from the internally
120 parse dictionary read from /proc/net/snmp.
121 """
122 try:
123 return self._data[proto][key]
124 except KeyError:
125 pass