250 lines
7.6 KiB
Python
250 lines
7.6 KiB
Python
from flask import Flask, jsonify, request
|
||
from flask_apscheduler import APScheduler
|
||
from typing import Dict, Union
|
||
|
||
import json, time, redis, yaml
|
||
|
||
from Components import *
|
||
# declare flask apps
|
||
app = Flask(__name__)
|
||
scheduler = APScheduler()
|
||
|
||
#######################################################################
|
||
### Settings Handler Functions
|
||
#######################################################################
|
||
|
||
# default application setting variables
|
||
app_settings = {
|
||
"noisy_test" : False,
|
||
"debug_output" : True,
|
||
"log_output" : True,
|
||
"secure_api" : True,
|
||
"push_redis" : False,
|
||
"run_background" : True,
|
||
"update_frequency": 1
|
||
}
|
||
|
||
with open('cosmostat_settings.yaml', 'r') as f:
|
||
print("Loading cosmostat_settings file")
|
||
cosmostat_settings = yaml.safe_load(f)
|
||
print("...Done")
|
||
# initialize system variables from settings file
|
||
print("Checking for system var overrides")
|
||
for setting in app_settings:
|
||
if setting in cosmostat_settings:
|
||
cosmos_setting = cosmostat_settings[setting]
|
||
if app_settings["debug_output"]:
|
||
print(f"{setting}: {cosmos_setting}")
|
||
app_settings[setting] = cosmos_setting
|
||
print("...Done")
|
||
|
||
# this returns the docker gateway from the settings
|
||
def docker_gateway_settings() -> str:
|
||
return cosmostat_settings["docker_gateway"]
|
||
|
||
# this returns the jenkins user that ran the pipeline
|
||
def jenkins_user_settings() -> str:
|
||
return cosmostat_settings["jenkins_user"]
|
||
|
||
# this returns the ansible_hostname from setup
|
||
def jenkins_hostname_settings() -> str:
|
||
return cosmostat_settings["ansible_hostname"]
|
||
|
||
# this returns the inventory_generation_timestamp
|
||
def jenkins_inventory_generation_timestamp_settings() -> str:
|
||
return cosmostat_settings["inventory_generation_timestamp"]
|
||
|
||
def service_gateway_ip():
|
||
if cosmostat_settings["secure_api"]:
|
||
return docker_gateway_settings()
|
||
else:
|
||
return "0.0.0.0"
|
||
|
||
#######################################################################
|
||
### Redis Functions
|
||
#######################################################################
|
||
|
||
# Redis client – will publish updates
|
||
r = redis.Redis(host=service_gateway_ip(), port=6379)
|
||
|
||
def update_redis_channel(redis_channel, data):
|
||
# Publish to the specified Redis channel
|
||
r.publish(redis_channel, json.dumps(data))
|
||
if app_settings["noisy_test"]:
|
||
print(f"{redis_channel} Redis Update")
|
||
print(data)
|
||
|
||
def update_redis_server():
|
||
# Update Stats Redis Channel
|
||
update_redis_channel("host_metrics", get_redis_data())
|
||
# Update history_stats Redis Channel
|
||
# update_redis_channel("history_stats", get_component_list())
|
||
|
||
#######################################################################
|
||
### Flask Routes
|
||
#######################################################################
|
||
|
||
# dynamic data
|
||
# this will go to the redis server
|
||
@app.route('/dynamic_data', methods=['GET'])
|
||
def dynamic_data():
|
||
return jsonify(get_dynamic_data())
|
||
|
||
# static data
|
||
@app.route('/static_data', methods=['GET'])
|
||
def static_data():
|
||
return jsonify(get_static_data())
|
||
|
||
# redis data
|
||
@app.route('/redis_data', methods=['GET'])
|
||
def redis_data():
|
||
return jsonify(get_redis_data())
|
||
|
||
# full summary
|
||
@app.route('/full_summary', methods=['GET'])
|
||
def full_summary():
|
||
return jsonify(get_full_summary())
|
||
|
||
# system info
|
||
@app.route('/info', methods=['GET'])
|
||
def info():
|
||
return jsonify(get_info())
|
||
|
||
# test route
|
||
@app.route('/test', methods=['GET'])
|
||
def test():
|
||
return jsonify(
|
||
{
|
||
"component_count:": len(cosmostat_system.components),
|
||
"user": jenkins_user_settings(),
|
||
"hostname": jenkins_hostname_settings(),
|
||
"cpu_model": cosmostat_system.get_components(component_type="CPU").description
|
||
}
|
||
)
|
||
|
||
#######################################################################
|
||
### Flask Helpers
|
||
#######################################################################
|
||
|
||
# needs to return array of {name: name, type: type, metrics: metrics}
|
||
# for redis table generation, includes system and component metrics
|
||
def get_dynamic_data():
|
||
return cosmostat_system.get_live_metrics()
|
||
|
||
def get_static_data():
|
||
result = []
|
||
for metric in cosmostat_system.get_system_properties():
|
||
result.append(metric)
|
||
for metric in cosmostat_system.get_component_properties():
|
||
result.append(metric)
|
||
return result
|
||
|
||
def get_redis_data():
|
||
result = []
|
||
for metric in get_dynamic_data():
|
||
result.append(metric)
|
||
for metric in get_static_data():
|
||
result.append(metric)
|
||
return result
|
||
|
||
def get_full_summary():
|
||
live_metrics = cosmostat_system.get_live_metrics()
|
||
system_components = cosmostat_system.get_component_strings()
|
||
system_info = get_info()
|
||
result = {
|
||
"system_settings":
|
||
{
|
||
"user": jenkins_user_settings(),
|
||
"hostname": jenkins_hostname_settings(),
|
||
"timestamp": jenkins_inventory_generation_timestamp_settings(),
|
||
"component_count:": len(cosmostat_system.components),
|
||
"object_name": cosmostat_system.name,
|
||
"docker_gateway": docker_gateway_settings()
|
||
},
|
||
"live_metrics": live_metrics,
|
||
"system_components": system_components,
|
||
"system_info": system_info
|
||
}
|
||
return result
|
||
|
||
def get_info():
|
||
component_strings = []
|
||
for component in cosmostat_system.get_components():
|
||
component_strings.append({"name": component.name, "description": component.description})
|
||
system_strings = []
|
||
|
||
result = {
|
||
"hostname": jenkins_hostname_settings(),
|
||
"component_strings": component_strings
|
||
}
|
||
#for component_string in component_strings:
|
||
# for name, description in component_string.items():
|
||
# result[name] = description
|
||
return result
|
||
|
||
#######################################################################
|
||
### Other Functions
|
||
#######################################################################
|
||
|
||
# instantiate and return the System object
|
||
def new_cosmos_system():
|
||
new_system = System(f"{jenkins_hostname_settings()}")
|
||
if app_settings["log_output"]:
|
||
print(f"New system object name: {new_system.name} - {new_system.get_component_count()} components:")
|
||
for component in new_system.components:
|
||
print(component.description)
|
||
return new_system
|
||
|
||
|
||
# Background Loop Function
|
||
def background_loop():
|
||
# Update all data on the System object
|
||
cosmostat_system.update_system_state()
|
||
|
||
if app_settings["push_redis"]:
|
||
update_redis_server()
|
||
|
||
if app_settings["noisy_test"]:
|
||
print("Sorry about the mess...")
|
||
print(f"Blame {jenkins_user_settings()}")
|
||
|
||
#######################################################################
|
||
### Main Subroutine
|
||
#######################################################################
|
||
|
||
if __name__ == '__main__':
|
||
|
||
|
||
# instantiate system
|
||
cosmostat_system = new_cosmos_system()
|
||
|
||
# send initial stats update to redis
|
||
if app_settings["push_redis"]:
|
||
update_redis_server()
|
||
|
||
# Flask scheduler for scanner
|
||
if app_settings["run_background"]:
|
||
if app_settings["log_output"]:
|
||
print("Loading flask background subroutine...")
|
||
|
||
scheduler.add_job(id='background_loop',
|
||
func=background_loop,
|
||
trigger='interval',
|
||
seconds=app_settings["update_frequency"])
|
||
scheduler.init_app(app)
|
||
scheduler.start()
|
||
|
||
if app_settings["log_output"]:
|
||
print("...Done")
|
||
else:
|
||
if app_settings["log_output"]:
|
||
print("Skipping flask background task")
|
||
|
||
# Flask API
|
||
app.run(debug=True, host=service_gateway_ip(), port=5000)
|
||
|
||
|
||
|
||
|
||
|
||
|