-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoci-list-bucketsize.py
More file actions
95 lines (73 loc) · 3.44 KB
/
oci-list-bucketsize.py
File metadata and controls
95 lines (73 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
from oci.config import validate_config
from oci.config import from_file
from oci.identity import IdentityClient
from oci.core import ComputeClient
from oci.core import BlockstorageClient
from oci.object_storage import ObjectStorageClient
import argparse
### HELPERS ###
def sizeof_fmt(num, suffix="B"):
for unit in ["", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"]:
if abs(num) < 1024.0:
return f"{num:3.1f}{unit}{suffix}"
num /= 1024.0
return f"{num:.1f}Yi{suffix}"
### CONSTANT ###
TB = 1024 * 1024 * 1024 * 1024
### MAIN CODE ###
# Parse Args
# Parse Arguments
parser = argparse.ArgumentParser()
parser.add_argument("-v", "--verbose", help="increase output verbosity", action="store_true")
parser.add_argument("-pr", "--profile", help="Config Profile, named", default="DEFAULT")
parser.add_argument("-t", "--threshold", help="Threshold in TB to show greater than, number", default=10, type=int)
args = parser.parse_args()
verbose = args.verbose
profile = args.profile
show_threshold = args.threshold
# Create / Validate Config
config = from_file(profile_name=profile)
validate_config(config)
tenancy_id = config["tenancy"]
identity = IdentityClient(config)
# Overall counter
total_bytes_tenancy = 0
try:
regions = identity.list_region_subscriptions(tenancy_id=tenancy_id).data
for region in regions:
print(f"Region Name {region.region_name}")
# Change Region and re-create Clients
config["region"] = region.region_name
identity = IdentityClient(config)
compute = ComputeClient(config)
block_storage = BlockstorageClient(config)
object_storage = ObjectStorageClient(config)
os_ns = object_storage.get_namespace().data
instances = []
boot_volumes = []
block_volumes = []
#print(f"AD: {ad.name}", flush=True)
# Total Counter
total_bytes = 0
compartment_id = config["tenancy"]
compartments = identity.list_compartments (tenancy_id,compartment_id_in_subtree=True).data
for compartment in compartments:
if verbose:
print(f" Compartment: {compartment.name}", flush=True)
buckets = object_storage.list_buckets(compartment_id=compartment.id,namespace_name=os_ns, limit=1000).data
for bucket in buckets:
bucket_details = object_storage.get_bucket(namespace_name=os_ns, bucket_name=bucket.name, fields=["approximateSize","approximateCount"]).data
# Add to counter
total_bytes += bucket_details.approximate_size
if verbose or bucket_details.approximate_size > show_threshold * TB:
#print(f' {"***" if bucket_details.approximate_size > show_threshold * TB else ""}Bucket Name: {bucket.name} Size: {sizeof_fmt(bucket_details.approximate_size)} Objects: {bucket_details.object_count}')
#else:
# if bucket_details.approximate_size > 10 * TB:
print(f' *** {compartment.name} / Bucket Name: {bucket.name} Size: {sizeof_fmt(bucket_details.approximate_size)} Objects: {sizeof_fmt(bucket_details.approximate_count)}')
# Summary Region
print(f"Storage Total (Region): {sizeof_fmt(total_bytes)}")
total_bytes_tenancy += total_bytes
# Summary Tenancy
print(f"Storage Total (Tenancy): {sizeof_fmt(total_bytes_tenancy)}")
except Exception as e:
raise RuntimeError("\nError - " + str(e))