Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove iperf feature and move to ruff #36

Merged
merged 6 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ venv.bak/

# generated by build
coverage-*3*.xml
junit-*3*.xml
junit*.xml
pylama-parent.ini
tox-parent.ini
adapt_template.sh
Expand Down
1 change: 1 addition & 0 deletions netprobify/common.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Common functions."""

import re

from socket import getaddrinfo, AF_INET, AF_INET6, IPPROTO_TCP
Expand Down
1 change: 1 addition & 0 deletions netprobify/external.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Module for all external functions."""

import math


Expand Down
98 changes: 5 additions & 93 deletions netprobify/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@
ICMP_LOSS_RATIO,
ICMP_ROUND_TRIP,
ICMP_SENT,
IPERF_BANDWIDTH,
IPERF_LOSS,
IPERF_LOSS_RATIO,
IPERF_OUT_OF_ORDER,
IPERF_SENT,
LIST_TARGET_MEASUREMENT_METRICS,
LIST_TARGET_METRICS,
NETPROBIFY_INFO,
Expand All @@ -62,7 +57,6 @@
)
from netprobify.protocol.common.protocols import list_self_ips
from netprobify.protocol.icmp_ping import ICMPping
from netprobify.protocol.iperf import Iperf
from netprobify.protocol.target import Group
from netprobify.protocol.tcpsyn import TCPsyn
from netprobify.protocol.udp_unreachable import UDPunreachable
Expand Down Expand Up @@ -106,9 +100,7 @@ def __init__(self, f="config.yaml"):
self.config_file = f
self.list_targets = []
self.list_target_name = []
self.list_special_targets = []
self.list_dynamic_targets = []
self.list_dynamic_special_targets = []
self.shared_dynamic_targets = {}
self.shared_dynamic_targets_backup = {}
self.list_groups = []
Expand Down Expand Up @@ -235,45 +227,14 @@ def load_target_conf(self, target, target_name, target_groups):
creation_date=target.get("creation_date"),
lifetime=target.get("lifetime"),
)
elif target["type"] == "iperf":
target = Iperf(
name=target_name,
active=True,
description=target.get("description", target_name),
destination=None,
config_destination=target["destination"],
address_family=target.get("address_family", DEFAULT_ADDRESS_FAMILY),
dst_port=target["dst_port"],
threshold=target.get("threshold"),
state=target.get("state"),
alert_level=target.get("alert_level", "no_alert"),
is_dynamic=target.get("is_dynamic", False),
# dns_update interval is global if not specified
dns_update_interval=target.get(
"dns_update_interval", self.global_vars.get("dns_update_interval", 0)
),
groups=target_groups,
duration=target.get("iperf_parameters", {}).get("duration", 5),
bandwidth=target.get("iperf_parameters", {}).get("bandwidth_per_stream", "1M"),
protocol=target.get("iperf_parameters", {}).get("protocol", "udp"),
num_streams=target.get("iperf_parameters", {}).get("nb_parallel_streams", 1),
creation_date=target.get("creation_date"),
lifetime=target.get("lifetime"),
)
else:
return

# we put the target in the right list
if target.is_dynamic:
if target.is_special:
self.list_dynamic_special_targets.append(target)
else:
self.list_dynamic_targets.append(target)
self.list_dynamic_targets.append(target)
else:
if target.is_special:
self.list_special_targets.append(target)
else:
self.list_targets.append(target)
self.list_targets.append(target)

def load_conf(self, schema_file="schema_config.yaml"):
"""Load the configuration from a config file.
Expand All @@ -286,7 +247,6 @@ def load_conf(self, schema_file="schema_config.yaml"):
# cleaning targets list
self.list_groups = []
self.list_targets = []
self.list_special_targets = []
self.list_target_name = []
self.global_vars = {}
self.first_iter = True
Expand Down Expand Up @@ -388,7 +348,7 @@ def load_conf(self, schema_file="schema_config.yaml"):

log.debug("Target %s created", target_name)

if len(target_groups) == 0 and target["type"] != "iperf":
if len(target_groups) == 0:
log.warning("Target %s disabled: not associated to any group", target_name)

def update_hosts(self, force=False):
Expand All @@ -400,9 +360,7 @@ def update_hosts(self, force=False):
self_ips = {af: list_self_ips(af, scapyconf) for af in ("ipv4", "ipv6")}
for target in itertools.chain(
self.list_targets,
self.list_special_targets,
self.list_dynamic_targets,
self.list_dynamic_special_targets,
):
if len(target.groups):
changed = False
Expand Down Expand Up @@ -773,47 +731,6 @@ def get_metrics(self):
destination=name,
address_family=address_family,
).inc(port_mismatch)
elif res["probing_type"] == "iperf":
loss_ratio = res["loss"] / res["sent"] if res["sent"] != 0 else 0
IPERF_SENT.labels(
probe_name=self.global_vars["probe_name"],
destination=name,
address_family=address_family,
state=res["state"],
group=grp.name,
).set(res["sent"])

IPERF_LOSS.labels(
probe_name=self.global_vars["probe_name"],
destination=name,
address_family=address_family,
state=res["state"],
group=grp.name,
).set(res["loss"])

IPERF_LOSS_RATIO.labels(
probe_name=self.global_vars["probe_name"],
destination=name,
address_family=address_family,
state=res["state"],
group=grp.name,
).set(loss_ratio)

IPERF_BANDWIDTH.labels(
probe_name=self.global_vars["probe_name"],
destination=name,
address_family=address_family,
state=res["state"],
group=grp.name,
).set(res["bandwidth"])

IPERF_OUT_OF_ORDER.labels(
probe_name=self.global_vars["probe_name"],
destination=name,
address_family=address_family,
state=res["state"],
group=grp.name,
).set(res["out_of_order"])

def reload_request(self, signum, frame):
"""Reload handler for SIGHUP. Will reload the configuration.
Expand Down Expand Up @@ -865,7 +782,7 @@ def get_dynamic_targets(self):
dynamic_targets = self.shared_dynamic_targets[inventory]
for target in dynamic_targets:
if self.check_expiration(target):
log.info("{}: {} is expired".format(inventory, target["hostname"]))
log.info("%s: %s is expired", inventory, target["hostname"])
dynamic_targets.remove(target)
continue
self.shared_dynamic_targets[inventory] = dynamic_targets
Expand All @@ -880,7 +797,6 @@ def get_dynamic_targets(self):

# we reset the targets
self.list_dynamic_targets = []
self.list_dynamic_special_targets = []

# get targets by inventory
for inventory in self.shared_dynamic_targets.keys():
Expand Down Expand Up @@ -1027,7 +943,7 @@ def start_prometheus_server(self):
)
serve(
app,
host=self.global_vars.get("prometheus_address", "0.0.0.0"),
host=self.global_vars.get("prometheus_address", "0.0.0.0"), # noqa: S104
port=self.global_vars["prometheus_port"],
)

Expand Down Expand Up @@ -1076,10 +992,6 @@ def main(self):
self.global_vars.get("timeout", 3600),
)

# start probing only for special targets which are not a priority
remaining_time = self.global_vars["interval"] - (time.time() - round_start)
self.start_processes(self.list_special_targets, remaining_time)

# the first iteration is done
self.first_iter = False

Expand Down
32 changes: 0 additions & 32 deletions netprobify/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,33 +88,6 @@
["probe_name", "destination", "address_family"],
)

# prometheus metrics - iperf probe
IPERF_SENT = Gauge(
"iperf_sent_total",
"number of sent packets reported by iperf.",
["probe_name", "destination", "address_family", "state", "group"],
)
IPERF_LOSS = Gauge(
"iperf_loss_total",
"number of lost packets reported by iperf.",
["probe_name", "destination", "address_family", "state", "group"],
)
IPERF_LOSS_RATIO = Gauge(
"iperf_loss_ratio",
"loss ratio reported by iperf.",
["probe_name", "destination", "address_family", "state", "group"],
)
IPERF_BANDWIDTH = Gauge(
"iperf_bandwidth_bps",
"bandwidth reported by iperf.",
["probe_name", "destination", "address_family", "state", "group"],
)
IPERF_OUT_OF_ORDER = Gauge(
"iperf_out_of_order_count",
"port source/destination mismatch.",
["probe_name", "destination", "address_family", "state", "group"],
)

# prometheus metrics - common
THRESHOLD = Gauge(
"threshold",
Expand Down Expand Up @@ -175,11 +148,6 @@
ICMP_LOSS,
ICMP_ROUND_TRIP,
ICMP_LOSS_RATIO,
IPERF_SENT,
IPERF_LOSS,
IPERF_LOSS_RATIO,
IPERF_BANDWIDTH,
IPERF_OUT_OF_ORDER,
]

LIST_TARGET_METRICS = LIST_TARGET_HEALTH_METRICS + LIST_TARGET_MEASUREMENT_METRICS
5 changes: 3 additions & 2 deletions netprobify/protocol/common/protocols.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Protocol related functions."""

from ipaddress import ip_network

from scapy.all import ICMP, IP, ICMPv6EchoRequest, IPv6
Expand Down Expand Up @@ -61,9 +62,9 @@ def list_self_ips(address_family, conf):
conf -- input configuration
"""
if address_family == "ipv4":
return set([addr[4] for addr in conf.route.routes])
return {addr[4] for addr in conf.route.routes}
elif address_family == "ipv6":
return set([addr[4][0] for addr in conf.route6.routes])
return {addr[4][0] for addr in conf.route6.routes}
raise ValueError("unknown address-family")


Expand Down
1 change: 1 addition & 0 deletions netprobify/protocol/icmp_ping.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Module for ICMP probing."""

import logging
from random import randint

Expand Down
Loading
Loading