adding projects to repo
This commit is contained in:
BIN
vrops-api/__dev/__pycache__/vropsAPI.cpython-37.pyc
Normal file
BIN
vrops-api/__dev/__pycache__/vropsAPI.cpython-37.pyc
Normal file
Binary file not shown.
8
vrops-api/__dev/devNotes.txt
Normal file
8
vrops-api/__dev/devNotes.txt
Normal file
@@ -0,0 +1,8 @@
|
||||
vrops._vmsResourcesRaw[0]['values'][0]['resourceList'][100]['links'][0]['href']
|
||||
|
||||
'/suite-api/api/resources/d38f3933-d432-4b62-9211-da6243fcfcaa'
|
||||
|
||||
|
||||
vrops._vmsResourcesRaw is the raw with [i] being all VMS under each host
|
||||
|
||||
the ['values'] comes from when you pull back for VM data, not when you ask for the relationships
|
||||
23
vrops-api/__dev/notes.md
Normal file
23
vrops-api/__dev/notes.md
Normal file
@@ -0,0 +1,23 @@
|
||||
<!-- MarkdownTOC -->
|
||||
|
||||
- [Added](#added)
|
||||
- [Info](#info)
|
||||
|
||||
<!-- /MarkdownTOC -->
|
||||
|
||||
|
||||
# Added
|
||||
- self.chosenCluster in getHostsFromCLuster
|
||||
|
||||
|
||||
# Info
|
||||
|
||||
```host info in {'pageInfo': {'totalCount': 37, 'page': 0, 'pageSize': 1000}, 'links': [{'href': '/suite-api/api/resources/15b3ea0c-9f62-4fc2-93b8-d4281196043e/relationships?page=0&pageSize=1000', 'rel': 'SELF', 'name': 'current'}, ```
|
||||
|
||||
in `self._vmsResources[i]['resourceList']`
|
||||
|
||||
|
||||
|
||||
if ```'resourceKey': {'name': 'SC1PRCONTXWHCUXCCL01', 'adapterKindKey': 'VMWARE', 'resourceKindKey': 'ClusterComputeResource', ```
|
||||
|
||||
if this is cluster compute resource, extract the name
|
||||
91
vrops-api/__dev/old/addhostcluster.py
Normal file
91
vrops-api/__dev/old/addhostcluster.py
Normal file
@@ -0,0 +1,91 @@
|
||||
import base64
|
||||
|
||||
# import os
|
||||
import sys
|
||||
|
||||
sys.path.append(
|
||||
'/Users/dtomlinson/OneDrive - William Hill Organisation Limited/Mac/'
|
||||
'projects/vrops-api/__dev'
|
||||
)
|
||||
# sys.path.remove(
|
||||
# '/Users/dtomlinson/OneDrive - William Hill Organisation Limited/Mac/'
|
||||
# 'projects/vrops-api/'
|
||||
# )
|
||||
from vropsAPI import vropsAPI
|
||||
|
||||
|
||||
# Authenticate:
|
||||
vrops = vropsAPI.authenticate(
|
||||
'https://sc1prapvro01/',
|
||||
'svc_splunkVROPS@Group.WilliamHill.PLC',
|
||||
'whgroup',
|
||||
base64.b64decode(b'UmFjaW5nMjEyMg==').decode(),
|
||||
verify=False,
|
||||
)
|
||||
|
||||
|
||||
# Get all clusters and store a list of Names:
|
||||
vrops.getClusters()
|
||||
vrops.getClusterIdentifiers()
|
||||
allClustersList = vropsAPI.getKeysList(vrops.allClusters)
|
||||
|
||||
|
||||
# Print all these clusters
|
||||
print(allClustersList)
|
||||
|
||||
|
||||
# Get all hosts and store a list of Names:
|
||||
vrops.getHostsFromCluster(cluster='SC1PRCONTXWHCUXCCL01')
|
||||
vrops.getHostIdentifiers()
|
||||
allHostsList = vrops.getKeysList(vrops.allHosts)
|
||||
|
||||
|
||||
# Print all these hosts
|
||||
print(allHostsList)
|
||||
|
||||
# # Add just 2 hots
|
||||
# counter = 0
|
||||
# hostList = []
|
||||
# for host in allHostsList:
|
||||
# if counter == 2:
|
||||
# break
|
||||
# else:
|
||||
# hostList.append(host)
|
||||
# counter += 1
|
||||
|
||||
|
||||
# Get all VMs and sore a list of IDs
|
||||
vrops.getVMSFromHost(allHostsList)
|
||||
vrops.getVMSIdentifiers()
|
||||
allVMSIdList = vrops.getValuesList(vrops.allVMS)
|
||||
|
||||
|
||||
# Save all VMs to disk
|
||||
vrops.saveToDisk(vrops.allVMS, indent=4, filePrefix='approach1-vms')
|
||||
|
||||
|
||||
# Get data for a vm
|
||||
vrops.getStatsFromVMS(
|
||||
begin=vrops.epochRelativeTime(vrops.epochNow, minutes=-11),
|
||||
end=vrops.epochNow,
|
||||
intervalType='MINUTES',
|
||||
intervalQuantifier='5',
|
||||
rollUpType='AVG',
|
||||
resourceId=allVMSIdList,
|
||||
statKey=['cpu|usage_average', 'config|hardware|num_Cpu'],
|
||||
)
|
||||
|
||||
|
||||
# Export the data into readable format
|
||||
vrops.exportVMData()
|
||||
|
||||
|
||||
# Save to disk
|
||||
vrops.saveToDisk(
|
||||
vrops.export,
|
||||
indent=4,
|
||||
filePrefix='approach1-export',
|
||||
breakLine=True,
|
||||
path='/Users/dtomlinson/OneDrive - William Hill Organisation Limited'
|
||||
'/Mac/projects/vrops-api/__dev',
|
||||
)
|
||||
22
vrops-api/__dev/old/authenticate.py
Normal file
22
vrops-api/__dev/old/authenticate.py
Normal file
@@ -0,0 +1,22 @@
|
||||
import requests
|
||||
|
||||
url = "https://sc1prapvro01/suite-api/api/auth/token/acquire"
|
||||
|
||||
payload = "{\n\"username\" : \"dtomlinson@Group.WilliamHill.PLC\",\
|
||||
\n\"authSource\" : \"whgroup\",\n\"password\" : \"yi3Uw9Mw\"\n}"
|
||||
|
||||
headers = {
|
||||
'Accept': "application/json",
|
||||
'Content-Type': "application/json",
|
||||
'Cache-Control': "no-cache",
|
||||
'Host': "sc1prapvro01",
|
||||
'Accept-Encoding': "gzip, deflate",
|
||||
'Content-Length': "102",
|
||||
'Connection': "keep-alive",
|
||||
'cache-control': "no-cache"
|
||||
}
|
||||
|
||||
response = requests.request("POST", url, data=payload, headers=headers,
|
||||
verify=False)
|
||||
|
||||
print(response.text)
|
||||
87
vrops-api/__dev/old/getHostInfo.py
Normal file
87
vrops-api/__dev/old/getHostInfo.py
Normal file
@@ -0,0 +1,87 @@
|
||||
import base64
|
||||
import os
|
||||
import sys
|
||||
|
||||
sys.path.append(os.getcwd())
|
||||
from vropsAPI import vropsAPI
|
||||
|
||||
|
||||
# Authenticate:
|
||||
vrops = vropsAPI.authenticate(
|
||||
'https://sc1prapvro01/',
|
||||
'svc_splunkVROPS@Group.WilliamHill.PLC',
|
||||
'whgroup',
|
||||
base64.b64decode(b'UmFjaW5nMjEyMg==').decode(),
|
||||
verify=False,
|
||||
)
|
||||
|
||||
|
||||
# Get all clusters and store a list of Names:
|
||||
vrops.getClusters()
|
||||
vrops.getClusterIdentifiers()
|
||||
allClustersList = vrops.getKeysList(vrops.allClusters)
|
||||
|
||||
|
||||
# Print all these clusters
|
||||
print(allClustersList)
|
||||
|
||||
# Get all hosts and store a list of Names:
|
||||
vrops.getHostsFromCluster(cluster='SC1PRCONTXWHCUXCCL01')
|
||||
vrops.getHostIdentifiers()
|
||||
allHostsList = vrops.getValuesList(vrops.allHosts)
|
||||
|
||||
|
||||
# # Print all these hosts
|
||||
# print(allHostsList)
|
||||
|
||||
|
||||
# # Get all VMs and sore a list of IDs:
|
||||
# vrops.getVMSFromHost(allHostsList)
|
||||
# vrops.getVMSIdentifiers()
|
||||
# allVMSIdList = vrops.getValuesList(vrops.allVMS)
|
||||
|
||||
# # Save all VMs to disk
|
||||
# vrops.saveToDisk(vrops.allVMS, indent=4, filePrefix='approach1-vms')
|
||||
|
||||
# # Save all VMs:Hosts to disk
|
||||
# vrops.saveToDisk(
|
||||
# vrops.VMSHostsNames, indent=4, filePrefix='approach1-vms_hosts'
|
||||
# )
|
||||
|
||||
|
||||
# # Add a single VM to a list to pull back
|
||||
# myList = []
|
||||
# myList.append(allVMSIdList[0])
|
||||
|
||||
|
||||
# Get data for a vm
|
||||
vrops.getStatsFromVMS(
|
||||
begin=vrops.epochRelativeTime(
|
||||
vrops.epochNow, days=-3, hour=15, minute=0, second=0
|
||||
),
|
||||
end=vrops.epochRelativeTime(
|
||||
vrops.epochNow, days=-3, hour=17, minute=0, second=0
|
||||
),
|
||||
intervalType='MINUTES',
|
||||
intervalQuantifier='5',
|
||||
rollUpType='AVG',
|
||||
resourceId=allHostsList,
|
||||
statKey=['cpu|corecount_provisioned'],
|
||||
vropsType='host',
|
||||
)
|
||||
|
||||
|
||||
# Export the data into readable format
|
||||
vrops.exportVMData()
|
||||
|
||||
# Save to disk
|
||||
vrops.saveToDisk(
|
||||
vrops._vmsResources,
|
||||
indent=4,
|
||||
filePrefix='reddit-help-before',
|
||||
breakLine=True,
|
||||
)
|
||||
|
||||
vrops.saveToDisk(
|
||||
vrops.export, indent=4, filePrefix='reddit-help-after', breakLine=True
|
||||
)
|
||||
26
vrops-api/__dev/old/getMetrics.py
Normal file
26
vrops-api/__dev/old/getMetrics.py
Normal file
@@ -0,0 +1,26 @@
|
||||
import requests
|
||||
|
||||
url = "https://sc1prapvro01/suite-api//api/resources/stats"
|
||||
|
||||
querystring = {"resourceId": "0389a5e4-9b4c-49aa-8139-92ef54f8dd9f",
|
||||
"begin": "1564587600000", "end": "1564588200000",
|
||||
"statKey":
|
||||
["cpu|workload", "mem|usage_average", "mem|swapped_average"],
|
||||
"currentOnly": "True"}
|
||||
|
||||
headers = {
|
||||
'Authorization': "vRealizeOpsToken f72a2910-88c3-442d-9dfb-58f61aa833fe"
|
||||
"::10e2fe6d-55dc-416e-ad3c-9177901f9b3a",
|
||||
'Content-Type': "application/json",
|
||||
'Accept': "application/json",
|
||||
'Cache-Control': "no-cache",
|
||||
'Host': "sc1prapvro01",
|
||||
'Accept-Encoding': "gzip, deflate",
|
||||
'Connection': "keep-alive",
|
||||
'cache-control': "no-cache"
|
||||
}
|
||||
|
||||
response = requests.request("GET", url, headers=headers, params=querystring,
|
||||
verify=False)
|
||||
|
||||
print(response.text)
|
||||
42
vrops-api/__dev/old/getVms.py
Normal file
42
vrops-api/__dev/old/getVms.py
Normal file
@@ -0,0 +1,42 @@
|
||||
import requests
|
||||
import json
|
||||
import math
|
||||
|
||||
url = "https://sc1prapvro01/suite-api/api/resources"
|
||||
|
||||
querystring = {"page": "0", "regex": "^prdx*"}
|
||||
|
||||
headers = {
|
||||
'Content-Type': "application/json",
|
||||
'Authorization': "vRealizeOpsToken f72a2910-88c3-442d-9dfb-58f61aa833fe"
|
||||
"::29abe2bf-a1f7-464d-b48a-508320a98627",
|
||||
'Content-Type': "application/json",
|
||||
'Accept': "application/json",
|
||||
'Cache-Control': "no-cache",
|
||||
'Host': "sc1prapvro01",
|
||||
'Accept-Encoding': "gzip, deflate",
|
||||
'Connection': "keep-alive",
|
||||
'cache-control': "no-cache"
|
||||
}
|
||||
|
||||
response = requests.request("GET", url, headers=headers,
|
||||
params=querystring, verify=False)
|
||||
|
||||
t = json.loads(response.text)
|
||||
listNames = []
|
||||
totalVms = (t['pageInfo']['totalCount'])
|
||||
vmsPerPage = (t['pageInfo']['pageSize'])
|
||||
pages = math.ceil(totalVms/vmsPerPage)
|
||||
|
||||
queryList = [i for i in range(0, pages)]
|
||||
|
||||
for page in queryList:
|
||||
querystring = {'page': page}
|
||||
response = requests.request("GET", url, headers=headers,
|
||||
params=querystring, verify=False)
|
||||
for i in (range(0, 1000)):
|
||||
t = json.loads(response.text)
|
||||
listNames.append(t['resourceList'][i]['resourceKey']['name'])
|
||||
print(listNames[i])
|
||||
|
||||
print(listNames)
|
||||
134
vrops-api/__dev/old/getoutput.py
Normal file
134
vrops-api/__dev/old/getoutput.py
Normal file
@@ -0,0 +1,134 @@
|
||||
import base64
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
sys.path.append(os.getcwd())
|
||||
from vropsAPI import vropsAPI
|
||||
|
||||
|
||||
# Authenticate:
|
||||
vrops = vropsAPI.authenticate(
|
||||
'https://sc1prapvro01/',
|
||||
'svc_splunkVROPS@Group.WilliamHill.PLC',
|
||||
'whgroup',
|
||||
base64.b64decode(b'UmFjaW5nMjEyMg==').decode(),
|
||||
verify=False,
|
||||
)
|
||||
|
||||
# Load VM dict
|
||||
|
||||
with open('vms.json', 'r') as file:
|
||||
vrops.allVMS = json.load(file)
|
||||
|
||||
# Add a single VM to a list to pull back
|
||||
myList = []
|
||||
for i in range(0, len(vrops.allVMS)):
|
||||
myList.append(list(vrops.allVMS.values())[i])
|
||||
|
||||
testList = []
|
||||
testList.append(myList[10])
|
||||
testList.append(myList[11])
|
||||
|
||||
# print(myList)
|
||||
|
||||
# Get data for a vm
|
||||
vrops.getStatsFromVMS(
|
||||
begin=vrops.epochRelativeTime(vrops.epochNow, minutes=-20),
|
||||
end=vrops.epochNow,
|
||||
intervalType='MINUTES',
|
||||
intervalQuantifier='5',
|
||||
rollUpType='AVG',
|
||||
resourceId=testList,
|
||||
statKey=['cpu|usage_average', 'config|hardware|num_Cpu'],
|
||||
)
|
||||
|
||||
# print(vrops.vmsResources)
|
||||
|
||||
# Save output to disk
|
||||
# vrops.saveToDisk(vrops.vmsResources, indent=4, fileName='approach2-output')
|
||||
|
||||
export, metric = {}, {}
|
||||
|
||||
# All vms to loop through
|
||||
print(len(vrops.vmsResources['values']))
|
||||
|
||||
|
||||
# vm name
|
||||
print(vrops.vmsResources['values'][0]['name'])
|
||||
|
||||
|
||||
# data stored in
|
||||
print(vrops.vmsResources['values'][0]['stat-list']['stat'])
|
||||
|
||||
# how many keys there are
|
||||
print(len(vrops.vmsResources['values'][0]['stat-list']['stat']))
|
||||
|
||||
# timestamp earliest, -1 latest?
|
||||
print(vrops.vmsResources['values'][0]['stat-list']['stat'][0]['timestamps'][0])
|
||||
|
||||
# statkey
|
||||
print(
|
||||
vrops.vmsResources['values'][0]['stat-list']['stat'][0]['statKey']['key']
|
||||
)
|
||||
|
||||
# Rolluptype avg etc
|
||||
print(vrops.vmsResources['values'][0]['stat-list']['stat'][0]['rollUpType'])
|
||||
|
||||
# time interval
|
||||
print(vrops.vmsResources['values'][0]['stat-list']['stat'][0]['intervalUnit'][
|
||||
'quantifier'
|
||||
])
|
||||
|
||||
# intervaltype minutes etc
|
||||
print(vrops.vmsResources['values'][0]['stat-list']['stat'][0]['intervalUnit'][
|
||||
'intervalType'
|
||||
])
|
||||
|
||||
# data earliest, -1 latest?
|
||||
print(vrops.vmsResources['values'][0]['stat-list']['stat'][0]['data'][0])
|
||||
|
||||
|
||||
loopLength = len(vrops.vmsResources['values'])
|
||||
print(f'loop length - {loopLength}')
|
||||
|
||||
timeLength = len(
|
||||
vrops.vmsResources['values'][0]['stat-list']['stat'][0]['timestamps']
|
||||
)
|
||||
|
||||
metricLength = len(vrops.vmsResources['values'][0]['stat-list']['stat'])
|
||||
print(metricLength)
|
||||
|
||||
print('\n')
|
||||
|
||||
for i in range(0, loopLength):
|
||||
# timeLength = len(
|
||||
# vrops.vmsResources['values'][1]['stat-list']['stat'][0]['timestamps']
|
||||
# )
|
||||
# print(json.dumps({'name': vrops.vmsResources['values'][i]['name']}))
|
||||
pass
|
||||
|
||||
print(
|
||||
len(vrops.vmsResources['values'][0]['stat-list']['stat'][0]['timestamps']))
|
||||
|
||||
for i in range(0, loopLength):
|
||||
statKeyLength = len(vrops.vmsResources['values'][i]['stat-list']['stat'])
|
||||
timeLength = len(
|
||||
vrops.vmsResources['values'][i]['stat-list']['stat'][0]['timestamps']
|
||||
)
|
||||
for k in range(0, statKeyLength):
|
||||
for j in range(0, timeLength):
|
||||
print(type(
|
||||
json.dumps({
|
||||
'name': vrops.vmsResources['values'][i]['name'],
|
||||
'timesamp': vrops.vmsResources['values'][i]['stat-list']
|
||||
['stat'][0]['timestamps'][j],
|
||||
'data': vrops.vmsResources['values'][i]['stat-list']
|
||||
['stat'][k]['data'][j],
|
||||
'statKey': vrops.vmsResources['values'][i]['stat-list']
|
||||
['stat'][k]['statKey']['key'],
|
||||
'rollUpType': vrops.vmsResources['values'][i]['stat-list']
|
||||
['stat'][k]['rollUpType'],
|
||||
'intervalUnit': vrops.vmsResources['values'][i]
|
||||
['stat-list']['stat'][k]['intervalUnit']['quantifier']
|
||||
}),
|
||||
))
|
||||
12
vrops-api/__dev/old/regex.py
Normal file
12
vrops-api/__dev/old/regex.py
Normal file
@@ -0,0 +1,12 @@
|
||||
import re
|
||||
import json
|
||||
|
||||
data = "host info in {'pageInfo': {'totalCount': 37, 'page': 0, 'pageSize': 1000}, 'links': [{'href': '/suite-api/api/resources/15b3ea0c-9f62-4fc2-93b8-d4281196043e/relationships?page=0&pageSize=1000', 'rel': 'SELF', 'name': 'current'},"
|
||||
|
||||
pattern = r'(?:.*resources\/)(?P<host_id_response>[^\/]+)'
|
||||
|
||||
match = re.findall(pattern, data)
|
||||
|
||||
host = [x for x in match]
|
||||
|
||||
print(host)
|
||||
Binary file not shown.
76
vrops-api/__dev/spaghetti_code/redditHelp.py
Normal file
76
vrops-api/__dev/spaghetti_code/redditHelp.py
Normal file
@@ -0,0 +1,76 @@
|
||||
import base64
|
||||
import os
|
||||
import sys
|
||||
|
||||
sys.path.append(os.getcwd())
|
||||
from vropsAPI import vropsAPI
|
||||
|
||||
|
||||
# Authenticate:
|
||||
vrops = vropsAPI.authenticate(
|
||||
'https://sc1prapvro01/',
|
||||
'svc_splunkVROPS@Group.WilliamHill.PLC',
|
||||
'whgroup',
|
||||
base64.b64decode(b'UmFjaW5nMjEyMg==').decode(),
|
||||
verify=False,
|
||||
)
|
||||
|
||||
|
||||
# Get all clusters and store a list of Names:
|
||||
vrops.getClusters()
|
||||
vrops.getClusterIdentifiers()
|
||||
allClustersList = vrops.getKeysList(vrops.allClusters)
|
||||
|
||||
|
||||
# Print all these clusters
|
||||
print(allClustersList)
|
||||
|
||||
# Get all hosts and store a list of Names:
|
||||
vrops.getHostsFromCluster(cluster='SC1PRCONTXWHCUXCCL01')
|
||||
vrops.getHostIdentifiers()
|
||||
allHostsList = vrops.getKeysList(vrops.allHosts)
|
||||
allHostsIdList = vrops.getValuesList(vrops.allHosts)
|
||||
|
||||
# Print all these hosts
|
||||
print(allHostsList)
|
||||
|
||||
|
||||
# Get all VMs and sore a list of IDs:
|
||||
vrops.getVMSFromHost('sc1hsesx156.prod.williamhill.plc')
|
||||
vrops.getVMSIdentifiers()
|
||||
allVMSIdList = vrops.getValuesList(vrops.allVMS)
|
||||
|
||||
# Save all VMs to disk
|
||||
vrops.saveToDisk(vrops.allVMS, indent=4, filePrefix='approach1-vms')
|
||||
|
||||
# Save all VMs:Hosts to disk
|
||||
vrops.saveToDisk(
|
||||
vrops.VMSHostsNames, indent=4, filePrefix='approach1-vms_hosts'
|
||||
)
|
||||
|
||||
|
||||
# Add a single VM to a list to pull back
|
||||
# myList = []
|
||||
# myList.append(allVMSIdList[0])
|
||||
|
||||
|
||||
# Get data for a vm
|
||||
vrops.getStatsFromVMS(
|
||||
begin=vrops.epochRelativeTime(vrops.epochNow, minutes=-30),
|
||||
end=vrops.epochNow,
|
||||
intervalType='MINUTES',
|
||||
intervalQuantifier='5',
|
||||
rollUpType='AVG',
|
||||
resourceId=allVMSIdList,
|
||||
statKey=['cpu|usage_average', 'config|hardware|num_Cpu'],
|
||||
vropsType='virtualmachine'
|
||||
)
|
||||
|
||||
|
||||
# # Export the data into readable format
|
||||
# vrops.exportVMData()
|
||||
|
||||
# # Save to disk
|
||||
# vrops.saveToDisk(
|
||||
# vrops.export, indent=4, filePrefix='approach1-export', breakLine=True
|
||||
# )
|
||||
65
vrops-api/__dev/spaghetti_code/spaghettiFixer.py
Normal file
65
vrops-api/__dev/spaghetti_code/spaghettiFixer.py
Normal file
@@ -0,0 +1,65 @@
|
||||
import os
|
||||
import sys
|
||||
import json
|
||||
|
||||
sys.path.append(os.getcwd())
|
||||
# from vropsAPI import vropsAPI
|
||||
|
||||
|
||||
# Open files
|
||||
|
||||
with open('_vmsResourcesaim.json', 'r+') as jsonFile:
|
||||
myDict = json.load(jsonFile)
|
||||
|
||||
with open('_vmsResources.json', 'r+') as jsonFile:
|
||||
myDictBefore = json.load(jsonFile)
|
||||
|
||||
with open('vmid.json', 'r+') as jsonFile:
|
||||
vmId = json.load(jsonFile)
|
||||
|
||||
with open('vmname.json', 'r+') as jsonFile:
|
||||
vmName = json.load(jsonFile)
|
||||
|
||||
print(myDict['values'][0])
|
||||
print()
|
||||
|
||||
# Playground
|
||||
|
||||
output0 = map(lambda x: x in 'values', myDict)
|
||||
|
||||
output1 = map(lambda i: i[0:], myDict)
|
||||
|
||||
|
||||
output2 = [item['name'] for item in myDict['values']]
|
||||
|
||||
print(myDictBefore['values'][0])
|
||||
|
||||
print()
|
||||
|
||||
# output2 = (x for x in myDict(iter(output1)))
|
||||
|
||||
print(output2)
|
||||
|
||||
print()
|
||||
|
||||
name = (
|
||||
key
|
||||
for item in [item['resourceId'] for item in myDictBefore['values']]
|
||||
for key, value in vmId.items()
|
||||
if item == value
|
||||
)
|
||||
|
||||
|
||||
for entry, name in zip(myDictBefore['values'], name):
|
||||
entry['name'] = name
|
||||
|
||||
print()
|
||||
print(name)
|
||||
print()
|
||||
print(myDictBefore['values'][0])
|
||||
print(myDictBefore['values'][-2])
|
||||
|
||||
"""
|
||||
names_res = map(lambda i: i['name'][0], data)
|
||||
ids = [item['owner']['id'] for owner in results['playlists']['items']]
|
||||
"""
|
||||
767
vrops-api/__dev/spaghetti_code/vropsAPI.py
Normal file
767
vrops-api/__dev/spaghetti_code/vropsAPI.py
Normal file
@@ -0,0 +1,767 @@
|
||||
import requests
|
||||
import urllib3
|
||||
import json
|
||||
from typing import Union
|
||||
import copy
|
||||
from datetime import datetime
|
||||
from dateutil.relativedelta import relativedelta
|
||||
import os
|
||||
import math
|
||||
import time
|
||||
from collections import OrderedDict
|
||||
import re
|
||||
from deprecated import deprecated
|
||||
|
||||
# warnings.filterwarnings('ignore')
|
||||
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
|
||||
|
||||
|
||||
class vropsAPI(object):
|
||||
"""Module for the vropsAPI for Capacity Management:
|
||||
Author: Daniel Tomlinson
|
||||
Team: Capacity & Monitoring
|
||||
Date: October 2019
|
||||
|
||||
VROPS documentation: https://sc1prapvro01/suite-api/docs/rest/
|
||||
index.html
|
||||
StatKeys for VMS:
|
||||
https://docs.vmware.com/en/vRealize-Operations-Manager/6.7/com.vmware.vcom.metrics.doc/GUID-1322F5A4-DA1D-481F-BBEA-99B228E96AF2.html
|
||||
|
||||
Usage
|
||||
-----
|
||||
Authenticate a session:
|
||||
vrops = vropsAPI.authenticate(
|
||||
'https://sc1prapvro01/',
|
||||
'username',
|
||||
'authSource',
|
||||
'password',
|
||||
verify=False,
|
||||
)
|
||||
|
||||
|
||||
Get all clusters:
|
||||
Query VROPS for available clusters:
|
||||
vrops.getClusters()
|
||||
vrops.getClusterIdentifiers()
|
||||
|
||||
Get dict of all cluster IDs and cluster names:
|
||||
allClustersDict = vrops.allClusters
|
||||
|
||||
Get list of all cluster names:
|
||||
allClustersList = vrops.getList(vrops.allClusters)
|
||||
|
||||
|
||||
Get all hosts:
|
||||
Query VROPS for available hosts:
|
||||
From a single cluster:
|
||||
vrops.getHostsFromCluster(cluster='SC1PRCONTXWHCUXCCL01')
|
||||
vrops.getHostIdentifiers()
|
||||
From a list of clusters:
|
||||
<Not implemented>
|
||||
|
||||
Get dict of all host IDs and host names:
|
||||
allHostsDisct =vrops.allHosts
|
||||
|
||||
Get list of host names:
|
||||
allHostsList = vrops.getList(vrops.allHosts)
|
||||
|
||||
|
||||
Get all VMs:
|
||||
Query VROPS for available VMs:
|
||||
For a single host:
|
||||
vrops.getVMSFromHost('sc1hsesx148.prod.williamhill.plc')
|
||||
vrops.getVMSIdentifiers()
|
||||
|
||||
For a list of hosts:
|
||||
vrops.getVMSFromHost(allHostsList)
|
||||
vrops.getVMSIdentifiers()
|
||||
|
||||
Get dict of all VM IDs and VM names:
|
||||
allVMSDict = vrops.allVMS
|
||||
|
||||
Get list of all VMs:
|
||||
allVMSList = vrops.getList(vrops.allVMS)
|
||||
|
||||
|
||||
Get epoch time relative to another time:
|
||||
Similar to Splunks relative_time command:
|
||||
1. Can go back N hours/minutes etc.
|
||||
2. Can set the hour/minute etc. to a specified value (snapping)
|
||||
|
||||
vrops.epochRelativeTime(epochTime, **kwargs)
|
||||
|
||||
**kwargs:
|
||||
epochTime: int - start time
|
||||
|
||||
year: int = datetime.now().year # set year
|
||||
month: int = datetime.now().month # set month
|
||||
day: int = datetime.now().day # set day
|
||||
hour: int = datetime.now().hour # set hour
|
||||
minute: int = datetime.now().minute # set minute
|
||||
second: int = datetime.now().second # set second
|
||||
|
||||
years: int = 0 # go back/forward N years
|
||||
months: int = 0 # go back/forward N months
|
||||
days: int = 0 # go back/forward N days
|
||||
hours: int = 0 # go back/forward N hours
|
||||
minutes: int = 0 # go back/forward N minutes
|
||||
seconds: int = 0 # go back/forward N seconds
|
||||
Usage:
|
||||
|
||||
Get epoch 5 minutes ago:
|
||||
vrops.epochRelativeTime(vrops.epochNow, minutes=-5)
|
||||
|
||||
Get epoch at start of current hour:
|
||||
vrops.epochRelativeTime(
|
||||
vrops.epochNow,
|
||||
hour=0,
|
||||
minute=0,
|
||||
second=0,
|
||||
)
|
||||
|
||||
Get epoch 1 week ago at start of day:
|
||||
vrops.epochRelativeTime(
|
||||
vrops.epochNow,
|
||||
days=-7
|
||||
hour=0,
|
||||
minute=0,
|
||||
second=0,
|
||||
)
|
||||
|
||||
|
||||
Get stats from VMs:
|
||||
Pull back results:
|
||||
Last 30 minutes, 5 minute intervals, average
|
||||
for CPU average and ready %:
|
||||
|
||||
vrops.getStatsFromVMS(
|
||||
begin=vrops.epochRelativeTime(vrops.epochNow, minutes=-30),
|
||||
end=vrops.epochNow,
|
||||
intervalType='MINUTES',
|
||||
intervalQuantifier='5',
|
||||
rollUpType='AVG',
|
||||
resourceId=list(vrops.allVMS.values()),
|
||||
statKey=['cpu|usage_average', 'cpu|readyPct'],
|
||||
)
|
||||
|
||||
Attributes:
|
||||
intervalType: <ns3:intervalType> (see
|
||||
https://sc1prapvro01/suite-api/docs/rest/
|
||||
models.html#repr-1190589417)
|
||||
|
||||
intervalQuantifier: int
|
||||
|
||||
rollUpType: <ns3:rollUpType> (see:
|
||||
https://sc1prapvro01/suite-api/
|
||||
docs/rest/models.html#repr-1735704374)
|
||||
|
||||
resourceId: string or list of vrops resourceIds (not names)
|
||||
|
||||
statKey: vrops api metrics (see https://docs.vmware.com/en/
|
||||
vRealize-Operations-Manager/6.7/com.vmware.vcom.metrics.doc/
|
||||
GUID-1322F5A4-DA1D-481F-BBEA-99B228E96AF2.html)
|
||||
|
||||
|
||||
Print results:
|
||||
for i in range(0, vrops.totalVMS):
|
||||
print(vrops.vmsResources['values'][i])
|
||||
|
||||
Save to disk as json:
|
||||
vrops.saveToDisk(vrops.vmsResources)
|
||||
|
||||
|
||||
Attributes
|
||||
----------
|
||||
vropsURL: str
|
||||
URL of the VROPS instance
|
||||
"https://sc1prapvro01/"
|
||||
|
||||
"""
|
||||
|
||||
defaultHeaders = {
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
'Cache-Control': 'no-cache',
|
||||
'Accept-Encoding': 'gzip, deflate',
|
||||
'Connection': 'keep-alive',
|
||||
'cache-control': 'no-cache',
|
||||
}
|
||||
|
||||
def __init__(self, vropsURL: str, authToken: str, verify: bool = True):
|
||||
super(vropsAPI, self).__init__()
|
||||
self.vropsURL = vropsURL
|
||||
self.authToken = authToken
|
||||
self.verify = verify
|
||||
self.headers = vropsAPI.defaultHeaders
|
||||
self.headers['Authorization'] = f'vRealizeOpsToken {self.authToken}'
|
||||
|
||||
@classmethod
|
||||
def authenticate(
|
||||
cls,
|
||||
vropsURL: str,
|
||||
username: str,
|
||||
authSource: str,
|
||||
password: str,
|
||||
verify: bool = True,
|
||||
):
|
||||
vropsURLauth = vropsAPI.getVropsURL(vropsURL, 'authenticationURL')
|
||||
payload = {}
|
||||
for key, value in zip(
|
||||
['username', 'authSource', 'password'],
|
||||
[username, authSource, password],
|
||||
):
|
||||
payload[key] = value
|
||||
authToken = vropsAPI.getAuthenticationToken(
|
||||
vropsURLauth, payload, verify
|
||||
)
|
||||
return cls(vropsURL, authToken, verify)
|
||||
|
||||
@staticmethod
|
||||
def getVropsURL(vropsURL: str, endpointKey: str) -> str:
|
||||
endpoints = {
|
||||
'authenticationURL': 'suite-api/api/auth/token/acquire',
|
||||
'resourcesURL': 'suite-api/api/resources',
|
||||
'statsURL': 'suite-api/api/resources/stats/query',
|
||||
}
|
||||
if endpoints[endpointKey] not in vropsURL:
|
||||
if vropsURL[-1] != '/':
|
||||
vropsURL = vropsURL + '/'
|
||||
vropsURL = vropsURL + endpoints[endpointKey]
|
||||
else:
|
||||
vropsURL = vropsURL + endpoints[endpointKey]
|
||||
return vropsURL
|
||||
|
||||
@staticmethod
|
||||
def pythonToJSON(pythonObject: any, indent=4) -> str:
|
||||
return json.dumps(pythonObject, indent=indent)
|
||||
|
||||
@staticmethod
|
||||
def jsonToPython(jsonObject: str) -> any:
|
||||
return json.loads(jsonObject)
|
||||
|
||||
@staticmethod
|
||||
def getAuthenticationToken(
|
||||
vropsURL: str, payload: dict, verify=True
|
||||
) -> str:
|
||||
|
||||
payload = vropsAPI.pythonToJSON(payload)
|
||||
vropsURL = vropsAPI.getVropsURL(vropsURL, 'authenticationURL')
|
||||
response = requests.request(
|
||||
'POST',
|
||||
vropsURL,
|
||||
data=payload,
|
||||
headers=vropsAPI.defaultHeaders,
|
||||
verify=verify,
|
||||
)
|
||||
print(response)
|
||||
authToken = vropsAPI.jsonToPython(response.text)['token']
|
||||
return authToken
|
||||
|
||||
@staticmethod
|
||||
def getIdentifiers(
|
||||
identifierDict: dict,
|
||||
vropsJSON: dict,
|
||||
length: int,
|
||||
resourceKindKey: str,
|
||||
) -> dict:
|
||||
for i in range(0, length):
|
||||
if (
|
||||
vropsJSON['resourceList'][i]['resourceKey']['resourceKindKey']
|
||||
== resourceKindKey
|
||||
):
|
||||
identifierDict[
|
||||
vropsJSON['resourceList'][i]['resourceKey']['name']
|
||||
] = vropsJSON['resourceList'][i]['identifier']
|
||||
else:
|
||||
pass
|
||||
return identifierDict
|
||||
|
||||
@staticmethod
|
||||
def getKeysList(pythonDict: dict) -> list:
|
||||
pythonList = []
|
||||
for i in pythonDict.keys():
|
||||
pythonList.append(i)
|
||||
return pythonList
|
||||
|
||||
@staticmethod
|
||||
def getValuesList(pythonDict: dict) -> list:
|
||||
pythonList = []
|
||||
for i in pythonDict.values():
|
||||
pythonList.append(i)
|
||||
return pythonList
|
||||
|
||||
@staticmethod
|
||||
def epochRelativeTime(
|
||||
epochTime: int,
|
||||
year: int = datetime.now().year,
|
||||
month: int = datetime.now().month,
|
||||
day: int = datetime.now().day,
|
||||
hour: int = datetime.now().hour,
|
||||
minute: int = datetime.now().minute,
|
||||
second: int = datetime.now().second,
|
||||
years: int = 0,
|
||||
months: int = 0,
|
||||
days: int = 0,
|
||||
hours: int = 0,
|
||||
minutes: int = 0,
|
||||
seconds: int = 0,
|
||||
) -> int:
|
||||
delta = relativedelta(
|
||||
year=year,
|
||||
month=month,
|
||||
day=day,
|
||||
hour=hour,
|
||||
minute=minute,
|
||||
second=second,
|
||||
years=years,
|
||||
months=months,
|
||||
days=days,
|
||||
hours=hours,
|
||||
minutes=minutes,
|
||||
seconds=seconds,
|
||||
)
|
||||
if len(str(epochTime)) >= 12:
|
||||
epochTime /= 1000
|
||||
relativeTime = datetime.fromtimestamp(epochTime) + delta
|
||||
relativeTime = math.ceil(relativeTime.timestamp() * 1000)
|
||||
return relativeTime
|
||||
|
||||
def getClusters(self):
|
||||
queryString = {'resourceKind': 'ClusterComputeResource'}
|
||||
vropsURL = vropsAPI.getVropsURL(self.vropsURL, 'resourcesURL')
|
||||
response = requests.request(
|
||||
"GET",
|
||||
vropsURL,
|
||||
headers=self.headers,
|
||||
params=queryString,
|
||||
verify=self.verify,
|
||||
)
|
||||
print(response)
|
||||
self._clusterResources = vropsAPI.jsonToPython(response.text)
|
||||
return self
|
||||
|
||||
def getClusterIdentifiers(self):
|
||||
self.totalClusters = len(self._clusterResources['resourceList'])
|
||||
self.allClusters = {}
|
||||
self.getIdentifiers(
|
||||
self.allClusters,
|
||||
self._clusterResources,
|
||||
self.totalClusters,
|
||||
'ClusterComputeResource',
|
||||
)
|
||||
return self
|
||||
|
||||
def getHostsFromCluster(self, cluster='SC1PRCONTXWHCUXCCL01'):
|
||||
self.cluster = cluster
|
||||
url = (
|
||||
f'{self.vropsURL}/suite-api/api/resources/'
|
||||
f'{self.allClusters[cluster]}/relationships'
|
||||
)
|
||||
self.chosenCluster = cluster
|
||||
response = requests.request(
|
||||
"GET", url, headers=self.headers, verify=self.verify
|
||||
)
|
||||
print(response)
|
||||
self._hostResources = vropsAPI.jsonToPython(response.text)
|
||||
return self
|
||||
|
||||
def getHostIdentifiers(self):
|
||||
self.totalHosts = len(self._hostResources['resourceList'])
|
||||
self.allHosts = {}
|
||||
self.getIdentifiers(
|
||||
self.allHosts, self._hostResources, self.totalHosts, 'HostSystem'
|
||||
)
|
||||
return self
|
||||
|
||||
def getVMSFromHost(self, host: Union[str, list]):
|
||||
if isinstance(host, list):
|
||||
self.vmType = 'list'
|
||||
self._vmsResourcesRelationships = []
|
||||
self.urlList = []
|
||||
response = []
|
||||
for item in host:
|
||||
self.urlList.append(
|
||||
(
|
||||
f'{self.vropsURL}suite-api/api/resources/'
|
||||
f'{self.allHosts[item]}/relationships'
|
||||
)
|
||||
)
|
||||
for urlItem in self.urlList:
|
||||
response.append(
|
||||
requests.request(
|
||||
'GET',
|
||||
urlItem,
|
||||
headers=self.headers,
|
||||
verify=self.verify,
|
||||
)
|
||||
)
|
||||
print(response)
|
||||
for i in range(0, len(response)):
|
||||
self._vmsResourcesRelationships.append(
|
||||
vropsAPI.jsonToPython(response[i].text)
|
||||
)
|
||||
if isinstance(host, str):
|
||||
hostToList = []
|
||||
hostToList.append(host)
|
||||
print(hostToList)
|
||||
return self.getVMSFromHost(host=hostToList)
|
||||
return self
|
||||
|
||||
def _getHostInformation(self, i: int, j: int):
|
||||
pattern = r'(?:.*resources\/)([^\/]+)'
|
||||
vm = self._vmsResourcesRelationships[i]['resourceList'][j][
|
||||
'resourceKey'
|
||||
]['name']
|
||||
host = self.urlList[i]
|
||||
match = re.findall(pattern, host)
|
||||
for key, value in self.allHosts.items():
|
||||
if match[0] == value:
|
||||
self.VMSHostsNames[vm] = key
|
||||
return self
|
||||
|
||||
def getVMSIdentifiers(self):
|
||||
self.VMSHostsNames = {}
|
||||
self.allVMS = OrderedDict()
|
||||
if self.vmType == 'list':
|
||||
self.countVMS = []
|
||||
self.countVMSFiltered = []
|
||||
for i in range(0, len(self._vmsResourcesRelationships)):
|
||||
counter = 0
|
||||
for j in range(
|
||||
0, len(self._vmsResourcesRelationships[i]['resourceList'])
|
||||
):
|
||||
if (
|
||||
self._vmsResourcesRelationships[i]['resourceList'][j][
|
||||
'resourceKey'
|
||||
]['resourceKindKey']
|
||||
) == 'VirtualMachine':
|
||||
counter += 1
|
||||
self._getHostInformation(i, j)
|
||||
self.countVMS.append(
|
||||
len(self._vmsResourcesRelationships[i]['resourceList'])
|
||||
)
|
||||
self.countVMSFiltered.append(counter)
|
||||
for i in range(0, len(self._vmsResourcesRelationships)):
|
||||
self.getIdentifiers(
|
||||
self.allVMS,
|
||||
self._vmsResourcesRelationships[i],
|
||||
self.countVMS[i],
|
||||
'VirtualMachine',
|
||||
)
|
||||
if self.vmType == 'string':
|
||||
counter = 0
|
||||
self.countVMS = len(
|
||||
self._vmsResourcesRelationships['resourceList']
|
||||
)
|
||||
for j in range(0, self.countVMS):
|
||||
if (
|
||||
self._vmsResourcesRelationships['resourceList'][j][
|
||||
'resourceKey'
|
||||
]['resourceKindKey']
|
||||
) == 'VirtualMachine':
|
||||
counter += 1
|
||||
self.countVMSFiltered = counter
|
||||
self.getIdentifiers(
|
||||
self.allVMS,
|
||||
self._vmsResourcesRelationships,
|
||||
self.countVMS,
|
||||
'VirtualMachine',
|
||||
)
|
||||
return self
|
||||
|
||||
def getStats(
|
||||
self,
|
||||
begin: int,
|
||||
end: int,
|
||||
intervalType: str,
|
||||
intervalQuantifier: str,
|
||||
rollUpType: str,
|
||||
resourceId: list,
|
||||
statKey: Union[str, list],
|
||||
vropsType: str,
|
||||
*args,
|
||||
**kwargs,
|
||||
):
|
||||
self.vropsType = vropsType
|
||||
argList = copy.deepcopy(locals())
|
||||
for i in ('self', 'vropsType'):
|
||||
del argList[i]
|
||||
vropsURL = self.getVropsURL(self.vropsURL, 'statsURL')
|
||||
payload = self.pythonToJSON(argList, indent=0)
|
||||
response = requests.request(
|
||||
'POST',
|
||||
vropsURL,
|
||||
headers=self.headers,
|
||||
data=payload,
|
||||
verify=self.verify,
|
||||
)
|
||||
self._vmsResources = OrderedDict(self.jsonToPython(response.text))
|
||||
if self.vropsType == 'virtualmachine':
|
||||
for key, value in self.allVMS.items():
|
||||
for i in range(0, len(self._vmsResources['values'])):
|
||||
if self._vmsResources['values'][i]['resourceId'] == value:
|
||||
self._vmsResources['values'][i] = OrderedDict(
|
||||
self._vmsResources['values'][i]
|
||||
)
|
||||
self._vmsResources['values'][i]['name'] = key
|
||||
self._vmsResources['values'][i][
|
||||
'host'
|
||||
] = self.VMSHostsNames[key]
|
||||
self._vmsResources['values'][i][
|
||||
'cluster'
|
||||
] = self.chosenCluster
|
||||
for item in ['cluster', 'host', 'name']:
|
||||
self._vmsResources['values'][i].move_to_end(
|
||||
item, last=False
|
||||
)
|
||||
if self.vropsType == 'host':
|
||||
for key, value in self.allHosts.items():
|
||||
for i in range(0, len(self._vmsResources['values'])):
|
||||
if self._vmsResources['values'][i]['resourceId'] == value:
|
||||
self._vmsResources['values'][i] = OrderedDict(
|
||||
self._vmsResources['values'][i]
|
||||
)
|
||||
self._vmsResources['values'][i]['name'] = key
|
||||
self._vmsResources['values'][i][
|
||||
'cluster'
|
||||
] = self.chosenCluster
|
||||
for item in ['cluster', 'name']:
|
||||
self._vmsResources['values'][i].move_to_end(
|
||||
item, last=False
|
||||
)
|
||||
|
||||
@deprecated(
|
||||
version='2.1',
|
||||
reason='this method is deprecated, use getStats() instead with the'
|
||||
' same functionality',
|
||||
)
|
||||
def getStatsFromVMS(
|
||||
self,
|
||||
begin: int,
|
||||
end: int,
|
||||
intervalType: str,
|
||||
intervalQuantifier: str,
|
||||
rollUpType: str,
|
||||
resourceId: list,
|
||||
statKey: Union[str, list],
|
||||
vropsType: str,
|
||||
):
|
||||
argList = copy.deepcopy(locals())
|
||||
del argList['self']
|
||||
self.getStats(**argList)
|
||||
return self
|
||||
|
||||
@staticmethod
|
||||
def saveToDisk(
|
||||
pythonObject: any,
|
||||
path: str = os.getcwd(),
|
||||
filePrefix: str = '',
|
||||
type: str = 'json',
|
||||
indent: int = 4,
|
||||
breakLine: bool = False,
|
||||
) -> None:
|
||||
timeNow = datetime.now().strftime('%d-%m-%Y_%H-%M-%S')
|
||||
fileName = f'{path}/{filePrefix}-{timeNow}.json'
|
||||
if breakLine:
|
||||
if not isinstance(pythonObject, list):
|
||||
raise TypeError(
|
||||
'You must pass a list when using' ' breakLine=True'
|
||||
)
|
||||
else:
|
||||
with open(fileName, 'a+') as outputFile:
|
||||
for item in pythonObject:
|
||||
try:
|
||||
outputFile.write(
|
||||
json.dump(
|
||||
json.loads(item), outputFile, indent=indent
|
||||
)
|
||||
)
|
||||
except TypeError:
|
||||
pass
|
||||
outputFile.write('\n')
|
||||
else:
|
||||
with open(fileName, 'w+') as outputFile:
|
||||
json.dump(pythonObject, outputFile, indent=indent)
|
||||
|
||||
def exportVMData(self):
|
||||
self.export = []
|
||||
loopLength = len(self._vmsResources['values'])
|
||||
for i in range(0, loopLength):
|
||||
statKeyLength = len(
|
||||
self._vmsResources['values'][i]['stat-list']['stat']
|
||||
)
|
||||
timeLength = len(
|
||||
self._vmsResources['values'][i]['stat-list']['stat'][0][
|
||||
'timestamps'
|
||||
]
|
||||
)
|
||||
for k in range(0, statKeyLength):
|
||||
for j in range(0, timeLength):
|
||||
if self.vropsType == 'virtualmachine':
|
||||
self.export.append(
|
||||
json.dumps(
|
||||
{
|
||||
'type': self.vropsType,
|
||||
'name': self._vmsResources['values'][i][
|
||||
'name'
|
||||
],
|
||||
'host': self._vmsResources['values'][i][
|
||||
'host'
|
||||
],
|
||||
'cluster': self.chosenCluster,
|
||||
'timestamp': self._vmsResources['values'][
|
||||
i
|
||||
]['stat-list']['stat'][0]['timestamps'][j],
|
||||
'value': self._vmsResources['values'][i][
|
||||
'stat-list'
|
||||
]['stat'][k]['data'][j],
|
||||
'statKey': self._vmsResources['values'][i][
|
||||
'stat-list'
|
||||
]['stat'][k]['statKey']['key'],
|
||||
'rollUpType': self._vmsResources['values'][
|
||||
i
|
||||
]['stat-list']['stat'][k]['rollUpType'],
|
||||
'intervalQuantifier': self._vmsResources[
|
||||
'values'
|
||||
][i]['stat-list']['stat'][k][
|
||||
'intervalUnit'
|
||||
][
|
||||
'quantifier'
|
||||
],
|
||||
'intervalType': self._vmsResources[
|
||||
'values'
|
||||
][i]['stat-list']['stat'][0][
|
||||
'intervalUnit'
|
||||
][
|
||||
'intervalType'
|
||||
],
|
||||
}
|
||||
)
|
||||
)
|
||||
elif self.vropsType == 'host':
|
||||
self.export.append(
|
||||
json.dumps(
|
||||
{
|
||||
'type': self.vropsType,
|
||||
'name': self._vmsResources['values'][i][
|
||||
'name'
|
||||
],
|
||||
'cluster': self.chosenCluster,
|
||||
'timestamp': self._vmsResources['values'][
|
||||
i
|
||||
]['stat-list']['stat'][0]['timestamps'][j],
|
||||
'value': self._vmsResources['values'][i][
|
||||
'stat-list'
|
||||
]['stat'][k]['data'][j],
|
||||
'statKey': self._vmsResources['values'][i][
|
||||
'stat-list'
|
||||
]['stat'][k]['statKey']['key'],
|
||||
'rollUpType': self._vmsResources['values'][
|
||||
i
|
||||
]['stat-list']['stat'][k]['rollUpType'],
|
||||
'intervalQuantifier': self._vmsResources[
|
||||
'values'
|
||||
][i]['stat-list']['stat'][k][
|
||||
'intervalUnit'
|
||||
][
|
||||
'quantifier'
|
||||
],
|
||||
'intervalType': self._vmsResources[
|
||||
'values'
|
||||
][i]['stat-list']['stat'][0][
|
||||
'intervalUnit'
|
||||
][
|
||||
'intervalType'
|
||||
],
|
||||
}
|
||||
)
|
||||
)
|
||||
return self
|
||||
|
||||
@property
|
||||
def totalVMS(self):
|
||||
if isinstance(self.countVMSFiltered, list):
|
||||
self.__totalVMS = sum(self.countVMSFiltered)
|
||||
elif isinstance(self.countVMSFiltered, int):
|
||||
self.__totalVMS = self.countVMSFiltered
|
||||
return self.__totalVMS
|
||||
|
||||
@totalVMS.setter
|
||||
def totalVMS(self, totalVMS):
|
||||
self.__totalVMS = totalVMS
|
||||
return self.__totalVMS
|
||||
|
||||
# @property
|
||||
# def totalHosts(self):
|
||||
# if isinstance(self.countVMSFiltered, list):
|
||||
# self.__totalVMS = sum(self.countVMSFiltered)
|
||||
# elif isinstance(self.countVMSFiltered, int):
|
||||
# self.__totalVMS = self.countVMSFiltered
|
||||
# return self.__totalVMS
|
||||
|
||||
# @totalHosts.setter
|
||||
# def totalHosts(self, totalHosts):
|
||||
# self.__totalHosts = totalHosts
|
||||
# return self.__totalHosts
|
||||
|
||||
@property
|
||||
def epochNow(self):
|
||||
self.__epochNow = math.ceil(time.time() * 1000)
|
||||
return self.__epochNow
|
||||
|
||||
@property
|
||||
def epochToday(self):
|
||||
now = datetime.now()
|
||||
self.__epochtoday = now + relativedelta(hour=0, minute=0, second=0)
|
||||
self.__epochToday = math.ceil(time.time() * 1000)
|
||||
return self.__epochToday
|
||||
|
||||
@property
|
||||
def allVMS(self):
|
||||
return self.__allVMS
|
||||
|
||||
@allVMS.setter
|
||||
def allVMS(self, allVMS):
|
||||
if not isinstance(allVMS, dict):
|
||||
raise TypeError(
|
||||
'You must pass a dictionary with a key of the name'
|
||||
f' and a value of the VROPS ID, not {type(allVMS)}.'
|
||||
)
|
||||
else:
|
||||
pass
|
||||
self.__allVMS = allVMS
|
||||
print('Successfully imported the dictionary.')
|
||||
return self.__allVMS
|
||||
|
||||
@property
|
||||
def VMSHostsNames(self):
|
||||
return self.__VMSHostsNames
|
||||
|
||||
@VMSHostsNames.setter
|
||||
def VMSHostsNames(self, VMSHostsNames):
|
||||
if not isinstance(VMSHostsNames, dict):
|
||||
raise TypeError(
|
||||
'You must pass a dictionary with a key of the name'
|
||||
f' and a value of the VROPS ID, not {type(VMSHostsNames)}.'
|
||||
)
|
||||
else:
|
||||
pass
|
||||
self.__VMSHostsNames = VMSHostsNames
|
||||
print('Successfully imported the dictionary.')
|
||||
return self.__VMSHostsNames
|
||||
|
||||
@property
|
||||
def chosenCluster(self):
|
||||
return self.__chosenCluster
|
||||
|
||||
@chosenCluster.setter
|
||||
def chosenCluster(self, chosenCluster):
|
||||
if not isinstance(chosenCluster, str):
|
||||
raise TypeError(
|
||||
'You must pass a dictionary with a key of the name'
|
||||
f' and a value of the VROPS ID, not {type(chosenCluster)}.'
|
||||
)
|
||||
else:
|
||||
pass
|
||||
self.__chosenCluster = chosenCluster
|
||||
return self.__chosenCluster
|
||||
767
vrops-api/__dev/vropsAPI.py
Normal file
767
vrops-api/__dev/vropsAPI.py
Normal file
@@ -0,0 +1,767 @@
|
||||
import requests
|
||||
import urllib3
|
||||
import json
|
||||
from typing import Union
|
||||
import copy
|
||||
from datetime import datetime
|
||||
from dateutil.relativedelta import relativedelta
|
||||
import os
|
||||
import math
|
||||
import time
|
||||
from collections import OrderedDict
|
||||
import re
|
||||
from deprecated import deprecated
|
||||
|
||||
# warnings.filterwarnings('ignore')
|
||||
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
|
||||
|
||||
|
||||
class vropsAPI(object):
|
||||
"""Module for the vropsAPI for Capacity Management:
|
||||
Author: Daniel Tomlinson
|
||||
Team: Capacity & Monitoring
|
||||
Date: October 2019
|
||||
|
||||
VROPS documentation: https://sc1prapvro01/suite-api/docs/rest/
|
||||
index.html
|
||||
StatKeys for VMS:
|
||||
https://docs.vmware.com/en/vRealize-Operations-Manager/6.7/com.vmware.vcom.metrics.doc/GUID-1322F5A4-DA1D-481F-BBEA-99B228E96AF2.html
|
||||
|
||||
Usage
|
||||
-----
|
||||
Authenticate a session:
|
||||
vrops = vropsAPI.authenticate(
|
||||
'https://sc1prapvro01/',
|
||||
'username',
|
||||
'authSource',
|
||||
'password',
|
||||
verify=False,
|
||||
)
|
||||
|
||||
|
||||
Get all clusters:
|
||||
Query VROPS for available clusters:
|
||||
vrops.getClusters()
|
||||
vrops.getClusterIdentifiers()
|
||||
|
||||
Get dict of all cluster IDs and cluster names:
|
||||
allClustersDict = vrops.allClusters
|
||||
|
||||
Get list of all cluster names:
|
||||
allClustersList = vrops.getList(vrops.allClusters)
|
||||
|
||||
|
||||
Get all hosts:
|
||||
Query VROPS for available hosts:
|
||||
From a single cluster:
|
||||
vrops.getHostsFromCluster(cluster='SC1PRCONTXWHCUXCCL01')
|
||||
vrops.getHostIdentifiers()
|
||||
From a list of clusters:
|
||||
<Not implemented>
|
||||
|
||||
Get dict of all host IDs and host names:
|
||||
allHostsDisct =vrops.allHosts
|
||||
|
||||
Get list of host names:
|
||||
allHostsList = vrops.getList(vrops.allHosts)
|
||||
|
||||
|
||||
Get all VMs:
|
||||
Query VROPS for available VMs:
|
||||
For a single host:
|
||||
vrops.getVMSFromHost('sc1hsesx148.prod.williamhill.plc')
|
||||
vrops.getVMSIdentifiers()
|
||||
|
||||
For a list of hosts:
|
||||
vrops.getVMSFromHost(allHostsList)
|
||||
vrops.getVMSIdentifiers()
|
||||
|
||||
Get dict of all VM IDs and VM names:
|
||||
allVMSDict = vrops.allVMS
|
||||
|
||||
Get list of all VMs:
|
||||
allVMSList = vrops.getList(vrops.allVMS)
|
||||
|
||||
|
||||
Get epoch time relative to another time:
|
||||
Similar to Splunks relative_time command:
|
||||
1. Can go back N hours/minutes etc.
|
||||
2. Can set the hour/minute etc. to a specified value (snapping)
|
||||
|
||||
vrops.epochRelativeTime(epochTime, **kwargs)
|
||||
|
||||
**kwargs:
|
||||
epochTime: int - start time
|
||||
|
||||
year: int = datetime.now().year # set year
|
||||
month: int = datetime.now().month # set month
|
||||
day: int = datetime.now().day # set day
|
||||
hour: int = datetime.now().hour # set hour
|
||||
minute: int = datetime.now().minute # set minute
|
||||
second: int = datetime.now().second # set second
|
||||
|
||||
years: int = 0 # go back/forward N years
|
||||
months: int = 0 # go back/forward N months
|
||||
days: int = 0 # go back/forward N days
|
||||
hours: int = 0 # go back/forward N hours
|
||||
minutes: int = 0 # go back/forward N minutes
|
||||
seconds: int = 0 # go back/forward N seconds
|
||||
Usage:
|
||||
|
||||
Get epoch 5 minutes ago:
|
||||
vrops.epochRelativeTime(vrops.epochNow, minutes=-5)
|
||||
|
||||
Get epoch at start of current hour:
|
||||
vrops.epochRelativeTime(
|
||||
vrops.epochNow,
|
||||
hour=0,
|
||||
minute=0,
|
||||
second=0,
|
||||
)
|
||||
|
||||
Get epoch 1 week ago at start of day:
|
||||
vrops.epochRelativeTime(
|
||||
vrops.epochNow,
|
||||
days=-7
|
||||
hour=0,
|
||||
minute=0,
|
||||
second=0,
|
||||
)
|
||||
|
||||
|
||||
Get stats from VMs:
|
||||
Pull back results:
|
||||
Last 30 minutes, 5 minute intervals, average
|
||||
for CPU average and ready %:
|
||||
|
||||
vrops.getStatsFromVMS(
|
||||
begin=vrops.epochRelativeTime(vrops.epochNow, minutes=-30),
|
||||
end=vrops.epochNow,
|
||||
intervalType='MINUTES',
|
||||
intervalQuantifier='5',
|
||||
rollUpType='AVG',
|
||||
resourceId=list(vrops.allVMS.values()),
|
||||
statKey=['cpu|usage_average', 'cpu|readyPct'],
|
||||
)
|
||||
|
||||
Attributes:
|
||||
intervalType: <ns3:intervalType> (see
|
||||
https://sc1prapvro01/suite-api/docs/rest/
|
||||
models.html#repr-1190589417)
|
||||
|
||||
intervalQuantifier: int
|
||||
|
||||
rollUpType: <ns3:rollUpType> (see:
|
||||
https://sc1prapvro01/suite-api/
|
||||
docs/rest/models.html#repr-1735704374)
|
||||
|
||||
resourceId: string or list of vrops resourceIds (not names)
|
||||
|
||||
statKey: vrops api metrics (see https://docs.vmware.com/en/
|
||||
vRealize-Operations-Manager/6.7/com.vmware.vcom.metrics.doc/
|
||||
GUID-1322F5A4-DA1D-481F-BBEA-99B228E96AF2.html)
|
||||
|
||||
|
||||
Print results:
|
||||
for i in range(0, vrops.totalVMS):
|
||||
print(vrops.vmsResources['values'][i])
|
||||
|
||||
Save to disk as json:
|
||||
vrops.saveToDisk(vrops.vmsResources)
|
||||
|
||||
|
||||
Attributes
|
||||
----------
|
||||
vropsURL: str
|
||||
URL of the VROPS instance
|
||||
"https://sc1prapvro01/"
|
||||
|
||||
"""
|
||||
|
||||
defaultHeaders = {
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
'Cache-Control': 'no-cache',
|
||||
'Accept-Encoding': 'gzip, deflate',
|
||||
'Connection': 'keep-alive',
|
||||
'cache-control': 'no-cache',
|
||||
}
|
||||
|
||||
def __init__(self, vropsURL: str, authToken: str, verify: bool = True):
|
||||
super(vropsAPI, self).__init__()
|
||||
self.vropsURL = vropsURL
|
||||
self.authToken = authToken
|
||||
self.verify = verify
|
||||
self.headers = vropsAPI.defaultHeaders
|
||||
self.headers['Authorization'] = f'vRealizeOpsToken {self.authToken}'
|
||||
|
||||
@classmethod
|
||||
def authenticate(
|
||||
cls,
|
||||
vropsURL: str,
|
||||
username: str,
|
||||
authSource: str,
|
||||
password: str,
|
||||
verify: bool = True,
|
||||
):
|
||||
vropsURLauth = vropsAPI.getVropsURL(vropsURL, 'authenticationURL')
|
||||
payload = {}
|
||||
for key, value in zip(
|
||||
['username', 'authSource', 'password'],
|
||||
[username, authSource, password],
|
||||
):
|
||||
payload[key] = value
|
||||
authToken = vropsAPI.getAuthenticationToken(
|
||||
vropsURLauth, payload, verify
|
||||
)
|
||||
return cls(vropsURL, authToken, verify)
|
||||
|
||||
@staticmethod
|
||||
def getVropsURL(vropsURL: str, endpointKey: str) -> str:
|
||||
endpoints = {
|
||||
'authenticationURL': 'suite-api/api/auth/token/acquire',
|
||||
'resourcesURL': 'suite-api/api/resources',
|
||||
'statsURL': 'suite-api/api/resources/stats/query',
|
||||
}
|
||||
if endpoints[endpointKey] not in vropsURL:
|
||||
if vropsURL[-1] != '/':
|
||||
vropsURL = vropsURL + '/'
|
||||
vropsURL = vropsURL + endpoints[endpointKey]
|
||||
else:
|
||||
vropsURL = vropsURL + endpoints[endpointKey]
|
||||
return vropsURL
|
||||
|
||||
@staticmethod
|
||||
def pythonToJSON(pythonObject: any, indent=4) -> str:
|
||||
return json.dumps(pythonObject, indent=indent)
|
||||
|
||||
@staticmethod
|
||||
def jsonToPython(jsonObject: str) -> any:
|
||||
return json.loads(jsonObject)
|
||||
|
||||
@staticmethod
|
||||
def getAuthenticationToken(
|
||||
vropsURL: str, payload: dict, verify=True
|
||||
) -> str:
|
||||
|
||||
payload = vropsAPI.pythonToJSON(payload)
|
||||
vropsURL = vropsAPI.getVropsURL(vropsURL, 'authenticationURL')
|
||||
response = requests.request(
|
||||
'POST',
|
||||
vropsURL,
|
||||
data=payload,
|
||||
headers=vropsAPI.defaultHeaders,
|
||||
verify=verify,
|
||||
)
|
||||
print(response)
|
||||
authToken = vropsAPI.jsonToPython(response.text)['token']
|
||||
return authToken
|
||||
|
||||
@staticmethod
|
||||
def getIdentifiers(
|
||||
identifierDict: dict,
|
||||
vropsJSON: dict,
|
||||
length: int,
|
||||
resourceKindKey: str,
|
||||
) -> dict:
|
||||
for i in range(0, length):
|
||||
if (
|
||||
vropsJSON['resourceList'][i]['resourceKey']['resourceKindKey']
|
||||
== resourceKindKey
|
||||
):
|
||||
identifierDict[
|
||||
vropsJSON['resourceList'][i]['resourceKey']['name']
|
||||
] = vropsJSON['resourceList'][i]['identifier']
|
||||
else:
|
||||
pass
|
||||
return identifierDict
|
||||
|
||||
@staticmethod
|
||||
def getKeysList(pythonDict: dict) -> list:
|
||||
pythonList = []
|
||||
for i in pythonDict.keys():
|
||||
pythonList.append(i)
|
||||
return pythonList
|
||||
|
||||
@staticmethod
|
||||
def getValuesList(pythonDict: dict) -> list:
|
||||
pythonList = []
|
||||
for i in pythonDict.values():
|
||||
pythonList.append(i)
|
||||
return pythonList
|
||||
|
||||
@staticmethod
|
||||
def epochRelativeTime(
|
||||
epochTime: int,
|
||||
year: int = datetime.now().year,
|
||||
month: int = datetime.now().month,
|
||||
day: int = datetime.now().day,
|
||||
hour: int = datetime.now().hour,
|
||||
minute: int = datetime.now().minute,
|
||||
second: int = datetime.now().second,
|
||||
years: int = 0,
|
||||
months: int = 0,
|
||||
days: int = 0,
|
||||
hours: int = 0,
|
||||
minutes: int = 0,
|
||||
seconds: int = 0,
|
||||
) -> int:
|
||||
delta = relativedelta(
|
||||
year=year,
|
||||
month=month,
|
||||
day=day,
|
||||
hour=hour,
|
||||
minute=minute,
|
||||
second=second,
|
||||
years=years,
|
||||
months=months,
|
||||
days=days,
|
||||
hours=hours,
|
||||
minutes=minutes,
|
||||
seconds=seconds,
|
||||
)
|
||||
if len(str(epochTime)) >= 12:
|
||||
epochTime /= 1000
|
||||
relativeTime = datetime.fromtimestamp(epochTime) + delta
|
||||
relativeTime = math.ceil(relativeTime.timestamp() * 1000)
|
||||
return relativeTime
|
||||
|
||||
def getClusters(self):
|
||||
queryString = {'resourceKind': 'ClusterComputeResource'}
|
||||
vropsURL = vropsAPI.getVropsURL(self.vropsURL, 'resourcesURL')
|
||||
response = requests.request(
|
||||
"GET",
|
||||
vropsURL,
|
||||
headers=self.headers,
|
||||
params=queryString,
|
||||
verify=self.verify,
|
||||
)
|
||||
print(response)
|
||||
self._clusterResources = vropsAPI.jsonToPython(response.text)
|
||||
return self
|
||||
|
||||
def getClusterIdentifiers(self):
|
||||
self.totalClusters = len(self._clusterResources['resourceList'])
|
||||
self.allClusters = {}
|
||||
self.getIdentifiers(
|
||||
self.allClusters,
|
||||
self._clusterResources,
|
||||
self.totalClusters,
|
||||
'ClusterComputeResource',
|
||||
)
|
||||
return self
|
||||
|
||||
def getHostsFromCluster(self, cluster='SC1PRCONTXWHCUXCCL01'):
|
||||
self.cluster = cluster
|
||||
url = (
|
||||
f'{self.vropsURL}/suite-api/api/resources/'
|
||||
f'{self.allClusters[cluster]}/relationships'
|
||||
)
|
||||
self.chosenCluster = cluster
|
||||
response = requests.request(
|
||||
"GET", url, headers=self.headers, verify=self.verify
|
||||
)
|
||||
print(response)
|
||||
self._hostResources = vropsAPI.jsonToPython(response.text)
|
||||
return self
|
||||
|
||||
def getHostIdentifiers(self):
|
||||
self.totalHosts = len(self._hostResources['resourceList'])
|
||||
self.allHosts = {}
|
||||
self.getIdentifiers(
|
||||
self.allHosts, self._hostResources, self.totalHosts, 'HostSystem'
|
||||
)
|
||||
return self
|
||||
|
||||
def getVMSFromHost(self, host: Union[str, list]):
|
||||
if isinstance(host, list):
|
||||
self.vmType = 'list'
|
||||
self._vmsResourcesRelationships = []
|
||||
self.urlList = []
|
||||
response = []
|
||||
for item in host:
|
||||
self.urlList.append(
|
||||
(
|
||||
f'{self.vropsURL}suite-api/api/resources/'
|
||||
f'{self.allHosts[item]}/relationships'
|
||||
)
|
||||
)
|
||||
for urlItem in self.urlList:
|
||||
response.append(
|
||||
requests.request(
|
||||
'GET',
|
||||
urlItem,
|
||||
headers=self.headers,
|
||||
verify=self.verify,
|
||||
)
|
||||
)
|
||||
print(response)
|
||||
for i in range(0, len(response)):
|
||||
self._vmsResourcesRelationships.append(
|
||||
vropsAPI.jsonToPython(response[i].text)
|
||||
)
|
||||
if isinstance(host, str):
|
||||
hostToList = []
|
||||
hostToList.append(host)
|
||||
print(hostToList)
|
||||
return self.getVMSFromHost(host=hostToList)
|
||||
return self
|
||||
|
||||
def _getHostInformation(self, i: int, j: int):
|
||||
pattern = r'(?:.*resources\/)([^\/]+)'
|
||||
vm = self._vmsResourcesRelationships[i]['resourceList'][j][
|
||||
'resourceKey'
|
||||
]['name']
|
||||
host = self.urlList[i]
|
||||
match = re.findall(pattern, host)
|
||||
for key, value in self.allHosts.items():
|
||||
if match[0] == value:
|
||||
self.VMSHostsNames[vm] = key
|
||||
return self
|
||||
|
||||
def getVMSIdentifiers(self):
|
||||
self.VMSHostsNames = {}
|
||||
self.allVMS = OrderedDict()
|
||||
if self.vmType == 'list':
|
||||
self.countVMS = []
|
||||
self.countVMSFiltered = []
|
||||
for i in range(0, len(self._vmsResourcesRelationships)):
|
||||
counter = 0
|
||||
for j in range(
|
||||
0, len(self._vmsResourcesRelationships[i]['resourceList'])
|
||||
):
|
||||
if (
|
||||
self._vmsResourcesRelationships[i]['resourceList'][j][
|
||||
'resourceKey'
|
||||
]['resourceKindKey']
|
||||
) == 'VirtualMachine':
|
||||
counter += 1
|
||||
self._getHostInformation(i, j)
|
||||
self.countVMS.append(
|
||||
len(self._vmsResourcesRelationships[i]['resourceList'])
|
||||
)
|
||||
self.countVMSFiltered.append(counter)
|
||||
for i in range(0, len(self._vmsResourcesRelationships)):
|
||||
self.getIdentifiers(
|
||||
self.allVMS,
|
||||
self._vmsResourcesRelationships[i],
|
||||
self.countVMS[i],
|
||||
'VirtualMachine',
|
||||
)
|
||||
if self.vmType == 'string':
|
||||
counter = 0
|
||||
self.countVMS = len(
|
||||
self._vmsResourcesRelationships['resourceList']
|
||||
)
|
||||
for j in range(0, self.countVMS):
|
||||
if (
|
||||
self._vmsResourcesRelationships['resourceList'][j][
|
||||
'resourceKey'
|
||||
]['resourceKindKey']
|
||||
) == 'VirtualMachine':
|
||||
counter += 1
|
||||
self.countVMSFiltered = counter
|
||||
self.getIdentifiers(
|
||||
self.allVMS,
|
||||
self._vmsResourcesRelationships,
|
||||
self.countVMS,
|
||||
'VirtualMachine',
|
||||
)
|
||||
return self
|
||||
|
||||
def getStats(
|
||||
self,
|
||||
begin: int,
|
||||
end: int,
|
||||
intervalType: str,
|
||||
intervalQuantifier: str,
|
||||
rollUpType: str,
|
||||
resourceId: list,
|
||||
statKey: Union[str, list],
|
||||
vropsType: str,
|
||||
*args,
|
||||
**kwargs,
|
||||
):
|
||||
self.vropsType = vropsType
|
||||
argList = copy.deepcopy(locals())
|
||||
for i in ('self', 'vropsType'):
|
||||
del argList[i]
|
||||
vropsURL = self.getVropsURL(self.vropsURL, 'statsURL')
|
||||
payload = self.pythonToJSON(argList, indent=0)
|
||||
response = requests.request(
|
||||
'POST',
|
||||
vropsURL,
|
||||
headers=self.headers,
|
||||
data=payload,
|
||||
verify=self.verify,
|
||||
)
|
||||
self._vmsResources = OrderedDict(self.jsonToPython(response.text))
|
||||
if self.vropsType == 'virtualmachine':
|
||||
for key, value in self.allVMS.items():
|
||||
for i in range(0, len(self._vmsResources['values'])):
|
||||
if self._vmsResources['values'][i]['resourceId'] == value:
|
||||
self._vmsResources['values'][i] = OrderedDict(
|
||||
self._vmsResources['values'][i]
|
||||
)
|
||||
self._vmsResources['values'][i]['name'] = key
|
||||
self._vmsResources['values'][i][
|
||||
'host'
|
||||
] = self.VMSHostsNames[key]
|
||||
self._vmsResources['values'][i][
|
||||
'cluster'
|
||||
] = self.chosenCluster
|
||||
for item in ['cluster', 'host', 'name']:
|
||||
self._vmsResources['values'][i].move_to_end(
|
||||
item, last=False
|
||||
)
|
||||
if self.vropsType == 'host':
|
||||
for key, value in self.allHosts.items():
|
||||
for i in range(0, len(self._vmsResources['values'])):
|
||||
if self._vmsResources['values'][i]['resourceId'] == value:
|
||||
self._vmsResources['values'][i] = OrderedDict(
|
||||
self._vmsResources['values'][i]
|
||||
)
|
||||
self._vmsResources['values'][i]['name'] = key
|
||||
self._vmsResources['values'][i][
|
||||
'cluster'
|
||||
] = self.chosenCluster
|
||||
for item in ['cluster', 'name']:
|
||||
self._vmsResources['values'][i].move_to_end(
|
||||
item, last=False
|
||||
)
|
||||
|
||||
@deprecated(
|
||||
version='2.1',
|
||||
reason='this method is deprecated, use getStats() instead with the'
|
||||
' same functionality',
|
||||
)
|
||||
def getStatsFromVMS(
|
||||
self,
|
||||
begin: int,
|
||||
end: int,
|
||||
intervalType: str,
|
||||
intervalQuantifier: str,
|
||||
rollUpType: str,
|
||||
resourceId: list,
|
||||
statKey: Union[str, list],
|
||||
vropsType: str,
|
||||
):
|
||||
argList = copy.deepcopy(locals())
|
||||
del argList['self']
|
||||
self.getStats(**argList)
|
||||
return self
|
||||
|
||||
@staticmethod
|
||||
def saveToDisk(
|
||||
pythonObject: any,
|
||||
path: str = os.getcwd(),
|
||||
filePrefix: str = '',
|
||||
type: str = 'json',
|
||||
indent: int = 4,
|
||||
breakLine: bool = False,
|
||||
) -> None:
|
||||
timeNow = datetime.now().strftime('%d-%m-%Y_%H-%M-%S')
|
||||
fileName = f'{path}/{filePrefix}-{timeNow}.json'
|
||||
if breakLine:
|
||||
if not isinstance(pythonObject, list):
|
||||
raise TypeError(
|
||||
'You must pass a list when using' ' breakLine=True'
|
||||
)
|
||||
else:
|
||||
with open(fileName, 'a+') as outputFile:
|
||||
for item in pythonObject:
|
||||
try:
|
||||
outputFile.write(
|
||||
json.dump(
|
||||
json.loads(item), outputFile, indent=indent
|
||||
)
|
||||
)
|
||||
except TypeError:
|
||||
pass
|
||||
outputFile.write('\n')
|
||||
else:
|
||||
with open(fileName, 'w+') as outputFile:
|
||||
json.dump(pythonObject, outputFile, indent=indent)
|
||||
|
||||
def exportVMData(self):
|
||||
self.export = []
|
||||
loopLength = len(self._vmsResources['values'])
|
||||
for i in range(0, loopLength):
|
||||
statKeyLength = len(
|
||||
self._vmsResources['values'][i]['stat-list']['stat']
|
||||
)
|
||||
timeLength = len(
|
||||
self._vmsResources['values'][i]['stat-list']['stat'][0][
|
||||
'timestamps'
|
||||
]
|
||||
)
|
||||
for k in range(0, statKeyLength):
|
||||
for j in range(0, timeLength):
|
||||
if self.vropsType == 'virtualmachine':
|
||||
self.export.append(
|
||||
json.dumps(
|
||||
{
|
||||
'type': self.vropsType,
|
||||
'name': self._vmsResources['values'][i][
|
||||
'name'
|
||||
],
|
||||
'host': self._vmsResources['values'][i][
|
||||
'host'
|
||||
],
|
||||
'cluster': self.chosenCluster,
|
||||
'timestamp': self._vmsResources['values'][
|
||||
i
|
||||
]['stat-list']['stat'][0]['timestamps'][j],
|
||||
'value': self._vmsResources['values'][i][
|
||||
'stat-list'
|
||||
]['stat'][k]['data'][j],
|
||||
'statKey': self._vmsResources['values'][i][
|
||||
'stat-list'
|
||||
]['stat'][k]['statKey']['key'],
|
||||
'rollUpType': self._vmsResources['values'][
|
||||
i
|
||||
]['stat-list']['stat'][k]['rollUpType'],
|
||||
'intervalQuantifier': self._vmsResources[
|
||||
'values'
|
||||
][i]['stat-list']['stat'][k][
|
||||
'intervalUnit'
|
||||
][
|
||||
'quantifier'
|
||||
],
|
||||
'intervalType': self._vmsResources[
|
||||
'values'
|
||||
][i]['stat-list']['stat'][0][
|
||||
'intervalUnit'
|
||||
][
|
||||
'intervalType'
|
||||
],
|
||||
}
|
||||
)
|
||||
)
|
||||
elif self.vropsType == 'host':
|
||||
self.export.append(
|
||||
json.dumps(
|
||||
{
|
||||
'type': self.vropsType,
|
||||
'name': self._vmsResources['values'][i][
|
||||
'name'
|
||||
],
|
||||
'cluster': self.chosenCluster,
|
||||
'timestamp': self._vmsResources['values'][
|
||||
i
|
||||
]['stat-list']['stat'][0]['timestamps'][j],
|
||||
'value': self._vmsResources['values'][i][
|
||||
'stat-list'
|
||||
]['stat'][k]['data'][j],
|
||||
'statKey': self._vmsResources['values'][i][
|
||||
'stat-list'
|
||||
]['stat'][k]['statKey']['key'],
|
||||
'rollUpType': self._vmsResources['values'][
|
||||
i
|
||||
]['stat-list']['stat'][k]['rollUpType'],
|
||||
'intervalQuantifier': self._vmsResources[
|
||||
'values'
|
||||
][i]['stat-list']['stat'][k][
|
||||
'intervalUnit'
|
||||
][
|
||||
'quantifier'
|
||||
],
|
||||
'intervalType': self._vmsResources[
|
||||
'values'
|
||||
][i]['stat-list']['stat'][0][
|
||||
'intervalUnit'
|
||||
][
|
||||
'intervalType'
|
||||
],
|
||||
}
|
||||
)
|
||||
)
|
||||
return self
|
||||
|
||||
@property
|
||||
def totalVMS(self):
|
||||
if isinstance(self.countVMSFiltered, list):
|
||||
self.__totalVMS = sum(self.countVMSFiltered)
|
||||
elif isinstance(self.countVMSFiltered, int):
|
||||
self.__totalVMS = self.countVMSFiltered
|
||||
return self.__totalVMS
|
||||
|
||||
@totalVMS.setter
|
||||
def totalVMS(self, totalVMS):
|
||||
self.__totalVMS = totalVMS
|
||||
return self.__totalVMS
|
||||
|
||||
# @property
|
||||
# def totalHosts(self):
|
||||
# if isinstance(self.countVMSFiltered, list):
|
||||
# self.__totalVMS = sum(self.countVMSFiltered)
|
||||
# elif isinstance(self.countVMSFiltered, int):
|
||||
# self.__totalVMS = self.countVMSFiltered
|
||||
# return self.__totalVMS
|
||||
|
||||
# @totalHosts.setter
|
||||
# def totalHosts(self, totalHosts):
|
||||
# self.__totalHosts = totalHosts
|
||||
# return self.__totalHosts
|
||||
|
||||
@property
|
||||
def epochNow(self):
|
||||
self.__epochNow = math.ceil(time.time() * 1000)
|
||||
return self.__epochNow
|
||||
|
||||
@property
|
||||
def epochToday(self):
|
||||
now = datetime.now()
|
||||
self.__epochtoday = now + relativedelta(hour=0, minute=0, second=0)
|
||||
self.__epochToday = math.ceil(time.time() * 1000)
|
||||
return self.__epochToday
|
||||
|
||||
@property
|
||||
def allVMS(self):
|
||||
return self.__allVMS
|
||||
|
||||
@allVMS.setter
|
||||
def allVMS(self, allVMS):
|
||||
if not isinstance(allVMS, dict):
|
||||
raise TypeError(
|
||||
'You must pass a dictionary with a key of the name'
|
||||
f' and a value of the VROPS ID, not {type(allVMS)}.'
|
||||
)
|
||||
else:
|
||||
pass
|
||||
self.__allVMS = allVMS
|
||||
print('Successfully imported the dictionary.')
|
||||
return self.__allVMS
|
||||
|
||||
@property
|
||||
def VMSHostsNames(self):
|
||||
return self.__VMSHostsNames
|
||||
|
||||
@VMSHostsNames.setter
|
||||
def VMSHostsNames(self, VMSHostsNames):
|
||||
if not isinstance(VMSHostsNames, dict):
|
||||
raise TypeError(
|
||||
'You must pass a dictionary with a key of the name'
|
||||
f' and a value of the VROPS ID, not {type(VMSHostsNames)}.'
|
||||
)
|
||||
else:
|
||||
pass
|
||||
self.__VMSHostsNames = VMSHostsNames
|
||||
print('Successfully imported the dictionary.')
|
||||
return self.__VMSHostsNames
|
||||
|
||||
@property
|
||||
def chosenCluster(self):
|
||||
return self.__chosenCluster
|
||||
|
||||
@chosenCluster.setter
|
||||
def chosenCluster(self, chosenCluster):
|
||||
if not isinstance(chosenCluster, str):
|
||||
raise TypeError(
|
||||
'You must pass a dictionary with a key of the name'
|
||||
f' and a value of the VROPS ID, not {type(chosenCluster)}.'
|
||||
)
|
||||
else:
|
||||
pass
|
||||
self.__chosenCluster = chosenCluster
|
||||
return self.__chosenCluster
|
||||
Reference in New Issue
Block a user