]>
git.ipfire.org Git - oddments/collecty.git/blob - src/collecty/util.py
fdde1262e6f9aebdec3ca640da66f05f7979a25b
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 class ProcNetSnmpParser(object):
77 This class parses /proc/net/snmp{,6} and allows
78 easy access to the values.
80 def __init__(self
, intf
=None):
85 self
._data
.update(self
._parse
())
87 self
._data
.update(self
._parse
6())
92 with
open("/proc/net/snmp") as f
:
95 for line
in f
.readlines():
98 # Stop after an empty line
102 type, values
= line
.split(": ", 1)
104 # Check if the keys are already known
106 values
= (int(v
) for v
in values
.split())
107 res
[type] = dict(zip(keys
[type], values
))
109 # Otherwise remember the keys
111 keys
[type] = values
.split()
118 fn
= "/proc/net/snmp6"
120 fn
= os
.path
.join("/proc/net/dev_snmp6", self
.intf
)
123 for line
in f
.readlines():
124 key
, val
= line
.split()
127 type, key
= key
.split("6", 1)
137 res
[type] = { key
: val
}
141 def get(self
, proto
, key
):
143 Retrieves a value from the internally
144 parse dictionary read from /proc/net/snmp.
147 return self
._data
[proto
][key
]