Skip to content

Commit

Permalink
reverted stuff pushed by mistake
Browse files Browse the repository at this point in the history
  • Loading branch information
maunope committed Sep 30, 2022
1 parent 10e1a5e commit ec54165
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 238 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,15 @@ def monitoring_interval():

config = {
# Organization ID containing the projects to be monitored
"organization": #os.environ.get("ORGANIZATION_ID"),
'34855741773',
"organization":
os.environ.get("ORGANIZATION_ID"),
# list of projects from which function will get quotas information
"monitored_projects": #os.environ.get("MONITORED_PROJECTS_LIST").split(","),
[
"mnoseda-prod-net-landing-0", "mnoseda-prod-net-spoke-0",
"mnoseda-dev-net-spoke-0"
],
"monitoring_project": #os.environ.get('MONITORING_PROJECT_ID'),
"monitoring-tlc",
"monitoring_project_link": #f"projects/{os.environ.get('MONITORING_PROJECT_ID')}",
f"projects/monitoring-tlc",
"monitored_projects":
os.environ.get("MONITORED_PROJECTS_LIST").split(","),
"monitoring_project_link":
os.environ.get('MONITORING_PROJECT_ID'),
"monitoring_project_link":
f"projects/{os.environ.get('MONITORING_PROJECT_ID')}",
"monitoring_interval":
monitoring_interval(),
"limit_names": {
Expand Down Expand Up @@ -146,9 +143,6 @@ def main(event, context):

metrics_dict, limits_dict = metrics.create_metrics(
config["monitoring_project_link"])
project_quotas_dict = limits.get_quota_project_limit(config)

firewalls_dict = vpc_firewalls.get_firewalls_dict(config)

# IP utilization subnet level metrics
subnets.get_subnets(config, metrics_dict)
Expand All @@ -159,10 +153,6 @@ def main(event, context):
l7_forwarding_rules_dict = ilb_fwrules.get_forwarding_rules_dict(config, "L7")
subnet_range_dict = networks.get_subnet_ranges_dict(config)

# Per Project metrics
vpc_firewalls.get_firewalls_data(config, metrics_dict, project_quotas_dict,
firewalls_dict)

# Per Network metrics
instances.get_gce_instances_data(config, metrics_dict, gce_instance_dict,
limits_dict['number_of_instances_limit'])
Expand Down Expand Up @@ -207,4 +197,4 @@ def main(event, context):


if __name__ == "__main__":
main(None, None)
main(None, None)
Original file line number Diff line number Diff line change
Expand Up @@ -159,16 +159,4 @@ metrics_per_peering_group:
default_value: 300
utilization:
name: dynamic_routes_per_peering_group_utilization
description: Number of Dynamic routes per peering group - utilization.
metrics_per_project:
firewalls:
usage:
name: firewalls_per_project_vpc_usage
description: Number of VPC firewall rules in a project - usage.
limit:
#no default limit as we can assume quotas can be always read from gcloud API
name: firewalls_per_project_limit
description: Number of VPC firewall rules in a project - limit.
utilization:
name: firewalls_per_project_utilization
description: Number of VPC firewall rules in a project - utilization.
description: Number of Dynamic routes per peering group - utilization.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
# limitations under the License.
#

from http.cookiejar import LWPCookieJar
from urllib import response
from google.api_core import exceptions
from google.cloud import monitoring_v3
from . import metrics
Expand Down Expand Up @@ -73,59 +71,6 @@ def set_limits(network_dict, quota_limit, limit_dict):
network_dict['limit'] = 0


def get_quotas_dict(quotas_list):
'''
Creates a dictionary of quotas from a list, with lowe case keys
Parameters:
quotas_array (array): array of quotas
Returns:
quotas_dict (dict): dictionary of quotas
'''
quota_keys=[q['metric'] for q in quotas_list]
quotas_dict=dict()
i=0
for key in quota_keys:
if ("metric" in quotas_list[i] ):
del(quotas_list[i]["metric"])
quotas_dict[key.lower()]=quotas_list[i]
i+=1
return quotas_dict



def get_quota_project_limit(config,regions=["global"]):
'''
Retrieves limit for a specific project quota
Parameters:
project_link (string): Project link.
Returns:
quotas (dict): quotas for all selected regions, default 'global'
'''
try:
request={}
quotas=dict()
for project in config["monitored_projects"]:
quotas[project]=dict()
if regions != ["global"]:
for region in regions:
request=config["clients"]["discovery_client"].compute.regions().get(region=region,project=project)
response=request.execute()
quotas[project][region]=get_quotas_dict(response['quotas'])
else:
region="global"
request=config["clients"]["discovery_client"].projects().get(project=project,fields="quotas")
response=request.execute()
quotas[project][region]=get_quotas_dict(response['quotas'])

return quotas
except exceptions.PermissionDenied as err:
print(
f"Warning: error reading quotas for {project}. " +
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"
)
return None

#def get_project_quota_current_limit(config,project_link,metric_name,)
def get_quota_current_limit(config, project_link, metric_name):
'''
Retrieves limit for a specific metric.
Expand Down Expand Up @@ -223,4 +168,4 @@ def count_effective_limit(config, project_id, network_dict, usage_metric_name,
limit_metric_name, network_dict['network_name'])
metrics.write_data_to_metric(config, project_id, utilization,
utilization_metric_name,
network_dict['network_name'])
network_dict['network_name'])
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ def create_metrics(monitoring_project):
existing_metrics.append(desc.type)
limits_dict = {}

with open(
"/Users/mnoseda/Fabric/cloud-foundation-fabric/blueprints/cloud-operations/network-dashboard/cloud-function/metrics.yaml",
'r') as stream: #f
with open("metrics.yaml", 'r') as stream:
try:
metrics_dict = yaml.safe_load(stream)

Expand All @@ -54,9 +52,8 @@ def create_metrics(monitoring_project):
# Subnet level metrics have a different limit: the subnet IP range size
if sub_metric_key == "limit" and metric_name != "ip_usage_per_subnet":
limits_dict_for_metric = {}
if "values" in sub_metric:
for network_link, limit_value in sub_metric["values"].items():
limits_dict_for_metric[network_link] = limit_value
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

return metrics_dict, limits_dict
Expand Down Expand Up @@ -87,7 +84,7 @@ def create_metric(metric_name, description, monitoring_project):


def write_data_to_metric(config, monitored_project_id, value, metric_name,
network_name=None, subnet_id=None):
network_name, subnet_id=None):
'''
Writes data to Cloud Monitoring custom metrics.
Parameters:
Expand All @@ -106,8 +103,7 @@ def write_data_to_metric(config, monitored_project_id, value, metric_name,
series = monitoring_v3.TimeSeries()
series.metric.type = f"custom.googleapis.com/{metric_name}"
series.resource.type = "global"
if network_name:
series.metric.labels["network_name"] = network_name
series.metric.labels["network_name"] = network_name
series.metric.labels["project"] = monitored_project_id
if subnet_id:
series.metric.labels["subnet_id"] = subnet_id
Expand Down

This file was deleted.

0 comments on commit ec54165

Please sign in to comment.