forked from chrisbeckett/d9-azure-sizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd9-sizer.py
132 lines (120 loc) · 7.11 KB
/
d9-sizer.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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# This script logs into Azure AD and iterates through subscriptions to onboard them into CloudGuard
# Feedback to chrisbe@checkpoint.com or open an issue on https://github.com/chrisbeckett/d9-azure-sizer/issues
# To run the script, you will need to set environment variables for AZURE_CLIENT_ID, AZURE_CLIENT_SECRET and AZURE_TENANT_ID
# Import required libraries
import os
import sys
import logging
from azure.identity import ClientSecretCredential
#from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.sql import SqlManagementClient
from azure.mgmt.subscription import SubscriptionClient
from azure.mgmt.subscription.operations import SubscriptionsOperations
from msrestazure.azure_exceptions import CloudError
from azure.mgmt.compute import ComputeManagementClient
from colorama import Fore,init
from azure.mgmt.web import WebSiteManagementClient
from azure.core.exceptions import HttpResponseError
init()
# Verify the environment variables have been set
def verify_env_variables():
try:
if 'AZURE_TENANT_ID' in os.environ:
pass
else:
print("ERROR : The Azure AD tenant ID has not been defined in environment variables")
sys.exit(0)
if 'AZURE_CLIENT_ID' in os.environ:
pass
else:
print("ERROR : The Azure AD application ID has not been defined in environment variables")
sys.exit(0)
if 'AZURE_CLIENT_SECRET' in os.environ:
pass
else:
print("ERROR : The Azure AD application secret key has not been defined in environment variables")
sys.exit(0)
except:
sys.exit(0)
verify_env_variables()
# Set Azure AD credentials from the environment variables
credentials = ClientSecretCredential(
client_id=os.environ['AZURE_CLIENT_ID'],
client_secret=os.environ['AZURE_CLIENT_SECRET'],
tenant_id=os.environ['AZURE_TENANT_ID']
)
# Read in required environment variables
az_tenant=os.environ['AZURE_TENANT_ID']
az_appid=os.environ['AZURE_CLIENT_ID']
az_appkey=os.environ['AZURE_CLIENT_SECRET']
# INSTANTIATE SDK CLIENT INSTANCES
sub_client = SubscriptionClient(credentials)
# Connect to each subscription in turn and list all VMs, Functions and Azure SQL servers, collecting CloudGuard billable asset counts
def run_sizer():
total_number_sql_servers = 0
total_number_vms = 0
total_number_functions = 0
try:
for sub in sub_client.subscriptions.list():
print("\n"),
print(Fore.CYAN + "================================================================================================")
print(Fore.WHITE + 'Subscription found:', sub.subscription_id, sub.display_name)
print(Fore.CYAN + "================================================================================================")
resource_client = ResourceManagementClient(credentials, sub.subscription_id)
resource_client.providers.register('Microsoft.Sql')
sql_client = SqlManagementClient(credentials, sub.subscription_id)
compute_client = ComputeManagementClient(credentials, sub.subscription_id)
web_client = WebSiteManagementClient(credentials, sub.subscription_id)
sub_total_number_sql_servers = 0
sub_total_number_vms = 0
sub_total_number_functions = 0
print(Fore.WHITE + "================================================================================================")
print(Fore.YELLOW + "{:20} {:20} {:20}".format("SQL Server Name", "||","Azure Region",))
print(Fore.WHITE + "================================================================================================")
for item in sql_client.servers.list():
print("{:20} {:20} {:20}".format(item.name,"||",item.location))
sub_total_number_sql_servers = sub_total_number_sql_servers + 1
print("\n")
print(Fore.WHITE + "================================================================================================")
print(Fore.YELLOW + "{:20} {:20} {:20} {:20} {:20}".format("Virtual machine name","||","Instance Size","||","Azure Region"))
print(Fore.WHITE + "================================================================================================")
for vm in compute_client.virtual_machines.list_all():
print("{:20} {:20} {:20} {:20} {:20}".format(vm.name,"||",vm.hardware_profile.vm_size,"||",vm.location))
if vm.hardware_profile.vm_size not in ("Standard_A0","Standard_D0","Basic_A0","Basic_D0"):
sub_total_number_vms = sub_total_number_vms + 1
print("\n")
print(Fore.WHITE + "================================================================================================")
print(Fore.YELLOW + "{:20} {:20} {:20}".format("Function name","||","Azure Region"))
print(Fore.WHITE + "================================================================================================")
for resource_group in resource_client.resource_groups.list():
rg_name = resource_group.name
apps_list = web_client.web_apps.list_by_resource_group(rg_name)
for a in apps_list:
appkind = a.kind
if "functionapp" in appkind:
sub_total_number_functions += 1
print("{:20} {:20} {:20}".format(a.name,"||", a.location))
total_number_sql_servers = total_number_sql_servers + sub_total_number_sql_servers
total_number_vms = total_number_vms + sub_total_number_vms
total_number_functions = total_number_functions + sub_total_number_functions
print("\n")
print("Total number of billable SQL Servers in subscription", sub.display_name,":",sub_total_number_sql_servers)
print("Total number of billable virtual machines in subscription", sub.display_name,":",sub_total_number_vms)
print("Total number of billable functions in subscription", sub.display_name, ":",sub_total_number_functions)
except HttpResponseError as e:
print(e)
print("\n")
print(Fore.GREEN + "================================================================================================")
print("CloudGuard Azure Sizer - Report Summary")
print("================================================================================================")
print("\n")
print("Total number of billable SQL Servers in Azure AD tenant", az_tenant,":",total_number_sql_servers)
print("Total number of billable virtual machines in Azure AD tenant", az_tenant,":",total_number_vms)
print("Total number of billable functions in Azure AD tenant", az_tenant,":",total_number_functions )
print("\n")
total_number_functions_licenses = total_number_functions //6
print("Total number of CloudGuard billable assets licenses is :", total_number_sql_servers + total_number_vms + total_number_functions_licenses)
print
if __name__ == "__main__":
run_sizer()