]>
git.ipfire.org Git - collecty.git/blob - src/collecty/util.py
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 transparency(colour
, scale
=0.1):
62 Adds transparency to the given colour code
64 return "%s%02X" % (colour
, 0xff * scale
)
66 def get_network_interfaces():
68 Returns all real network interfaces
70 for interface
in os
.listdir("/sys/class/net"):
71 # Skip some unwanted interfaces.
72 if interface
== "lo" or interface
.startswith("mon."):
75 path
= os
.path
.join("/sys/class/net", interface
)
76 if not os
.path
.isdir(path
):
81 def make_interval(interval
):
83 return INTERVALS
[interval
]
85 return "end-%s" % interval
87 class ProcNetSnmpParser(object):
89 This class parses /proc/net/snmp{,6} and allows
90 easy access to the values.
92 def __init__(self
, intf
=None):
97 self
._data
.update(self
._parse
())
99 self
._data
.update(self
._parse
6())
104 with
open("/proc/net/snmp") as f
:
107 for line
in f
.readlines():
110 # Stop after an empty line
114 type, values
= line
.split(": ", 1)
116 # Check if the keys are already known
118 values
= (int(v
) for v
in values
.split())
119 res
[type] = dict(zip(keys
[type], values
))
121 # Otherwise remember the keys
123 keys
[type] = values
.split()
130 fn
= "/proc/net/snmp6"
132 fn
= os
.path
.join("/proc/net/dev_snmp6", self
.intf
)
135 for line
in f
.readlines():
136 key
, val
= line
.split()
139 type, key
= key
.split("6", 1)
149 res
[type] = { key
: val
}
153 def get(self
, proto
, key
):
155 Retrieves a value from the internally
156 parse dictionary read from /proc/net/snmp.
159 return self
._data
[proto
][key
]