cosmostat working
This commit is contained in:
124
files/api/app.py
124
files/api/app.py
@ -4,6 +4,7 @@ from typing import Dict, Union
|
||||
|
||||
import json, time, redis, yaml
|
||||
import base64, hashlib
|
||||
import secrets, string
|
||||
|
||||
import requests
|
||||
from requests import RequestException, Response
|
||||
@ -35,7 +36,7 @@ def update_redis_server():
|
||||
|
||||
if run_cosmostat_server():
|
||||
update_redis_channel("client_summary", get_server_redis_data())
|
||||
update_redis_channel("client_hostnames", get_server_hostnames())
|
||||
#update_redis_channel("client_hostnames", get_server_hostnames())
|
||||
|
||||
# History Redis Tree
|
||||
# Update history_stats Redis Channel
|
||||
@ -54,6 +55,7 @@ def get_server_redis_data():
|
||||
for client in cosmostat_server.systems:
|
||||
this_client_key = {
|
||||
"hostname": client.hostname,
|
||||
"data_timestamp": client.data_timestamp,
|
||||
"uuid": client.uuid,
|
||||
"short_id": client.name,
|
||||
"redis_data": client.redis_data
|
||||
@ -64,6 +66,7 @@ def get_server_redis_data():
|
||||
def get_server_hostnames():
|
||||
return cosmostat_server.get_client_hostnames()
|
||||
|
||||
|
||||
#######################################################################
|
||||
### Client Flask Routes
|
||||
#######################################################################
|
||||
@ -152,7 +155,7 @@ def get_static_data(human_readable = False):
|
||||
return cosmostat_client.get_static_metrics(human_readable)
|
||||
|
||||
def get_php_summary():
|
||||
system_properties = cosmostat_client.get_system_properties(human_readable = True)
|
||||
system_properties = cosmostat_client.get_system_properties(human_readable = True, php_extra = True)
|
||||
system_components = []
|
||||
for component in cosmostat_client.get_components():
|
||||
this_component = {
|
||||
@ -161,6 +164,18 @@ def get_php_summary():
|
||||
}
|
||||
system_components.append(this_component)
|
||||
|
||||
if run_cosmostat_server():
|
||||
print(cosmostat_client.name)
|
||||
client_uuid = cosmostat_server.get_uuid_from_hostname(cosmostat_client.name)
|
||||
print(client_uuid)
|
||||
data_timestamp = cosmostat_server.get_system(client_uuid)
|
||||
print(data_timestamp)
|
||||
component_age = {
|
||||
"component_name": "Data Timestamp",
|
||||
"info_strings": f"Data is {data_timestamp} seconds old"
|
||||
}
|
||||
system_components.append(component_age)
|
||||
|
||||
result = [{
|
||||
"system_properties": system_properties,
|
||||
"system_components": system_components
|
||||
@ -229,7 +244,17 @@ def client_details():
|
||||
def client_hostnames():
|
||||
result = []
|
||||
if run_cosmostat_server():
|
||||
result = cosmostat_server.get_client_hostnames()
|
||||
result = cosmostat_server.get_client_hostnames(send_age = True)
|
||||
else:
|
||||
result = {"message": "server not running on this endpoint"}
|
||||
return jsonify(result)
|
||||
|
||||
# api to get server redis data
|
||||
@app.route('/get_server_redis', methods=['GET'])
|
||||
def get_server_redis():
|
||||
result = []
|
||||
if run_cosmostat_server():
|
||||
result = get_server_redis_data()
|
||||
else:
|
||||
result = {"message": "server not running on this endpoint"}
|
||||
return jsonify(result)
|
||||
@ -241,30 +266,48 @@ def client_hostnames():
|
||||
|
||||
# update client on server
|
||||
def run_update_client(this_client):
|
||||
if not cosmostat_server.check_uuid(this_client["uuid"]):
|
||||
return { "message": "client not found" }
|
||||
else:
|
||||
timestamp_update = cosmostat_server.update_system(system_dictionary = this_client["redis_data"], system_uuid = this_client["uuid"])
|
||||
update_status = f'updated client {this_client["short_id"]}'
|
||||
if public_api_check(this_client):
|
||||
if not cosmostat_server.check_uuid(this_client["uuid"]):
|
||||
return { "message": "client not found" }
|
||||
else:
|
||||
timestamp_update = cosmostat_server.update_system(system_dictionary = this_client["redis_data"], system_uuid = this_client["uuid"])
|
||||
update_status = f'updated client {this_client["short_id"]}'
|
||||
|
||||
return {
|
||||
"status": update_status,
|
||||
"uuid": this_client["uuid"],
|
||||
"redis_data": this_client,
|
||||
"timestamp_update": timestamp_update
|
||||
}
|
||||
return {
|
||||
"status": update_status,
|
||||
"uuid": this_client["uuid"],
|
||||
"redis_data": this_client,
|
||||
"timestamp_update": timestamp_update
|
||||
}
|
||||
else:
|
||||
return{
|
||||
"status": "api failure"
|
||||
}
|
||||
|
||||
# create client on server
|
||||
def run_create_client(this_client):
|
||||
timestamp_update = cosmostat_server.add_system(system_dictionary = this_client)
|
||||
update_status = f'created client {this_client["short_id"]}'
|
||||
return {
|
||||
"status": update_status,
|
||||
"uuid": this_client["uuid"],
|
||||
"client_properties": this_client,
|
||||
"timestamp_update": timestamp_update
|
||||
if public_api_check(this_client):
|
||||
timestamp_update = cosmostat_server.add_system(system_dictionary = this_client)
|
||||
update_status = f'created client {this_client["short_id"]}'
|
||||
return {
|
||||
"status": update_status,
|
||||
"uuid": this_client["uuid"],
|
||||
"client_properties": this_client,
|
||||
"timestamp_update": timestamp_update
|
||||
}
|
||||
else:
|
||||
return{
|
||||
"status": "api failure"
|
||||
}
|
||||
|
||||
def public_api_check(this_client):
|
||||
result = False
|
||||
default_key = ''.join(secrets.choice(string.ascii_letters + string.digits) for _ in range(256))
|
||||
api_key = this_client.get('API_KEY', default_key)
|
||||
if api_key == app_settings["REAL_API_KEY"]:
|
||||
result = True
|
||||
return result
|
||||
|
||||
# flask submission check function
|
||||
def client_submit_check(request, dict_name: str):
|
||||
payload = {}
|
||||
@ -324,6 +367,7 @@ def get_client_details():
|
||||
# Cosmostat Client Reporter
|
||||
def client_update():
|
||||
api_url = f"{cosmostat_server_api()}update_client"
|
||||
print(api_url)
|
||||
payload = get_client_payload(get_client_redis_data(human_readable = False), "redis_data")
|
||||
log_data(log_output = "client_update - redis data from local client:", log_level = "noisy_test")
|
||||
log_data(log_output = payload, log_level = "noisy_test")
|
||||
@ -366,7 +410,8 @@ def get_client_payload(system_dictionary: dict, dictionary_name: str):
|
||||
"uuid": this_uuid,
|
||||
"short_id": this_short_id,
|
||||
"hostname": this_hostname,
|
||||
dictionary_name: system_dictionary
|
||||
dictionary_name: system_dictionary,
|
||||
"API_KEY": app_settings["REAL_API_KEY"]
|
||||
|
||||
}
|
||||
return payload
|
||||
@ -401,25 +446,26 @@ if __name__ == '__main__':
|
||||
|
||||
# Background Loop Function
|
||||
def background_loop():
|
||||
# Update all data on the System object
|
||||
if cosmostat_client.check_system_timer():
|
||||
# Update all data on the System object unless this is the server
|
||||
if cosmostat_client.check_system_timer() and not run_cosmostat_server():
|
||||
cosmostat_client.update_system_state()
|
||||
|
||||
if app_settings["push_redis"]:
|
||||
if app_settings["push_redis"] and not app_settings["disable_local_api"]:
|
||||
update_redis_server()
|
||||
|
||||
if run_cosmostat_reporter():
|
||||
if int(time.time()) % 5 == 0 and not cosmostat_client.check_system_timer():
|
||||
cosmostat_client.update_system_state()
|
||||
client_update()
|
||||
client_update()
|
||||
|
||||
if run_cosmostat_server():
|
||||
# update the client state since that was skipped
|
||||
cosmostat_client.update_system_state()
|
||||
this_client = get_client_payload(get_client_redis_data(human_readable = False), "redis_data")
|
||||
if app_settings["noisy_test"]:
|
||||
print(this_client)
|
||||
run_update_client(this_client)
|
||||
|
||||
|
||||
|
||||
|
||||
time.sleep(0.5)
|
||||
|
||||
######################################
|
||||
@ -443,14 +489,14 @@ if __name__ == '__main__':
|
||||
# send initial stats update to redis
|
||||
######################################
|
||||
|
||||
if app_settings["push_redis"]:
|
||||
if app_settings["push_redis"] and not app_settings["disable_local_api"]:
|
||||
update_redis_server()
|
||||
|
||||
######################################
|
||||
# Flask scheduler for scanner
|
||||
######################################
|
||||
|
||||
if app_settings["run_background"]:
|
||||
if app_settings["run_background"] and not app_settings["disable_local_api"]:
|
||||
log_data(log_output = "Loading flask background subroutine...", log_level = "log_output")
|
||||
|
||||
scheduler.add_job(id='background_loop',
|
||||
@ -467,11 +513,15 @@ if __name__ == '__main__':
|
||||
######################################
|
||||
# Flask API
|
||||
######################################
|
||||
|
||||
app.run(debug=False, host=service_gateway_ip(), port=service_api_port())
|
||||
|
||||
|
||||
|
||||
|
||||
print(f"gateway: {service_gateway_ip()} - port: {service_api_port()}")
|
||||
if not app_settings["disable_local_api"]:
|
||||
app.run(debug=False, host=service_gateway_ip(), port=service_api_port())
|
||||
else:
|
||||
print("Internal API Disabled.")
|
||||
while True:
|
||||
if int(time.time()) % 5 == 0 and not cosmostat_client.check_system_timer():
|
||||
cosmostat_client.update_system_state()
|
||||
client_update()
|
||||
time.sleep(0.5)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user