-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdigitalocean-units.py
62 lines (51 loc) · 2.47 KB
/
digitalocean-units.py
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
import argparse
import json
import subprocess
# Usage python3 ./digitalocean-units.py --contexts <context_1> <context_2> <context_3> <context_4>
parser = argparse.ArgumentParser(prog="SentinelOne CNS Digital Ocean Unit Audit")
parser.add_argument("--contexts", help="Digital Ocean CLI Contexts separated by space", nargs='+', default=[], required=False)
args = parser.parse_args()
CONTEXTS = args.contexts
class SentinelOneCNSDigitalOceanUnitAudit:
def __init__(self, context):
self.file_path = "digitalocean-{context}-units.csv".format(context=context) if context else 'digitalocean-units.csv'
self.context_flag = "--context {context}".format(context=context) if context else ''
self.total_resource_count = 0
self.total_workload_count = 0
with open(self.file_path, 'w') as f:
f.write("Resource Type, Unit Counted, Workloads\n")
def add_result(self, k, v, w=""):
with open(self.file_path, 'a') as f:
f.write('{k}, {v}, {w}\n'.format(k=k,v=v,w=w))
def count_all(self):
self.count("Digital Ocean Droplets", self.count_droplets, workload_multiplier=1)
self.add_result('TOTAL', self.total_resource_count, round(self.total_workload_count))
print("[Info] results stored at", self.file_path)
def count(self, svcName, svcCb, workload_multiplier):
try:
count = svcCb()
if count:
workloads = count * workload_multiplier
self.total_resource_count += count
self.total_workload_count += workloads
self.add_result(svcName, count, workloads)
print('[Info] Fetched ', svcName)
except subprocess.CalledProcessError as e:
print('[Error] Error getting ', svcName)
print("[Error] [Command]", e.cmd)
print("[Error] [Command-Output]", e.output)
self.add_result(svcName, "Error")
except json.decoder.JSONDecodeError as e:
print("[Error] parsing data from Cloud Provider\n", e)
self.add_result(svcName, "JSON Error")
def count_droplets(self):
output = subprocess.check_output(
f"doctl compute droplet list --output json {self.context_flag}",
text=True, shell=True
)
j = json.loads(output)
return len(j)
if __name__ == '__main__':
contexts = CONTEXTS if len(CONTEXTS) > 0 else [None]
for context in contexts:
SentinelOneCNSDigitalOceanUnitAudit(context).count_all()