2022-03-08 09:36:02 -08:00
|
|
|
#
|
|
|
|
# Copyright 2022 Google LLC
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
2022-03-09 10:02:59 -08:00
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
2022-03-08 09:36:02 -08:00
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
#
|
|
|
|
|
2022-04-12 00:58:12 -07:00
|
|
|
from code import interact
|
2022-03-08 09:36:02 -08:00
|
|
|
import os
|
2022-04-12 00:58:12 -07:00
|
|
|
from pickletools import int4
|
2022-03-17 12:35:33 -07:00
|
|
|
import time
|
2022-04-12 05:13:05 -07:00
|
|
|
import http
|
2022-03-17 08:36:36 -07:00
|
|
|
import yaml
|
2022-03-28 09:44:16 -07:00
|
|
|
from collections import defaultdict
|
2022-03-17 12:35:33 -07:00
|
|
|
from google.api import metric_pb2 as ga_metric
|
2022-04-12 05:13:05 -07:00
|
|
|
from google.api_core import exceptions, protobuf_helpers
|
2022-03-24 02:07:58 -07:00
|
|
|
from google.cloud import monitoring_v3, asset_v1
|
|
|
|
from google.protobuf import field_mask_pb2
|
2022-04-12 05:13:05 -07:00
|
|
|
from googleapiclient import discovery, errors
|
2022-03-08 09:36:02 -08:00
|
|
|
|
2022-03-24 02:07:58 -07:00
|
|
|
# Organization ID containing the projects to be monitored
|
|
|
|
ORGANIZATION_ID = os.environ.get("ORGANIZATION_ID")
|
2022-03-17 09:28:17 -07:00
|
|
|
# list of projects from which function will get quotas information
|
|
|
|
MONITORED_PROJECTS_LIST = os.environ.get("MONITORED_PROJECTS_LIST").split(",")
|
|
|
|
# project where the metrics and dahsboards will be created
|
2022-03-17 12:08:58 -07:00
|
|
|
MONITORING_PROJECT_ID = os.environ.get("MONITORING_PROJECT_ID")
|
2022-03-17 09:28:17 -07:00
|
|
|
MONITORING_PROJECT_LINK = f"projects/{MONITORING_PROJECT_ID}"
|
2022-03-08 09:36:02 -08:00
|
|
|
service = discovery.build('compute', 'v1')
|
|
|
|
|
2022-03-17 12:08:58 -07:00
|
|
|
# Existing GCP metrics per network
|
2022-03-17 12:35:33 -07:00
|
|
|
GCE_INSTANCES_LIMIT_METRIC = "compute.googleapis.com/quota/instances_per_vpc_network/limit"
|
2022-03-17 12:08:58 -07:00
|
|
|
L4_FORWARDING_RULES_LIMIT_METRIC = "compute.googleapis.com/quota/internal_lb_forwarding_rules_per_vpc_network/limit"
|
|
|
|
L7_FORWARDING_RULES_LIMIT_METRIC = "compute.googleapis.com/quota/internal_managed_forwarding_rules_per_vpc_network/limit"
|
|
|
|
SUBNET_RANGES_LIMIT_METRIC = "compute.googleapis.com/quota/subnet_ranges_per_vpc_network/limit"
|
|
|
|
|
|
|
|
|
2022-03-17 08:36:36 -07:00
|
|
|
def main(event, context):
|
2022-03-09 10:02:59 -08:00
|
|
|
'''
|
|
|
|
Cloud Function Entry point, called by the scheduler.
|
|
|
|
|
|
|
|
Parameters:
|
2022-03-17 08:36:36 -07:00
|
|
|
event: Not used for now (Pubsub trigger)
|
|
|
|
context: Not used for now (Pubsub trigger)
|
2022-03-09 10:02:59 -08:00
|
|
|
Returns:
|
|
|
|
'Function executed successfully'
|
|
|
|
'''
|
2022-03-28 09:44:16 -07:00
|
|
|
metrics_dict, limits_dict = create_metrics()
|
2022-03-08 09:36:02 -08:00
|
|
|
|
2022-03-24 02:07:58 -07:00
|
|
|
# Asset inventory queries
|
|
|
|
gce_instance_dict = get_gce_instance_dict()
|
|
|
|
l4_forwarding_rules_dict = get_l4_forwarding_rules_dict()
|
|
|
|
l7_forwarding_rules_dict = get_l7_forwarding_rules_dict()
|
|
|
|
subnet_range_dict = get_subnet_ranges_dict()
|
|
|
|
|
2022-03-17 08:36:36 -07:00
|
|
|
# Per Network metrics
|
2022-03-28 09:47:11 -07:00
|
|
|
get_gce_instances_data(metrics_dict, gce_instance_dict,
|
|
|
|
limits_dict['number_of_instances_limit'])
|
|
|
|
get_l4_forwarding_rules_data(
|
|
|
|
metrics_dict, l4_forwarding_rules_dict,
|
|
|
|
limits_dict['internal_forwarding_rules_l4_limit'])
|
2022-04-12 05:21:14 -07:00
|
|
|
get_l7_forwarding_rules_data(
|
|
|
|
metrics_dict, l7_forwarding_rules_dict,
|
|
|
|
limits_dict['internal_forwarding_rules_l7_limit'])
|
2022-03-28 09:47:11 -07:00
|
|
|
get_vpc_peering_data(metrics_dict,
|
|
|
|
limits_dict['number_of_vpc_peerings_limit'])
|
2022-04-12 00:58:12 -07:00
|
|
|
dynamic_routes_dict = get_dynamic_routes(
|
|
|
|
metrics_dict, limits_dict['dynamic_routes_per_network_limit'])
|
2022-03-17 12:35:33 -07:00
|
|
|
|
2022-03-30 08:03:31 -07:00
|
|
|
# Per VPC peering group metrics
|
2022-03-17 12:35:33 -07:00
|
|
|
get_pgg_data(
|
|
|
|
metrics_dict["metrics_per_peering_group"]["instance_per_peering_group"],
|
2022-03-28 09:47:11 -07:00
|
|
|
gce_instance_dict, GCE_INSTANCES_LIMIT_METRIC,
|
|
|
|
limits_dict['number_of_instances_ppg_limit'])
|
2022-03-08 09:36:02 -08:00
|
|
|
|
2022-03-17 12:08:58 -07:00
|
|
|
get_pgg_data(
|
|
|
|
metrics_dict["metrics_per_peering_group"]
|
2022-03-24 02:07:58 -07:00
|
|
|
["l4_forwarding_rules_per_peering_group"], l4_forwarding_rules_dict,
|
2022-03-28 09:47:11 -07:00
|
|
|
L4_FORWARDING_RULES_LIMIT_METRIC,
|
|
|
|
limits_dict['internal_forwarding_rules_l4_ppg_limit'])
|
2022-03-08 09:36:02 -08:00
|
|
|
|
2022-03-17 12:08:58 -07:00
|
|
|
get_pgg_data(
|
|
|
|
metrics_dict["metrics_per_peering_group"]
|
2022-03-24 02:07:58 -07:00
|
|
|
["l7_forwarding_rules_per_peering_group"], l7_forwarding_rules_dict,
|
2022-03-28 09:47:11 -07:00
|
|
|
L7_FORWARDING_RULES_LIMIT_METRIC,
|
|
|
|
limits_dict['internal_forwarding_rules_l7_ppg_limit'])
|
2022-03-08 09:36:02 -08:00
|
|
|
|
2022-03-17 12:08:58 -07:00
|
|
|
get_pgg_data(
|
|
|
|
metrics_dict["metrics_per_peering_group"]
|
2022-03-24 02:07:58 -07:00
|
|
|
["subnet_ranges_per_peering_group"], subnet_range_dict,
|
2022-03-28 09:47:11 -07:00
|
|
|
SUBNET_RANGES_LIMIT_METRIC,
|
2022-03-30 08:03:31 -07:00
|
|
|
limits_dict['number_of_subnet_IP_ranges_ppg_limit'])
|
2022-03-08 09:36:02 -08:00
|
|
|
|
2022-04-12 00:58:12 -07:00
|
|
|
get_dynamic_routes_ppg(
|
|
|
|
metrics_dict["metrics_per_peering_group"]
|
|
|
|
["dynamic_routes_per_peering_group"], dynamic_routes_dict,
|
|
|
|
limits_dict['number_of_subnet_IP_ranges_ppg_limit'])
|
|
|
|
|
2022-03-09 10:02:59 -08:00
|
|
|
return 'Function executed successfully'
|
2022-03-08 09:36:02 -08:00
|
|
|
|
2022-03-17 12:08:58 -07:00
|
|
|
|
2022-03-24 02:07:58 -07:00
|
|
|
def get_l4_forwarding_rules_dict():
|
|
|
|
'''
|
|
|
|
Calls the Asset Inventory API to get all L4 Forwarding Rules under the GCP organization.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
None
|
|
|
|
Returns:
|
|
|
|
forwarding_rules_dict (dictionary of string: int): Keys are the network links and values are the number of Forwarding Rules per network.
|
|
|
|
'''
|
|
|
|
client = asset_v1.AssetServiceClient()
|
|
|
|
|
|
|
|
read_mask = field_mask_pb2.FieldMask()
|
|
|
|
read_mask.FromJsonString('name,versionedResources')
|
|
|
|
|
2022-03-28 09:44:16 -07:00
|
|
|
forwarding_rules_dict = defaultdict(int)
|
2022-03-24 02:07:58 -07:00
|
|
|
|
|
|
|
response = client.search_all_resources(
|
|
|
|
request={
|
|
|
|
"scope": f"organizations/{ORGANIZATION_ID}",
|
|
|
|
"asset_types": ["compute.googleapis.com/ForwardingRule"],
|
|
|
|
"read_mask": read_mask,
|
|
|
|
})
|
|
|
|
for resource in response:
|
|
|
|
internal = False
|
|
|
|
network_link = ""
|
|
|
|
for versioned in resource.versioned_resources:
|
|
|
|
for field_name, field_value in versioned.resource.items():
|
|
|
|
if field_name == "loadBalancingScheme":
|
|
|
|
internal = (field_value == "INTERNAL")
|
|
|
|
if field_name == "network":
|
|
|
|
network_link = field_value
|
|
|
|
if internal:
|
|
|
|
if network_link in forwarding_rules_dict:
|
|
|
|
forwarding_rules_dict[network_link] += 1
|
|
|
|
else:
|
|
|
|
forwarding_rules_dict[network_link] = 1
|
|
|
|
|
|
|
|
return forwarding_rules_dict
|
|
|
|
|
|
|
|
|
|
|
|
def get_l7_forwarding_rules_dict():
|
|
|
|
'''
|
|
|
|
Calls the Asset Inventory API to get all L7 Forwarding Rules under the GCP organization.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
None
|
|
|
|
Returns:
|
|
|
|
forwarding_rules_dict (dictionary of string: int): Keys are the network links and values are the number of Forwarding Rules per network.
|
|
|
|
'''
|
|
|
|
client = asset_v1.AssetServiceClient()
|
|
|
|
|
|
|
|
read_mask = field_mask_pb2.FieldMask()
|
|
|
|
read_mask.FromJsonString('name,versionedResources')
|
|
|
|
|
2022-03-28 09:44:16 -07:00
|
|
|
forwarding_rules_dict = defaultdict(int)
|
2022-03-24 02:07:58 -07:00
|
|
|
|
|
|
|
response = client.search_all_resources(
|
|
|
|
request={
|
|
|
|
"scope": f"organizations/{ORGANIZATION_ID}",
|
|
|
|
"asset_types": ["compute.googleapis.com/ForwardingRule"],
|
|
|
|
"read_mask": read_mask,
|
|
|
|
})
|
|
|
|
for resource in response:
|
|
|
|
internal = False
|
|
|
|
network_link = ""
|
|
|
|
for versioned in resource.versioned_resources:
|
|
|
|
for field_name, field_value in versioned.resource.items():
|
|
|
|
if field_name == "loadBalancingScheme":
|
|
|
|
internal = (field_value == "INTERNAL_MANAGED")
|
|
|
|
if field_name == "network":
|
|
|
|
network_link = field_value
|
|
|
|
if internal:
|
|
|
|
if network_link in forwarding_rules_dict:
|
|
|
|
forwarding_rules_dict[network_link] += 1
|
|
|
|
else:
|
|
|
|
forwarding_rules_dict[network_link] = 1
|
|
|
|
|
|
|
|
return forwarding_rules_dict
|
|
|
|
|
|
|
|
|
|
|
|
def get_gce_instance_dict():
|
|
|
|
'''
|
|
|
|
Calls the Asset Inventory API to get all GCE instances under the GCP organization.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
None
|
|
|
|
Returns:
|
|
|
|
gce_instance_dict (dictionary of string: int): Keys are the network links and values are the number of GCE Instances per network.
|
|
|
|
'''
|
|
|
|
client = asset_v1.AssetServiceClient()
|
|
|
|
|
2022-03-28 09:44:16 -07:00
|
|
|
gce_instance_dict = defaultdict(int)
|
2022-03-24 02:07:58 -07:00
|
|
|
|
|
|
|
response = client.search_all_resources(
|
|
|
|
request={
|
|
|
|
"scope": f"organizations/{ORGANIZATION_ID}",
|
|
|
|
"asset_types": ["compute.googleapis.com/Instance"],
|
|
|
|
})
|
|
|
|
for resource in response:
|
|
|
|
for field_name, field_value in resource.additional_attributes.items():
|
|
|
|
if field_name == "networkInterfaceNetworks":
|
|
|
|
for network in field_value:
|
|
|
|
if network in gce_instance_dict:
|
|
|
|
gce_instance_dict[network] += 1
|
|
|
|
else:
|
|
|
|
gce_instance_dict[network] = 1
|
|
|
|
|
|
|
|
return gce_instance_dict
|
|
|
|
|
|
|
|
|
|
|
|
def get_subnet_ranges_dict():
|
|
|
|
'''
|
|
|
|
Calls the Asset Inventory API to get all Subnet ranges under the GCP organization.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
None
|
|
|
|
Returns:
|
|
|
|
subnet_range_dict (dictionary of string: int): Keys are the network links and values are the number of subnet ranges per network.
|
|
|
|
'''
|
|
|
|
client = asset_v1.AssetServiceClient()
|
2022-03-28 09:44:16 -07:00
|
|
|
subnet_range_dict = defaultdict(int)
|
2022-03-24 02:07:58 -07:00
|
|
|
read_mask = field_mask_pb2.FieldMask()
|
|
|
|
read_mask.FromJsonString('name,versionedResources')
|
|
|
|
|
|
|
|
response = client.search_all_resources(
|
|
|
|
request={
|
|
|
|
"scope": f"organizations/{ORGANIZATION_ID}",
|
|
|
|
"asset_types": ["compute.googleapis.com/Subnetwork"],
|
|
|
|
"read_mask": read_mask,
|
|
|
|
})
|
|
|
|
for resource in response:
|
|
|
|
ranges = 0
|
|
|
|
network_link = None
|
|
|
|
|
|
|
|
for versioned in resource.versioned_resources:
|
|
|
|
for field_name, field_value in versioned.resource.items():
|
|
|
|
if field_name == "network":
|
|
|
|
network_link = field_value
|
|
|
|
ranges += 1
|
|
|
|
if field_name == "secondaryIpRanges":
|
|
|
|
for range in field_value:
|
|
|
|
ranges += 1
|
|
|
|
|
|
|
|
if network_link in subnet_range_dict:
|
|
|
|
subnet_range_dict[network_link] += ranges
|
|
|
|
else:
|
|
|
|
subnet_range_dict[network_link] = ranges
|
|
|
|
|
|
|
|
return subnet_range_dict
|
|
|
|
|
|
|
|
|
2022-03-08 09:36:02 -08:00
|
|
|
def create_client():
|
2022-03-09 10:02:59 -08:00
|
|
|
'''
|
|
|
|
Creates the monitoring API client, that will be used to create, read and update custom metrics.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
None
|
|
|
|
Returns:
|
|
|
|
client (monitoring_v3.MetricServiceClient): Monitoring API client
|
|
|
|
interval (monitoring_v3.TimeInterval): Interval for the metric data points (24 hours)
|
|
|
|
'''
|
|
|
|
try:
|
|
|
|
client = monitoring_v3.MetricServiceClient()
|
|
|
|
now = time.time()
|
|
|
|
seconds = int(now)
|
2022-03-17 12:08:58 -07:00
|
|
|
nanos = int((now - seconds) * 10**9)
|
|
|
|
interval = monitoring_v3.TimeInterval({
|
|
|
|
"end_time": {
|
|
|
|
"seconds": seconds,
|
|
|
|
"nanos": nanos
|
|
|
|
},
|
|
|
|
"start_time": {
|
|
|
|
"seconds": (seconds - 86400),
|
|
|
|
"nanos": nanos
|
|
|
|
},
|
2022-03-09 10:02:59 -08:00
|
|
|
})
|
|
|
|
return (client, interval)
|
|
|
|
except Exception as e:
|
|
|
|
raise Exception("Error occurred creating the client: {}".format(e))
|
|
|
|
|
2022-03-17 12:08:58 -07:00
|
|
|
|
2022-03-17 08:36:36 -07:00
|
|
|
def create_metrics():
|
2022-03-28 09:44:16 -07:00
|
|
|
'''
|
|
|
|
Creates all Cloud Monitoring custom metrics based on the metric.yaml file
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
None
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
metrics_dict (dictionary of dictionary of string: string): metrics names and descriptions
|
|
|
|
limits_dict (dictionary of dictionary of string: int): limits_dict[metric_name]: dict[network_name] = limit_value
|
|
|
|
'''
|
2022-03-24 02:07:58 -07:00
|
|
|
client = monitoring_v3.MetricServiceClient()
|
|
|
|
existing_metrics = []
|
|
|
|
for desc in client.list_metric_descriptors(name=MONITORING_PROJECT_LINK):
|
|
|
|
existing_metrics.append(desc.type)
|
2022-03-28 09:44:16 -07:00
|
|
|
limits_dict = {}
|
2022-03-24 02:07:58 -07:00
|
|
|
|
2022-03-17 08:36:36 -07:00
|
|
|
with open("metrics.yaml", 'r') as stream:
|
|
|
|
try:
|
2022-03-17 12:08:58 -07:00
|
|
|
metrics_dict = yaml.safe_load(stream)
|
2022-03-08 09:36:02 -08:00
|
|
|
|
2022-03-17 12:08:58 -07:00
|
|
|
for metric_list in metrics_dict.values():
|
|
|
|
for metric in metric_list.values():
|
2022-03-28 09:44:16 -07:00
|
|
|
for sub_metric_key, sub_metric in metric.items():
|
2022-03-24 02:07:58 -07:00
|
|
|
metric_link = f"custom.googleapis.com/{sub_metric['name']}"
|
|
|
|
# If the metric doesn't exist yet, then we create it
|
|
|
|
if metric_link not in existing_metrics:
|
|
|
|
create_metric(sub_metric["name"], sub_metric["description"])
|
2022-03-28 09:44:16 -07:00
|
|
|
# Parse limits (both default values and network specific ones)
|
|
|
|
if sub_metric_key == "limit":
|
|
|
|
limits_dict_for_metric = {}
|
|
|
|
for network_link, limit_value in sub_metric["values"].items():
|
|
|
|
limits_dict_for_metric[network_link] = limit_value
|
|
|
|
limits_dict[sub_metric["name"]] = limits_dict_for_metric
|
2022-03-28 09:47:11 -07:00
|
|
|
|
2022-03-28 09:44:16 -07:00
|
|
|
return metrics_dict, limits_dict
|
2022-03-17 08:36:36 -07:00
|
|
|
except yaml.YAMLError as exc:
|
2022-03-17 12:08:58 -07:00
|
|
|
print(exc)
|
|
|
|
|
2022-03-08 09:36:02 -08:00
|
|
|
|
2022-03-09 10:02:59 -08:00
|
|
|
def create_metric(metric_name, description):
|
|
|
|
'''
|
|
|
|
Creates a Cloud Monitoring metric based on the parameter given if the metric is not already existing
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
metric_name (string): Name of the metric to be created
|
|
|
|
description (string): Description of the metric to be created
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
None
|
|
|
|
'''
|
|
|
|
client = monitoring_v3.MetricServiceClient()
|
|
|
|
|
2022-03-24 02:07:58 -07:00
|
|
|
descriptor = ga_metric.MetricDescriptor()
|
|
|
|
descriptor.type = f"custom.googleapis.com/{metric_name}"
|
|
|
|
descriptor.metric_kind = ga_metric.MetricDescriptor.MetricKind.GAUGE
|
|
|
|
descriptor.value_type = ga_metric.MetricDescriptor.ValueType.DOUBLE
|
|
|
|
descriptor.description = description
|
|
|
|
descriptor = client.create_metric_descriptor(name=MONITORING_PROJECT_LINK,
|
|
|
|
metric_descriptor=descriptor)
|
|
|
|
print("Created {}.".format(descriptor.name))
|
2022-03-08 09:36:02 -08:00
|
|
|
|
2022-03-17 12:08:58 -07:00
|
|
|
|
2022-03-28 09:44:16 -07:00
|
|
|
def get_gce_instances_data(metrics_dict, gce_instance_dict, limit_dict):
|
2022-03-09 10:02:59 -08:00
|
|
|
'''
|
|
|
|
Gets the data for GCE instances per VPC Network and writes it to the metric defined in instance_metric.
|
2022-03-08 09:36:02 -08:00
|
|
|
|
2022-03-09 10:02:59 -08:00
|
|
|
Parameters:
|
2022-03-17 08:36:36 -07:00
|
|
|
metrics_dict (dictionary of dictionary of string: string): metrics names and descriptions
|
2022-03-24 02:07:58 -07:00
|
|
|
gce_instance_dict (dictionary of string: int): Keys are the network links and values are the number of GCE Instances per network.
|
2022-03-28 09:44:16 -07:00
|
|
|
limit_dict (dictionary of string:int): Dictionary with the network link as key and the limit as value
|
2022-03-09 10:02:59 -08:00
|
|
|
Returns:
|
2022-03-24 02:07:58 -07:00
|
|
|
gce_instance_dict
|
2022-03-09 10:02:59 -08:00
|
|
|
'''
|
|
|
|
# Existing GCP Monitoring metrics for GCE instances
|
|
|
|
metric_instances_limit = "compute.googleapis.com/quota/instances_per_vpc_network/limit"
|
2022-03-08 09:36:02 -08:00
|
|
|
|
2022-03-17 09:28:17 -07:00
|
|
|
for project in MONITORED_PROJECTS_LIST:
|
2022-03-09 10:02:59 -08:00
|
|
|
network_dict = get_networks(project)
|
2022-03-08 09:36:02 -08:00
|
|
|
|
2022-03-17 12:08:58 -07:00
|
|
|
current_quota_limit = get_quota_current_limit(f"projects/{project}",
|
|
|
|
metric_instances_limit)
|
2022-04-12 05:13:05 -07:00
|
|
|
if current_quota_limit is None:
|
2022-04-13 04:10:15 -07:00
|
|
|
print(
|
|
|
|
f"Could not write number of instances for projects/{project} due to missing quotas"
|
|
|
|
)
|
2022-04-12 05:13:05 -07:00
|
|
|
|
2022-03-09 10:02:59 -08:00
|
|
|
current_quota_limit_view = customize_quota_view(current_quota_limit)
|
2022-03-08 09:36:02 -08:00
|
|
|
|
2022-03-09 10:02:59 -08:00
|
|
|
for net in network_dict:
|
2022-03-28 09:44:16 -07:00
|
|
|
set_limits(net, current_quota_limit_view, limit_dict)
|
2022-03-24 02:07:58 -07:00
|
|
|
|
|
|
|
usage = 0
|
2022-03-30 08:03:31 -07:00
|
|
|
if net['self_link'] in gce_instance_dict:
|
|
|
|
usage = gce_instance_dict[net['self_link']]
|
2022-03-24 02:07:58 -07:00
|
|
|
|
2022-03-17 12:08:58 -07:00
|
|
|
write_data_to_metric(
|
2022-03-24 02:07:58 -07:00
|
|
|
project, usage, metrics_dict["metrics_per_network"]
|
2022-03-28 09:44:16 -07:00
|
|
|
["instance_per_network"]["usage"]["name"], net['network_name'])
|
2022-03-17 12:08:58 -07:00
|
|
|
write_data_to_metric(
|
|
|
|
project, net['limit'], metrics_dict["metrics_per_network"]
|
2022-03-28 09:44:16 -07:00
|
|
|
["instance_per_network"]["limit"]["name"], net['network_name'])
|
2022-03-17 12:08:58 -07:00
|
|
|
write_data_to_metric(
|
2022-03-24 02:07:58 -07:00
|
|
|
project, usage / net['limit'], metrics_dict["metrics_per_network"]
|
2022-03-28 09:44:16 -07:00
|
|
|
["instance_per_network"]["utilization"]["name"], net['network_name'])
|
2022-03-08 09:36:02 -08:00
|
|
|
|
2022-03-09 10:02:59 -08:00
|
|
|
print(f"Wrote number of instances to metric for projects/{project}")
|
2022-03-08 09:36:02 -08:00
|
|
|
|
|
|
|
|
2022-03-28 09:44:16 -07:00
|
|
|
def get_vpc_peering_data(metrics_dict, limit_dict):
|
2022-03-09 10:02:59 -08:00
|
|
|
'''
|
2022-03-17 08:36:36 -07:00
|
|
|
Gets the data for VPC peerings (active or not) and writes it to the metric defined (vpc_peering_active_metric and vpc_peering_metric).
|
2022-03-09 10:02:59 -08:00
|
|
|
|
|
|
|
Parameters:
|
2022-03-17 08:36:36 -07:00
|
|
|
metrics_dict (dictionary of dictionary of string: string): metrics names and descriptions
|
2022-03-28 09:44:16 -07:00
|
|
|
limit_dict (dictionary of string:int): Dictionary with the network link as key and the limit as value
|
2022-03-09 10:02:59 -08:00
|
|
|
Returns:
|
|
|
|
None
|
|
|
|
'''
|
2022-03-17 09:28:17 -07:00
|
|
|
for project in MONITORED_PROJECTS_LIST:
|
2022-03-28 09:47:11 -07:00
|
|
|
active_vpc_peerings, vpc_peerings = gather_vpc_peerings_data(
|
|
|
|
project, limit_dict)
|
2022-03-09 10:02:59 -08:00
|
|
|
for peering in active_vpc_peerings:
|
2022-03-17 12:08:58 -07:00
|
|
|
write_data_to_metric(
|
|
|
|
project, peering['active_peerings'],
|
|
|
|
metrics_dict["metrics_per_network"]["vpc_peering_active_per_network"]
|
|
|
|
["usage"]["name"], peering['network_name'])
|
|
|
|
write_data_to_metric(
|
|
|
|
project, peering['network_limit'], metrics_dict["metrics_per_network"]
|
|
|
|
["vpc_peering_active_per_network"]["limit"]["name"],
|
|
|
|
peering['network_name'])
|
|
|
|
write_data_to_metric(
|
|
|
|
project, peering['active_peerings'] / peering['network_limit'],
|
|
|
|
metrics_dict["metrics_per_network"]["vpc_peering_active_per_network"]
|
|
|
|
["utilization"]["name"], peering['network_name'])
|
|
|
|
print("Wrote number of active VPC peerings to custom metric for project:",
|
|
|
|
project)
|
2022-03-09 10:02:59 -08:00
|
|
|
|
|
|
|
for peering in vpc_peerings:
|
2022-03-17 12:08:58 -07:00
|
|
|
write_data_to_metric(
|
|
|
|
project, peering['peerings'], metrics_dict["metrics_per_network"]
|
|
|
|
["vpc_peering_per_network"]["usage"]["name"], peering['network_name'])
|
|
|
|
write_data_to_metric(
|
|
|
|
project, peering['network_limit'], metrics_dict["metrics_per_network"]
|
|
|
|
["vpc_peering_per_network"]["limit"]["name"], peering['network_name'])
|
|
|
|
write_data_to_metric(
|
|
|
|
project, peering['peerings'] / peering['network_limit'],
|
|
|
|
metrics_dict["metrics_per_network"]["vpc_peering_per_network"]
|
|
|
|
["utilization"]["name"], peering['network_name'])
|
2022-03-09 10:02:59 -08:00
|
|
|
print("Wrote number of VPC peerings to custom metric for project:", project)
|
2022-03-08 09:36:02 -08:00
|
|
|
|
2022-03-17 12:08:58 -07:00
|
|
|
|
2022-03-28 09:44:16 -07:00
|
|
|
def gather_vpc_peerings_data(project_id, limit_dict):
|
2022-03-09 10:02:59 -08:00
|
|
|
'''
|
|
|
|
Gets the data for all VPC peerings (active or not) in project_id and writes it to the metric defined in vpc_peering_active_metric and vpc_peering_metric.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
project_id (string): We will take all VPCs in that project_id and look for all peerings to these VPCs.
|
2022-03-28 09:44:16 -07:00
|
|
|
limit_dict (dictionary of string:int): Dictionary with the network link as key and the limit as value
|
2022-03-09 10:02:59 -08:00
|
|
|
Returns:
|
|
|
|
active_peerings_dict (dictionary of string: string): Contains project_id, network_name, network_limit for each active VPC peering.
|
|
|
|
peerings_dict (dictionary of string: string): Contains project_id, network_name, network_limit for each VPC peering.
|
|
|
|
'''
|
2022-03-17 12:08:58 -07:00
|
|
|
active_peerings_dict = []
|
2022-03-09 10:02:59 -08:00
|
|
|
peerings_dict = []
|
|
|
|
request = service.networks().list(project=project_id)
|
|
|
|
response = request.execute()
|
|
|
|
if 'items' in response:
|
|
|
|
for network in response['items']:
|
|
|
|
if 'peerings' in network:
|
|
|
|
STATE = network['peerings'][0]['state']
|
|
|
|
if STATE == "ACTIVE":
|
2022-03-17 12:08:58 -07:00
|
|
|
active_peerings_count = len(network['peerings'])
|
2022-03-09 10:02:59 -08:00
|
|
|
else:
|
|
|
|
active_peerings_count = 0
|
2022-03-08 09:36:02 -08:00
|
|
|
|
2022-03-17 12:08:58 -07:00
|
|
|
peerings_count = len(network['peerings'])
|
2022-03-09 10:02:59 -08:00
|
|
|
else:
|
|
|
|
peerings_count = 0
|
|
|
|
active_peerings_count = 0
|
2022-03-28 09:47:11 -07:00
|
|
|
|
2022-03-28 09:44:16 -07:00
|
|
|
network_link = f"https://www.googleapis.com/compute/v1/projects/{project_id}/global/networks/{network['name']}"
|
|
|
|
network_limit = get_limit_ppg(network_link, limit_dict)
|
2022-03-08 09:36:02 -08:00
|
|
|
|
2022-03-17 12:08:58 -07:00
|
|
|
active_d = {
|
|
|
|
'project_id': project_id,
|
|
|
|
'network_name': network['name'],
|
|
|
|
'active_peerings': active_peerings_count,
|
2022-03-28 09:44:16 -07:00
|
|
|
'network_limit': network_limit
|
2022-03-17 12:08:58 -07:00
|
|
|
}
|
2022-03-09 10:02:59 -08:00
|
|
|
active_peerings_dict.append(active_d)
|
2022-03-17 12:08:58 -07:00
|
|
|
d = {
|
|
|
|
'project_id': project_id,
|
|
|
|
'network_name': network['name'],
|
|
|
|
'peerings': peerings_count,
|
2022-03-28 09:44:16 -07:00
|
|
|
'network_limit': network_limit
|
2022-03-17 12:08:58 -07:00
|
|
|
}
|
2022-03-09 10:02:59 -08:00
|
|
|
peerings_dict.append(d)
|
2022-03-08 09:36:02 -08:00
|
|
|
|
2022-03-09 10:02:59 -08:00
|
|
|
return active_peerings_dict, peerings_dict
|
2022-03-08 09:36:02 -08:00
|
|
|
|
2022-03-17 12:08:58 -07:00
|
|
|
|
2022-03-28 09:44:16 -07:00
|
|
|
def get_limit_ppg(network_link, limit_dict):
|
2022-03-09 10:02:59 -08:00
|
|
|
'''
|
|
|
|
Checks if this network has a specific limit for a metric, if so, returns that limit, if not, returns the default limit.
|
|
|
|
|
|
|
|
Parameters:
|
2022-03-28 09:44:16 -07:00
|
|
|
network_link (string): VPC network link.
|
2022-03-09 10:02:59 -08:00
|
|
|
limit_list (list of string): Used to get the limit per VPC or the default limit.
|
|
|
|
Returns:
|
2022-03-28 09:44:16 -07:00
|
|
|
limit_dict (dictionary of string:int): Dictionary with the network link as key and the limit as value
|
2022-03-09 10:02:59 -08:00
|
|
|
'''
|
2022-03-28 09:44:16 -07:00
|
|
|
if network_link in limit_dict:
|
|
|
|
return limit_dict[network_link]
|
2022-03-09 10:02:59 -08:00
|
|
|
else:
|
2022-03-28 09:44:16 -07:00
|
|
|
if 'default_value' in limit_dict:
|
|
|
|
return limit_dict['default_value']
|
2022-03-08 09:36:02 -08:00
|
|
|
else:
|
2022-03-28 09:44:16 -07:00
|
|
|
print(f"Error: limit not found for {network_link}")
|
2022-03-09 10:02:59 -08:00
|
|
|
return 0
|
2022-03-08 09:36:02 -08:00
|
|
|
|
2022-03-17 12:08:58 -07:00
|
|
|
|
2022-03-28 09:47:11 -07:00
|
|
|
def get_l4_forwarding_rules_data(metrics_dict, forwarding_rules_dict,
|
|
|
|
limit_dict):
|
2022-03-09 10:02:59 -08:00
|
|
|
'''
|
|
|
|
Gets the data for L4 Internal Forwarding Rules per VPC Network and writes it to the metric defined in forwarding_rules_metric.
|
2022-03-08 09:36:02 -08:00
|
|
|
|
2022-03-09 10:02:59 -08:00
|
|
|
Parameters:
|
2022-03-28 09:44:16 -07:00
|
|
|
metrics_dict (dictionary of dictionary of string: string): metrics names and descriptions.
|
2022-03-24 02:07:58 -07:00
|
|
|
forwarding_rules_dict (dictionary of string: int): Keys are the network links and values are the number of Forwarding Rules per network.
|
2022-03-28 09:44:16 -07:00
|
|
|
limit_dict (dictionary of string:int): Dictionary with the network link as key and the limit as value.
|
2022-03-09 10:02:59 -08:00
|
|
|
Returns:
|
|
|
|
None
|
|
|
|
'''
|
2022-03-17 09:28:17 -07:00
|
|
|
for project in MONITORED_PROJECTS_LIST:
|
2022-03-09 10:02:59 -08:00
|
|
|
network_dict = get_networks(project)
|
2022-03-08 09:36:02 -08:00
|
|
|
|
2022-03-28 09:47:11 -07:00
|
|
|
current_quota_limit = get_quota_current_limit(
|
|
|
|
f"projects/{project}", L4_FORWARDING_RULES_LIMIT_METRIC)
|
2022-03-08 09:36:02 -08:00
|
|
|
|
2022-04-12 05:13:05 -07:00
|
|
|
if current_quota_limit is None:
|
2022-04-13 04:10:15 -07:00
|
|
|
print(
|
|
|
|
f"Could not write L4 forwarding rules to metric for projects/{project} due to missing quotas"
|
|
|
|
)
|
2022-04-12 05:13:05 -07:00
|
|
|
continue
|
|
|
|
|
2022-03-09 10:02:59 -08:00
|
|
|
current_quota_limit_view = customize_quota_view(current_quota_limit)
|
2022-03-08 09:36:02 -08:00
|
|
|
|
2022-03-09 10:02:59 -08:00
|
|
|
for net in network_dict:
|
2022-03-28 09:44:16 -07:00
|
|
|
set_limits(net, current_quota_limit_view, limit_dict)
|
2022-03-24 02:07:58 -07:00
|
|
|
|
|
|
|
usage = 0
|
2022-03-30 08:03:31 -07:00
|
|
|
if net['self_link'] in forwarding_rules_dict:
|
|
|
|
usage = forwarding_rules_dict[net['self_link']]
|
2022-03-24 02:07:58 -07:00
|
|
|
|
2022-03-17 12:08:58 -07:00
|
|
|
write_data_to_metric(
|
2022-03-24 02:07:58 -07:00
|
|
|
project, usage, metrics_dict["metrics_per_network"]
|
2022-03-17 12:08:58 -07:00
|
|
|
["l4_forwarding_rules_per_network"]["usage"]["name"],
|
2022-03-28 09:44:16 -07:00
|
|
|
net['network_name'])
|
2022-03-17 12:08:58 -07:00
|
|
|
write_data_to_metric(
|
|
|
|
project, net['limit'], metrics_dict["metrics_per_network"]
|
|
|
|
["l4_forwarding_rules_per_network"]["limit"]["name"],
|
2022-03-28 09:44:16 -07:00
|
|
|
net['network_name'])
|
2022-03-17 12:08:58 -07:00
|
|
|
write_data_to_metric(
|
2022-03-24 02:07:58 -07:00
|
|
|
project, usage / net['limit'], metrics_dict["metrics_per_network"]
|
|
|
|
["l4_forwarding_rules_per_network"]["utilization"]["name"],
|
2022-03-28 09:44:16 -07:00
|
|
|
net['network_name'])
|
2022-03-17 12:08:58 -07:00
|
|
|
|
|
|
|
print(
|
|
|
|
f"Wrote number of L4 forwarding rules to metric for projects/{project}")
|
2022-03-08 09:36:02 -08:00
|
|
|
|
2022-04-13 04:10:15 -07:00
|
|
|
|
2022-04-12 05:21:14 -07:00
|
|
|
def get_l7_forwarding_rules_data(metrics_dict, forwarding_rules_dict,
|
|
|
|
limit_dict):
|
|
|
|
'''
|
|
|
|
Gets the data for L7 Internal Forwarding Rules per VPC Network and writes it to the metric defined in forwarding_rules_metric.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
metrics_dict (dictionary of dictionary of string: string): metrics names and descriptions.
|
|
|
|
forwarding_rules_dict (dictionary of string: int): Keys are the network links and values are the number of Forwarding Rules per network.
|
|
|
|
limit_dict (dictionary of string:int): Dictionary with the network link as key and the limit as value.
|
|
|
|
Returns:
|
|
|
|
None
|
|
|
|
'''
|
|
|
|
for project in MONITORED_PROJECTS_LIST:
|
|
|
|
network_dict = get_networks(project)
|
|
|
|
|
|
|
|
current_quota_limit = get_quota_current_limit(
|
2022-04-12 06:31:42 -07:00
|
|
|
f"projects/{project}", L7_FORWARDING_RULES_LIMIT_METRIC)
|
2022-04-12 05:21:14 -07:00
|
|
|
if current_quota_limit is None:
|
2022-04-13 04:10:15 -07:00
|
|
|
print(
|
|
|
|
f"Could not write number of L7 forwarding rules to metric for projects/{project} due to missing quotas"
|
|
|
|
)
|
2022-04-12 05:21:14 -07:00
|
|
|
continue
|
|
|
|
|
|
|
|
current_quota_limit_view = customize_quota_view(current_quota_limit)
|
|
|
|
|
|
|
|
for net in network_dict:
|
|
|
|
set_limits(net, current_quota_limit_view, limit_dict)
|
|
|
|
|
|
|
|
usage = 0
|
|
|
|
if net['self_link'] in forwarding_rules_dict:
|
|
|
|
usage = forwarding_rules_dict[net['self_link']]
|
|
|
|
|
|
|
|
write_data_to_metric(
|
|
|
|
project, usage, metrics_dict["metrics_per_network"]
|
|
|
|
["l7_forwarding_rules_per_network"]["usage"]["name"],
|
|
|
|
net['network_name'])
|
|
|
|
write_data_to_metric(
|
|
|
|
project, net['limit'], metrics_dict["metrics_per_network"]
|
|
|
|
["l7_forwarding_rules_per_network"]["limit"]["name"],
|
|
|
|
net['network_name'])
|
|
|
|
write_data_to_metric(
|
|
|
|
project, usage / net['limit'], metrics_dict["metrics_per_network"]
|
|
|
|
["l7_forwarding_rules_per_network"]["utilization"]["name"],
|
|
|
|
net['network_name'])
|
|
|
|
|
|
|
|
print(
|
|
|
|
f"Wrote number of L7 forwarding rules to metric for projects/{project}")
|
|
|
|
|
2022-03-08 09:36:02 -08:00
|
|
|
|
2022-03-28 09:44:16 -07:00
|
|
|
def get_pgg_data(metric_dict, usage_dict, limit_metric, limit_dict):
|
2022-03-09 10:02:59 -08:00
|
|
|
'''
|
|
|
|
This function gets the usage, limit and utilization per VPC peering group for a specific metric for all projects to be monitored.
|
|
|
|
|
|
|
|
Parameters:
|
2022-03-24 02:07:58 -07:00
|
|
|
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
|
2022-04-12 00:58:12 -07:00
|
|
|
limit_metric (string): Name of the existing GCP metric for limit per VPC network
|
2022-03-28 09:44:16 -07:00
|
|
|
limit_dict (dictionary of string:int): Dictionary with the network link as key and the limit as value
|
2022-03-09 10:02:59 -08:00
|
|
|
Returns:
|
|
|
|
None
|
|
|
|
'''
|
2022-03-17 09:28:17 -07:00
|
|
|
for project in MONITORED_PROJECTS_LIST:
|
2022-03-09 10:02:59 -08:00
|
|
|
network_dict_list = gather_peering_data(project)
|
|
|
|
# Network dict list is a list of dictionary (one for each network)
|
|
|
|
# For each network, this dictionary contains:
|
|
|
|
# project_id, network_name, network_id, usage, limit, peerings (list of peered networks)
|
|
|
|
# peerings is a list of dictionary (one for each peered network) and contains:
|
|
|
|
# project_id, network_name, network_id
|
2022-04-12 05:13:05 -07:00
|
|
|
current_quota_limit = get_quota_current_limit(f"projects/{project}",
|
|
|
|
limit_metric)
|
|
|
|
if current_quota_limit is None:
|
2022-04-13 04:10:15 -07:00
|
|
|
print(
|
|
|
|
f"Could not write number of L7 forwarding rules to metric for projects/{project} due to missing quotas"
|
|
|
|
)
|
2022-04-12 05:13:05 -07:00
|
|
|
continue
|
|
|
|
|
|
|
|
current_quota_limit_view = customize_quota_view(current_quota_limit)
|
2022-03-17 12:08:58 -07:00
|
|
|
|
2022-03-09 10:02:59 -08:00
|
|
|
# For each network in this GCP project
|
|
|
|
for network_dict in network_dict_list:
|
2022-04-12 05:13:05 -07:00
|
|
|
if network_dict['network_id'] == 0:
|
|
|
|
print(
|
|
|
|
f"Could not write {metric_dict['usage']['name']} for peering group {network_dict['network_name']} in {project} due to missing permissions."
|
|
|
|
)
|
|
|
|
continue
|
2022-03-28 09:44:16 -07:00
|
|
|
network_link = f"https://www.googleapis.com/compute/v1/projects/{project}/global/networks/{network_dict['network_name']}"
|
|
|
|
|
2022-03-28 09:47:11 -07:00
|
|
|
limit = get_limit_network(network_dict, network_link,
|
|
|
|
current_quota_limit_view, limit_dict)
|
2022-03-24 02:07:58 -07:00
|
|
|
|
|
|
|
usage = 0
|
|
|
|
if network_link in usage_dict:
|
|
|
|
usage = usage_dict[network_link]
|
2022-03-08 09:36:02 -08:00
|
|
|
|
2022-03-09 10:02:59 -08:00
|
|
|
# Here we add usage and limit to the network dictionary
|
|
|
|
network_dict["usage"] = usage
|
|
|
|
network_dict["limit"] = limit
|
2022-03-08 09:36:02 -08:00
|
|
|
|
2022-03-09 10:02:59 -08:00
|
|
|
# For every peered network, get usage and limits
|
2022-03-28 09:44:16 -07:00
|
|
|
for peered_network_dict in network_dict['peerings']:
|
|
|
|
peered_network_link = f"https://www.googleapis.com/compute/v1/projects/{peered_network_dict['project_id']}/global/networks/{peered_network_dict['network_name']}"
|
2022-03-24 02:07:58 -07:00
|
|
|
peered_usage = 0
|
|
|
|
if peered_network_link in usage_dict:
|
|
|
|
peered_usage = usage_dict[peered_network_link]
|
|
|
|
|
2022-04-12 05:13:05 -07:00
|
|
|
current_peered_quota_limit = get_quota_current_limit(
|
|
|
|
f"projects/{peered_network_dict['project_id']}", limit_metric)
|
|
|
|
if current_peered_quota_limit is None:
|
2022-04-13 04:10:15 -07:00
|
|
|
print(
|
|
|
|
f"Could not write metrics for peering to projects/{peered_network_dict['project_id']} due to missing quotas"
|
|
|
|
)
|
2022-04-12 05:13:05 -07:00
|
|
|
continue
|
|
|
|
|
|
|
|
peering_project_limit = customize_quota_view(current_peered_quota_limit)
|
2022-03-17 12:08:58 -07:00
|
|
|
|
2022-03-28 09:47:11 -07:00
|
|
|
peered_limit = get_limit_network(peered_network_dict,
|
|
|
|
peered_network_link,
|
|
|
|
peering_project_limit, limit_dict)
|
2022-03-09 10:02:59 -08:00
|
|
|
# Here we add usage and limit to the peered network dictionary
|
2022-03-28 09:44:16 -07:00
|
|
|
peered_network_dict["usage"] = peered_usage
|
|
|
|
peered_network_dict["limit"] = peered_limit
|
2022-03-08 09:36:02 -08:00
|
|
|
|
2022-03-17 12:08:58 -07:00
|
|
|
count_effective_limit(project, network_dict, metric_dict["usage"]["name"],
|
|
|
|
metric_dict["limit"]["name"],
|
2022-03-28 09:44:16 -07:00
|
|
|
metric_dict["utilization"]["name"], limit_dict)
|
2022-03-17 12:08:58 -07:00
|
|
|
print(
|
2022-03-30 08:03:31 -07:00
|
|
|
f"Wrote {metric_dict['usage']['name']} for peering group {network_dict['network_name']} in {project}"
|
2022-03-17 12:08:58 -07:00
|
|
|
)
|
|
|
|
|
2022-03-08 09:36:02 -08:00
|
|
|
|
2022-04-12 00:58:12 -07:00
|
|
|
def get_dynamic_routes_ppg(metric_dict, usage_dict, limit_dict):
|
|
|
|
'''
|
|
|
|
This function gets the usage, limit and utilization for the dynamic routes per VPC peering group.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
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
|
|
|
|
limit_dict (dictionary of string:int): Dictionary with the network link as key and the limit as value
|
|
|
|
Returns:
|
|
|
|
None
|
|
|
|
'''
|
|
|
|
for project in MONITORED_PROJECTS_LIST:
|
|
|
|
network_dict_list = gather_peering_data(project)
|
|
|
|
|
|
|
|
for network_dict in network_dict_list:
|
|
|
|
network_link = f"https://www.googleapis.com/compute/v1/projects/{project}/global/networks/{network_dict['network_name']}"
|
|
|
|
|
|
|
|
limit = get_limit_ppg(network_link, limit_dict)
|
|
|
|
|
|
|
|
usage = 0
|
|
|
|
if network_link in usage_dict:
|
|
|
|
usage = usage_dict[network_link]
|
|
|
|
|
|
|
|
# Here we add usage and limit to the network dictionary
|
|
|
|
network_dict["usage"] = usage
|
|
|
|
network_dict["limit"] = limit
|
|
|
|
|
|
|
|
# For every peered network, get usage and limits
|
|
|
|
for peered_network_dict in network_dict['peerings']:
|
|
|
|
peered_network_link = f"https://www.googleapis.com/compute/v1/projects/{peered_network_dict['project_id']}/global/networks/{peered_network_dict['network_name']}"
|
|
|
|
peered_usage = 0
|
|
|
|
if peered_network_link in usage_dict:
|
|
|
|
peered_usage = usage_dict[peered_network_link]
|
|
|
|
|
|
|
|
peered_limit = get_limit_ppg(peered_network_link, limit_dict)
|
|
|
|
|
|
|
|
# Here we add usage and limit to the peered network dictionary
|
|
|
|
peered_network_dict["usage"] = peered_usage
|
|
|
|
peered_network_dict["limit"] = peered_limit
|
|
|
|
|
|
|
|
count_effective_limit(project, network_dict, metric_dict["usage"]["name"],
|
|
|
|
metric_dict["limit"]["name"],
|
|
|
|
metric_dict["utilization"]["name"], limit_dict)
|
|
|
|
print(
|
|
|
|
f"Wrote {metric_dict['usage']['name']} for peering group {network_dict['network_name']} in {project}"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-03-17 12:08:58 -07:00
|
|
|
def count_effective_limit(project_id, network_dict, usage_metric_name,
|
|
|
|
limit_metric_name, utilization_metric_name,
|
2022-03-28 09:44:16 -07:00
|
|
|
limit_dict):
|
2022-03-09 10:02:59 -08:00
|
|
|
'''
|
|
|
|
Calculates the effective limits (using algorithm in the link below) for peering groups and writes data (usage, limit, utilization) to the custom metrics.
|
|
|
|
Source: https://cloud.google.com/vpc/docs/quota#vpc-peering-effective-limit
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
project_id (string): Project ID for the project to be analyzed.
|
|
|
|
network_dict (dictionary of string: string): Contains all required information about the network to get the usage, limit and utilization.
|
|
|
|
usage_metric_name (string): Name of the custom metric to be populated for usage per VPC peering group.
|
|
|
|
limit_metric_name (string): Name of the custom metric to be populated for limit per VPC peering group.
|
|
|
|
utilization_metric_name (string): Name of the custom metric to be populated for utilization per VPC peering group.
|
2022-03-28 09:44:16 -07:00
|
|
|
limit_dict (dictionary of string:int): Dictionary containing the limit per peering group (either VPC specific or default limit).
|
2022-03-09 10:02:59 -08:00
|
|
|
Returns:
|
|
|
|
None
|
|
|
|
'''
|
|
|
|
|
|
|
|
if network_dict['peerings'] == []:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Get usage: Sums usage for current network + all peered networks
|
|
|
|
peering_group_usage = network_dict['usage']
|
|
|
|
for peered_network in network_dict['peerings']:
|
2022-04-12 05:13:05 -07:00
|
|
|
if 'usage' not in peered_network:
|
2022-04-13 04:10:15 -07:00
|
|
|
print(
|
|
|
|
f"Can not add metrics for peered network in projects/{project_id} as no usage metrics exist due to missing permissions"
|
|
|
|
)
|
2022-04-12 05:13:05 -07:00
|
|
|
continue
|
2022-03-09 10:02:59 -08:00
|
|
|
peering_group_usage += peered_network['usage']
|
|
|
|
|
2022-03-28 09:44:16 -07:00
|
|
|
network_link = f"https://www.googleapis.com/compute/v1/projects/{project_id}/global/networks/{network_dict['network_name']}"
|
|
|
|
|
2022-03-09 10:02:59 -08:00
|
|
|
# Calculates effective limit: Step 1: max(per network limit, per network_peering_group limit)
|
2022-03-28 09:47:11 -07:00
|
|
|
limit_step1 = max(network_dict['limit'],
|
|
|
|
get_limit_ppg(network_link, limit_dict))
|
2022-03-09 10:02:59 -08:00
|
|
|
|
|
|
|
# Calculates effective limit: Step 2: List of max(per network limit, per network_peering_group limit) for each peered network
|
|
|
|
limit_step2 = []
|
|
|
|
for peered_network in network_dict['peerings']:
|
2022-03-28 09:44:16 -07:00
|
|
|
peered_network_link = f"https://www.googleapis.com/compute/v1/projects/{peered_network['project_id']}/global/networks/{peered_network['network_name']}"
|
|
|
|
|
2022-04-12 05:13:05 -07:00
|
|
|
if 'limit' in peered_network:
|
|
|
|
limit_step2.append(
|
|
|
|
max(peered_network['limit'],
|
|
|
|
get_limit_ppg(peered_network_link, limit_dict)))
|
|
|
|
else:
|
2022-04-13 04:10:15 -07:00
|
|
|
print(
|
|
|
|
f"Ignoring projects/{peered_network['project_id']} for limits in peering group of project {project_id} as no limits are available."
|
|
|
|
+
|
|
|
|
"This can happen if you don't have permissions on the project, for example if the project is in another organization or a Google managed project"
|
|
|
|
)
|
2022-03-09 10:02:59 -08:00
|
|
|
|
|
|
|
# Calculates effective limit: Step 3: Find minimum from the list created by Step 2
|
2022-04-12 05:13:05 -07:00
|
|
|
limit_step3 = 0
|
|
|
|
if len(limit_step2) > 0:
|
|
|
|
limit_step3 = min(limit_step2)
|
2022-03-09 10:02:59 -08:00
|
|
|
|
|
|
|
# Calculates effective limit: Step 4: Find maximum from step 1 and step 3
|
|
|
|
effective_limit = max(limit_step1, limit_step3)
|
|
|
|
utilization = peering_group_usage / effective_limit
|
|
|
|
|
2022-03-17 12:08:58 -07:00
|
|
|
write_data_to_metric(project_id, peering_group_usage, usage_metric_name,
|
|
|
|
network_dict['network_name'])
|
|
|
|
write_data_to_metric(project_id, effective_limit, limit_metric_name,
|
|
|
|
network_dict['network_name'])
|
|
|
|
write_data_to_metric(project_id, utilization, utilization_metric_name,
|
|
|
|
network_dict['network_name'])
|
|
|
|
|
2022-03-28 09:47:11 -07:00
|
|
|
|
2022-03-08 09:36:02 -08:00
|
|
|
def get_networks(project_id):
|
2022-03-09 10:02:59 -08:00
|
|
|
'''
|
|
|
|
Returns a dictionary of all networks in a project.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
project_id (string): Project ID for the project containing the networks.
|
|
|
|
Returns:
|
|
|
|
network_dict (dictionary of string: string): Contains the project_id, network_name(s) and network_id(s)
|
|
|
|
'''
|
|
|
|
request = service.networks().list(project=project_id)
|
|
|
|
response = request.execute()
|
|
|
|
network_dict = []
|
|
|
|
if 'items' in response:
|
|
|
|
for network in response['items']:
|
2022-03-28 09:44:16 -07:00
|
|
|
network_name = network['name']
|
|
|
|
network_id = network['id']
|
2022-03-30 08:03:31 -07:00
|
|
|
self_link = network['selfLink']
|
2022-03-28 09:47:11 -07:00
|
|
|
d = {
|
|
|
|
'project_id': project_id,
|
|
|
|
'network_name': network_name,
|
2022-03-30 08:03:31 -07:00
|
|
|
'network_id': network_id,
|
|
|
|
'self_link': self_link
|
2022-03-28 09:47:11 -07:00
|
|
|
}
|
2022-03-09 10:02:59 -08:00
|
|
|
network_dict.append(d)
|
|
|
|
return network_dict
|
|
|
|
|
2022-04-12 00:58:12 -07:00
|
|
|
|
2022-03-30 08:03:31 -07:00
|
|
|
def get_dynamic_routes(metrics_dict, limits_dict):
|
2022-03-28 09:44:16 -07:00
|
|
|
'''
|
2022-03-30 08:03:31 -07:00
|
|
|
Writes all dynamic routes per VPC to custom metrics.
|
2022-03-28 09:44:16 -07:00
|
|
|
|
|
|
|
Parameters:
|
2022-03-30 08:03:31 -07:00
|
|
|
metrics_dict (dictionary of dictionary of string: string): metrics names and descriptions.
|
2022-03-30 08:46:01 -07:00
|
|
|
limits_dict (dictionary of string: int): key is network link (or 'default_value') and value is the limit for that network
|
2022-03-28 09:44:16 -07:00
|
|
|
Returns:
|
2022-04-12 00:58:12 -07:00
|
|
|
dynamic_routes_dict (dictionary of string: int): key is network link and value is the number of dynamic routes for that network
|
2022-03-30 08:03:31 -07:00
|
|
|
'''
|
|
|
|
routers_dict = get_routers()
|
2022-04-12 00:58:12 -07:00
|
|
|
dynamic_routes_dict = defaultdict(int)
|
2022-03-30 08:03:31 -07:00
|
|
|
|
|
|
|
for project_id in MONITORED_PROJECTS_LIST:
|
|
|
|
network_dict = get_networks(project_id)
|
|
|
|
|
|
|
|
for network in network_dict:
|
|
|
|
sum_routes = get_routes_for_network(network['self_link'], project_id,
|
|
|
|
routers_dict)
|
2022-04-12 00:58:12 -07:00
|
|
|
dynamic_routes_dict[network['self_link']] = sum_routes
|
2022-03-30 08:03:31 -07:00
|
|
|
|
|
|
|
if network['self_link'] in limits_dict:
|
|
|
|
limit = limits_dict[network['self_link']]
|
|
|
|
else:
|
|
|
|
if 'default_value' in limits_dict:
|
|
|
|
limit = limits_dict['default_value']
|
|
|
|
else:
|
|
|
|
print("Error: couldn't find limit for dynamic routes.")
|
|
|
|
break
|
|
|
|
|
|
|
|
utilization = sum_routes / limit
|
|
|
|
|
|
|
|
write_data_to_metric(
|
|
|
|
project_id, sum_routes, metrics_dict["metrics_per_network"]
|
|
|
|
["dynamic_routes_per_network"]["usage"]["name"],
|
|
|
|
network['network_name'])
|
|
|
|
write_data_to_metric(
|
|
|
|
project_id, limit, metrics_dict["metrics_per_network"]
|
|
|
|
["dynamic_routes_per_network"]["limit"]["name"],
|
|
|
|
network['network_name'])
|
|
|
|
write_data_to_metric(
|
|
|
|
project_id, utilization, metrics_dict["metrics_per_network"]
|
|
|
|
["dynamic_routes_per_network"]["utilization"]["name"],
|
|
|
|
network['network_name'])
|
|
|
|
|
|
|
|
print("Wrote metrics for dynamic routes for VPCs in project", project_id)
|
|
|
|
|
2022-04-12 00:58:12 -07:00
|
|
|
return dynamic_routes_dict
|
|
|
|
|
2022-03-30 08:03:31 -07:00
|
|
|
|
|
|
|
def get_routes_for_network(network_link, project_id, routers_dict):
|
|
|
|
'''
|
|
|
|
Returns a the number of dynamic routes for a given network
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
network_link (string): Network self link.
|
|
|
|
project_id (string): Project ID containing the network.
|
|
|
|
routers_dict (dictionary of string: list of string): Dictionary with key as network link and value as list of router links.
|
|
|
|
Returns:
|
|
|
|
sum_routes (int): Number of routes in that network.
|
|
|
|
'''
|
|
|
|
sum_routes = 0
|
|
|
|
|
|
|
|
if network_link in routers_dict:
|
|
|
|
for router_link in routers_dict[network_link]:
|
|
|
|
# Router link is using the following format:
|
|
|
|
# 'https://www.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION/routers/ROUTER_NAME'
|
|
|
|
start = router_link.find("/regions/") + len("/regions/")
|
|
|
|
end = router_link.find("/routers/")
|
|
|
|
router_region = router_link[start:end]
|
|
|
|
router_name = router_link.split('/routers/')[1]
|
|
|
|
routes = get_routes_for_router(project_id, router_region, router_name)
|
|
|
|
|
|
|
|
sum_routes += routes
|
|
|
|
|
|
|
|
return sum_routes
|
|
|
|
|
|
|
|
|
|
|
|
def get_routes_for_router(project_id, router_region, router_name):
|
2022-03-28 09:44:16 -07:00
|
|
|
'''
|
2022-03-30 08:03:31 -07:00
|
|
|
Returns the same of dynamic routes learned by a specific Cloud Router instance
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
project_id (string): Project ID for the project containing the Cloud Router.
|
|
|
|
router_region (string): GCP region for the Cloud Router.
|
|
|
|
router_name (string): Cloud Router name.
|
|
|
|
Returns:
|
|
|
|
sum_routes (int): Number of dynamic routes learned by the Cloud Router.
|
|
|
|
'''
|
|
|
|
request = service.routers().getRouterStatus(project=project_id,
|
|
|
|
region=router_region,
|
|
|
|
router=router_name)
|
2022-03-28 09:44:16 -07:00
|
|
|
response = request.execute()
|
2022-03-30 08:03:31 -07:00
|
|
|
|
|
|
|
sum_routes = 0
|
|
|
|
|
|
|
|
if 'result' in response:
|
2022-04-12 05:13:05 -07:00
|
|
|
if 'bgpPeerStatus' in response['result']:
|
|
|
|
for peer in response['result']['bgpPeerStatus']:
|
|
|
|
sum_routes += peer['numLearnedRoutes']
|
2022-03-30 08:03:31 -07:00
|
|
|
|
|
|
|
return sum_routes
|
|
|
|
|
|
|
|
|
|
|
|
def get_routers():
|
|
|
|
'''
|
|
|
|
Returns a dictionary of all Cloud Routers in the GCP organization.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
None
|
|
|
|
Returns:
|
|
|
|
routers_dict (dictionary of string: list of string): Key is the network link and value is a list of router links.
|
|
|
|
'''
|
|
|
|
client = asset_v1.AssetServiceClient()
|
|
|
|
|
|
|
|
read_mask = field_mask_pb2.FieldMask()
|
|
|
|
read_mask.FromJsonString('name,versionedResources')
|
|
|
|
|
|
|
|
routers_dict = {}
|
|
|
|
|
|
|
|
response = client.search_all_resources(
|
|
|
|
request={
|
|
|
|
"scope": f"organizations/{ORGANIZATION_ID}",
|
|
|
|
"asset_types": ["compute.googleapis.com/Router"],
|
|
|
|
"read_mask": read_mask,
|
|
|
|
})
|
|
|
|
for resource in response:
|
|
|
|
network_link = None
|
|
|
|
router_link = None
|
|
|
|
for versioned in resource.versioned_resources:
|
|
|
|
for field_name, field_value in versioned.resource.items():
|
|
|
|
if field_name == "network":
|
|
|
|
network_link = field_value
|
|
|
|
if field_name == "selfLink":
|
|
|
|
router_link = field_value
|
|
|
|
|
|
|
|
if network_link in routers_dict:
|
|
|
|
routers_dict[network_link].append(router_link)
|
|
|
|
else:
|
|
|
|
routers_dict[network_link] = [router_link]
|
|
|
|
|
|
|
|
return routers_dict
|
2022-03-17 12:08:58 -07:00
|
|
|
|
2022-03-28 09:47:11 -07:00
|
|
|
|
2022-03-08 09:36:02 -08:00
|
|
|
def gather_peering_data(project_id):
|
2022-03-09 10:02:59 -08:00
|
|
|
'''
|
|
|
|
Returns a dictionary of all peerings for all networks in a project.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
project_id (string): Project ID for the project containing the networks.
|
|
|
|
Returns:
|
|
|
|
network_list (dictionary of string: string): Contains the project_id, network_name(s) and network_id(s) of peered networks.
|
|
|
|
'''
|
|
|
|
request = service.networks().list(project=project_id)
|
|
|
|
response = request.execute()
|
|
|
|
|
|
|
|
network_list = []
|
|
|
|
if 'items' in response:
|
|
|
|
for network in response['items']:
|
2022-03-17 12:08:58 -07:00
|
|
|
net = {
|
|
|
|
'project_id': project_id,
|
|
|
|
'network_name': network['name'],
|
|
|
|
'network_id': network['id'],
|
|
|
|
'peerings': []
|
|
|
|
}
|
2022-03-09 10:02:59 -08:00
|
|
|
if 'peerings' in network:
|
|
|
|
STATE = network['peerings'][0]['state']
|
|
|
|
if STATE == "ACTIVE":
|
2022-03-17 12:08:58 -07:00
|
|
|
for peered_network in network[
|
|
|
|
'peerings']: # "projects/{project_name}/global/networks/{network_name}"
|
|
|
|
start = peered_network['network'].find("projects/") + len(
|
|
|
|
'projects/')
|
2022-03-09 10:02:59 -08:00
|
|
|
end = peered_network['network'].find("/global")
|
|
|
|
peered_project = peered_network['network'][start:end]
|
2022-03-17 12:08:58 -07:00
|
|
|
peered_network_name = peered_network['network'].split(
|
|
|
|
"networks/")[1]
|
|
|
|
peered_net = {
|
|
|
|
'project_id':
|
|
|
|
peered_project,
|
|
|
|
'network_name':
|
|
|
|
peered_network_name,
|
|
|
|
'network_id':
|
|
|
|
get_network_id(peered_project, peered_network_name)
|
|
|
|
}
|
2022-03-09 10:02:59 -08:00
|
|
|
net["peerings"].append(peered_net)
|
|
|
|
network_list.append(net)
|
|
|
|
return network_list
|
2022-03-08 09:36:02 -08:00
|
|
|
|
2022-03-17 12:08:58 -07:00
|
|
|
|
2022-03-08 09:36:02 -08:00
|
|
|
def get_network_id(project_id, network_name):
|
2022-03-09 10:02:59 -08:00
|
|
|
'''
|
|
|
|
Returns the network_id for a specific project / network name.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
project_id (string): Project ID for the project containing the networks.
|
|
|
|
network_name (string): Name of the network
|
|
|
|
Returns:
|
|
|
|
network_id (int): Network ID.
|
|
|
|
'''
|
|
|
|
request = service.networks().list(project=project_id)
|
2022-04-12 05:13:05 -07:00
|
|
|
try:
|
|
|
|
response = request.execute()
|
|
|
|
except errors.HttpError as err:
|
|
|
|
# TODO: log proper warning
|
|
|
|
if err.resp.status == http.HTTPStatus.FORBIDDEN:
|
2022-04-13 04:10:15 -07:00
|
|
|
print(
|
|
|
|
f"Warning: error reading networks for {project_id}. " +
|
|
|
|
f"This can happen if you don't have permissions on the project, for example if the project is in another organization or a Google managed project"
|
|
|
|
)
|
2022-04-12 05:13:05 -07:00
|
|
|
else:
|
|
|
|
print(f"Warning: error reading networks for {project_id}: {err}")
|
|
|
|
return 0
|
|
|
|
|
2022-03-09 10:02:59 -08:00
|
|
|
network_id = 0
|
|
|
|
|
|
|
|
if 'items' in response:
|
|
|
|
for network in response['items']:
|
|
|
|
if network['name'] == network_name:
|
|
|
|
network_id = network['id']
|
|
|
|
break
|
2022-03-17 12:08:58 -07:00
|
|
|
|
2022-03-09 10:02:59 -08:00
|
|
|
if network_id == 0:
|
|
|
|
print(f"Error: network_id not found for {network_name} in {project_id}")
|
2022-03-17 12:08:58 -07:00
|
|
|
|
2022-03-09 10:02:59 -08:00
|
|
|
return network_id
|
|
|
|
|
2022-03-28 09:47:11 -07:00
|
|
|
|
2022-03-09 10:02:59 -08:00
|
|
|
def get_quota_current_limit(project_link, metric_name):
|
|
|
|
'''
|
|
|
|
Retrieves limit for a specific metric.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
project_link (string): Project link.
|
|
|
|
metric_name (string): Name of the metric.
|
|
|
|
Returns:
|
|
|
|
results_list (list of string): Current limit.
|
|
|
|
'''
|
2022-03-17 09:04:31 -07:00
|
|
|
client, interval = create_client()
|
|
|
|
|
2022-04-12 05:13:05 -07:00
|
|
|
try:
|
|
|
|
results = client.list_time_series(
|
|
|
|
request={
|
|
|
|
"name": project_link,
|
|
|
|
"filter": f'metric.type = "{metric_name}"',
|
|
|
|
"interval": interval,
|
|
|
|
"view": monitoring_v3.ListTimeSeriesRequest.TimeSeriesView.FULL
|
|
|
|
})
|
|
|
|
results_list = list(results)
|
|
|
|
return results_list
|
|
|
|
except exceptions.PermissionDenied as err:
|
2022-04-13 04:10:15 -07:00
|
|
|
print(
|
|
|
|
f"Warning: error reading quotas for {project_link}. " +
|
|
|
|
f"This can happen if you don't have permissions on the project, for example if the project is in another organization or a Google managed project"
|
|
|
|
)
|
2022-04-12 05:13:05 -07:00
|
|
|
return None
|
2022-03-17 12:08:58 -07:00
|
|
|
|
2022-04-13 04:10:15 -07:00
|
|
|
|
2022-03-08 09:36:02 -08:00
|
|
|
def customize_quota_view(quota_results):
|
2022-03-09 10:02:59 -08:00
|
|
|
'''
|
|
|
|
Customize the quota output for an easier parsable output.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
quota_results (string): Input from get_quota_current_usage or get_quota_current_limit. Contains the Current usage or limit for all networks in that project.
|
|
|
|
Returns:
|
|
|
|
quotaViewList (list of dictionaries of string: string): Current quota usage or limit.
|
|
|
|
'''
|
|
|
|
quotaViewList = []
|
|
|
|
for result in quota_results:
|
|
|
|
quotaViewJson = {}
|
|
|
|
quotaViewJson.update(dict(result.resource.labels))
|
|
|
|
quotaViewJson.update(dict(result.metric.labels))
|
|
|
|
for val in result.points:
|
|
|
|
quotaViewJson.update({'value': val.value.int64_value})
|
|
|
|
quotaViewList.append(quotaViewJson)
|
|
|
|
return quotaViewList
|
|
|
|
|
2022-03-17 12:08:58 -07:00
|
|
|
|
2022-03-28 09:44:16 -07:00
|
|
|
def set_limits(network_dict, quota_limit, limit_dict):
|
2022-03-09 10:02:59 -08:00
|
|
|
'''
|
2022-03-24 02:07:58 -07:00
|
|
|
Updates the network dictionary with quota limit values.
|
2022-03-09 10:02:59 -08:00
|
|
|
|
|
|
|
Parameters:
|
|
|
|
network_dict (dictionary of string: string): Contains network information.
|
|
|
|
quota_limit (list of dictionaries of string: string): Current quota limit.
|
2022-03-28 09:44:16 -07:00
|
|
|
limit_dict (dictionary of string:int): Dictionary with the network link as key and the limit as value
|
2022-03-09 10:02:59 -08:00
|
|
|
Returns:
|
|
|
|
None
|
|
|
|
'''
|
2022-03-28 09:44:16 -07:00
|
|
|
|
|
|
|
network_dict['limit'] = None
|
2022-03-28 09:47:11 -07:00
|
|
|
|
2022-03-09 10:02:59 -08:00
|
|
|
if quota_limit:
|
|
|
|
for net in quota_limit:
|
2022-03-28 09:47:11 -07:00
|
|
|
if net['network_id'] == network_dict['network_id']:
|
2022-03-28 09:44:16 -07:00
|
|
|
network_dict['limit'] = net['value']
|
|
|
|
return
|
2022-03-17 12:08:58 -07:00
|
|
|
|
2022-03-28 09:44:16 -07:00
|
|
|
network_link = f"https://www.googleapis.com/compute/v1/projects/{network_dict['project_id']}/global/networks/{network_dict['network_name']}"
|
|
|
|
|
|
|
|
if network_link in limit_dict:
|
|
|
|
network_dict['limit'] = limit_dict[network_link]
|
|
|
|
else:
|
|
|
|
if 'default_value' in limit_dict:
|
|
|
|
network_dict['limit'] = limit_dict['default_value']
|
|
|
|
else:
|
|
|
|
print(f"Error: Couldn't find limit for {network_link}")
|
|
|
|
network_dict['limit'] = 0
|
2022-03-08 09:36:02 -08:00
|
|
|
|
2022-03-28 09:47:11 -07:00
|
|
|
|
2022-03-28 09:44:16 -07:00
|
|
|
def get_limit_network(network_dict, network_link, quota_limit, limit_dict):
|
2022-03-09 10:02:59 -08:00
|
|
|
'''
|
2022-03-28 09:44:16 -07:00
|
|
|
Returns limit for a specific network and metric, using the GCP quota metrics or the values in the yaml file if not found.
|
2022-03-09 10:02:59 -08:00
|
|
|
|
|
|
|
Parameters:
|
|
|
|
network_dict (dictionary of string: string): Contains network information.
|
2022-03-28 09:44:16 -07:00
|
|
|
network_link (string): Contains network link
|
2022-03-09 10:02:59 -08:00
|
|
|
quota_limit (list of dictionaries of string: string): Current quota limit for all networks in that project.
|
2022-03-28 09:44:16 -07:00
|
|
|
limit_dict (dictionary of string:int): Dictionary with the network link as key and the limit as value
|
2022-03-09 10:02:59 -08:00
|
|
|
Returns:
|
2022-03-24 02:07:58 -07:00
|
|
|
limit (int): Current limit for that network.
|
2022-03-09 10:02:59 -08:00
|
|
|
'''
|
|
|
|
if quota_limit:
|
|
|
|
for net in quota_limit:
|
2022-03-28 09:47:11 -07:00
|
|
|
if net['network_id'] == network_dict['network_id']:
|
2022-03-28 09:44:16 -07:00
|
|
|
return net['value']
|
2022-03-28 09:47:11 -07:00
|
|
|
|
2022-03-28 09:44:16 -07:00
|
|
|
if network_link in limit_dict:
|
|
|
|
return limit_dict[network_link]
|
|
|
|
else:
|
|
|
|
if 'default_value' in limit_dict:
|
|
|
|
return limit_dict['default_value']
|
2022-03-09 10:02:59 -08:00
|
|
|
else:
|
2022-03-28 09:44:16 -07:00
|
|
|
print(f"Error: Couldn't find limit for {network_link}")
|
2022-03-08 09:36:02 -08:00
|
|
|
|
2022-03-28 09:44:16 -07:00
|
|
|
return 0
|
2022-03-08 09:36:02 -08:00
|
|
|
|
2022-03-17 12:08:58 -07:00
|
|
|
|
|
|
|
def write_data_to_metric(monitored_project_id, value, metric_name,
|
|
|
|
network_name):
|
2022-03-09 10:02:59 -08:00
|
|
|
'''
|
|
|
|
Writes data to Cloud Monitoring custom metrics.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
monitored_project_id: ID of the project where the resource lives (will be added as a label)
|
|
|
|
value (int): Value for the data point of the metric.
|
|
|
|
metric_name (string): Name of the metric
|
|
|
|
network_name (string): Name of the network (will be added as a label)
|
|
|
|
Returns:
|
|
|
|
usage (int): Current usage for that network.
|
|
|
|
limit (int): Current usage for that network.
|
|
|
|
'''
|
2022-03-17 09:04:31 -07:00
|
|
|
client = monitoring_v3.MetricServiceClient()
|
|
|
|
|
2022-03-09 10:02:59 -08:00
|
|
|
series = monitoring_v3.TimeSeries()
|
|
|
|
series.metric.type = f"custom.googleapis.com/{metric_name}"
|
2022-03-17 12:08:58 -07:00
|
|
|
series.resource.type = "global"
|
2022-03-09 10:02:59 -08:00
|
|
|
series.metric.labels["network_name"] = network_name
|
|
|
|
series.metric.labels["project"] = monitored_project_id
|
|
|
|
|
|
|
|
now = time.time()
|
|
|
|
seconds = int(now)
|
2022-03-17 12:08:58 -07:00
|
|
|
nanos = int((now - seconds) * 10**9)
|
|
|
|
interval = monitoring_v3.TimeInterval(
|
|
|
|
{"end_time": {
|
|
|
|
"seconds": seconds,
|
|
|
|
"nanos": nanos
|
|
|
|
}})
|
|
|
|
point = monitoring_v3.Point({
|
|
|
|
"interval": interval,
|
|
|
|
"value": {
|
|
|
|
"double_value": value
|
|
|
|
}
|
|
|
|
})
|
2022-03-09 10:02:59 -08:00
|
|
|
series.points = [point]
|
|
|
|
|
2022-03-28 09:44:16 -07:00
|
|
|
# TODO: sometimes this cashes with 'DeadlineExceeded: 504 Deadline expired before operation could complete' error
|
|
|
|
# Implement exponential backoff retries?
|
|
|
|
try:
|
2022-03-28 09:47:11 -07:00
|
|
|
client.create_time_series(name=MONITORING_PROJECT_LINK,
|
|
|
|
time_series=[series])
|
2022-03-28 09:44:16 -07:00
|
|
|
except Exception as e:
|
2022-03-28 09:47:11 -07:00
|
|
|
print(e)
|