-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path71.vm_perf_sample.py
124 lines (99 loc) · 4.23 KB
/
71.vm_perf_sample.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
#!/usr/bin/env python
"""
Written by Lance Hasson
Github: https://github.com/JLHasson
Script to report all available realtime performance metrics from a
virtual machine. Based on a Java example available in the VIM API 6.0
documentationavailable online at:
https://pubs.vmware.com/vsphere-60/index.jsp?topic=%2Fcom.vmware.wssdk.pg.
doc%2FPG_Performance.18.4.html&path=7_1_0_1_15_2_4
Requirements:
VM tools must be installed on all virtual machines.
"""
import os
import sys
import sys
sys.path.append(os.path.realpath(".") + '/venv/Lib/site-packages/')
from pyVim.connect import SmartConnect
from pyVmomi import vim
from tools import cli, service_instance
################################
from configparser import ConfigParser
config = ConfigParser()
# read user/pw from ini
fileConf = '../vc_info.ini' # File config store login info, see vc_info.ini.sample
# If file config exists:
if os.path.exists(fileConf):
config.read(fileConf)
serverDomain = config.get('config', 'server')
username = config.get('config', 'username')
password = config.get('config', 'password')
else:
serverDomain = "<enter your server domain>"
username = "<enter user, ex: [email protected]>"
password = "<enter password>"
###############################
def main():
# parser = cli.Parser()
# args = parser.get_args()
# si = service_instance.connect(args)
si = SmartConnect(host=serverDomain,
user=username,
pwd=password,
port=443, disableSslCertValidation=True)
content = si.RetrieveContent()
perf_manager = content.perfManager
# create a mapping from performance stats to their counterIDs
# counterInfo: [performance stat => counterId]
# performance stat example: cpu.usagemhz.LATEST
# counterId example: 6
counter_info = {}
for counter in perf_manager.perfCounter:
full_name = counter.groupInfo.key + "." + \
counter.nameInfo.key + "." + counter.rollupType
counter_info[full_name] = counter.key
# create a list of vim.VirtualMachine objects so
# that we can query them for statistics
container = content.rootFolder
view_type = [vim.VirtualMachine]
recursive = True
container_view = content.viewManager.CreateContainerView(container, view_type, recursive)
children = container_view.view
# Loop through all the VMs
for child in children:
# Get all available metric IDs for this VM
counter_ids = [m.counterId for m in perf_manager.QueryAvailablePerfMetric(entity=child)]
# Using the IDs form a list of MetricId
# objects for building the Query Spec
metric_ids = [vim.PerformanceManager.MetricId(
counterId=counter, instance="*") for counter in counter_ids]
# Build the specification to be used
# for querying the performance manager
spec = vim.PerformanceManager.QuerySpec(maxSample=1,
entity=child,
metricId=metric_ids)
# Query the performance manager
# based on the metrics created above
result_stats = perf_manager.QueryStats(querySpec=[spec])
# Loop through the results and print the output
output = ""
for _ in result_stats:
output += " vm-name: " + child.summary.config.name + "\n"
for val in result_stats[0].value:
# python3
if sys.version_info[0] > 2:
counterinfo_k_to_v = list(counter_info.keys())[
list(counter_info.values()).index(val.id.counterId)]
# python2
else:
counterinfo_k_to_v = counter_info.keys()[
counter_info.values().index(val.id.counterId)]
if val.id.instance == '':
output += child.summary.config.name + " - %s: %s\n" % (
counterinfo_k_to_v, str(val.value[0]))
else:
output += child.summary.config.name + " %s (%s): %s\n" % (
counterinfo_k_to_v, val.id.instance, str(val.value[0]))
print(output)
if __name__ == "__main__":
main()