#2 Only sending telegram message when a state has changed
This commit is contained in:
parent
d2d904cf06
commit
3dad914171
51
LastStateCrawler.py
Normal file
51
LastStateCrawler.py
Normal file
|
@ -0,0 +1,51 @@
|
||||||
|
import pymysql
|
||||||
|
|
||||||
|
|
||||||
|
def getWebsiteStates(conn: pymysql.Connection) -> dict:
|
||||||
|
# Get latest known states to check against
|
||||||
|
cur = conn.cursor()
|
||||||
|
|
||||||
|
cur.execute("""
|
||||||
|
WITH summary AS (
|
||||||
|
SELECT web.website_id,
|
||||||
|
web.success,
|
||||||
|
ROW_NUMBER() OVER(PARTITION BY web.website_id
|
||||||
|
ORDER BY web.ping_id DESC) AS rk
|
||||||
|
FROM website_pings web)
|
||||||
|
SELECT s.*
|
||||||
|
FROM summary s
|
||||||
|
WHERE s.rk = 1;
|
||||||
|
""")
|
||||||
|
websiteStates = cur.fetchall()
|
||||||
|
cur.close()
|
||||||
|
|
||||||
|
returnStates = {}
|
||||||
|
for site in websiteStates:
|
||||||
|
returnStates[site[0]] = (site[1] == 1)
|
||||||
|
|
||||||
|
return returnStates
|
||||||
|
|
||||||
|
|
||||||
|
def getServerStates(conn: pymysql.Connection) -> dict:
|
||||||
|
# Get latest known states to check against
|
||||||
|
cur = conn.cursor()
|
||||||
|
|
||||||
|
cur.execute("""
|
||||||
|
WITH summary AS (
|
||||||
|
SELECT srv.server_id,
|
||||||
|
srv.success,
|
||||||
|
ROW_NUMBER() OVER(PARTITION BY srv.server_id
|
||||||
|
ORDER BY srv.ping_id DESC) AS rk
|
||||||
|
FROM server_pings srv)
|
||||||
|
SELECT s.*
|
||||||
|
FROM summary s
|
||||||
|
WHERE s.rk = 1;
|
||||||
|
""")
|
||||||
|
serverStates = cur.fetchall()
|
||||||
|
cur.close()
|
||||||
|
|
||||||
|
returnStates = {}
|
||||||
|
for server in serverStates:
|
||||||
|
returnStates[server[0]] = (server[1] == 1)
|
||||||
|
|
||||||
|
return returnStates
|
37
main.py
37
main.py
|
@ -4,6 +4,7 @@ import ServerPingService
|
||||||
import WebsitePingService
|
import WebsitePingService
|
||||||
import SQLConnectionHandler
|
import SQLConnectionHandler
|
||||||
import TelegramHandler
|
import TelegramHandler
|
||||||
|
import LastStateCrawler
|
||||||
|
|
||||||
def runPings():
|
def runPings():
|
||||||
telegram = TelegramHandler.TelegramHandler()
|
telegram = TelegramHandler.TelegramHandler()
|
||||||
|
@ -21,20 +22,37 @@ def runPings():
|
||||||
serverPingResults = []
|
serverPingResults = []
|
||||||
websitePingResults = []
|
websitePingResults = []
|
||||||
|
|
||||||
|
websiteStates = LastStateCrawler.getWebsiteStates(conn)
|
||||||
|
serverStates = LastStateCrawler.getServerStates(conn)
|
||||||
|
|
||||||
for server in servers:
|
for server in servers:
|
||||||
ping = ServerPingService.ping(server['hostname'])
|
ping = ServerPingService.ping(server['hostname'])
|
||||||
|
success = False
|
||||||
if ping != -1:
|
if ping != -1:
|
||||||
|
# Ping successful, save success
|
||||||
|
success = True
|
||||||
resTuple = (server['server_id'], True, ping)
|
resTuple = (server['server_id'], True, ping)
|
||||||
else:
|
else:
|
||||||
resTuple = (server['server_id'], False, ping)
|
resTuple = (server['server_id'], False, ping)
|
||||||
telegram.sendMessage(users[server['admin_id']]['telegram_id'], (
|
|
||||||
'Your server {} ({}) is not reachable at the moment. Maybe give it a check.'
|
|
||||||
).format(server['name'], server['hostname']))
|
|
||||||
serverPingResults.append(resTuple)
|
serverPingResults.append(resTuple)
|
||||||
|
|
||||||
|
# Send Telegram message if state has changed
|
||||||
|
if success != serverStates[server['server_id']]:
|
||||||
|
# State has changed, send a message
|
||||||
|
if success:
|
||||||
|
telegram.sendMessage(users[server['admin_id']]['telegram_id'], (
|
||||||
|
'Your server {} ({}) is back online again!'
|
||||||
|
).format(server['name'], server['hostname']))
|
||||||
|
else:
|
||||||
|
telegram.sendMessage(users[server['admin_id']]['telegram_id'], (
|
||||||
|
'Your server {} ({}) is not reachable at the moment. Maybe give it a check.'
|
||||||
|
).format(server['name'], server['hostname']))
|
||||||
|
|
||||||
for website in websites:
|
for website in websites:
|
||||||
statusCode = WebsitePingService.ping(website['url'])
|
statusCode = WebsitePingService.ping(website['url'])
|
||||||
|
success = False
|
||||||
if statusCode == 200:
|
if statusCode == 200:
|
||||||
|
success = True
|
||||||
resTuple = (website['website_id'], True, statusCode)
|
resTuple = (website['website_id'], True, statusCode)
|
||||||
else:
|
else:
|
||||||
resTuple = (website['website_id'], False, statusCode)
|
resTuple = (website['website_id'], False, statusCode)
|
||||||
|
@ -43,7 +61,20 @@ def runPings():
|
||||||
).format(website['name'], website['url'], statusCode))
|
).format(website['name'], website['url'], statusCode))
|
||||||
websitePingResults.append(resTuple)
|
websitePingResults.append(resTuple)
|
||||||
|
|
||||||
|
# Send Telegram message if state has changed
|
||||||
|
if success != websiteStates[website['website_id']]:
|
||||||
|
# State has changed, send a message
|
||||||
|
if success:
|
||||||
|
telegram.sendMessage(users[website['admin_id']]['telegram_id'], (
|
||||||
|
'Your website {} ({}) is back online again!'
|
||||||
|
).format(website['name'], website['url']))
|
||||||
|
else:
|
||||||
|
telegram.sendMessage(users[website['admin_id']]['telegram_id'], (
|
||||||
|
'Your website {} ({}) is not reachable at the moment (Code {}). Maybe give it a check.'
|
||||||
|
).format(website['name'], website['url'], statusCode))
|
||||||
|
|
||||||
cur = conn.cursor()
|
cur = conn.cursor()
|
||||||
|
|
||||||
# Insert server ping results
|
# Insert server ping results
|
||||||
cur.executemany('INSERT INTO server_pings (server_id, success, milliseconds) VALUES (%s, %s, %s)', serverPingResults)
|
cur.executemany('INSERT INTO server_pings (server_id, success, milliseconds) VALUES (%s, %s, %s)', serverPingResults)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user