adding projects to repo

This commit is contained in:
dtomlinson
2019-11-04 14:38:31 +00:00
parent 6728b1ecf3
commit a285fad1ee
65 changed files with 171244 additions and 1 deletions

BIN
vrops-api/.DS_Store vendored Normal file

Binary file not shown.

4
vrops-api/.gitignore vendored Executable file
View File

@@ -0,0 +1,4 @@
*.json
!approach1-output-example.json
!approach1-vms-example.json
!approach2-output-example.json

Binary file not shown.

View 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
View 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&amp;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

View 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',
)

View 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)

View 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
)

View 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)

View 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)

View 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']
}),
))

View 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&amp;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)

View 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
# )

View 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']]
"""

View 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
View 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

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,21 @@
Documentation:
✔ Finish documentating attributes @created(19-10-20 07:06) @done (19-10-21 14:49)
✔ Change - to : in documentation for **kwargs @created(19-10-20 07:15) @done (19-10-20 20:53)
✔ Document how it can be ran on a schedule, without having to query the hosts every time @created(19-10-21 03:44) @done (19-10-25 03:03)
✔ Tidy up documentation - with flow and clear examples on what to use and when and why. @created(19-10-21 14:49) @done (19-10-23 12:52)
✔ Document the attributes that are returned from the main methods, and ones that you can edit and see (totalvms, allvms etc.) @created(19-10-23 12:52) @done (19-10-25 03:03)
≡ Document the extra saving steps needed for vm/id information @created(19-10-26 18:49)
≡ Tidy up the documentation generally @created(19-10-26 18:49)
≡ document how to send host metrics (need to change hostlist to hostidlist) @created(19-10-29 01:09)
Bugs:
✔ Fix single host query not having countVMSFiltered being set @created(19-10-20 07:06) @critical @bug @done (19-10-20 15:12)
✔ Allow the querying of VMs without a dict of their name/id before hand, either allow the script to run without and have no name, or require dict to be passed in that matches the name, id pulling back. @created(19-10-21 03:44) @bug @high @done (19-10-25 03:03)
Tasks:
✔ Check the time actually pulls back the range you give it @created(19-10-20 20:59) @done (19-10-20 22:04)
To DO:
✔ Add hosts and cluster to the results @created(19-10-25 03:34) @done (19-10-26 18:49)
✔ Add this functionality to the import method @created(19-10-25 03:34) @done (19-10-26 18:49)
✔ Add path option to savetodisk @created(19-10-25 03:35) @done (19-10-26 18:49)

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

75
vrops-api/approach1.py Normal file
View File

@@ -0,0 +1,75 @@
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)
# 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, minutes=-30),
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
)

File diff suppressed because it is too large Load Diff

62
vrops-api/approach2.py Normal file
View File

@@ -0,0 +1,62 @@
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('approach1-vms-example.json', 'r') as vms:
vrops.allVMS = json.load(vms)
# Get list of these VMS
allVMSIdList = vrops.getValuesList(vrops.allVMS)
# Load VM_Host dict
with open('approach1-vms_hosts-example.json', 'r') as vmsHosts:
vrops.VMSHostsNames = json.load(vmsHosts)
# Set the cluster
vrops.chosenCluster = 'SC1PRCONTXWHCUXCCL01'
# # Add a single VM to a list to pull back (can add more VMs to this list if
# # needed)
# myList = []
# myList.append(list(vrops.allVMS.values())[100])
# Get data for a vm
vrops.getStatsFromVMS(
begin=vrops.epochRelativeTime(vrops.epochNow, minutes=-30),
end=vrops.epochNow,
intervalType='MINUTES',
intervalQuantifier='1',
rollUpType='AVG',
resourceId=allVMSIdList,
statKey=['rescpu|actav1_latest'],
)
# Export the data into readable format
vrops.exportVMData()
# Save to disk
vrops.saveToDisk(
vrops.export, indent=4, filePrefix='approach2-export', breakLine=True
)

677
vrops-api/readme.md Normal file
View File

@@ -0,0 +1,677 @@
# VROPS API Module
_Author: Daniel Tomlinson_
_Team: Capacity & Monitoring_
_Date: October 2019_
See example.py for an example of the following methods.
**Requires**: python version 3.6+
For any bugs create a gitlab issue and give a description of the error and sample code where necessary.
**To jump right in, clone the repo, install the dependencies and run `approach1.py` and `approach2.py` for a quick and easy demonstration.**
If you need VROPS api attributes (rollUpTypes, statKeys etc.) follow the links in [Get stats for VMs](#get-stats-for-vms).
<br/>
<!-- MarkdownTOC -->
- [Requirements](#requirements)
- [Modules](#modules)
- [Workflow](#workflow)
* [Get stats for every VM in a cluster](#get-stats-for-every-vm-in-a-cluster)
+ [Approach 1: If you **do not** have a dictionary of name:id pairs for all VMs in a cluster.](#approach-1-if-you-do-not-have-a-dictionary-of-nameid-pairs-for-all-vms-in-a-cluster)
+ [Approach 2: If you have a dictionary of name:id pairs for all VMs from a previous run.](#approach-2-if-you-have-a-dictionary-of-nameid-pairs-for-all-vms-from-a-previous-run)
* [Usage](#usage)
+ [Authenticate](#authenticate)
+ [Get all clusters](#get-all-clusters)
- [Pull back clusters](#pull-back-clusters)
- [Convert to list](#convert-to-list)
+ [Get all hosts](#get-all-hosts)
- [Pull back hosts](#pull-back-hosts)
- [Convert to list](#convert-to-list-1)
+ [Get VMs](#get-vms)
- [Pull back VMs](#pull-back-vms)
- [Convert to list](#convert-to-list-2)
- [Save VM dictionary as json for import](#save-vm-dictionary-as-json-for-import)
- [Load saved dictionary and import](#load-saved-dictionary-and-import)
- [Load VM information](#load-vm-information)
- [Set the VM dictionary](#set-the-vm-dictionary)
+ [Get stats for VMs](#get-stats-for-vms)
- [Get results](#get-results)
- [Save results to json file on disk](#save-results-to-json-file-on-disk)
+ [Get epoch time](#get-epoch-time)
- [Get epoch time now](#get-epoch-time-now)
- [Get epoch time relative to another tie](#get-epoch-time-relative-to-another-tie)
<!-- /MarkdownTOC -->
<br/>
## Requirements
See requirements.txt and install to a virtualenv
```bash
python3 -m venv vrops-api
source vrops-api/bin/activate
pip install -r requirements.txt
```
## Modules
The current working directory has to be added to the system path for python to import a custom module.
```python
import sys
import os
sys.path.append(os.getcwd())
from vropsAPI import vropsAPI
```
## Workflow
The following documents how you can use the module to do common tasks with the VROPs api. We cover pulling VM metrics for each VM in a **cluster.**
### Get stats for every VM in a cluster
To pull back data from VMs you need two things:
1. A dictionary containing the name:id pairs for each VM.
2. A list of these id's to pass to the api call.
Step 1 is optional, but without it you won't have the name of the VM when you query the API.
The following approaches document how to use this module to accomplish this.
There are two approaches to do this using this module.
- The first approach is if you don't have a dictionary containing the name:id pairs for all VMs in the cluster. **Do this if you haven't run the script before.**
- The second approach can skip a lot of the steps and jump straight to querying the api using this dictonary of the name:id pairs from an earlier run. **Do this if you've saved the dictionary from a previous run.**
#### Approach 1: If you **do not** have a dictionary of name:id pairs for all VMs in a cluster.
*see ```approach1.py``` for an example of these steps*
1. Authenticate a session with VROPS api. - [Authenticate](#authenticate)
2. Get all cluster names and choose the one you require. - [Get all clusters](#get-all-clusters)
3. Get all hosts in this cluster + store these **names in a list**. - [Get all hosts](#get-all-hosts)
4. Get all VMs under these hosts and store the **names and id's in a dictionary.** - [Get VMs](#get-vms)
5. *(Optional) Dump this dictionary in a json file to be reused for future runs.* - [Save VM dictionary as json for import](#save-vm-dictionary-as-json-for-import)
6. Get data for these VMs by passing in a list of VM **id's.** - [Get stats for VMs](#get-stats-for-vms)
7. Save output to json. - [Save results to json file on disk](#save-results-to-json-file-on-disk)
#### Approach 2: If you have a dictionary of name:id pairs for all VMs from a previous run.
*see ```approach2.py``` for an example of these steps*
1. Authenticate a session with VROPS api. - [Authenticate](#authenticate)
2. Load this dictionary from a json file, and update the module's dictionary. - [Load saved dictionary and import](#load-saved-dictionary-and-import)
3. Get data for these VMs by passing in a list of VM **id's.** - [Get stats for VMs](#get-stats-for-vms)
4. Save output to json. - [Save results to json file on disk](#save-results-to-json-file-on-disk)
<br>
### Usage
#### Authenticate
Authenticates a session with VROPs and generates a token.
Definition:
```python
@classmethod
def authenticate(
cls,
vropsURL: str,
username: str,
authSource: str,
password: str,
verify: bool = True,
):
...
return cls(vropsURL, authToken, verify)
```
Example:
```python
vrops = vropsAPI.authenticate(
'https://sc1prapvro01/',
'svc_splunkVROPS@Group.WilliamHill.PLC',
'whgroup',
base64.b64decode(b'UmFjaW5nMjEyMg==').decode(),
verify=False,
)
```
#### Get all clusters
##### Pull back clusters
To get all clusters you need to do the following two methods: ```vropsAPI.getClusters()``` and ```vropsAPI.getClusterIdentifiers()```
You can then use the ```vropsAPI.getKeysList()``` method to get a list of names without the ids.
Definition:
```python
def getClusters(self):
...
return(self)
```
```python
def getClusterIdentifiers(self):
...
return(self)
```
Example:
```python
vropsAPI.getClusters()
vropsAPI.getClusterIdentifiers()
```
```python
print(vropsAPI.allClusters)
```
Attributes created:
- `vropsAPI.allClusters` - A dictionary containing the name:id pairs of all clusters.
Output:
```json
{
"SC1PRINNOGEUXCCL01": "00276144-2eda-4f2b-9660-fa9f9cf3cd82",
"BRSDRGEUX01": "037f8d5e-01d5-411b-afdc-c25868f04a27",
"BRSDRCTXWN01": "05c2ba9a-d6d4-47aa-8fe5-855b40364625",
"LD6-BRS-Hosts": "0612327c-d637-4e95-8782-97c97d1e99ed",
"BRSPRPCMGEUXCCL01": "08b7d0fb-92ee-4cd9-ba7d-96f21965d7a4",
"LD6DRGEUX01": "84f052a8-8aed-4efb-8c39-8bce0f4a3c54"
}
```
##### Convert to list
Optionally you can convert this to a list of names with
```python
allClustersList = vropsAPI.getKeysList(vropsAPI.allClusters)
print(allClustersList)
```
Output:
```python
[
"SC1PRINNOGEUXCCL01",
"BRSDRGEUX01",
"BRSDRCTXWN01",
"LD6-BRS-Hosts",
"BRSPRPCMGEUXCCL01",
"LD6DRGEUX01"
]
```
<br>
#### Get all hosts
##### Pull back hosts
To get all hosts for cluster ```SC1PRCONTXWHCUXCCL01``` you need to use the following two methods `getHostsFromCluster()` and `getHostIdentifiers()`:
Definition:
```python
def getHostsFromCluster(self, cluster='SC1PRCONTXWHCUXCCL01'):
...
return(self)
```
```python
def getHostIdentifiers(self):
...
return(self)
```
Attributes created:
- `vropsAPI.allHosts`. - A dictionary containing the name:ID pairs for all hosts.
Example:
```python
vropsAPI.getHostsFromCluster(cluster='SC1PRCONTXWHCUXCCL01')
vropsAPI.getHostIdentifiers()
```
```python
print(vropsAPI.allHosts)
```
Output:
```
{
"sc1hsesx156.prod.williamhill.plc": "15b3ea0c-9f62-4fc2-93b8-d4281196043e",
"sc1hsesx187.prod.williamhill.plc": "18ecb9d5-842f-4a4b-b43b-f8bbcdd54775",
"sc1hsesx148.prod.williamhill.plc": "1bdf892a-121e-461e-8ef7-8dd174c4c01a",
"sc1hsesx155.prod.williamhill.plc": "1ef01a7b-9e61-40b7-8d72-78363352fbfc"
}
```
##### Convert to list
You can convert this to a list of names with:
```python
allHostsList = vropsAPI.getKeysList(vropsAPI.allHosts)
print(allHostsList)
```
Output:
```python
[
"sc1hsesx156.prod.williamhill.plc",
"sc1hsesx187.prod.williamhill.plc",
"sc1hsesx148.prod.williamhill.plc",
"sc1hsesx155.prod.williamhill.plc",
"sc1hsesx093.prod.williamhill.plc"
]
```
<br>
#### Get VMs
##### Pull back VMs
To get all VMs for a single host or a list of hosts (all hosts from a cluster) you need to use the following two methods `getVMSFromHost()` and `getVMSIdentifiers()`:
Defintion:
```python
def getVMSFromHost(self, host: Union(str, list)):
...
return self
```
```python
def getVMSIdentifiers(self):
...
return self
```
Attributes created:
- `vropsAPI.allVMS` - A dictionary containing the name:ID pairs for all VMs.
- `vropsAPI.totalVMS` - An integer showing the total number of VMs.
Example (pulling back all VMs for all hosts in a cluster:
```python
vropsAPI.getVMSFromHost(allHostsList)
vropsAPI.getVMSIdentifiers()
```
Print total number of VMs
```python
print(vropsAPI.totalVMS)
```
Print VM IDs and names
```python
print(vropsAPI.allVMS)
```
Output:
```python
OrderedDict(
[
('prdxinf21box001', '0981bfe1-b6ba-4a62-bfcc-39892fd038c6'),
('prdxcmr21ssb001', '3017e298-67e8-4a5f-bf10-f8c903e3d75f'),
('prdxtms14sln014', '35c5a694-2361-44dd-9e5e-893cea16119d'),
('prdxtms10sln014', '3b76b4b1-76b3-4fa7-a032-6448a60eded5'),
('prdxria01trd003', '458c11be-88ba-4301-aa32-3b748c92a47b')
]
)
```
##### Convert to list
**You will need a list of IDs, not names, to pull back data from the API**
You can convert this to a list of names with
```python
allVMSList = vropsAPI.getKeysList(vropsAPI.allVMS)
```
You can convert this to a list of IDs with
```python
allVMSIdList = vropsAPI.getValuesList(vropsAPI.allVMS)
```
##### Save VM dictionary as json for import
You can save this dictionary of VM names:ids for import in future runs.
```python
vropsAPI.saveToDisk(vropsAPI.allVMS, indent=4, filePrefix='vm-export')
```
##### Load saved dictionary and import
You can do the following if you already have a list of VMs for the cluster/hosts in question. This means you can import, and go straight to importing metrics, rather than querying the API for clusters/hosts each time.
Assuming file on disk is:
`vm-export-21-10-2019_09-12-53.json`
##### Load VM information
Load this .json into python as a dictionary
```python
import json
with open('vm-export-21-10-2019_09-12-53.json', 'r') as file:
myFile = json.load(file)
```
##### Set the VM dictionary
```python
vropsAPI.allVMS = myFile
```
You should see
```
Successfully imported the dictionary.
```
You now have the results from a previous run added to your instance.
<br>
#### Get stats for VMs
Uses post to pull back data from VROPs for a list of VMs
VROPs needs epoch time in ms format without the decimal (e.g 1571607237000)
You can use the following to pull data back for VMs:
Definition:
```python
def getStatsFromVMS(
self,
begin: int,
end: int,
intervalType: str,
intervalQuantifier: str,
rollUpType: str,
resourceId: list,
statKey: Union[str, list],
):
...
return self
```
Attributes:
- Attributes:
- begin: epoch time for start
- end: epoch time for end
- intervalType: `<ns3:intervalType>` [see documentation](https://sc1prapvro01/suite-api/docs/rest/models.html#repr-1190589417)
- intervalQuantifier: int
- rollUpType: `<ns3:rollUpType>` [see documentation](https://sc1prapvro01/suite-api/-docs/rest/models.html#repr-1735704374)
- resourceId: list of vrops resourceIds (**not Names!**)
- statKey: list containing vrops StatKey metrics [see documentation](https://docs.vmware.com/en/vRealize-Operations-Manager/6.7/com.vmware.vcom.metrics.doc/GUID-1322F5A4-DA1D-481F-BBEA-99B228E96AF2.html)
##### Get results
Give 5 min avg for CPU usage % and number of cores assigned for past 10 minutes.
*for relative time go back 11 minutes to ensure we have a complete 10 minute interval.*
**You need to pass in a list for resourceId. If you have a single VM this should be a list with one entry only.**
Example, passing in allVMSIdList from before:
```python
vropsAPI.getStatsFromVMS(
begin=vropsAPI.epochRelativeTime(vropsAPI.epochNow, minutes=-11),
end=vropsAPI.epochNow,
intervalType='MINUTES',
intervalQuantifier='5',
rollUpType='AVG',
resourceId=list(allVMSIdList),
statKey=['cpu|usage_average', 'config|hardware|num_Cpu'],
)
```
Attributes returned:
- `vropsAPI.allVMS` - A string containing the raw output from the VROPS api. You should use the method `vropsAPI.exportVMData()` for a more workable format.
Output:
prdxmes21zoo004 has 2 vCPUs and an average CPU utilisation of 1.2% for the past 10 minutes.
```json
{
"values": [
{
"name": "prdxmes21zoo004",
"resourceId": "528eb4db-813b-45a1-a456-ce5b44751da6",
"stat-list": {
"stat": [
{
"timestamps": [
1571608439999,
1571608739999
],
"statKey": {
"key": "config|hardware|num_Cpu"
},
"rollUpType": "AVG",
"intervalUnit": {
"quantifier": 5,
"intervalType": "MINUTES"
},
"data": [
2.0,
2.0
]
},
{
"timestamps": [
1571608439999,
1571608739999
],
"statKey": {
"key": "cpu|usage_average"
},
"rollUpType": "AVG",
"intervalUnit": {
"quantifier": 5,
"intervalType": "MINUTES"
},
"data": [
1.218666672706604,
1.2406666278839111
]
}
]
}
}
]
}
```
##### Save results to json file on disk
To save results to disk you should use `vropsAPI.exportVMData()` then use `vropsAPI.saveToDisk()`.
The method `vropsAPI.exportVMData()` will format the raw output from VROPS and create the attribute `vropsAPI.export` which is a list that contains a json formatted string for each datapoint you requested. You can pass this list to ```vropsAPI.saveToDisk(breakLine=True)``` **(you should include `breakLine=True`)** which will save each item as a json formatted dictionary.
Definition:
```python
def exportVMData(self):
...
return self
```
Attributes returned:
- `vropsAPI.export` - A python list where each entry is a json string containing the dictionary representation for each datapoint requested.
Example:
```python
vropsAPI.exportVMData()
vropsAPI.saveToDisk(
vropsAPI.export, indent=4, filePrefix='SC1PRCONTXWHCUXCCL01', breakLine = True
)
```
Output:
```json
{
"name": "prdxmes14sln010",
"timestamp": 1571841899999,
"value": 4.0,
"statKey": "config|hardware|num_Cpu",
"rollUpType": "AVG",
"intervalUnit": 5
}
{
"name": "prdxmes14sln010",
"timestamp": 1571842199999,
"value": 4.0,
"statKey": "config|hardware|num_Cpu",
"rollUpType": "AVG",
"intervalUnit": 5
}
{
"name": "prdxmes14sln010",
"timestamp": 1571842499999,
"value": 4.0,
"statKey": "config|hardware|num_Cpu",
"rollUpType": "AVG",
"intervalUnit": 5
}
```
<br>
#### Get epoch time
The following method is built in to provide easy generation of epoch times. This functions similar to Splunks relative_time and time snapping.
##### Get epoch time now
- `vropsAPI.epochNow` - The following attribute is available instance wide to get the current time in Epoch correctly formatted for the API.
##### Get epoch time relative to another tie
Get epoch time relative to another time:
1. Can go back N hours/minutes etc.
2. Can set the hour/minute etc. to a specified value (snapping)
Definition:
```python
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:
...
return relativeTime
```
Attributes:
```
**kwargs:
epochTime: epoch time for start
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
```
Examples:
Get epoch 5 minutes ago.
```python
vropsAPI.epochRelativeTime(vropsAPI.epochNow, minutes=-5)
```
Get epoch at start of current hour.
```python
vropsAPI.epochRelativeTime(
vropsAPI.epochNow,
hour=0,
minute=0,
second=0,
)
```
Get epoch 1 week ago at start of day.
```python
vropsAPI.epochRelativeTime(
vropsAPI.epochNow,
days=-7
hour=0,
minute=0,
second=0,
)
```
<br>

View File

@@ -0,0 +1,7 @@
certifi==2019.9.11
chardet==3.0.4
idna==2.8
python-dateutil==2.8.0
requests==2.22.0
six==1.12.0
urllib3==1.25.6

75
vrops-api/test.py Normal file
View File

@@ -0,0 +1,75 @@
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)
# 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, minutes=-30),
end=vrops.epochNow,
intervalType='MINUTES',
intervalQuantifier='1',
rollUpType='AVG',
resourceId=allVMSIdList,
statKey=['rescpu|actav1_latest'],
)
# Export the data into readable format
vrops.exportVMData()
# Save to disk
vrops.saveToDisk(
vrops.export, indent=4, filePrefix='approach1-export', breakLine=True
)

View File

@@ -0,0 +1,64 @@
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('approach1-vms-example.json', 'r') as vms:
vrops.allVMS = json.load(vms)
# Get list of these VMS
allVMSIdList = vrops.getValuesList(vrops.allVMS)
# Load VM_Host dict
with open('approach1-vms_hosts-example.json', 'r') as vmsHosts:
vrops.VMSHostsNames = json.load(vmsHosts)
# Set the cluster
vrops.chosenCluster = 'SC1PRCONTXWHCUXCCL01'
# # Add a single VM to a list to pull back (can add more VMs to this list if
# # needed)
# myList = []
# myList.append(list(vrops.allVMS.values())[100])
# Get data for a vm
vrops.getStatsFromVMS(
begin=vrops.epochRelativeTime(vrops.epochNow, minutes=-30),
end=vrops.epochNow,
intervalType='MINUTES',
intervalQuantifier='1',
rollUpType='AVG',
resourceId=allVMSIdList,
statKey=['rescpu|actav1_latest'],
)
# Export the data into readable format
vrops.exportVMData()
print(vrops.export)
# Save to disk
vrops.saveToDisk(
vrops.export, indent=4, filePrefix='approach2-export', breakLine=True
)

661
vrops-api/vropsAPI.py Normal file
View File

@@ -0,0 +1,661 @@
import requests
import json
import warnings
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
warnings.filterwarnings('ignore')
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 getStatsFromVMS(
self,
begin: int,
end: int,
intervalType: str,
intervalQuantifier: str,
rollUpType: str,
resourceId: list,
statKey: Union[str, list],
):
argList = copy.deepcopy(locals())
del argList['self']
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,
)
# print(response.text)
# raise Exception
self._vmsResources = OrderedDict(self.jsonToPython(response.text))
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
)
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:
try:
outputFile.write(
json.dump(
pythonObject, outputFile, indent=indent
)
)
except TypeError:
pass
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):
self.export.append(
{
'name': self._vmsResources['values'][i][
'name'
],
'host': self._vmsResources['values'][i][
'host'
],
'cluster': self.chosenCluster,
'timestamp': str(self._vmsResources['values'][i][
'stat-list'
]['stat'][0]['timestamps'][j]),
'value': str(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': str(self._vmsResources[
'values'
][i]['stat-list']['stat'][k]['intervalUnit'][
'quantifier'
]),
'intervalType': str(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
@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

View File

@@ -0,0 +1,6 @@
{
"cells": [],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 2
}

View File

@@ -0,0 +1,316 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import sys\n",
"\n",
"workingDir = '/Users/dtomlinson/OneDrive - William Hill Organisation Limited/Mac/projects/vrops-api'\n",
"sys.path.append(workingDir)\n",
"\n",
"import json\n",
"import base64\n",
"from vropsAPI import vropsAPI"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<Response [200]>\n"
]
}
],
"source": [
"vrops = vropsAPI.authenticate(\n",
" 'https://sc1prapvro01/',\n",
" 'svc_splunkVROPS@Group.WilliamHill.PLC',\n",
" 'whgroup',\n",
" base64.b64decode(b'UmFjaW5nMjEyMg==').decode(),\n",
" verify=False,\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<Response [200]>\n"
]
}
],
"source": [
"vrops.getClusters()\n",
"vrops.getClusterIdentifiers()\n",
"allClustersList = vrops.getKeysList(vrops.allClusters)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['SC1PRINNOGEUXCCL01',\n",
" 'BRSDRGEUX01',\n",
" 'BRSDRCTXWN01',\n",
" 'LD6-BRS-Hosts',\n",
" 'BRSPRPCMGEUXCCL01',\n",
" 'LD6DRGEUX01',\n",
" 'BRSPRRTRGEWNCCL01',\n",
" 'BRSPRTSRGEUXCCL01',\n",
" 'LD6PTWHCUX01',\n",
" 'Cluster-2',\n",
" 'BRSPRDVOGEUXCCL01',\n",
" 'SC1PRPCM01',\n",
" 'SC1PRGEWN01',\n",
" 'Performance-Test-Cluster',\n",
" 'NJ2PRGE01',\n",
" 'BRSPRCTXGEWNCCL01',\n",
" 'SC1PRRETWN01',\n",
" 'SC1PRMGMT01',\n",
" 'BRSPRPCM01',\n",
" 'AutoDeployHosts',\n",
" 'GIBPRCONTXWHCUXCCL01',\n",
" 'AutoDeployHosts-DVS',\n",
" 'STJPRGEUX01',\n",
" 'Cluster-1',\n",
" 'SC1PRGEUX01',\n",
" 'LD6PTGEUX01',\n",
" 'MNLPRGE01',\n",
" 'BRSPRSPLGEUXCCL01',\n",
" 'LD6PTGEWN01',\n",
" 'BRSTSRGEWN01',\n",
" 'BRSPRCONTXWHCUXCCL01',\n",
" 'BRSDRRETWN01',\n",
" 'BRSPRMGMT01',\n",
" 'BRSDVGEUX01',\n",
" 'BRSTSGEWN01',\n",
" 'BRSPRDROGEWNCCL01',\n",
" 'BRSDRGEWN01',\n",
" 'BRSPRTSOGEUXCCL01',\n",
" 'GIBACITEST',\n",
" 'LD6DRGEWN01',\n",
" 'SC1PRCTXWN01',\n",
" 'GIBPRGEWN01',\n",
" 'STJPRSHGEUXCCL01',\n",
" 'GIBPRSPUX01',\n",
" 'LD6PRMGMT01',\n",
" 'SC1PRSPLGEUXCCL01',\n",
" 'SC1PRCONTXWHCUXCCL01',\n",
" 'GIBPRMGMT01',\n",
" 'SOFPRGE01',\n",
" 'LD6DRWHCUX01',\n",
" 'BRSPRTSRGEWNCCL01',\n",
" 'BRSPRSHGEMGMTPRC01',\n",
" 'GIBPRGEUX01',\n",
" 'INDPRGE01',\n",
" 'KRAPRGE01',\n",
" 'BRSPRDRRGEWNCCL01',\n",
" 'BRSPRDROGEUXCCL01',\n",
" 'BRSPRTSOGEWNCCL01',\n",
" 'WV1PRGE01',\n",
" 'BRSTSGEUX01']"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"allClustersList"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<Response [200]>\n"
]
}
],
"source": [
"vrops.getHostsFromCluster(cluster='SC1PRCONTXWHCUXCCL01')\n",
"vrops.getHostIdentifiers()\n",
"allHostsList = vrops.getKeysList(vrops.allHosts)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['sc1hsesx156.prod.williamhill.plc',\n",
" 'sc1hsesx187.prod.williamhill.plc',\n",
" 'sc1hsesx148.prod.williamhill.plc',\n",
" 'sc1hsesx155.prod.williamhill.plc',\n",
" 'sc1hsesx214.prod.williamhill.plc',\n",
" 'sc1hsesx093.prod.williamhill.plc',\n",
" 'sc1hsesx145.prod.williamhill.plc',\n",
" 'sc1hsesx150.prod.williamhill.plc',\n",
" 'sc1hsesx136.prod.williamhill.plc',\n",
" 'sc1hsesx209.prod.williamhill.plc',\n",
" 'sc1hsesx212.prod.williamhill.plc',\n",
" 'sc1hsesx175.prod.williamhill.plc',\n",
" 'sc1hsesx162.prod.williamhill.plc',\n",
" 'sc1hsesx158.prod.williamhill.plc',\n",
" 'sc1hsesx140.prod.williamhill.plc',\n",
" 'sc1hsesx126.prod.williamhill.plc',\n",
" 'sc1hsesx142.prod.williamhill.plc',\n",
" 'sc1hsesx208.prod.williamhill.plc',\n",
" 'sc1hsesx202.prod.williamhill.plc',\n",
" 'sc1hsesx204.prod.williamhill.plc',\n",
" 'sc1hsesx182.prod.williamhill.plc',\n",
" 'sc1hsesx127.prod.williamhill.plc',\n",
" 'sc1hsesx133.prod.williamhill.plc',\n",
" 'sc1hsesx152.prod.williamhill.plc',\n",
" 'sc1hsesx185.prod.williamhill.plc',\n",
" 'sc1hsesx154.prod.williamhill.plc',\n",
" 'sc1hsesx121.prod.williamhill.plc',\n",
" 'sc1hsesx205.prod.williamhill.plc',\n",
" 'sc1hsesx135.prod.williamhill.plc',\n",
" 'sc1hsesx138.prod.williamhill.plc',\n",
" 'sc1hsesx159.prod.williamhill.plc',\n",
" 'sc1hsesx149.prod.williamhill.plc',\n",
" 'sc1hsesx147.prod.williamhill.plc',\n",
" 'sc1hsesx091.prod.williamhill.plc',\n",
" 'sc1hsesx161.prod.williamhill.plc',\n",
" 'sc1hsesx146.prod.williamhill.plc',\n",
" 'sc1hsesx203.prod.williamhill.plc',\n",
" 'sc1hsesx143.prod.williamhill.plc',\n",
" 'sc1hsesx132.prod.williamhill.plc',\n",
" 'sc1hsesx157.prod.williamhill.plc',\n",
" 'sc1hsesx139.prod.williamhill.plc',\n",
" 'sc1hsesx160.prod.williamhill.plc',\n",
" 'sc1hsesx120.prod.williamhill.plc',\n",
" 'sc1hsesx207.prod.williamhill.plc',\n",
" 'sc1hsesx095.prod.williamhill.plc',\n",
" 'sc1hsesx151.prod.williamhill.plc',\n",
" 'sc1hsesx200.prod.williamhill.plc',\n",
" 'sc1hsesx092.prod.williamhill.plc',\n",
" 'sc1hsesx094.prod.williamhill.plc',\n",
" 'sc1hsesx186.prod.williamhill.plc',\n",
" 'sc1hsesx090.prod.williamhill.plc',\n",
" 'sc1hsesx206.prod.williamhill.plc',\n",
" 'sc1hsesx210.prod.williamhill.plc',\n",
" 'sc1hsesx141.prod.williamhill.plc',\n",
" 'sc1hsesx131.prod.williamhill.plc',\n",
" 'sc1hsesx213.prod.williamhill.plc',\n",
" 'sc1hsesx137.prod.williamhill.plc',\n",
" 'sc1hsesx211.prod.williamhill.plc',\n",
" 'sc1hsesx130.prod.williamhill.plc',\n",
" 'sc1hsesx134.prod.williamhill.plc',\n",
" 'sc1hsesx144.prod.williamhill.plc',\n",
" 'sc1hsesx153.prod.williamhill.plc',\n",
" 'sc1hsesx201.prod.williamhill.plc']"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"allHostsList"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<Response [200]>\n",
"Successfully imported the dictionary.\n",
"<Response [200]>\n"
]
}
],
"source": [
"for host in allHostsList:\n",
" vrops.getVMSFromHost(host)\n",
" vrops.getVMSIdentifiers()\n",
" allVMSIdList = vrops.getValuesList(vrops.allVMS)\n",
" allVMSNameList = vrops.getKeysList(vrops.allVMS)\n",
" \n",
" vrops.getStatsFromVMS(\n",
" begin=vrops.epochRelativeTime(vrops.epochNow, minutes=-11),\n",
" end=vrops.epochNow,\n",
" intervalType='MINUTES',\n",
" intervalQuantifier='5',\n",
" rollUpType='AVG',\n",
" resourceId=allVMSIdList,\n",
" statKey=['cpu|usage_average', 'config|hardware|num_Cpu'],\n",
" )\n",
" \n",
" vrops.exportVMData()\n",
"\n",
" vrops.export\n",
" break"
]
},
{
"cell_type": "raw",
"metadata": {},
"source": [
"print(vrops.export)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}