]> git.ipfire.org Git - collecty.git/blame - src/collecty/util.py
Add graphs for IP fragmentation
[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
9823dfef
MT
30def __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
46def 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
53def 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)
29455c5f
MT
59
60def 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
75class ProcNetSnmpParser(object):
76 """
77 This class parses /proc/net/snmp{,6} and allows
78 easy access to the values.
79 """
80 def __init__(self, intf=None):
81 self.intf = intf
82 self._data = {}
83
84 if not self.intf:
85 self._data.update(self._parse())
86
87 self._data.update(self._parse6())
88
89 def _parse(self):
90 res = {}
91
92 with open("/proc/net/snmp") as f:
93 keys = {}
94
95 for line in f.readlines():
96 line = line.strip()
97
98 # Stop after an empty line
99 if not line:
100 break
101
102 type, values = line.split(": ", 1)
103
104 # Check if the keys are already known
105 if type in keys:
106 values = (int(v) for v in values.split())
107 res[type] = dict(zip(keys[type], values))
108
109 # Otherwise remember the keys
110 else:
111 keys[type] = values.split()
112
113 return res
114
115 def _parse6(self):
116 res = {}
117
118 fn = "/proc/net/snmp6"
119 if self.intf:
120 fn = os.path.join("/proc/net/dev_snmp6", self.intf)
121
122 with open(fn) as f:
123 for line in f.readlines():
124 key, val = line.split()
125
126 try:
127 type, key = key.split("6", 1)
128 except ValueError:
129 continue
130
131 type += "6"
132 val = int(val)
133
134 try:
135 res[type][key] = val
136 except KeyError:
137 res[type] = { key : val }
138
139 return res
140
141 def get(self, proto, key):
142 """
143 Retrieves a value from the internally
144 parse dictionary read from /proc/net/snmp.
145 """
146 try:
147 return self._data[proto][key]
148 except KeyError:
149 pass