diff --git a/main.py b/main.py index 1de9088..496f428 100644 --- a/main.py +++ b/main.py @@ -36,8 +36,8 @@ if not log_dest: log_dest = os.getenv("SSLA_LD", "stdout") try: - from sla.config import config - config.load(args.get("config")) + from sla import SLAConfig + SLAConfig.load(args.get("config")) from sla.sla import Sla sla = Sla( config_file=args.get("config"), log_level=log_level, log_dest=log_dest diff --git a/sla/__init__.py b/sla/__init__.py index e69de29..0165fbe 100644 --- a/sla/__init__.py +++ b/sla/__init__.py @@ -0,0 +1,8 @@ +from .config import config as SLAConfig +from .device import Device as SLADevice +from .service import ( + Service as SLAService, + ServiceGroup as SLAServiceGroup, + SubService as SLASubService +) +from .policy import Policy as SLAPolicy diff --git a/sla/config.py b/sla/config.py index 9b95bb8..0b507f9 100644 --- a/sla/config.py +++ b/sla/config.py @@ -1,8 +1,5 @@ import yaml from pydantic import BaseModel, ValidationError, validator, BaseSettings -from sla.service import Service -from sla.device import Device -from sla.policy import Policy from sla.logger import LG import os import ipaddress diff --git a/sla/device.py b/sla/device.py index cc53337..c6cff75 100644 --- a/sla/device.py +++ b/sla/device.py @@ -10,6 +10,7 @@ from ping3 import ping from sla.tracer import device_tracer from pydantic import BaseModel +from sla import SLAConfig class DeviceMapping(BaseModel): @@ -111,7 +112,14 @@ def __batch_remote_check(self, hnd: BaseConnection, targets: list, fsm: textfsm. def __get_rtt_local(self, target: str): _ = None try: - _ = ping(target, timeout=1, unit="ms", src_addr="0.0.0.0", ttl=64, size=56) + _ = ping( + target, + timeout=SLAConfig.Local.timeout, + unit=SLAConfig.Global.unit, + src_addr=SLAConfig.Local.src_addr, + ttl=SLAConfig.Local.ttl, + size=SLAConfig.Local.timeout + ) except UnicodeError: LG.error(f"Wrong Ipv4 syntax: {target}") except OSError as error: @@ -124,7 +132,6 @@ def __get_rtt_local(self, target: str): def get_rtt(self, target: str | list) -> str | dict: with device_tracer.start_as_current_span(__name__) as span: span.set_attribute("device.type", self.type) - span.set_attribute("device.target", target) rtt = None if self.type == "local": rtt = self.__get_rtt_local(target) @@ -146,6 +153,4 @@ def get_rtt(self, target: str | list) -> str | dict: LG.warning(f"Authentication with {self.name} failed. Check credentials") else: LG.error(f"Unknown device type {self.type}") - if rtt is not None: - span.set_attribute("device.rtt", rtt) return rtt diff --git a/sla/service.py b/sla/service.py index cc83ee2..d6171bb 100644 --- a/sla/service.py +++ b/sla/service.py @@ -1,9 +1,8 @@ -from sla.policy import Policy -from sla.device import Device +from .device import Device as SLADevice +from .policy import Policy as SLAPolicy from threading import Lock from time import sleep -from sla.logger import LG -import sys + SERVICE_RESULTS: dict = dict() SERVICE_STATUSES = { @@ -23,10 +22,10 @@ def __init__( delay: int, description: str = str(), verbose_name: str = str(), - policy: Policy = None, + policy: SLAPolicy = None, ) -> None: self.name: str = name - self.policy: Policy = policy + self.policy: SLAPolicy = policy self.target: str = target self.delay: int = delay self.description: str = description @@ -74,7 +73,7 @@ def __init__( delay: int, description: str = str(), verbose_name: str = str(), - policy: Policy = None + policy: SLAPolicy = None ) -> None: super().__init__(name, target, delay, description, verbose_name, policy) @@ -83,13 +82,13 @@ class ServiceGroup: def __init__( self, name: str, - device: Device, + device: SLADevice, services: list[SubService], description: str = "", verbose_name: str = "", ) -> None: self.name: str = name - self.device: Device = device + self.device: SLADevice = device self.services: list[SubService] = services self.description: str = description self.verbose_name: str = verbose_name @@ -110,12 +109,12 @@ def __init__( name: str, target: str, delay: int, - device: Device, + device: SLADevice, description: str = str(), verbose_name: str = str(), - policy: Policy = None, + policy: SLAPolicy = None, ) -> None: - self.device: Device = device + self.device: SLADevice = device super().__init__(name, target, delay, description, verbose_name, policy) def check(self): diff --git a/sla/sla.py b/sla/sla.py index 1df7339..566b945 100644 --- a/sla/sla.py +++ b/sla/sla.py @@ -1,12 +1,16 @@ from threading import Thread, Lock -from sla.device import Device as SLADevice +from sla import ( + SLADevice, + SLAService, + SLAServiceGroup, + SLAConfig, + SLAPolicy, + SLASubService +) from sla.generator import REGISTRY, Collector from prometheus_client import start_http_server from time import sleep from sla.logger import LG, logger_init -from sla.policy import Policy -from sla.service import Service, ServiceGroup, SubService -from sla.config import config class Sla: @@ -21,8 +25,8 @@ def __init__(self, config_file: str, log_level: str, log_dest: str) -> None: self.policies: dict = dict() def _load_devices(self): - if config.Policies.Policies: - for device in config.Devices.Devices: + if SLAConfig.Policies.Policies: + for device in SLAConfig.Devices.Devices: self.devices.update( { device.name: SLADevice( @@ -40,12 +44,12 @@ def _load_devices(self): ) def _load_policies(self): - if not config.Policies.Policies: + if not SLAConfig.Policies.Policies: return - for policy in config.Policies.Policies: + for policy in SLAConfig.Policies.Policies: self.policies.update( { - policy.name: Policy( + policy.name: SLAPolicy( name=policy.name, max_rtt=policy.max_rtt ) @@ -53,12 +57,12 @@ def _load_policies(self): ) def _load_services(self): - if not config.Services.Services: + if not SLAConfig.Services.Services: return - for service in config.Services.Services: + for service in SLAConfig.Services.Services: self.services.update( { - service.name: Service( + service.name: SLAService( name=service.name, target=service.target, delay=service.delay, @@ -71,17 +75,17 @@ def _load_services(self): ) def _load_service_groups(self): - if not config.ServicesGroups.ServicesGroups: + if not SLAConfig.ServicesGroups.ServicesGroups: return - for service_group in config.ServicesGroups.ServicesGroups: - sub_services: list[SubService] = list() + for service_group in SLAConfig.ServicesGroups.ServicesGroups: + sub_services: list[SLASubService] = list() for sub in service_group.services: if not sub.delay: sub.delay = service_group.delay if not sub.policy: sub.policy = service_group.policy sub_services.append( - SubService( + SLASubService( name=sub.name, target=sub.target, delay=sub.delay, @@ -92,7 +96,7 @@ def _load_service_groups(self): ) self.service_groups.update( { - service_group.name: ServiceGroup( + service_group.name: SLAServiceGroup( name=service_group.name, device=self.devices.get(service_group.device), description='foobar', @@ -125,12 +129,12 @@ def _create_services(self): REGISTRY.register(Collector()) LG.info("Services was registered in registry collector") - _ = (config.Server.bind_address, config.Server.port) + _ = (SLAConfig.Server.bind_address, SLAConfig.Server.port) start_http_server(_[1], _[0]) LG.info(f"Prometeus HTTP endpoint started on {_[0]}:{_[1]}") def __collect(self): - _ = config.Server.refresh_time + _ = SLAConfig.Server.refresh_time while True: with Lock(): REGISTRY.collect()