cosmostat active host inventory file api
This commit is contained in:
@ -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
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user