blades-ssh-scraper/prom.py
2021-12-16 01:18:25 +02:00

35 lines
1.2 KiB
Python

"""
ENV VARIABLES AVAILABLE:
- SSH_HOSTNAME
- SSH_KEYPATH (defaults: /bss/id_rsa)
- PREFIX (default: bladetest_)
"""
"""
Run in development env:
export FLASK_ENV=development
export FLASK_APP=prom.py
flask run
"""
from flask import Flask, Response, request # General flask, and the handle requests on the fly thing.
import os # for ENV arguments
import scraperMain, prom_servers
hostname = str(os.environ['SSH_HOSTNAME'])
encname = str(os.environ['ENC_NAME'])
sshkeypath = os.getenv('SSH_KEYPATH','/bss/.ssh/id_rsa')
PREFIX = os.getenv('PREFIX','bladetest_')
### FLASK ###
app = Flask(__name__)
@app.route('/metrics', methods=['GET'])
def parse_request(): # If somebody accesses us
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)
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!
return Response(response + '\n', mimetype='text/plain') # The pizza is already cold.