Skip to content

Instantly share code, notes, and snippets.

@kumo
Last active December 9, 2020 21:05
Show Gist options
  • Save kumo/839f0e29c57cd368b4967bd56084ddd1 to your computer and use it in GitHub Desktop.
Save kumo/839f0e29c57cd368b4967bd56084ddd1 to your computer and use it in GitHub Desktop.
Python version of a simple monitoring web app
#!/usr/bin/env python3
import os
import time
from pathlib import Path
import telegram_send
from bottle import Bottle, route, run
@route('/hello')
def hello():
# Update the 'last modified' time of a specific file to the current time
Path('status').touch()
# Show some text to the user, but since this wep app will be
# called by a script, it doesn't matter what we write
return "Hello to you too!"
@route('/check/<minutes:int>')
def check(minutes):
# Get some information about the file
stinfo = os.stat('status')
# Get the last modified time in seconds
last_modified_time = stinfo.st_mtime
# Get the current time in seconds
now = time.time()
# Calculate how much time has passed
difference = now - last_modified_time
# and convert it into minutes
difference = difference / 60
# Firstly we check if there has been a hello in the last X minutes.
# If there hasn't we check if the last hello was in the last 2*X minutes,
# if so, we can send a Telegram message saying that something has gone wrong.
# If the last hello was more than 2*x minutes ago, then it is still offline.
if difference < minutes:
return "It has said hello recently"
elif difference < minutes * 2:
telegram_send.send(messages=["It is offline!"], conf="telegram-send.conf")
return "Offline and I should send a Telegram message"
else:
return "Still offline. Should I send a message?"
run(host='localhost', port=8080, debug=True)
@kumo
Copy link
Author

kumo commented Dec 9, 2020

Adding support for sending a telegram message using telegram_send

@kumo
Copy link
Author

kumo commented Dec 9, 2020

Using a specific configuration file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment