def test_get_kvm_pods_stats(self): pod1 = self.make_pod( cpu=10, mem=100, cpu_over_commit=2, mem_over_commit=3 ) pod2 = self.make_pod( cpu=20, mem=200, cpu_over_commit=3, mem_over_commit=2 ) total_cores = pod1.cores + pod2.cores total_memory = pod1.memory + pod2.memory over_cores = ( pod1.cores * pod1.cpu_over_commit_ratio + pod2.cores * pod2.cpu_over_commit_ratio ) over_memory = ( pod1.memory * pod1.memory_over_commit_ratio + pod2.memory * pod2.memory_over_commit_ratio ) stats = get_kvm_pods_stats() compare = { "kvm_pods": 2, "kvm_machines": 0, "kvm_available_resources": { "cores": total_cores, "memory": total_memory, "over_cores": over_cores, "over_memory": over_memory, "storage": 0, }, "kvm_utilized_resources": {"cores": 0, "memory": 0, "storage": 0}, } self.assertEqual(compare, stats)
def get_stats_for_prometheus(): registry = prom_cli.CollectorRegistry() stats = json.loads(get_maas_stats()) architectures = get_machines_by_architecture() pods = get_kvm_pods_stats() # Gather counter for machines per status counter = prom_cli.Gauge("machine_status", "Number of machines per status", ["status"], registry=registry) for status, machines in stats['machine_status'].items(): counter.labels(status).set(machines) # Gather counter for number of nodes (controllers/machine/devices) counter = prom_cli.Gauge( "nodes", "Number of nodes per type (e.g. racks, machines, etc).", ["type"], registry=registry) for ctype, number in stats['controllers'].items(): counter.labels(ctype).set(number) for ctype, number in stats['nodes'].items(): counter.labels(ctype).set(number) # Gather counter for networks counter = prom_cli.Gauge("networks", "General statistics for subnets.", ["type"], registry=registry) for stype, number in stats['network_stats'].items(): counter.labels(stype).set(number) # Gather overall amount of machine resources counter = prom_cli.Gauge("machine_resources", "Amount of combined resources for all machines", ["resource"], registry=registry) for resource, value in stats['machine_stats'].items(): counter.labels(resource).set(value) # Gather all stats for pods counter = prom_cli.Gauge("kvm_pods", "General stats for KVM pods", ["type"], registry=registry) for resource, value in pods.items(): if isinstance(value, dict): for r, v in value.items(): counter.labels("%s_%s" % (resource, r)).set(v) else: counter.labels(resource).set(value) # Gather statistics for architectures if len(architectures.keys()) > 0: counter = prom_cli.Gauge("machine_arches", "Number of machines per architecture.", ["arches"], registry=registry) for arch, machines in architectures.items(): counter.labels(arch).set(machines) return registry
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('machine_status', 'set', value=machines, labels={'status': status}) # Gather counter for number of nodes (controllers/machine/devices) for ctype, number in stats['controllers'].items(): metrics.update('nodes', 'set', value=number, labels={'type': ctype}) for ctype, number in stats['nodes'].items(): metrics.update('nodes', 'set', value=number, labels={'type': ctype}) # Gather counter for networks for stype, number in stats['network_stats'].items(): metrics.update('networks', 'set', value=number, labels={'type': stype}) # Gather overall amount of machine resources for resource, value in stats['machine_stats'].items(): metrics.update('machine_resources', 'set', value=value, labels={'resource': resource}) # Gather all stats for pods for resource, value in pods.items(): if isinstance(value, dict): for r, v in value.items(): metrics.update('kvm_pods', 'set', value=v, labels={'type': '{}_{}'.format(resource, r)}) else: metrics.update('kvm_pods', 'set', value=value, labels={'type': resource}) # Gather statistics for architectures if len(architectures.keys()) > 0: for arch, machines in architectures.items(): metrics.update('machine_arches', 'set', value=machines, labels={'arches': arch}) return metrics
def test_get_kvm_pods_stats_no_pod(self): self.assertEqual( get_kvm_pods_stats(), { 'kvm_pods': 0, 'kvm_machines': 0, 'kvm_available_resources': { 'cores': 0, 'memory': 0, 'storage': 0, 'over_cores': 0, 'over_memory': 0 }, 'kvm_utilized_resources': { 'cores': 0, 'memory': 0, 'storage': 0 } })
def test_get_kvm_pods_stats_no_pod(self): self.assertEqual( get_kvm_pods_stats(), { "kvm_pods": 0, "kvm_machines": 0, "kvm_available_resources": { "cores": 0, "memory": 0, "storage": 0, "over_cores": 0, "over_memory": 0, }, "kvm_utilized_resources": { "cores": 0, "memory": 0, "storage": 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