Skip to content

Instantly share code, notes, and snippets.

@kumo
Created December 9, 2020 22:00
Show Gist options
  • Save kumo/24780b3c27021f85de23837915678bc6 to your computer and use it in GitHub Desktop.
Save kumo/24780b3c27021f85de23837915678bc6 to your computer and use it in GitHub Desktop.
Ruby version of a simple monitoring web app
require 'sinatra'
get '/hello' do
# Update the 'last modified' time of a specific file to the current time
FileUtils.touch('status')
"Hello to you too!"
end
get '/check/:minutes' do |minutes|
# Get the last modified time of a specific file in seconds
last_modified = File.mtime('status')
# Get the current time in seconds
now = Time.now
# Calculate how much time has passed
difference = now - last_modified
# Convert the minutes into seconds
seconds = minutes.to_i * 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 < seconds
"It has said hello recently"
elsif difference < seconds * 2
"Offline and I should send a Telegram message"
else
"Still offline. Should I send a message?"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment