blades-ssh-scraper/prom.py

35 lines
1.2 KiB
Python
Raw Normal View History

2020-12-23 02:37:15 +00:00
"""
ENV VARIABLES AVAILABLE:
- SSH_HOSTNAME
2020-12-23 04:52:52 +00:00
- SSH_KEYPATH (defaults: /bss/id_rsa)
- PREFIX (default: bladetest_)
2020-12-23 02:37:15 +00:00
"""
"""
2020-12-23 04:52:52 +00:00
Run in development env:
2020-12-23 02:37:15 +00:00
export FLASK_ENV=development
export FLASK_APP=prom.py
flask run
"""
2020-12-23 08:31:32 +00:00
from flask import Flask, Response, request # General flask, and the handle requests on the fly thing.
2020-12-23 02:37:15 +00:00
import os # for ENV arguments
import scraperMain, prom_servers
hostname = str(os.environ['SSH_HOSTNAME'])
2020-12-23 05:40:52 +00:00
encname = str(os.environ['ENC_NAME'])
2020-12-23 04:52:52 +00:00
sshkeypath = os.getenv('SSH_KEYPATH','/bss/.ssh/id_rsa')
PREFIX = os.getenv('PREFIX','bladetest_')
2020-12-23 02:37:15 +00:00
### FLASK ###
app = Flask(__name__)
@app.route('/metrics', methods=['GET'])
2020-12-23 02:37:15 +00:00
def parse_request(): # If somebody accesses us
2020-12-23 05:40:52 +00:00
data = scraperMain.scraperMain(hostname, encname, sshkeypath) # Gather up, we're going to wait a few minutes on the data.
#with open("prom_servers.out", "a") as f: # DO NOT UNCOMMENT IN PROD
# print(data, file=f)
2020-12-23 02:37:15 +00:00
response_list = prom_servers.prom_servers(PREFIX, data[0], data[1]) # Convert our python lists to prometheus format whatever.
response = '\n'.join(response_list) # Oh you still don't want a list? Fine, newlines!
2020-12-23 08:31:32 +00:00
return Response(response + '\n', mimetype='text/plain') # The pizza is already cold.