Skip to content

Commit

Permalink
Add dump stat for GCE
Browse files Browse the repository at this point in the history
  • Loading branch information
asmorodskyi committed Jul 23, 2024
1 parent bda4e40 commit b04b358
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
32 changes: 32 additions & 0 deletions ocw/lib/dump_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from webui.PCWConfig import PCWConfig
from ocw.lib.azure import Azure
from ocw.lib.ec2 import EC2
from ocw.lib.gce import GCE
from ocw.enums import ProviderChoice
from ocw.lib.influx import Influx

Expand Down Expand Up @@ -71,6 +72,37 @@ def dump_state():
namespace,
EC2(namespace).count_all_vpc
)
if ProviderChoice.GCE in providers:
Influx().dump_resource(
ProviderChoice.GCE.value,
Influx.VMS_QUANTITY,
namespace,
GCE(namespace).count_all_instances
)
Influx().dump_resource(
ProviderChoice.GCE.value,
Influx.IMAGES_QUANTITY,
namespace,
GCE(namespace).count_all_images
)
Influx().dump_resource(
ProviderChoice.GCE.value,
Influx.DISK_QUANTITY,
namespace,
GCE(namespace).count_all_disks
)
Influx().dump_resource(
ProviderChoice.GCE.value,
Influx.BLOB_QUANTITY,
namespace,
GCE(namespace).count_all_blobs
)
Influx().dump_resource(
ProviderChoice.GCE.value,
Influx.NETWORK_QUANTITY,
namespace,
GCE(namespace).count_all_networks
)
except Exception:
logger.exception(
"[%s] Dump state failed!: \n %s", namespace, traceback.format_exc()
Expand Down
19 changes: 19 additions & 0 deletions ocw/lib/gce.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,3 +264,22 @@ def cleanup_networks(self) -> None:
self._delete_resource(
self.compute_client().networks, network["name"], project=self.project, network=network["name"]
)

def count_all_instances(self) -> int:
return len(self.list_all_instances())

def count_all_images(self) -> int:
return len(self._paginated(self.compute_client().images, project=self.project))

def count_all_disks(self) -> int:
all_disks = 0
for region in self.list_regions():
for zone in self.list_zones(region):
all_disks += len(self._paginated(self.compute_client().disks, project=self.project, zone=zone))
return all_disks

def count_all_blobs(self) -> int:
return len(self._paginated(self.storage_client().objects, bucket=self.__bucket))

def count_all_networks(self) -> int:
return len(self._paginated(self.compute_client().networks, project=self.project))
2 changes: 2 additions & 0 deletions ocw/lib/influx.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ class Influx:
VMS_QUANTITY: str = "vms_quantity"
IMAGES_QUANTITY: str = "images_quantity"
DISK_QUANTITY: str = "disk_quantity"
BLOB_QUANTITY: str = "blob_quantity"
VOLUMES_QUANTITY: str = "volumes_quanity"
IMAGE_VERSION_QUANTITY: str = "img_version_quantity"
VPC_QUANTITY: str = "vpc_quantity"
NETWORK_QUANTITY: str = "network_quantity"
NAMESPACE_TAG: str = "namespace"

def __init__(self) -> None:
Expand Down

0 comments on commit b04b358

Please sign in to comment.