icmp ping #1
5 changed files with 123 additions and 1 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -123,6 +123,7 @@ celerybeat.pid
|
||||||
# Environments
|
# Environments
|
||||||
.env
|
.env
|
||||||
.venv
|
.venv
|
||||||
|
.macvenv
|
||||||
env/
|
env/
|
||||||
venv/
|
venv/
|
||||||
ENV/
|
ENV/
|
||||||
|
|
@ -159,4 +160,3 @@ cython_debug/
|
||||||
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
||||||
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||||
#.idea/
|
#.idea/
|
||||||
|
|
||||||
|
|
|
||||||
33
config.py
Normal file
33
config.py
Normal file
|
|
@ -0,0 +1,33 @@
|
||||||
|
import os
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
|
defConfig = {
|
||||||
|
"DB_HOST": "",
|
||||||
|
"DB_DATABASE": "",
|
||||||
|
"DB_USER": "",
|
||||||
|
"DB_PASSWORD": "",
|
||||||
|
"LOCATION":""
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
config_data: dict
|
||||||
|
__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()
|
||||||
|
self.config_data = defConfig
|
||||||
|
for key in defConfig.keys():
|
||||||
|
if key in os.environ:
|
||||||
|
self.config_data[key] = os.environ[key]
|
||||||
56
database.py
Normal file
56
database.py
Normal file
|
|
@ -0,0 +1,56 @@
|
||||||
|
from config import Config
|
||||||
|
import psycopg2.extras
|
||||||
|
import psycopg2
|
||||||
|
|
||||||
|
|
||||||
|
class Database():
|
||||||
|
def __init__(self):
|
||||||
|
self.config = Config()
|
||||||
|
self.connect()
|
||||||
|
|
||||||
|
def connect(self):
|
||||||
|
self.conn = psycopg2.connect(
|
||||||
|
host=self.config["DB_HOST"],
|
||||||
|
database=self.config["DB_DATABASE"],
|
||||||
|
user=self.config["DB_USER"],
|
||||||
|
password=self.config["DB_PASSWORD"]
|
||||||
|
)
|
||||||
|
self.cur = self.conn.cursor(
|
||||||
|
cursor_factory=psycopg2.extras.RealDictCursor)
|
||||||
|
|
||||||
|
def get_all_services(self):
|
||||||
|
self.cur.execute("SELECT * FROM service")
|
||||||
|
return self.cur.fetchall()
|
||||||
|
|
||||||
|
def get_open_incident(self, id):
|
||||||
|
self.cur.execute("""SELECT incident.id FROM incident
|
||||||
|
JOIN state ON incident.state_id = state.id
|
||||||
|
JOIN service ON state.service_id = service.id
|
||||||
|
WHERE service.id = %s AND t_end IS NULL;""", (str(id),))
|
||||||
|
return self.cur.fetchall()
|
||||||
|
|
||||||
|
def create_state(self, id):
|
||||||
|
self.cur.execute("""INSERT INTO state (service_id)
|
||||||
|
VALUES (%s) RETURNING *;""", (str(id),))
|
||||||
|
self.conn.commit()
|
||||||
|
return self.cur.fetchall()
|
||||||
|
|
||||||
|
def create_incident(self, *argv):
|
||||||
|
self.cur.execute("""INSERT INTO incident (state_id, error)
|
||||||
|
VALUES (%s,%s) RETURNING *;""", *argv)
|
||||||
|
self.conn.commit()
|
||||||
|
return self.cur.fetchall()
|
||||||
|
fil marked this conversation as resolved
|
|||||||
|
|
||||||
|
def close_incident(self, id):
|
||||||
|
self.cur.execute("""UPDATE incident
|
||||||
|
SET t_end = NOW()
|
||||||
|
WHERE id = %s RETURNING *;""", (str(id),))
|
||||||
|
self.conn.commit()
|
||||||
|
return self.cur.fetchall()
|
||||||
|
|
||||||
|
def add_latency(self, *argv):
|
||||||
|
self.cur.execute("""INSERT INTO latency
|
||||||
|
(state_id, location, ping)
|
||||||
|
VALUES (%s,%s,%s) RETURNING *;""", *argv)
|
||||||
|
self.conn.commit()
|
||||||
|
return self.cur.fetchall()
|
||||||
5
main.py
Normal file
5
main.py
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
from pingpong import PingPong
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
runner = PingPong()
|
||||||
|
runner.start()
|
||||||
28
pingpong.py
Normal file
28
pingpong.py
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
from database import Database
|
||||||
|
from pythonping import ping
|
||||||
|
from config import Config
|
||||||
|
|
||||||
|
class PingPong:
|
||||||
|
def __init__(self):
|
||||||
|
self.db = Database()
|
||||||
|
self.config = Config()
|
||||||
|
|
||||||
|
def start(self):
|
||||||
|
latency = {
|
||||||
|
"location": self.config["LOCATION"]
|
||||||
|
}
|
||||||
|
for el in self.db.get_all_services():
|
||||||
|
state = self.db.create_state(el['id'])[0]
|
||||||
|
open_incident = self.db.get_open_incident(el['id'])
|
||||||
|
ping_data = ping(el['ipaddr'])
|
||||||
|
best_effort = sorted(ping_data, key=lambda x: x.time_elapsed_ms)[0]
|
||||||
|
if best_effort.success:
|
||||||
|
latency['ping'] = best_effort.time_elapsed_ms
|
||||||
|
self.db.add_latency([state['id'], *latency.values()])
|
||||||
|
if open_incident:
|
||||||
|
self.db.close_incident(open_incident[0]['id'])
|
||||||
|
else:
|
||||||
|
latency['ping'] = 0
|
||||||
|
self.db.add_latency([state['id'], *latency.values()])
|
||||||
|
if not open_incident:
|
||||||
|
self.db.create_incident([state['id'], best_effort.error_message])
|
||||||
Loading…
Reference in a new issue
Don't you have a way here to just take the state you just created? It seems pointless to me to take them all since you're only interested in the latest created anyway