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