icmp ping #1
3 changed files with 35 additions and 9 deletions
|
|
@ -35,9 +35,9 @@ class Database():
|
|||
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)
|
||||
def create_incident(self, id):
|
||||
self.cur.execute("""INSERT INTO incident (state_id)
|
||||
VALUES (%s) RETURNING *;""", (str(id),))
|
||||
self.conn.commit()
|
||||
return self.cur.fetchall()
|
||||
|
fil marked this conversation as resolved
|
||||
|
||||
|
|
|
|||
34
pingpong.py
34
pingpong.py
|
|
@ -1,5 +1,6 @@
|
|||
from database import Database
|
||||
from pythonping import ping
|
||||
from tcppinglib import tcpping
|
||||
from config import Config
|
||||
|
||||
class PingPong:
|
||||
|
|
@ -7,6 +8,25 @@ class PingPong:
|
|||
self.db = Database()
|
||||
self.config = Config()
|
||||
|
||||
def icmp_ping(self, service):
|
||||
r = {}
|
||||
ping_data = ping(service)
|
||||
avg_rtt = sum([e.time_elapsed_ms for e in ping_data])
|
||||
best_effort = sorted(ping_data, key=lambda x: x.time_elapsed_ms)[0]
|
||||
r['success'] = best_effort.success
|
||||
r['ping'] = avg_rtt
|
||||
return r
|
||||
|
||||
def tcp_ping(self, service):
|
||||
r = {}
|
||||
try:
|
||||
ping_data = tcpping(service, port=80, timeout=2, count=3, interval=1)
|
||||
r['success'] = ping_data.is_alive
|
||||
r['ping'] = ping_data.avg_rtt
|
||||
except:
|
||||
r['success'] = False
|
||||
return r
|
||||
|
||||
def start(self):
|
||||
latency = {
|
||||
"location": self.config["LOCATION"]
|
||||
|
|
@ -14,10 +34,14 @@ class PingPong:
|
|||
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
|
||||
|
||||
if el['type'] == 'physical':
|
||||
ping_data = self.icmp_ping(el['ipaddr'])
|
||||
else:
|
||||
ping_data = self.tcp_ping(el['domain'])
|
||||
|
||||
if ping_data['success']:
|
||||
latency['ping'] = ping_data['ping']
|
||||
self.db.add_latency([state['id'], *latency.values()])
|
||||
if open_incident:
|
||||
self.db.close_incident(open_incident[0]['id'])
|
||||
|
|
@ -25,4 +49,4 @@ class PingPong:
|
|||
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])
|
||||
self.db.create_incident(state['id'])
|
||||
|
|
|
|||
|
|
@ -1,9 +1,11 @@
|
|||
certifi==2024.2.2
|
||||
charset-normalizer==3.3.2
|
||||
idna==3.7
|
||||
load-dotenv==0.1.0
|
||||
pip-autoremove==0.10.0
|
||||
psycopg2-binary==2.9.9
|
||||
python-dotenv==1.0.1
|
||||
pythonping==1.1.4
|
||||
requests==2.31.0
|
||||
setuptools==69.5.1
|
||||
tcppinglib==2.0.3
|
||||
urllib3==2.2.1
|
||||
|
|
|
|||
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