-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvm_historic_stats.py
executable file
·202 lines (158 loc) · 6.38 KB
/
vm_historic_stats.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# The MIT License (MIT)
#
# Copyright (c) 2016 Tintri, Inc.
#
# 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.
import sys
import json
import time
import tintri_1_1 as tintri
from types import *
import datetime
"""
Collect VM historic stats
"""
# For exhaustive messages on console, make it to True; otherwise keep it False
debug_mode = False
def print_with_prefix(prefix, out):
print(prefix + out)
return
def print_debug(out):
if debug_mode:
print_with_prefix("[DEBUG] : ", out)
return
def print_info(out):
print_with_prefix("[INFO] : ", out)
return
def print_error(out):
print_with_prefix("[ERROR] : ", out)
return
def my_timezone():
tz_hours = time.timezone / -3600
tz_minutes = time.timezone % 3600
return "{0:0=+3d}:{1:0=2d}".format(tz_hours, tz_minutes)
def print_stats(paged_result, count):
page = paged_result["page"]
page_total = paged_result["pageTotal"]
print_info("Page " + str(page) + " of " + str(page_total))
items = paged_result["items"]
#print_info("items: " + str(items))
for stat in items:
num_slices = stat["numberOfSlices"]
start_time = stat["startTime"]
print(" " + start_time + " with " + str(num_slices) + " slices")
historic_stats = stat["sortedStats"]
for historic_stat in historic_stats:
start_time = historic_stat["timeStart"]
space_used = historic_stat["spaceUsedGiB"]
print(str(count) + ": " + start_time + ": " + str(space_used))
count += 1
return count
# main
if len(sys.argv) < 4:
print("\nCollect VM historic stats")
print("Usage: " + sys.argv[0] + " server_name user_name password <page_size>\n")
sys.exit(-1)
server_name = sys.argv[1]
user_name = sys.argv[2]
password = sys.argv[3]
page_size = 25 # init default
if len(sys.argv) == 5:
page_size = sys.argv[4]
try:
# Get the preferred version
r = tintri.api_version(server_name)
json_info = r.json()
print_info("API Version: " + json_info['preferredVersion'])
# Login to VMstore
session_id = tintri.api_login(server_name, user_name, password)
except tintri.TintriRequestsException as tre:
print_error(tre.__str__())
sys.exit(-10)
except tintri.TintriApiException as tae:
print_error(tae.__str__())
sys.exit(-11)
try:
# Get a VM to work with
vm_filter = {'limit' : 1,
'sortedBy' : 'LATENCY',
'sortOrder' : 'DESC',
'live' : "TRUE"}
url = "/v310/vm"
r = tintri.api_get_query(server_name, url, vm_filter, session_id)
print_debug("The JSON response of the get invoke to the server " +
server_name + " is: " + r.text)
if (r.status_code != 200):
msg = "The HTTP response for the get invoke to the server is " + \
server_name + "not 200, but is: " + str(r.status_code) + "."
raise tintri.TintriApiException(msg, r.status_code, url, str(vm_filter), r.text)
vm_paginated_result = r.json()
items = vm_paginated_result['items']
vm = items[0]
vm_uuid = vm['uuid']['uuid']
vm_name = vm['vmware']['name']
print_info("VM: " + vm_name + " : " + vm_uuid)
# Get the time 30 minutes ago in UTC.
now = datetime.datetime.utcnow()
minus_30 = now - datetime.timedelta(minutes=30)
# Add UTC time zone and print.
now_str = now.isoformat() + "-00:00"
minus_30_str = minus_30.isoformat() + "-00:00"
print_info(" Now: " + now_str)
print_info("Collectiong stats from: " + minus_30_str)
q_filter = {#'queryType': 'TOP_DOCS_BY_LATEST_TIME',
'since' : minus_30_str,
'offset' : 0,
'limit' : page_size }
# Get historic stats
historic_url = "/v310/vm/" + vm_uuid + "/statsHistoric"
print_info("URL: " + historic_url + ", " + str(q_filter))
r = tintri.api_get_query(server_name, historic_url, q_filter, session_id)
print_debug("The JSON response of the get invoke to the server " +
server_name + " is: " + r.text)
if (r.status_code != 200):
msg = "The HTTP response for the get invoke to the server is " + \
server_name + "not 200, but is: " + str(r.status_code) + "."
raise tintri.TintriApiException(msg, r.status_code, historic_url, str(q_filter), r.text)
historic_paginated_result = r.json()
print_info("Absolute Total: " + str(historic_paginated_result["absoluteTotal"]))
count = 1
print(" " + now.isoformat() + " is now")
count = print_stats(historic_paginated_result, count)
print_info("limit: " + str(historic_paginated_result['limit']))
# While there are more Vms, go get them
while 'next' in historic_paginated_result:
url = historic_url + "?" + historic_paginated_result['next']
print_info("Next GET URL: " + str(count) + ": " + url)
r = tintri.api_get(server_name, url, session_id)
historic_result = r.json()
count = print_stats(historic_paginated_result, count)
except tintri.TintriRequestsException as tre:
print_error(tre.__str__())
tintri.api_logout(server_name, session_id)
sys.exit(-20)
except tintri.TintriApiException as tae:
print_error(tae.__str__())
tintri.api_logout(server_name, session_id)
sys.exit(-21)
# Logout
tintri.api_logout(server_name, session_id)