This repository has been archived by the owner on Nov 9, 2020. It is now read-only.
/
nsx_usage.py
226 lines (185 loc) · 11.1 KB
/
nsx_usage.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/usr/bin/env python
# coding=utf-8
#
# Copyright © 2015-2016 VMware, Inc. All Rights Reserved.
#
# Licensed under the X11 (MIT) (the “License”) set forth below;
#
# you may not use this file except in compliance with the License. Unless required by applicable law or agreed to in
# writing, software distributed under the License is distributed on an “AS IS” BASIS, without warranties or conditions
# of any kind, EITHER EXPRESS OR IMPLIED. See the License for the specific language governing permissions and
# limitations under the License. Permission is hereby granted, free of charge, to any person obtaining a copy of this
# software and associated documentation files (the "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
# Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of
# the Software.
#
# "THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.”
__author__ = 'yfauser'
import argparse
import ConfigParser
from tabulate import tabulate
from nsxramlclient.client import NsxClient
from libutils import connect_to_vc
from libutils import VIM_TYPES
from libutils import get_all_objs
from pkg_resources import resource_filename
def host_prep_state(session):
resource_status = session.read('statusResourceType', uri_parameters={'resourceType': 'ClusterComputeResource'})
enabled_clusters = session.normalize_list_return(resource_status['body']['resourceStatuses']['resourceStatus'])
clusters = []
for cluster in enabled_clusters:
feature_status = cluster['nwFabricFeatureStatus']
dfw_enabled = [feature['enabled'] for feature in feature_status
if feature['featureId'] == 'com.vmware.vshield.firewall'][0]
clusters.append((cluster['resource']['objectId'], cluster['resource']['name'], dfw_enabled))
hosts = []
for cluster_moid, cluster_name, dfw_enabled in clusters:
hosts_status = session.read('childStatus', uri_parameters={'parentResourceID': cluster_moid})
enabled_hosts = session.normalize_list_return(hosts_status['body']['resourceStatuses']['resourceStatus'])
hosts.extend([(host['resource']['name'], host['resource']['scope']['name'],
host['resource']['objectId'], host['resource']['scope']['id'],
dfw_enabled) for host in enabled_hosts])
prepared_hosts_count = len(hosts)
dfw_enabled_hosts_count = len([host for host in hosts if host[4] == 'true'])
return prepared_hosts_count, dfw_enabled_hosts_count, hosts
def get_host_info(vccontent, host_list):
host_info = []
host_modict = get_all_objs(vccontent, VIM_TYPES['host'])
for host_name in [host[0] for host in host_list]:
print 'retrieving details (hardware & vms) for host {} ....'.format(host_name),
host_object = [host_mo for host_mo in host_modict if host_mo.name == host_name][0]
cpu_count = host_object.hardware.cpuInfo.numCpuPackages
#TODO: Filter service VMs out of the count
vm_moids = [vm._moId for vm in host_object.vm]
vm_count = len(vm_moids)
host_info.extend([(host_name, cpu_count, vm_count)])
print 'Done'
return host_info
def calculate_socket_usage(host_list, host_info):
nsx_socket_count = 0
dfw_scocket_count = 0
for host in host_info:
for nsx_host in host_list:
if host[0] == nsx_host[0]:
if nsx_host[4] == 'true':
dfw_scocket_count += int(host[1])
nsx_socket_count += int(host[1])
return nsx_socket_count, dfw_scocket_count
def ls_state(session):
all_logical_switches = session.read_all_pages('logicalSwitchesGlobal', 'read')
ls_list = [(ls['name'], ls['objectId']) for ls in all_logical_switches if ls['isUniversal'] == 'false']
uls_list = [(ls['name'], ls['objectId']) for ls in all_logical_switches if ls['isUniversal'] == 'true']
hwgwls_list = [(ls['name'], ls['objectId']) for ls in all_logical_switches if 'hardwareGatewayBinding' in ls]
return len(ls_list), ls_list, len(uls_list), uls_list, len(hwgwls_list), hwgwls_list
def edge_state(session):
edge_status = session.read_all_pages('nsxEdges', 'read')
esg_list = [(edge['objectId'], edge['name']) for edge in edge_status if edge['edgeType'] == 'gatewayServices']
dlr_list = [(edge['objectId'], edge['name']) for edge in edge_status if edge['edgeType'] == 'distributedRouter']
return len(esg_list), esg_list, len(dlr_list), dlr_list
def _single_esg_feature_collect(session, edge_id, edge_name):
print 'retrieving the features for Services Gateway {}/{} ....'.format(edge_name, edge_id),
edge_details = session.read('nsxEdge', uri_parameters={'edgeId': edge_id})['body']
print 'Done'
feature_map = {}
for feature in edge_details['edge']['features'].keys():
try:
feature_map.update({feature: edge_details['edge']['features'][feature]['enabled']})
except TypeError:
pass
return_tupple = (edge_name, edge_id, feature_map['loadBalancer'], feature_map['firewall'],
feature_map['routing'], feature_map['ipsec'], feature_map['l2Vpn'],
feature_map['sslvpnConfig'])
return return_tupple
def esg_features_collect(session, edge_list):
feature_list = []
for edge_name, edge_id in edge_list:
feature_list.append(_single_esg_feature_collect(session, edge_name, edge_id))
return feature_list
def contruct_parser(subparsers):
parser = subparsers.add_parser('usage', description="Functions to retrieve NSX-v usage statistics",
help="Functions to retrieve NSX-v usage statistics")
parser.set_defaults(func=_usage_main)
def _usage_main(args):
if args.debug:
debug = True
else:
debug = False
config = ConfigParser.ConfigParser()
assert config.read(args.ini), 'could not read config file {}'.format(args.ini)
try:
nsxramlfile = config.get('nsxraml', 'nsxraml_file')
except (ConfigParser.NoSectionError):
nsxramlfile_dir = resource_filename(__name__, 'api_spec')
nsxramlfile = '{}/nsxvapi.raml'.format(nsxramlfile_dir)
client_session = NsxClient(nsxramlfile, config.get('nsxv', 'nsx_manager'),
config.get('nsxv', 'nsx_username'), config.get('nsxv', 'nsx_password'), debug=debug)
vccontent = connect_to_vc(config.get('vcenter', 'vcenter'), config.get('vcenter', 'vcenter_user'),
config.get('vcenter', 'vcenter_passwd'))
print 'retrieving the hosts prepared for NSX ....',
host_count, dfw_enabled_hosts, host_list = host_prep_state(client_session)
print 'Done'
if args.verbose:
print tabulate(host_list, headers=["Host name", "Cluster name", "Host moid", "Cluster moid", "DFW enabled"],
tablefmt="psql"), "\n"
print 'retrieving the hosts detailed information ....'
host_info = get_host_info(vccontent, host_list)
if args.verbose:
print tabulate(host_info, headers=["Host name", "CPU Socket count", "VM count"], tablefmt="psql"), "\n"
print 'retrieving the number of NSX logical switches ....',
ls_count, ls_list, uls_count, uls_list, hwgwls_count, hwgwls_list = ls_state(client_session)
print 'Done'
if args.verbose:
print tabulate(ls_list, headers=["Logical switch name", "Logical switch Id"], tablefmt="psql"), "\n"
print tabulate(uls_list, headers=["Universal Logical switch name", "Logical switch Id"], tablefmt="psql"), "\n"
print tabulate(hwgwls_list, headers=["Logical switches using Hardware Gateway bindings", "Logical switch Id"],
tablefmt="psql"), "\n"
print 'retrieving the number of NSX gateways (ESGs and DLRs) ....',
esg_count, esg_list, dlr_count, dlr_list = edge_state(client_session)
print 'Done'
if args.verbose:
print tabulate(esg_list, headers=["Edge service gw name", "Edge service gw Id"], tablefmt="psql"), "\n"
print tabulate(dlr_list, headers=["Logical router name", "Logical router Id"], tablefmt="psql"), "\n"
edge_feature_list = esg_features_collect(client_session, esg_list)
if args.verbose:
print tabulate(edge_feature_list,
headers=["Edge service gw name", "Edge service gw Id", "Loadbalancer",
"Firewall", "Routing", "IPSec", "L2VPN", "SSL-VPN"], tablefmt="psql"), "\n"
lb_esg = len([edge for edge in edge_feature_list if edge[2] == 'true'])
fw_esg = len([edge for edge in edge_feature_list if edge[3] == 'true'])
rt_esg = len([edge for edge in edge_feature_list if edge[4] == 'true'])
ipsec_esg = len([edge for edge in edge_feature_list if edge[5] == 'true'])
l2vpn_esg = len([edge for edge in edge_feature_list if edge[6] == 'true'])
sslvpn_esg = len([edge for edge in edge_feature_list if edge[7] == 'true'])
nsx_sockets, dfw_sockets = calculate_socket_usage(host_list, host_info)
output_table = [('Number of hosts prepared for NSX', str(host_count)),
('Number of hosts enabled to use DFW', str(dfw_enabled_hosts)),
('Number of CPU Sockets enabled for NSX', str(nsx_sockets)),
('Number of CPU Sockets enabled for DFW', str(dfw_sockets)),
('Number of local logical switches', str(ls_count)),
('Number of universal logical switches', str(uls_count)),
('Number of logical switches with Hardware Gateway bindings', str(hwgwls_count)),
('Number of Edge services Gateways', str(esg_count)),
('Number of Distributed Routers', str(dlr_count)),
('Number of Service Gateways with Loadbalancing Enabled', str(lb_esg)),
('Number of Service Gateways with Firewall Enabled', str(fw_esg)),
('Number of Service Gateways with Routing Enabled', str(rt_esg)),
('Number of Service Gateways with IPSec Enabled', str(ipsec_esg)),
('Number of Service Gateways with L2VPN Enabled', str(l2vpn_esg)),
('Number of Service Gateways with SSL-VPN Enabled', str(sslvpn_esg))]
print '\n\nNSX usage summary:'
print tabulate(output_table, headers=["Feature / Property / Type", "Count"], tablefmt="psql")
def main():
main_parser = argparse.ArgumentParser()
subparsers = main_parser.add_subparsers()
contruct_parser(subparsers)
args = main_parser.parse_args()
args.func(args)
if __name__ == "__main__":
main()