]> git.ipfire.org Git - collecty.git/blame - src/collecty/util.py
Introduce a colour scheme and fix design of the graphs
[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 59
03ba5630
MT
60def transparency(colour, scale=0.1):
61 """
62 Adds transparency to the given colour code
63 """
64 return "%s%02X" % (colour, 0xff * scale)
65
29455c5f
MT
66def get_network_interfaces():
67 """
68 Returns all real network interfaces
69 """
70 for interface in os.listdir("/sys/class/net"):
71 # Skip some unwanted interfaces.
72 if interface == "lo" or interface.startswith("mon."):
73 continue
74
75 path = os.path.join("/sys/class/net", interface)
76 if not os.path.isdir(path):
77 continue
78
79 yield interface
80
4d9ed86e
MT
81def make_interval(interval):
82 try:
83 return INTERVALS[interval]
84 except KeyError:
85 return "end-%s" % interval
86
29455c5f
MT
87class ProcNetSnmpParser(object):
88 """
89 This class parses /proc/net/snmp{,6} and allows
90 easy access to the values.
91 """
92 def __init__(self, intf=None):
93 self.intf = intf
94 self._data = {}
95
96 if not self.intf:
97 self._data.update(self._parse())
98
99 self._data.update(self._parse6())
100
101 def _parse(self):
102 res = {}
103
104 with open("/proc/net/snmp") as f:
105 keys = {}
106
107 for line in f.readlines():
108 line = line.strip()
109
110 # Stop after an empty line
111 if not line:
112 break
113
114 type, values = line.split(": ", 1)
115
116 # Check if the keys are already known
117 if type in keys:
118 values = (int(v) for v in values.split())
119 res[type] = dict(zip(keys[type], values))
120
121 # Otherwise remember the keys
122 else:
123 keys[type] = values.split()
124
125 return res
126
127 def _parse6(self):
128 res = {}
129
130 fn = "/proc/net/snmp6"
131 if self.intf:
132 fn = os.path.join("/proc/net/dev_snmp6", self.intf)
133
134 with open(fn) as f:
135 for line in f.readlines():
136 key, val = line.split()
137
138 try:
139 type, key = key.split("6", 1)
140 except ValueError:
141 continue
142
143 type += "6"
144 val = int(val)
145
146 try:
147 res[type][key] = val
148 except KeyError:
149 res[type] = { key : val }
150
151 return res
152
153 def get(self, proto, key):
154 """
155 Retrieves a value from the internally
156 parse dictionary read from /proc/net/snmp.
157 """
158 try:
159 return self._data[proto][key]
160 except KeyError:
161 pass