]> git.ipfire.org Git - oddments/collecty.git/blob - src/collecty/util.py
ecb39b54b4ecb7d46fada4a552a67a63cadf2ff4
[oddments/collecty.git] / src / collecty / util.py
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
22 import logging
23 import os
24
25 log = logging.getLogger("collecty.util")
26
27 from .constants import *
28
29 def get_network_interfaces():
30 """
31 Returns all real network interfaces
32 """
33 for interface in os.listdir("/sys/class/net"):
34 # Skip some unwanted interfaces.
35 if interface == "lo" or interface.startswith("mon."):
36 continue
37
38 path = os.path.join("/sys/class/net", interface)
39 if not os.path.isdir(path):
40 continue
41
42 yield interface
43
44 def make_interval(interval):
45 try:
46 return INTERVALS[interval]
47 except KeyError:
48 return "end-%s" % interval
49
50 def guess_format(filename):
51 """
52 Returns the best format by filename extension
53 """
54 parts = filename.split(".")
55
56 if parts:
57 # The extension is the last part
58 extension = parts[-1]
59
60 # Image formats are all uppercase
61 extension = extension.upper()
62
63 if extension in SUPPORTED_IMAGE_FORMATS:
64 return extension
65
66 # Otherwise fall back to the default format
67 return DEFAULT_IMAGE_FORMAT
68
69 class ProcNetSnmpParser(object):
70 """
71 This class parses /proc/net/snmp{,6} and allows
72 easy access to the values.
73 """
74 def __init__(self, intf=None):
75 self.intf = intf
76 self._data = {}
77
78 if not self.intf:
79 self._data.update(self._parse())
80
81 self._data.update(self._parse6())
82
83 def _parse(self):
84 res = {}
85
86 with open("/proc/net/snmp") as f:
87 keys = {}
88
89 for line in f.readlines():
90 line = line.strip()
91
92 # Stop after an empty line
93 if not line:
94 break
95
96 type, values = line.split(": ", 1)
97
98 # Check if the keys are already known
99 if type in keys:
100 values = (int(v) for v in values.split())
101 res[type] = dict(zip(keys[type], values))
102
103 # Otherwise remember the keys
104 else:
105 keys[type] = values.split()
106
107 return res
108
109 def _parse6(self):
110 res = {}
111
112 fn = "/proc/net/snmp6"
113 if self.intf:
114 fn = os.path.join("/proc/net/dev_snmp6", self.intf)
115
116 with open(fn) as f:
117 for line in f.readlines():
118 key, val = line.split()
119
120 try:
121 type, key = key.split("6", 1)
122 except ValueError:
123 continue
124
125 type += "6"
126 val = int(val)
127
128 try:
129 res[type][key] = val
130 except KeyError:
131 res[type] = { key : val }
132
133 return res
134
135 def get(self, proto, key):
136 """
137 Retrieves a value from the internally
138 parse dictionary read from /proc/net/snmp.
139 """
140 try:
141 return self._data[proto][key]
142 except KeyError:
143 pass