icmp ping #1

Open
fil wants to merge 6 commits from dev into main
3 changed files with 11 additions and 24 deletions
Showing only changes of commit 5e479bba43 - Show all commits

View file

@ -6,23 +6,12 @@ with open("config.yaml") as f:
cfg = yaml.load(f, Loader=yaml.FullLoader) cfg = yaml.load(f, Loader=yaml.FullLoader)
class Config: class Config:
config_data: dict def __init__(self):
__instance = None
def __getitem__(self, name):
if name in self.config_data:
return self.config_data[name]
return None
def __new__(cls):
if cls.__instance is None:
cls.__instance = super().__new__(cls)
cls.__instance.__init()
return cls.__instance
def __init(self):
load_dotenv() load_dotenv()
self.config_data = cfg self.config_data = cfg
for key in cfg.keys(): for key in cfg.keys():
if key in os.environ: if key in os.environ:
self.config_data[key] = os.environ[key] self.config_data[key] = os.environ[key]
setattr(self, key, self.config_data[key])
CONFIG = Config()

View file

@ -1,18 +1,17 @@
from config import Config from config import CONFIG
import psycopg2.extras import psycopg2.extras
import psycopg2 import psycopg2
class Database(): class Database():
def __init__(self): def __init__(self):
self.config = Config()
self.connect() self.connect()
def connect(self): def connect(self):
self.conn = psycopg2.connect( self.conn = psycopg2.connect(
host=self.config["db_host"], host=CONFIG.db_host,
database=self.config["db_database"], database=CONFIG.db_database,
user=self.config["db_user"], user=CONFIG.db_user,
password=self.config["db_password"] password=CONFIG.db_password
) )
self.cur = self.conn.cursor( self.cur = self.conn.cursor(
cursor_factory=psycopg2.extras.RealDictCursor) cursor_factory=psycopg2.extras.RealDictCursor)

View file

@ -1,12 +1,11 @@
from database import Database from database import Database
from pythonping import ping from pythonping import ping
from tcppinglib import tcpping from tcppinglib import tcpping
from config import Config from config import CONFIG
class PingPong: class PingPong:
def __init__(self): def __init__(self):
self.db = Database() self.db = Database()
self.config = Config()
def icmp_ping(self, service): def icmp_ping(self, service):
r = {} r = {}
@ -29,7 +28,7 @@ class PingPong:
def start(self): def start(self):
latency = { latency = {
"location": self.config["location"] "location": CONFIG.location
} }
for el in self.db.get_all_services(): for el in self.db.get_all_services():
state = self.db.create_state(el['id'])[0] state = self.db.create_state(el['id'])[0]