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

@ -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