uptimerobot_exporter/exporter.py

84 lines
2.4 KiB
Python

import argparse
import http.server
import os
import requests
def fetch_data(api_key):
params = {
'api_key': api_key,
'format': 'json',
'response_times': 1,
'response_times_limit': 1,
}
req = requests.post(
'https://api.uptimerobot.com/v2/getMonitors',
data=params,
)
return req.json()
def format_prometheus(data):
result = ''
for item in data:
result += 'uptimerobot_up{{name="{}",type="{}",url="{}"}} {}\n'.format(
item.get('friendly_name'),
item.get('type'),
item.get('url'),
1 if item.get('status', 0) == 2 else 0,
)
result += 'uptimerobot_response_time{{name="{}",type="{}",url="{}"}} {}\n'.format(
item.get('friendly_name'),
item.get('type'),
item.get('url'),
item.get('response_times').pop().get('value'),
)
return result
class ReqHandler(http.server.BaseHTTPRequestHandler):
def do_GET(self):
answer = fetch_data(api_key)
self.send_response(200)
self.send_header("Content-type", "text/plain")
self.end_headers()
self.wfile.write(
format_prometheus(answer.get('monitors')).encode('utf-8')
)
if __name__ == '__main__':
if 'UPTIMEROBOT_API_KEY' in os.environ:
api_key = os.environ.get('UPTIMEROBOT_API_KEY')
server_name = os.environ.get('UPTIMEROBOT_SERVER_NAME', '0.0.0.0')
server_port = int(os.environ.get('UPTIMEROBOT_SERVER_PORT', '9705'))
else:
parser = argparse.ArgumentParser(
description='Export all check results from uptimerobot.txt'
'for prometheus scraping.'
)
parser.add_argument(
'apikey',
help='Your uptimerobot.com API key. See account details.'
)
parser.add_argument(
'--server_name', '-s',
default='0.0.0.0',
help='Server address to bind to.'
)
parser.add_argument(
'--server_port', '-p',
default=9705,
type=int,
help='Port to bind to.'
)
args = parser.parse_args()
api_key = args.apikey
server_name = args.server_name
server_port = args.server_port
httpd = http.server.HTTPServer((server_name, server_port), ReqHandler)
httpd.serve_forever()