-
Notifications
You must be signed in to change notification settings - Fork 590
/
ifaces.py
38 lines (30 loc) · 1.29 KB
/
ifaces.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
"""
Copyright 2020 The Magma Authors.
This source code is licensed under the BSD-style license found in the
LICENSE file in the root directory of this source tree.
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import asyncio
import netifaces
from magma.pipelined.metrics import NETWORK_IFACE_STATUS
POLL_INTERVAL_SECONDS = 3
@asyncio.coroutine
def monitor_ifaces(iface_names):
"""
Call to poll the network interfaces and set the corresponding metric
"""
while True:
active = set(netifaces.interfaces())
for iface in iface_names:
status = 1 if iface in active else 0
NETWORK_IFACE_STATUS.labels(iface_name=iface).set(status)
yield from asyncio.sleep(POLL_INTERVAL_SECONDS)
def get_mac_address_from_iface(interface_name: str) -> str:
if_addresses = netifaces.ifaddresses(interface_name)[netifaces.AF_LINK]
if not if_addresses or not if_addresses[0].get('addr'):
raise ValueError(f"No mac address found for interface {interface_name}")
return if_addresses[0]['addr']