cosmostat active host inventory file api

This commit is contained in:
2026-04-04 17:47:32 -07:00
parent be95ab7593
commit a89703c420
26 changed files with 1243 additions and 261 deletions

View File

@ -9,6 +9,7 @@ import json
import time
import weakref
import base64, hashlib
import ipaddress
from typing import Dict, Any, List
# Import Cosmos Settings
from Cosmos_Settings import *
@ -255,6 +256,19 @@ class Component:
if value not in empty_value and name not in self.virt_ignore:
result.append(this_metric)
return result
# simple data value return
def get_metrics_value(self, type = None):
result = []
print(f"Metric type: {type}")
for name, value in self._metrics.items():
print(f"Metric Property: {name}")
if type in name:
result.append(value)
if len(result) == 1:
return result[0]
else:
return result
########################################################
# various data functions
@ -285,7 +299,7 @@ class Component:
these_properties.append({"Property": name, "Value": value})
else:
for name, value in self._properties.items():
if name == type:
if type in name:
these_properties.append({"Property": name, "Value": value})
result = {
"Source": self.name,
@ -294,6 +308,19 @@ class Component:
}
return result
# simple data value return
def get_property_value(self, type = None):
result = []
print(f"Component type: {type}")
for name, value in self._properties.items():
print(f"Component Property: {name}")
if type in name:
result.append(value)
if len(result) == 1:
return result[0]
else:
return result
# full data return
def get_description(self):
these_properties = []
@ -360,7 +387,7 @@ class System:
self._properties: Dict[str, str] = {}
self._metrics: Dict[str, str] = {}
self._virt_string = run_command('systemd-detect-virt', zero_only = True, req_check = False)
self.default_gateway = run_command("ip route show | grep def | awk '{print $3}'", zero_only = True, req_check = False)
self._virt_ignore = self.virt_ignore
if self._virt_string == "none":
self._virt_ignore = []
@ -374,7 +401,9 @@ class System:
self.update_live_keys()
# initialze components
for component in component_types:
self.create_component(component)
self.create_component(component)
self.primary_ip = self.get_primary_ip()
print(f"Cosmostat System IP: {self.primary_ip}")
def __str__(self):
components_str = "\n".join(f" - {c}" for c in self.components)
@ -444,6 +473,22 @@ class System:
log_data(log_output = f'Creating component {component["name"]}', log_level = "debug_output")
new_component = Component(name = component_name, comp_type = component_name, parent_system = self)
self.components.append(new_component)
# return the IP of the interface with the gateway
# remember the IP is stored like this 192.168.1.0/24
def get_primary_ip(self):
primary_ip = None
interfaces = self.get_components(component_type = "LAN")
for interface in interfaces:
interface_ip = interface.get_metrics_value(type = "IP Address")
interface_subnet = None
if "/" in interface_ip:
interface_subnet = ipaddress.ip_network(interface_ip, strict=False)
print(f"interface IP: {interface_ip} - Default Gateway: {self.default_gateway}")
if is_ip_in_subnets(self.default_gateway ,interface_subnet):
primary_ip = str(interface_ip).split("/")[0]
print(f"Primary IP: {primary_ip}")
return primary_ip
########################################################
# helper class functions
@ -456,7 +501,7 @@ class System:
else:
result = []
for component in self.components:
if component.type == component_type:
if component_type in component.type:
result.append(component)
if component.is_multi():
return result
@ -648,4 +693,17 @@ def get_device_list(device_type_name: str):
return result
# subnet helper app
def is_ip_in_subnets(ip, subnet):
try:
ip_obj = ipaddress.IPv4Address(ip)
subnet_obj = ipaddress.IPv4Network(subnet)
if ip_obj in subnet_obj:
return True
return False
except ValueError as e:
# If the IP address is not valid, raise an error
return False

View File

@ -19,7 +19,7 @@ app_settings = {
"custom_api_port": "5000",
"cosmostat_server_api": "http://10.200.27.20:5000/",
"REAL_API_KEY": ''.join(secrets.choice(string.ascii_letters + string.digits) for _ in range(256)),
"disable_local_api": False,
"disable_local_dashboard": False,
"local_api_address": "http://10.200.27.20:5000/",
"cosmostat_server_ip": "10.200.27.20",
"api_bind_ip": "192.168.37.1"

View File

@ -16,6 +16,7 @@ import subprocess
import json
import time
import weakref
import ipaddress
import base64, hashlib
from typing import Dict, Any, List
# Import Cosmos Settings
@ -35,10 +36,11 @@ class CosmostatServer:
# instantiate new Cosmostat server
############################################################
def __init__(self, name: str):
def __init__(self, name: str, hostname: str):
# the system needs a name, should be equal to the uuid of the client
self.name = name
self.short_id = self.short_uuid(self.name)
self.hostname = hostname
log_data(log_output = f"Cosmostat Server {self.short_id} initializing", log_level = "log_output")
# system contains an array of CosmostatClient Objects
self.systems = []
@ -52,14 +54,18 @@ class CosmostatServer:
def add_system(self, system_dictionary: dict):
if not self.check_uuid(system_dictionary["uuid"]):
print(f"Adding Cosmostat Host: {system_dictionary['hostname']}")
new_cosmostat_clilent = CosmostatClient(
name = system_dictionary["short_id"],
uuid = system_dictionary["uuid"],
hostname = system_dictionary["hostname"],
active_ip = system_dictionary["active_interface"],
is_server = system_dictionary["is_server"],
data_timestamp = time.time(),
client_properties = system_dictionary["client_properties"],
redis_data = {}
)
print(f"New Cosmostat Server Object - IP {system_dictionary['active_interface']}")
self.systems.append(new_cosmostat_clilent)
log_data(log_output = f'Client system {system_dictionary["short_id"]} added', log_level = "log_output")
return new_cosmostat_clilent.data_timestamp
@ -123,6 +129,13 @@ class CosmostatServer:
result.append(system.hostname)
return result
def get_metrics_from_ip(self, ip):
this_metrics = ""
for system in self.systems:
if system.active_ip == ip:
this_metrics = system.redis_data
return this_metrics
def purge_stale_hostnames(self):
now = time.time()
fresh_systems = []
@ -131,6 +144,22 @@ class CosmostatServer:
if age <= 60: # keep only fresh servers
fresh_systems.append(system)
self.systems = fresh_systems # replace the old list
# return the VPN IP if present, if just_check then it returns true/false if th
def get_vpn_ip(self, remote_ip, just_check = False):
cosmos_vpn_subnet = "10.200.26.0/24"
vpn_ip = None
this_client_metrics = self.get_metrics_from_ip(remote_ip)
for metric in this_client_metrics:
# if the metric is from VPN, is an IP address, and it belongs to the Jenkins VPN subnet
if metric["Metric"] == "IP Address" and "VPN" in metric["Source"] and is_ip_in_subnets(metric["Data"].split("/")[0], cosmos_vpn_subnet):
vpn_ip = metric["Data"].split("/")[0]
if just_check and vpn_ip is not None:
vpn_ip = False
elif just_check and vpn_ip is None:
vpn_ip = True
return vpn_ip
#################################################################
### Cosmostat Client Class
@ -145,10 +174,12 @@ class CosmostatClient:
# instantiate new Cosmostat server
############################################################
def __init__(self, name: str, uuid: str, hostname: str, data_timestamp: float, client_properties: dict, redis_data: dict):
def __init__(self, name: str, uuid: str, hostname: str, active_ip: str, is_server: str, data_timestamp: float, client_properties: dict, redis_data: dict):
self.name = name
self.uuid = uuid
self.hostname = hostname
self.active_ip = active_ip
self.is_server = is_server
self.data_timestamp = data_timestamp
self.client_properties = client_properties
self.redis_data = redis_data
@ -165,4 +196,17 @@ class CosmostatClient:
return self.client.properties
def get_redis(self):
return self.redis_data
return self.redis_data
# subnet helper app
def is_ip_in_subnets(ip, subnet):
try:
ip_obj = ipaddress.IPv4Address(ip)
subnet_obj = ipaddress.IPv4Network(subnet)
if ip_obj in subnet_obj:
return True
return False
except ValueError as e:
# If the IP address is not valid, raise an error
return False

View File

@ -3,10 +3,10 @@
### cosmostat service handler
#######################################################################
from flask import Flask, jsonify, request, Response
from flask import Flask, jsonify, request, Response, abort
from flask_apscheduler import APScheduler
from typing import Dict, Union
import json, time, redis, yaml
import json, time, redis, yaml, datetime
import secrets, string
import requests
from requests import RequestException, Response
@ -63,6 +63,8 @@ def get_server_redis_data():
"data_timestamp": client.data_timestamp,
"uuid": client.uuid,
"short_id": client.name,
"active_ip": client.active_ip,
"is_server": client.is_server,
"redis_data": client.redis_data
}
result.append(this_client_key)
@ -168,7 +170,7 @@ def get_php_summary():
"info_strings": component.get_properties_strings(return_simple = True)
}
system_components.append(this_component)
this_primary_ip = cosmostat_client.primary_ip
if run_cosmostat_server():
client_uuid = cosmostat_server.get_uuid_from_hostname(cosmostat_client.name)
data_timestamp = cosmostat_server.get_system(client_uuid)
@ -178,11 +180,13 @@ def get_php_summary():
"component_name": "Data Timestamp",
"info_strings": f"Data is {data_timestamp} seconds old"
}
this_primary_ip = cosmostat_settings["cosmostat_server_ip"]
system_components.append(component_age)
result = [{
"system_properties": system_properties,
"system_components": system_components
"system_components": system_components,
"active_interface": this_primary_ip
}]
return result
@ -217,6 +221,7 @@ def create_client():
result = {}
# check the request and return payload dict {} if all good
payload = client_submit_check(request = request, dict_name = "client_properties")
# if the client does not exist, create it
if not cosmostat_server.check_uuid(payload["uuid"]):
result = run_create_client(payload)
else:
@ -266,6 +271,20 @@ def get_server_redis():
result = {"message": "server not running on this endpoint"}
return jsonify(result)
# api to get server redis data
@app.route('/client_ip_summary', methods=['GET'])
def client_ip_summary():
result = []
if run_cosmostat_server():
result = get_client_ip_summary()
else:
result = {"message": "server not running on this endpoint"}
return jsonify(result)
# return inventory file of all clients for update
@app.route("/client_inventory", methods=["GET"])
def client_inventory():
return build_inventory()
#######################################################################
### Server Flask Helpers
@ -295,6 +314,7 @@ def run_update_client(this_client):
# create client on server
def run_create_client(this_client):
if public_api_check(this_client):
#this_client["active_interface"] = cosmostat_client.primary_ip
timestamp_update = cosmostat_server.add_system(system_dictionary = this_client)
update_status = f'created client {this_client["short_id"]}'
return {
@ -331,6 +351,7 @@ def client_submit_check(request, dict_name: str):
missing = required_keys - payload.keys()
if missing:
raise ValueError(f"Missing required keys: {', '.join(sorted(missing))}")
return payload
# generate cosmostat server summary
@ -366,14 +387,92 @@ def get_client_details():
result = {"message": "no clients reporting"}
return result
# get client IP summary
def get_client_ip_summary(return_list = False):
result = []
for client in cosmostat_server.systems:
device_info = {
"small_id": client.name,
"uuid": client.uuid,
"hostname": client.hostname,
"lan_ip": client.active_ip,
}
if client.hostname != cosmostat_server.hostname:
if return_list:
result.append(client.active_ip)
else:
result.append(device_info)
return result
# build client ansible inventory file
def build_inventory():
all_ips = get_client_ip_summary(return_list = True)
cosmos_subnets = [
"172.25.1.0/24",
"172.20.0.0/16",
"172.19.10.0/24",
"10.200.26.0/24",
"10.200.27.0/24",
"192.168.60.0/24",
]
ips = []
bad_ips = []
# build list of reachable IPs
for ip in all_ips:
for subnet in cosmos_subnets:
if is_ip_in_subnets(ip, subnet) and cosmostat_server.get_vpn_ip(ip, just_check = True):
ips.append(ip)
# list of unreachable IPs
for ip in all_ips:
if ip not in ips:
bad_ips.append(ip)
# add any VPN IPs for bad IPs to the main IP list
for ip in bad_ips:
ips.append(cosmostat_server.get_vpn_ip(ip))
hosts = {ip: {"ansible_host": ip} for ip in ips}
inventory = {
"all": {
"hosts": hosts,
"vars": {
"refresh_only": "true",
"ansible_connection": "ssh",
"ansible_ssh_private_key_file": "/var/jenkins_home/jenkins_key",
"ansible_python_interpreter": "/usr/bin/python3",
"jenkins_user": 'automate',
"jenkins_group": 'Jenkins-Admin',
"subnet_group_check": 'Jenkins-AllSubnets',
"SERVER_SUBNET_GROUP": 'Jenkins-AllSubnets',
"inventory_generation_timestamp": f"{datetime.datetime.now().isoformat()}",
"playbook_file": '/var/jenkins_home/ansible/playbooks/cosmostat.yaml',
"quick_refresh": "true",
"noisy_test": "false",
"debug_output": "false",
"push_redis": "false",
"run_background": "true",
"log_output": "true",
"public_dashboard": "false",
"custom_port": "80",
"custom_api_port": "5000",
"cosmostat_server_reporter": "true",
"cosmostat_server": "false",
"secure_api": "false",
"disable_local_dashboard": "true",
"REAL_API_KEY": f"{cosmostat_settings['REAL_API_KEY']}"
},
}
}
return yaml.safe_dump(
inventory,
default_flow_style=False,
sort_keys=False,
)
#######################################################################
### Cosmostat Client Subroutines
#######################################################################
# since the API isn't running
# def local_client_update():
# Cosmostat Client Reporter
# Cosmostat Client Reporter Handler
def client_update():
api_url = f"{cosmostat_server_api()}update_client"
payload = get_client_payload(get_client_redis_data(human_readable = False), "redis_data")
@ -385,6 +484,7 @@ def client_update():
if not result or not result.get('client_updated'):
log_data(log_output = f"Client not updated, initializing", log_level = "log_output")
result = client_api_initialize()
# this result does not matter and is not used anywhere
return result
# Cosmostat Client Initializer
@ -393,6 +493,9 @@ def client_api_initialize():
# generate payload
payload = get_client_payload(get_php_summary(), "client_properties")
# execute API call
payload["active_interface"] = cosmostat_client.primary_ip
payload["is_server"] = False
result = client_submission_handler(api_url, payload)
return result
@ -410,7 +513,7 @@ def client_submission_handler(api_url: str, payload: dict):
try:
result = response.json()
except ValueError as exc:
log_data(log_output = "Server responded with non-JSON payload: {response.text!r}", log_level = "log_output")
log_data(log_output = f"Server responded with non-JSON payload: {response.text!r}", log_level = "log_output")
return result
def get_client_payload(system_dictionary: dict, dictionary_name: str):
@ -427,7 +530,6 @@ def get_client_payload(system_dictionary: dict, dictionary_name: str):
}
return payload
#######################################################################
#######################################################################
### Main Subroutine
@ -452,7 +554,7 @@ if __name__ == '__main__':
# instantiate and return the Cosmoserver System object
def new_cosmostat_server():
new_server = CosmostatServer(cosmostat_client.uuid)
new_server = CosmostatServer(name = cosmostat_client.uuid, hostname = jenkins_hostname_settings())
log_data(log_output = f"New Cosmostat serverobject name: {new_server.name}", log_level = "log_output")
return new_server
@ -473,7 +575,7 @@ if __name__ == '__main__':
cosmostat_client.update_system_state()
# publish to redis if the web dashboard is active locally
if app_settings["push_redis"] and not app_settings["disable_local_api"]:
if app_settings["push_redis"] and not app_settings["disable_local_dashboard"]:
update_redis_server()
# report data to the server if configured
@ -489,7 +591,7 @@ if __name__ == '__main__':
run_update_client(get_client_payload(get_client_redis_data(human_readable = False), "redis_data"))
log_data(log_output = f"{this_client}", log_level = "noisy_test")
time.sleep(0.5)
time.sleep(0.2)
######################################
# instantiate client
@ -514,15 +616,18 @@ if __name__ == '__main__':
log_data(log_output = f"Cosmostat Server Start", log_level = "log_output")
cosmostat_server = new_cosmostat_server()
this_client = get_client_payload(get_php_summary(), "client_properties")
this_client["active_interface"] = cosmostat_settings["cosmostat_server_ip"]
this_client["is_server"] = True
timestamp_update = cosmostat_server.add_system(system_dictionary = this_client)
# moving this here so all the bits exist
client_update()
# if not a server, update client on the server
else:
client_update()
######################################
# send initial stats update to redis
######################################
if app_settings["push_redis"] and not app_settings["disable_local_api"]:
if app_settings["push_redis"] and not app_settings["disable_local_dashboard"]:
log_data(log_output = f"Initial Redis Push", log_level = "log_output")
update_redis_server()
@ -530,7 +635,7 @@ if __name__ == '__main__':
# Flask scheduler for scanner
######################################
if app_settings["run_background"] and not app_settings["disable_local_api"]:
if app_settings["run_background"] and not app_settings["disable_local_dashboard"]:
log_data(log_output = f"Background Function Initializing", log_level = "log_output")
log_data(log_output = "Loading flask background subroutine...", log_level = "log_output")
@ -549,7 +654,7 @@ if __name__ == '__main__':
# Flask API
######################################
log_data(log_output = f"gateway: {service_gateway_ip()} - port: {service_api_port()}", log_level = "log_output")
if not app_settings["disable_local_api"]:
if not app_settings["disable_local_dashboard"]:
log_data(log_output = f"Main API Start", log_level = "log_output")
app.run(debug=False, host=service_gateway_ip(), port=service_api_port())
else:

View File

@ -247,5 +247,20 @@
"metrics": {
"placeholder": ""
}
},
{
"name": "BAT",
"description": "Battery - {Device Name} - capacity {Capacity}",
"multi_check": "True",
"device_list": "acpi | grep Battery | cut -d: -f1",
"properties": {
"Device Name": "echo {this_device}",
"Capacity": "acpi -V | jc --acpi | jq '.[] | select(.type==\"Battery\") | .design_capacity_mah' "
},
"metrics": {
"Percent Full": "acpi -V | jc --acpi | jq '.[] | select(.type==\"Battery\") | .charge_percent'",
"State": "acpi -V | jc --acpi | jq '.[] | select(.type==\"Battery\") | .state'"
},
"precheck": "acpi | grep Battery | wc -l"
}
]

View File

@ -41,6 +41,22 @@
]
},
{
"name": "BAT",
"description": "Battery - {Device Name} - capacity {Capacity}",
"multi_check": "True",
"device_list": "acpi | grep Battery | cut -d: -f1",
"properties": {
"Device Name": "echo {this_device}",
"Capacity": "acpi -V | jc --acpi | jq '.[] | select(.type==\"Battery\") | .design_capacity_mah' "
},
"metrics": {
"Percent Full": "acpi -V | jc --acpi | jq '.[] | select(.type==\"Battery\") | .charge_percent'",
"State": "acpi -V | jc --acpi | jq '.[] | select(.type==\"Battery\") | .state'"
},
"precheck": "acpi | grep Battery | wc -l"
},
{
"SATA GBW": "sudo /usr/sbin/smartctl -x --json /dev/{this_device} | jq -r '.physical_block_size as $block |.ata_device_statistics.pages[] | select(.name == \"General Statistics\") | .table[] | select(.name == \"Logical Sectors Written\") | .value as $sectors | ($sectors * $block) / 1073741824 ' | awk '{{printf \"%.2f GiB Written\\n\", $0}}' || true",
"NVMe GBW": "sudo /usr/sbin/smartctl -x --json /dev/{this_device} | jq -r ' .nvme_smart_health_information_log.data_units_written as $dw | .logical_block_size as $ls | ($dw * $ls) / 1073741824 ' | awk '{{printf \"%.2f GiB Written\\n\", $0}}' || true"