]>
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")
27 from .constants
import *
29 def get_network_interfaces():
31 Returns all real network interfaces
33 for interface
in os
.listdir("/sys/class/net"):
34 # Skip some unwanted interfaces.
35 if interface
== "lo" or interface
.startswith("mon."):
38 path
= os
.path
.join("/sys/class/net", interface
)
39 if not os
.path
.isdir(path
):
44 def make_interval(interval
):
55 return intervals
[interval
]
57 return "end-%s" % interval
59 def guess_format(filename
):
61 Returns the best format by filename extension
63 parts
= filename
.split(".")
66 # The extension is the last part
69 # Image formats are all uppercase
70 extension
= extension
.upper()
72 if extension
in SUPPORTED_IMAGE_FORMATS
:
75 # Otherwise fall back to the default format
76 return DEFAULT_IMAGE_FORMAT
78 class ProcNetSnmpParser(object):
80 This class parses /proc/net/snmp{,6} and allows
81 easy access to the values.
83 def __init__(self
, intf
=None):
88 self
._data
.update(self
._parse
())
90 self
._data
.update(self
._parse
6())
95 with
open("/proc/net/snmp") as f
:
98 for line
in f
.readlines():
101 # Stop after an empty line
105 type, values
= line
.split(": ", 1)
107 # Check if the keys are already known
109 values
= (int(v
) for v
in values
.split())
110 res
[type] = dict(zip(keys
[type], values
))
112 # Otherwise remember the keys
114 keys
[type] = values
.split()
121 fn
= "/proc/net/snmp6"
123 fn
= os
.path
.join("/proc/net/dev_snmp6", self
.intf
)
126 for line
in f
.readlines():
127 key
, val
= line
.split()
130 type, key
= key
.split("6", 1)
140 res
[type] = { key
: val
}
144 def get(self
, proto
, key
):
146 Retrieves a value from the internally
147 parse dictionary read from /proc/net/snmp.
150 return self
._data
[proto
][key
]