Skip to content

Instantly share code, notes, and snippets.

@pioz
Created May 7, 2024 12:03
Show Gist options
  • Save pioz/8e4e49596e8094a540c3d6db0635063e to your computer and use it in GitHub Desktop.
Save pioz/8e4e49596e8094a540c3d6db0635063e to your computer and use it in GitHub Desktop.
require 'logger'
require 'net/http'
PUSHOVER_USER_KEY = 'xxx'.freeze
PUSHOVER_API_TOKEN = 'xxx'.freeze
def send_notification(message)
uri = URI.parse('https://api.pushover.net/1/messages.json')
request = Net::HTTP::Post.new(uri)
request.set_form_data(
user: PUSHOVER_USER_KEY,
token: PUSHOVER_API_TOKEN,
message: message,
priority: 1,
# retry: 30,
# expire: 10 * 60, # 10 minutes
sound: :persistent,
url: 'https://mtgprint.net/'
)
response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
http.request(request)
end
raise "Push notification fail: #{response.body.inspect}" if response.code.to_i != 200
end
def check(method, url, body: nil, expected_code: 200, timeout: 10)
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = (uri.scheme == 'https')
http.open_timeout = timeout
http.read_timeout = timeout
http.write_timeout = timeout
request =
case method.to_s.upcase
when 'GET'
Net::HTTP::Get.new(uri.request_uri)
when 'POST'
req = Net::HTTP::Post.new(uri.request_uri)
req['Content-Type'] = 'application/json'
req.body = body
req
when 'PATCH'
req = Net::HTTP::Patch.new(uri.request_uri)
req['Content-Type'] = 'application/json'
req.body = body
req
when 'PUT'
req = Net::HTTP::Put.new(uri.request_uri)
req['Content-Type'] = 'application/json'
req.body = body
req
when 'DELETE'
Net::HTTP::Delete.new(uri.request_uri)
else
raise "HTTP method not supported: #{method}"
end
response = http.request(request)
return response.code.to_i == expected_code
end
# Start here
logger = Logger.new(STDOUT)
if check(:get, 'https://mtgprint.net/')
logger.info 'Check MTG Print frontend: OK'
else
logger.error 'Check MTG Print frontend: service is DOWN'
send_notification('MTG Print frontend is DOWN')
end
if check(:post, 'https://api.mtgprint.net/deck', body: '{"deck": "Absorb"}')
logger.info 'Check MTG Print backend: OK'
else
logger.error 'Check MTG Print backend: service is DOWN'
send_notification('MTG Print backend is DOWN')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment