added static routes per vpc tracking

This commit is contained in:
Maurizio Noseda Pedraglio
2022-10-10 22:06:57 +02:00
parent 3608b573fd
commit 311d50c183
6 changed files with 112 additions and 5 deletions

View File

@@ -176,6 +176,9 @@ def main(event, context):
ilb_fwrules.get_forwarding_rules_data(
config, metrics_dict, l7_forwarding_rules_dict,
limits_dict['internal_forwarding_rules_l7_limit'], "L7")
routes.get_static_routes_vpc(config, metrics_dict, project_quotas_dict)
peerings.get_vpc_peering_data(config, metrics_dict,
limits_dict['number_of_vpc_peerings_limit'])
dynamic_routes_dict = routes.get_dynamic_routes(

View File

@@ -99,6 +99,18 @@ metrics_per_network:
utilization:
name: dynamic_routes_per_network_utilization
description: Number of Dynamic routes per network - utilization.
static_routes_per_network:
usage:
name: static_routes_per_project_vpc_usage
description: Number of Static routes per project and network - usage.
limit:
name: static_routes_per_project_limit
description: Number of Static routes per project - limit.
values:
default_value: 250
utilization:
name: static_routes_per_project_utilization
description: Number of Static routes per project - utilization.
metrics_per_peering_group:
l4_forwarding_rules_per_peering_group:
usage:

View File

@@ -42,7 +42,7 @@ def get_quotas_dict(quotas_list):
def get_quota_project_limit(config, regions=["global"]):
'''
Retrieves limit for a specific project quota
Retrieves quotas for all monitored project in selected regions, default 'global'
Parameters:
project_link (string): Project link.
Returns:

View File

@@ -91,7 +91,8 @@ def create_metric(metric_name, description, monitoring_project):
def append_data_to_series_buffer(config, metric_name, metric_value,
metric_labels, timestamp=None):
'''
Writes data to Cloud Monitoring custom metrics.
Appends data to Cloud Monitoring custom metrics, using a buffer. buffer is flushed every BUFFER_LEN elements,
any unflushed series is discarded upon function closure
Parameters:
config (dict): The dict containing config like clients and limits
metric_name (string): Name of the metric
@@ -136,7 +137,7 @@ def append_data_to_series_buffer(config, metric_name, metric_value,
def flush_series_buffer(config):
'''
writes buffered metrics to Google Cloud Monitoring, empties buffer upon failure
writes buffered metrics to Google Cloud Monitoring, empties buffer upon both failure/success
config (dict): The dict containing config like clients and limits
'''
try:
@@ -237,7 +238,7 @@ def get_pgg_data(config, metric_dict, usage_dict, limit_metric, limit_dict):
metric_dict["utilization"]["name"],
limit_dict)
print(
f"Wrote {metric_dict['usage']['name']} for peering group {network_dict['network_name']} in {project_id}"
f"Buffered {metric_dict['usage']['name']} for peering group {network_dict['network_name']} in {project_id}"
)

View File

@@ -14,9 +14,11 @@
# limitations under the License.
#
import re
import time
from collections import defaultdict
from google.protobuf import field_mask_pb2
from . import metrics, networks, limits, peerings, routers
@@ -177,3 +179,92 @@ def get_dynamic_routes_ppg(config, metric_dict, usage_dict, limit_dict):
print(
f"Wrote {metric_dict['usage']['name']} for peering group {network_dict['network_name']} in {project}"
)
def get_static_routes_vpc(config, metrics_dict, project_quotas_dict):
'''
LOREM
Parameters:
config (dict): The dict containing config like clients and limits
metric_dict (dictionary of string: string): Dictionary with the metric names and description, that will be used to populate the metrics
usage_dict (dictionnary of string:int): Dictionary with the network link as key and the number of resources as value
project_quotas_dict (dictionary of string:int): Dictionary with the network link as key and the limit as value.
Returns:
None
'''
routes_per_vpc_dict = defaultdict()
usage_dict = defaultdict()
read_mask = field_mask_pb2.FieldMask()
read_mask.FromJsonString('name,versionedResources')
response = config["clients"]["asset_client"].search_all_resources(
request={
"scope": f"organizations/{config['organization']}",
"asset_types": ["compute.googleapis.com/Route"],
"read_mask": read_mask,
})
timestamp = time.time()
for resource in response:
for versioned in resource.versioned_resources:
static_route = dict()
for field_name, field_value in versioned.resource.items():
static_route[field_name] = field_value
static_route["project_id"] = re.search("\/([^\/]*)(\/[^\/]*){3}$",
static_route["network"]).group(1)
static_route["network_name"] = re.search("\/([^\/]*)$",
static_route["network"]).group(1)
#exclude default vpc and peering routes
if "nextHopPeering" not in static_route and "nextHopNetwork" not in static_route:
if static_route["project_id"] not in routes_per_vpc_dict:
routes_per_vpc_dict[static_route["project_id"]] = dict()
if static_route["network_name"] not in routes_per_vpc_dict[
static_route["project_id"]]:
routes_per_vpc_dict[static_route["project_id"]][
static_route["network_name"]] = dict()
if static_route["destRange"] not in routes_per_vpc_dict[
static_route["project_id"]][static_route["network_name"]]:
routes_per_vpc_dict[static_route["project_id"]][
static_route["network_name"]][static_route["destRange"]] = {}
if "usage" not in routes_per_vpc_dict[static_route["project_id"]][
static_route["network_name"]]:
routes_per_vpc_dict[static_route["project_id"]][
static_route["network_name"]]["usage"] = 0
routes_per_vpc_dict[static_route["project_id"]][
static_route["network_name"]]["usage"] = routes_per_vpc_dict[
static_route["project_id"]][
static_route["network_name"]]["usage"] + 1
for project_id in routes_per_vpc_dict:
current_quota_limit = project_quotas_dict[project_id]['global']["routes"][
"limit"]
if current_quota_limit is None:
print(
f"Could not determine static routes metric for projects/{project_id} due to missing quotas"
)
continue
for network_name in routes_per_vpc_dict[project_id]:
metric_labels = {"project": project_id, "network_name": network_name}
metrics.append_data_to_series_buffer(
config, metrics_dict["metrics_per_network"]
["static_routes_per_network"]["usage"]["name"],
routes_per_vpc_dict[project_id][network_name]["usage"], metric_labels,
timestamp=timestamp)
# limit and utilization are calculted by project
metric_labels = {"project": project_id}
metrics.append_data_to_series_buffer(
config, metrics_dict["metrics_per_network"]["static_routes_per_network"]
["limit"]["name"], current_quota_limit, metric_labels,
timestamp=timestamp)
metrics.append_data_to_series_buffer(
config, metrics_dict["metrics_per_network"]["static_routes_per_network"]
["utilization"]["name"],
routes_per_vpc_dict[project_id][network_name]["usage"] /
current_quota_limit, metric_labels, timestamp=timestamp)
return

View File

@@ -83,7 +83,7 @@ def get_firewalls_data(config, metrics_dict, project_quotas_dict,
current_quota_limit = project_quotas_dict[project_id]['global']["firewalls"]
if current_quota_limit is None:
print(
f"Could not determine VPC firewal rules to metric for projects/{project_id} due to missing quotas"
f"Could not determine VPC firewal rules metric for projects/{project_id} due to missing quotas"
)
continue