def test_stats_reserved(self): subnet = factory.make_Subnet(cidr="1.2.0.0/16", gateway_ip="1.2.0.254") factory.make_IPRange( subnet=subnet, start_ip="1.2.0.11", end_ip="1.2.0.20", alloc_type=IPRANGE_TYPE.RESERVED, ) factory.make_IPRange( subnet=subnet, start_ip="1.2.0.51", end_ip="1.2.0.60", alloc_type=IPRANGE_TYPE.RESERVED, ) factory.make_StaticIPAddress( ip="1.2.0.15", alloc_type=IPADDRESS_TYPE.USER_RESERVED, subnet=subnet, ) self.assertEqual( stats.get_subnets_utilisation_stats(), { "1.2.0.0/16": { "available": 2 ** 16 - 23, "dynamic_available": 0, "dynamic_used": 0, "reserved_available": 19, "reserved_used": 1, "static": 0, "unavailable": 21, } }, )
def test_stats_all(self): subnet = factory.make_Subnet(cidr='1.2.0.0/16', gateway_ip='1.2.0.254') factory.make_IPRange(subnet=subnet, start_ip='1.2.0.11', end_ip='1.2.0.20', alloc_type=IPRANGE_TYPE.DYNAMIC) factory.make_IPRange(subnet=subnet, start_ip='1.2.0.51', end_ip='1.2.0.70', alloc_type=IPRANGE_TYPE.RESERVED) factory.make_StaticIPAddress(ip='1.2.0.12', alloc_type=IPADDRESS_TYPE.DHCP, subnet=subnet) for n in (60, 61): factory.make_StaticIPAddress( ip='1.2.0.{}'.format(n), alloc_type=IPADDRESS_TYPE.USER_RESERVED, subnet=subnet) for n in (80, 90, 100): factory.make_StaticIPAddress(ip='1.2.0.{}'.format(n), alloc_type=IPADDRESS_TYPE.STICKY, subnet=subnet) self.assertEqual( stats.get_subnets_utilisation_stats(), { '1.2.0.0/16': { 'available': 2**16 - 36, 'dynamic_available': 9, 'dynamic_used': 1, 'reserved_available': 18, 'reserved_used': 2, 'static': 3, 'unavailable': 34 } })
def test_stats_totals(self): factory.make_Subnet(cidr="1.2.0.0/16", gateway_ip="1.2.0.254") factory.make_Subnet(cidr="::1/128", gateway_ip="") self.assertEqual( stats.get_subnets_utilisation_stats(), { "1.2.0.0/16": { "available": 2 ** 16 - 3, "dynamic_available": 0, "dynamic_used": 0, "reserved_available": 0, "reserved_used": 0, "static": 0, "unavailable": 1, }, "::1/128": { "available": 1, "dynamic_available": 0, "dynamic_used": 0, "reserved_available": 0, "reserved_used": 0, "static": 0, "unavailable": 0, }, }, )
def test_stats_reserved(self): subnet = factory.make_Subnet(cidr='1.2.0.0/16', gateway_ip='1.2.0.254') factory.make_IPRange(subnet=subnet, start_ip='1.2.0.11', end_ip='1.2.0.20', alloc_type=IPRANGE_TYPE.RESERVED) factory.make_IPRange(subnet=subnet, start_ip='1.2.0.51', end_ip='1.2.0.60', alloc_type=IPRANGE_TYPE.RESERVED) factory.make_StaticIPAddress(ip='1.2.0.15', alloc_type=IPADDRESS_TYPE.USER_RESERVED, subnet=subnet) self.assertEqual( stats.get_subnets_utilisation_stats(), { '1.2.0.0/16': { 'available': 2**16 - 23, 'dynamic_available': 0, 'dynamic_used': 0, 'reserved_available': 19, 'reserved_used': 1, 'static': 0, 'unavailable': 21 } })
def test_stats_static(self): subnet = factory.make_Subnet(cidr='1.2.0.0/16', gateway_ip='1.2.0.254') for n in (10, 20, 30): factory.make_StaticIPAddress(ip='1.2.0.{}'.format(n), alloc_type=IPADDRESS_TYPE.STICKY, subnet=subnet) self.assertEqual( stats.get_subnets_utilisation_stats(), { '1.2.0.0/16': { 'available': 2**16 - 6, 'dynamic_available': 0, 'dynamic_used': 0, 'reserved_available': 0, 'reserved_used': 0, 'static': 3, 'unavailable': 4 } })
def test_stats_all(self): subnet = factory.make_Subnet(cidr="1.2.0.0/16", gateway_ip="1.2.0.254") factory.make_IPRange( subnet=subnet, start_ip="1.2.0.11", end_ip="1.2.0.20", alloc_type=IPRANGE_TYPE.DYNAMIC, ) factory.make_IPRange( subnet=subnet, start_ip="1.2.0.51", end_ip="1.2.0.70", alloc_type=IPRANGE_TYPE.RESERVED, ) factory.make_StaticIPAddress( ip="1.2.0.12", alloc_type=IPADDRESS_TYPE.DHCP, subnet=subnet ) for n in (60, 61): factory.make_StaticIPAddress( ip="1.2.0.{}".format(n), alloc_type=IPADDRESS_TYPE.USER_RESERVED, subnet=subnet, ) for n in (80, 90, 100): factory.make_StaticIPAddress( ip="1.2.0.{}".format(n), alloc_type=IPADDRESS_TYPE.STICKY, subnet=subnet, ) self.assertEqual( stats.get_subnets_utilisation_stats(), { "1.2.0.0/16": { "available": 2 ** 16 - 36, "dynamic_available": 9, "dynamic_used": 1, "reserved_available": 18, "reserved_used": 2, "static": 3, "unavailable": 34, } }, )
def test_stats_static(self): subnet = factory.make_Subnet(cidr="1.2.0.0/16", gateway_ip="1.2.0.254") for n in (10, 20, 30): factory.make_StaticIPAddress( ip="1.2.0.{}".format(n), alloc_type=IPADDRESS_TYPE.STICKY, subnet=subnet, ) self.assertEqual( stats.get_subnets_utilisation_stats(), { "1.2.0.0/16": { "available": 2 ** 16 - 6, "dynamic_available": 0, "dynamic_used": 0, "reserved_available": 0, "reserved_used": 0, "static": 3, "unavailable": 4, } }, )
def test_stats_totals(self): factory.make_Subnet(cidr='1.2.0.0/16', gateway_ip='1.2.0.254') factory.make_Subnet(cidr='::1/128', gateway_ip='') self.assertEqual( stats.get_subnets_utilisation_stats(), { '1.2.0.0/16': { 'available': 2**16 - 3, 'dynamic_available': 0, 'dynamic_used': 0, 'reserved_available': 0, 'reserved_used': 0, 'static': 0, 'unavailable': 1 }, '::1/128': { 'available': 1, 'dynamic_available': 0, 'dynamic_used': 0, 'reserved_available': 0, 'reserved_used': 0, 'static': 0, 'unavailable': 0 } })
def update_prometheus_stats(metrics: PrometheusMetrics): """Update metrics in a PrometheusMetrics based on database values.""" stats = json.loads(get_maas_stats()) architectures = get_machines_by_architecture() pods = get_kvm_pods_stats() # Gather counter for machines per status for status, machines in stats["machine_status"].items(): metrics.update("maas_machines", "set", value=machines, labels={"status": status}) # Gather counter for number of nodes (controllers/machine/devices) for ctype, number in stats["controllers"].items(): metrics.update("maas_nodes", "set", value=number, labels={"type": ctype}) for ctype, number in stats["nodes"].items(): metrics.update("maas_nodes", "set", value=number, labels={"type": ctype}) # Gather counter for networks for stype, number in stats["network_stats"].items(): metrics.update("maas_net_{}".format(stype), "set", value=number) # Gather overall amount of machine resources for resource, value in stats["machine_stats"].items(): metrics.update("maas_machines_{}".format(resource), "set", value=value) # Gather all stats for pods metrics.update("maas_kvm_pods", "set", value=pods["kvm_pods"]) metrics.update("maas_kvm_machines", "set", value=pods["kvm_machines"]) for metric in ("cores", "memory", "storage"): metrics.update( "maas_kvm_{}".format(metric), "set", value=pods["kvm_available_resources"][metric], labels={"status": "available"}, ) metrics.update( "maas_kvm_{}".format(metric), "set", value=pods["kvm_utilized_resources"][metric], labels={"status": "used"}, ) metrics.update( "maas_kvm_overcommit_cores", "set", value=pods["kvm_available_resources"]["over_cores"], ) metrics.update( "maas_kvm_overcommit_memory", "set", value=pods["kvm_available_resources"]["over_memory"], ) # Gather statistics for architectures if len(architectures.keys()) > 0: for arch, machines in architectures.items(): metrics.update( "maas_machine_arches", "set", value=machines, labels={"arches": arch}, ) # Update metrics for subnets for cidr, stats in get_subnets_utilisation_stats().items(): for status in ("available", "unavailable"): metrics.update( "maas_net_subnet_ip_count", "set", value=stats[status], labels={ "cidr": cidr, "status": status }, ) metrics.update( "maas_net_subnet_ip_static", "set", value=stats["static"], labels={"cidr": cidr}, ) for addr_type in ("dynamic", "reserved"): metric_name = "maas_net_subnet_ip_{}".format(addr_type) for status in ("available", "used"): metrics.update( metric_name, "set", value=stats["{}_{}".format(addr_type, status)], labels={ "cidr": cidr, "status": status }, ) return metrics
def update_prometheus_stats(metrics: PrometheusMetrics): """Update metrics in a PrometheusMetrics based on database values.""" stats = json.loads(get_maas_stats()) architectures = get_machines_by_architecture() pods = get_kvm_pods_stats() # Gather counter for machines per status for status, machines in stats['machine_status'].items(): metrics.update( 'maas_machines', 'set', value=machines, labels={'status': status}) # Gather counter for number of nodes (controllers/machine/devices) for ctype, number in stats['controllers'].items(): metrics.update( 'maas_nodes', 'set', value=number, labels={'type': ctype}) for ctype, number in stats['nodes'].items(): metrics.update( 'maas_nodes', 'set', value=number, labels={'type': ctype}) # Gather counter for networks for stype, number in stats['network_stats'].items(): metrics.update('maas_net_{}'.format(stype), 'set', value=number) # Gather overall amount of machine resources for resource, value in stats['machine_stats'].items(): metrics.update('maas_machines_{}'.format(resource), 'set', value=value) # Gather all stats for pods metrics.update('maas_kvm_pods', 'set', value=pods['kvm_pods']) metrics.update('maas_kvm_machines', 'set', value=pods['kvm_machines']) for metric in ('cores', 'memory', 'storage'): metrics.update( 'maas_kvm_{}'.format(metric), 'set', value=pods['kvm_available_resources'][metric], labels={'status': 'available'}) metrics.update( 'maas_kvm_{}'.format(metric), 'set', value=pods['kvm_utilized_resources'][metric], labels={'status': 'used'}) metrics.update( 'maas_kvm_overcommit_cores', 'set', value=pods['kvm_available_resources']['over_cores']) metrics.update( 'maas_kvm_overcommit_memory', 'set', value=pods['kvm_available_resources']['over_memory']) # Gather statistics for architectures if len(architectures.keys()) > 0: for arch, machines in architectures.items(): metrics.update( 'maas_machine_arches', 'set', value=machines, labels={'arches': arch}) # Update metrics for subnets for cidr, stats in get_subnets_utilisation_stats().items(): for status in ('available', 'unavailable'): metrics.update( 'maas_net_subnet_ip_count', 'set', value=stats[status], labels={'cidr': cidr, 'status': status}) metrics.update( 'maas_net_subnet_ip_static', 'set', value=stats['static'], labels={'cidr': cidr}) for addr_type in ('dynamic', 'reserved'): metric_name = 'maas_net_subnet_ip_{}'.format(addr_type) for status in ('available', 'used'): metrics.update( metric_name, 'set', value=stats['{}_{}'.format(addr_type, status)], labels={'cidr': cidr, 'status': status}) return metrics