From a77a3ba218653b2fd40ad38e1168d4cae477e5dd Mon Sep 17 00:00:00 2001
From: romanov <romanov@mail.mm71>
Date: Mon, 5 Sep 2022 11:30:51 +0300
Subject: [PATCH 1/2] Fixed closed template IO wrapper

---
 Dockerfile                      |  2 +-
 sla/config.py                   |  6 +++---
 sla/device.py                   | 16 ++++++++++++----
 sla/sla.py                      |  1 -
 templates/potok-km-122.template |  5 +++++
 5 files changed, 21 insertions(+), 9 deletions(-)
 create mode 100644 templates/potok-km-122.template

diff --git a/Dockerfile b/Dockerfile
index 9854c99..e9b6167 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -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 . .
 
diff --git a/sla/config.py b/sla/config.py
index b5837e2..46f8e3b 100644
--- a/sla/config.py
+++ b/sla/config.py
@@ -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:
diff --git a/sla/device.py b/sla/device.py
index bb880b2..6c7eb5b 100644
--- a/sla/device.py
+++ b/sla/device.py
@@ -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:
@@ -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",
@@ -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 _
@@ -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:
@@ -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)
diff --git a/sla/sla.py b/sla/sla.py
index 95f5b46..96f3306 100644
--- a/sla/sla.py
+++ b/sla/sla.py
@@ -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)
diff --git a/templates/potok-km-122.template b/templates/potok-km-122.template
new file mode 100644
index 0000000..bfcbf6b
--- /dev/null
+++ b/templates/potok-km-122.template
@@ -0,0 +1,5 @@
+Value RTT (.*)
+Value TTL (.*)
+
+Start
+  ^(.*) TTL: ${TTL}, time: ${RTT} us -> Record
\ No newline at end of file

From 5671f2156d3bcda47b45c5f9318d7aace479aee6 Mon Sep 17 00:00:00 2001
From: romanov <romanov@mail.mm71>
Date: Mon, 5 Sep 2022 11:41:06 +0300
Subject: [PATCH 2/2] Updated README.md and bumped version

---
 README.md  | 12 ++++++++++++
 main.py    |  2 +-
 sla/sla.py |  1 +
 3 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/README.md b/README.md
index cff92b0..74b0c2c 100644
--- a/README.md
+++ b/README.md
@@ -16,6 +16,7 @@ Supported devices:
   - Juniper
   - Eltex (ESR series)
   - MT M716
+  - Potok KM-122
 
 TODO:
   - Extend devices support
@@ -41,6 +42,17 @@ optional arguments:
                         Logging path {stdout,FILE}
   --version             show programs version number and exit
 ```
+
+## SLA statuses
+
+| Status      | Int value   | Reccomended color | Description |
+| ----------- | ----------- |------------------ |------------ |
+| NoData      | 0           | Black             | No recieved data or parcing error |
+| Normal      | 1           | Green             | Target is available and RTT less then policy |
+| Warning     | 2           | Yellow            | Target is available and RTT bigger then policy |
+| Error       | 3           | Red               | Target is unavailable |
+| OutOfService| 4           | Green             | Target is available and policy was not defined |
+
 ### Install:
 
 ```bash
diff --git a/main.py b/main.py
index 481416b..937d318 100644
--- a/main.py
+++ b/main.py
@@ -6,7 +6,7 @@
 
 if __name__ == "__main__":
 
-    __version__ = "1.0.2"
+    __version__ = "1.0.3"
     __author__ = "https://github.com/northpowered"
 
     ap = argparse.ArgumentParser()
diff --git a/sla/sla.py b/sla/sla.py
index 96f3306..95f5b46 100644
--- a/sla/sla.py
+++ b/sla/sla.py
@@ -7,6 +7,7 @@
 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)