Scripts

Welcome to the Nutanix NEXT community. To get started please read our short welcome post. Thanks!

cancel
Showing results for 
Search instead for 
Did you mean: 

VMware vRealize Operations Adapter for Nutanix

VMware vRealize Operations Adapter for Nutanix

This script creates a custom adapter for a Nutanix Cluster in VMware vRealize Operations. It delivers simple values like freeBytes, capacityBytes and usageBytes and enables vROps to monitor and display the values.

By using the Nutanix REST API you can use nearly every value that is available.

 

 

Screen Shot 2015-07-31 at 09.45.16.png

 

config.py

 

global vmw 
global ntnx

vmw = dict(
    server = "your-vrops-server",
    username = "admin",
    password = "VMware1!"
)

ntnx = dict(
    server = "your-ntnx-server",
    username = "admin",
    password = "nutanix/4u",

    )

 

NTNX_vROPS.py

 

#!/usr/bin/python

### Nutanix to VMmware vRealize Operations ###
# Use-Case: Push your Nutanix environment data to vROps/vCOps
###

import json 
import requests
import time
import unicodedata
import config

global resourceName

getNutanixDataUrl = "https://"+config.ntnx["server"]+":9440/PrismGateway/services/rest/v1/clusters"
getResourcesUrl = "https://"+config.vmw["server"]+"/suite-api/api/resources"
createResourceUrl = "https://"+config.vmw["server"]+"/suite-api/api/resources/adapterkinds/http%20post"
addResourceDataUrl = "https://"+config.vmw["server"]+"/suite-api/api/resources/identifier/stats/adapterkinds/http%20post"

resourceName = "Nutanix Cluster"

def getMs():
    return int(round(time.time()*1000))

def getRest(base_url):
    requests.packages.urllib3.disable_warnings()
    s = requests.Session()
    s.headers.update({'Content-Type': 'application/json; charset=utf-8', 'Accept':'application/json'})
    request = s.get(base_url, verify=False, auth=(config.vmw["username"],config.vmw["password"]))
    return request.json()

def postRest(base_url, data):
    requests.packages.urllib3.disable_warnings()
    s = requests.Session()
    s.headers.update({'Content-Type': 'application/json; charset=utf-8', 'Accept':'application/json'})
    request = s.post(base_url, data=json.dumps(data), verify=False, auth=(config.vmw["username"],config.vmw["password"]))
    return request

def getNutanixData():
    values = {}
    base_url = getNutanixDataUrl
    requests.packages.urllib3.disable_warnings()
    s = requests.Session()
    s.headers.update({'Content-Type': 'application/json; charset=utf-8', 'Accept':'application/json'})
    request = s.get(base_url, verify=False, auth=(config.ntnx["username"],config.ntnx["password"]))
    data = request.json()
    for i in data["entities"]:
        values["freeBytes"] = i["usageStats"]["storage.free_bytes"]
        values["capacityBytes"] = i["usageStats"]["storage.capacity_bytes"]
        values["usageBytes"] = i["usageStats"]["storage.usage_bytes"]
        return values

def getResources():
    base_url = getResourcesUrl
    result = getRest(base_url)
    return result

def getResourceId(resource):
    resources = getResources()
    for i in resources["resourceList"]:
        name = i["resourceKey"]["name"]
        identifier = i["identifier"]
        if resource == name:
            return identifier
    
def createResourceByAdapterKind(resourceName):
    base_url = createResourceUrl
    data = {
        "description" : None,
        "resourceKey" : {
            "name" : resourceName,
            "adapterKindKey" : "OPENAPI",
            "resourceKindKey" : "Storage",
            "resourceIdentifiers" : [ {
                "identifierType" : {
                    "name" : "freeBytes",
                    "dataType" : "STRING",
                    "isPartOfUniqueness" : True
                    },
                "value" : "0"
            },{
                "identifierType" : {
                    "name" : "usageBytes",
                    "dataType" : "STRING",
                    "isPartOfUniqueness" : True
                    },
                "value" : "0"
            },{
                "identifierType" : {
                    "name" : "capacityBytes",
                    "dataType" : "STRING",
                    "isPartOfUniqueness" : True
                    },
                "value" : "0"
            }
        ]
        },
        "resourceStatusStates" : [ ],
        "dtEnabled" : True,
    }
    result = postRest(base_url, data)
    return result

def addResourceData(identifier):
    values = getNutanixData()
    identifier = str(identifier)
    base_url = addResourceDataUrl.replace("identifier", identifier)
    stats = {
        "stat-content" : [ {
        "statKey" : "freeBytes",
        "timestamps" : [ getMs() ],
        "data" : [ values["freeBytes"] ]
        },{
        "statKey" : "capacityBytes",
        "timestamps" : [ getMs() ],
        "data" : [ values["capacityBytes"] ]
        },{
        "statKey" : "usageBytes",
        "timestamps" : [ getMs() ],
        "data" : [ values["usageBytes"] ]
        }]
    }
    request = postRest(base_url, stats)
    return request

if __name__ == "__main__":
    identifier = getResourceId(resourceName)
    if identifier == None:
        print "resource is not available. creating..."
        createResourceByAdapterKind(resourceName)
        identifier = getResourceId(resourceName)
    while identifier != None:
        print "resource is available. adding data..."
        addResourceData(identifier)
        time.sleep(5) 

 

2 REPLIES
Hero

Re: VMware vRealize Operations Adapter for Nutanix

Great job @AndreasDiemer - good luck in the competition! 

Tags (1)
Squire

Re: VMware vRealize Operations Adapter for Nutanix

How can we create an adapter for nagios inside VROps using python for calling the API's??