Skip to content

Commit

Permalink
Fixed closed template IO wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
romanov committed Sep 5, 2022
1 parent 1e2cc74 commit a77a3ba
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ FROM python:3.10-buster
WORKDIR /app

COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
RUN pip3 install --no-cache-dir -r requirements.txt

COPY . .

Expand Down
6 changes: 3 additions & 3 deletions sla/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ def _load_config(self, config_from_file):
)

# Appending template to device

with open(f"templates/{device['type']}.template") as template:
device["template"] = template
device["template"] = f"templates/{device['type']}.template"
# with open(f"templates/{device['type']}.template", "r") as template:
# device["template"] = template

# Parsing transport field in device object
try:
Expand Down
16 changes: 12 additions & 4 deletions sla/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from sla.logger import LG
from ping3 import ping
from sla.tracer import device_tracer, rtt_tracer
from io import TextIOWrapper


class Device:
Expand Down Expand Up @@ -41,6 +42,10 @@ def __get_rtt_remote(self, target: str):
"command": f"ping {target} detailed packets 1",
"connections": {"ssh": "eltex"},
},
"potok-km-122": {
"command": f"ping {target} count 1",
"connections": {"ssh": "generic"},
}
}
connection = {
"device_type": "generic",
Expand Down Expand Up @@ -73,13 +78,14 @@ def __get_rtt_remote(self, target: str):
LG.debug(f"Connected to {self.name} device")
result = hnd.send_command(command)
LG.debug(f"RTT command sent to {self.name} device")
fsm = textfsm.TextFSM(self.template)
with open(self.template, "r") as template:
fsm = textfsm.TextFSM(template)
output = fsm.ParseText(result)
try:
_ = float(output[0][0])
except IndexError:
except IndexError as ex:
_ = None
except Exception:
except Exception as ex:
_ = False
finally:
return _
Expand All @@ -89,6 +95,8 @@ def __get_rtt_remote(self, target: str):
LG.warning(f"Unreachable device. Connection with {self.name} failed")
except NetmikoAuthenticationException as error:
LG.warning(f"Authentication with {self.name} failed. Check credentials")
except Exception as ex:
print(ex)
else:
pass
finally:
Expand All @@ -112,7 +120,7 @@ def get_rtt(self, target: str):
span.set_attribute("device.type", self.type)
span.set_attribute("device.target", target)
rtt = int()
if self.type in ["m716", "cisco"]:
if self.type in ["m716", "cisco", "juniper", "eltex", "potok-km-122"]:
rtt = self.__get_rtt_remote(target)
elif self.type == "local":
rtt = self.__get_rtt_local(target)
Expand Down
1 change: 0 additions & 1 deletion sla/sla.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from sla.service import Service
import sys


class Sla:
def __init__(self, config_file: str, log_level: str, log_dest: str) -> None:
logger_init(log_level, log_dest)
Expand Down
5 changes: 5 additions & 0 deletions templates/potok-km-122.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Value RTT (.*)
Value TTL (.*)

Start
^(.*) TTL: ${TTL}, time: ${RTT} us -> Record

0 comments on commit a77a3ba

Please sign in to comment.