#2 Only sending telegram message when a state has changed

This commit is contained in:
Patrick Müller 2021-04-20 09:16:24 +02:00
parent d2d904cf06
commit 3dad914171
2 changed files with 85 additions and 3 deletions

51
LastStateCrawler.py Normal file
View 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
View File

@ -4,6 +4,7 @@ import ServerPingService
import WebsitePingService
import SQLConnectionHandler
import TelegramHandler
import LastStateCrawler
def runPings():
telegram = TelegramHandler.TelegramHandler()
@ -21,20 +22,37 @@ def runPings():
serverPingResults = []
websitePingResults = []
websiteStates = LastStateCrawler.getWebsiteStates(conn)
serverStates = LastStateCrawler.getServerStates(conn)
for server in servers:
ping = ServerPingService.ping(server['hostname'])
success = False
if ping != -1:
# Ping successful, save success
success = True
resTuple = (server['server_id'], True, ping)
else:
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)
# 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:
statusCode = WebsitePingService.ping(website['url'])
success = False
if statusCode == 200:
success = True
resTuple = (website['website_id'], True, statusCode)
else:
resTuple = (website['website_id'], False, statusCode)
@ -43,7 +61,20 @@ def runPings():
).format(website['name'], website['url'], statusCode))
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()
# Insert server ping results
cur.executemany('INSERT INTO server_pings (server_id, success, milliseconds) VALUES (%s, %s, %s)', serverPingResults)