Skip to content

Commit

Permalink
Merge pull request #11 from northpowered/10-config-params-for-local-d…
Browse files Browse the repository at this point in the history
…evice

Local params for simplesla-local device
  • Loading branch information
northpowered authored Sep 6, 2022
2 parents 9ee772d + 4ca36b9 commit 14469ce
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 40 deletions.
4 changes: 2 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions sla/__init__.py
Original file line number Diff line number Diff line change
@@ -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
3 changes: 0 additions & 3 deletions sla/config.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
13 changes: 9 additions & 4 deletions sla/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand All @@ -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
23 changes: 11 additions & 12 deletions sla/service.py
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -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
Expand All @@ -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):
Expand Down
42 changes: 23 additions & 19 deletions sla/sla.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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(
Expand All @@ -40,25 +44,25 @@ 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
)
}
)

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,
Expand All @@ -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,
Expand All @@ -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',
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 14469ce

Please sign in to comment.