]>
git.ipfire.org Git - collecty.git/blob - src/collecty/util.py
72fc0c1d78227e4c92d29137a15c041cf0953b44
2 ###############################################################################
4 # collecty - A system statistics collection daemon for IPFire #
5 # Copyright (C) 2015 IPFire development team #
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. #
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. #
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/>. #
20 ###############################################################################
25 log
= logging
.getLogger("collecty.util")
28 from .constants
import *
30 def __add_colour(colour
, amount
):
31 colour
= colour
.strip("#")
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
)
44 return "#%02x%02x%02x" % tuple(colour
)
46 def lighten(colour
, scale
=0.1):
48 Takes a hexadecimal colour code
49 and brightens the colour.
51 return __add_colour(colour
, 0xff * scale
)
53 def darken(colour
, scale
=0.1):
55 Takes a hexadecimal colour code
56 and darkens the colour.
58 return __add_colour(colour
, 0xff * -scale
)
60 def get_network_interfaces():
62 Returns all real network interfaces
64 for interface
in os
.listdir("/sys/class/net"):
65 # Skip some unwanted interfaces.
66 if interface
== "lo" or interface
.startswith("mon."):
69 path
= os
.path
.join("/sys/class/net", interface
)
70 if not os
.path
.isdir(path
):
75 def make_interval(interval
):
77 return INTERVALS
[interval
]
79 return "end-%s" % interval
81 class ProcNetSnmpParser(object):
83 This class parses /proc/net/snmp{,6} and allows
84 easy access to the values.
86 def __init__(self
, intf
=None):
91 self
._data
.update(self
._parse
())
93 self
._data
.update(self
._parse
6())
98 with
open("/proc/net/snmp") as f
:
101 for line
in f
.readlines():
104 # Stop after an empty line
108 type, values
= line
.split(": ", 1)
110 # Check if the keys are already known
112 values
= (int(v
) for v
in values
.split())
113 res
[type] = dict(zip(keys
[type], values
))
115 # Otherwise remember the keys
117 keys
[type] = values
.split()
124 fn
= "/proc/net/snmp6"
126 fn
= os
.path
.join("/proc/net/dev_snmp6", self
.intf
)
129 for line
in f
.readlines():
130 key
, val
= line
.split()
133 type, key
= key
.split("6", 1)
143 res
[type] = { key
: val
}
147 def get(self
, proto
, key
):
149 Retrieves a value from the internally
150 parse dictionary read from /proc/net/snmp.
153 return self
._data
[proto
][key
]