initial commit

This commit is contained in:
hnrd 2018-04-08 13:37:13 +02:00
commit c78ab70298
4 changed files with 103 additions and 0 deletions

7
Dockerfile Normal file
View file

@ -0,0 +1,7 @@
FROM python:alpine
COPY exporter.py /exporter.py
RUN pip install --no-cache-dir requests
CMD [ "python", "/exporter.py" ]

12
docker-compose.yml Normal file
View file

@ -0,0 +1,12 @@
version: '2.1'
services:
exporter:
build: .
restart: unless-stopped
environment:
UPTIME_ROBOT_APIKEY: u123456-1a2b3c4d5e6f7890abc12323
ports:
- 9705:9705
read_only: true
pids_limit: 5

83
exporter.py Normal file
View file

@ -0,0 +1,83 @@
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()

1
requirements.txt Normal file
View file

@ -0,0 +1 @@
requests==2.18.4