VMware Cloud Community
defaultUser0815
Contributor
Contributor
Jump to solution

Monitoring ESXI Performance Metrics over API

Hi there,

i have some basic knowledge about virtualisation and linux debian/ubuntu but i would call me a beginner, so please don't be so hard with me.

I am looking for a simple way to monitor the ESXI system statistics like CPU usage and avalible memory. I found some perl & python scripts that are using the vSphere API for requesting the metrics but they where not free and I just need to monitor one single ESXI host without its hosts (vm's).

Is there a way to make a bash or python script that uses curl to request the esxi-api for the metrics. I want to understand the esxi api to make a bash script on my own that has no overhead like a perl-script cause of the modules u need to install for use. Can anyone show me an example how to request the esxi-api for a metric?

I found this, but it not really what i am looking for:

curl --insecure -u root:pass https://172.28.18.181/folder?dcPath=ha-datacenter

I hope anyone can give me some helpful answers Smiley Happy

1 Solution

Accepted Solutions
defaultUser0815
Contributor
Contributor
Jump to solution

ok, so this script helped me a lot to understand the basics for make queries to the vsphere api through pyvmomi, have fun with it :smileygrin:

import pyVmomi

import argparse

import atexit

import itertools

from pyVim import connect

from pyVmomi import vim, vmodl

from pyVim.connect import SmartConnect, Disconnect

import humanize

MBFACTOR = float(1 << 20)

printVM = True

printDatastore = True

printHost = True

def GetArgs():

    parser = argparse.ArgumentParser(

        description='Process args for retrieving all the Virtual Machines')

    parser.add_argument('-s', '--host', required=True, action='store',

                        help='Remote host to connect to')

    parser.add_argument('-o', '--port', type=int, default=443, action='store',

                        help='Port to connect on')

    parser.add_argument('-u', '--user', required=True, action='store',

                        help='User name to use when connecting to host')

    parser.add_argument('-p', '--password', required=False, action='store',

                        help='Password to use when connecting to host')

    args = parser.parse_args()

    return args

def printHostInformation(host):

    try:

        summary = host.summary

        stats = summary.quickStats

        hardware = host.hardware

        cpuCapacityMhz = (host.hardware.cpuInfo.hz * host.hardware.cpuInfo.numCpuCores) / 1000 / 1000

        cpuUsage = stats.overallCpuUsage

        memoryCapacity = hardware.memorySize

        memoryCapacityInMB = hardware.memorySize/MBFACTOR

        memoryUsage = stats.overallMemoryUsage

        freeMemoryPercentage = 100 - (

            (float(memoryUsage) / memoryCapacityInMB) * 100

        )

        print "--------------------------------------------------"

        print "Host name: ", host.name

        print "Host CPU capacity: ", cpuCapacityMhz, " Mhz"

        print "Host CPU usage: ", cpuUsage

        print "Host memory capacity: ", humanize.naturalsize(memoryCapacity,

                                                             binary=True)

        print "Host memory usage: ", memoryUsage / 1024, "GiB"

        print "Free memory percentage: " + str(freeMemoryPercentage) + "%"

        print "--------------------------------------------------"

    except Exception as error:

        print "Unable to access information for host: ", host.name

        print error

        pass

def printComputeResourceInformation(computeResource):

    try:

        hostList = computeResource.host

        for host in hostList:

            printHostInformation(host)

    except Exception as error:

        print "Unable to access information for compute resource: ",

        computeResource.name

        print error

        pass

def printDatastoreInformation(datastore):

    try:

        summary = datastore.summary

        capacity = summary.capacity

        freeSpace = summary.freeSpace

        uncommittedSpace = summary.uncommitted

        freeSpacePercentage = (float(freeSpace) / capacity) * 100

        print "##################################################"

        print "Datastore name: ", summary.name

        print "Capacity: ", humanize.naturalsize(capacity, binary=True)

        if uncommittedSpace is not None:

            provisionedSpace = (capacity - freeSpace) + uncommittedSpace

            print "Provisioned space: ", humanize.naturalsize(provisionedSpace,

                                                              binary=True)

        print "Free space: ", humanize.naturalsize(freeSpace, binary=True)

        print "Free space percentage: " + str(freeSpacePercentage) + "%"

        print "##################################################"

    except Exception as error:

        print "Unable to access summary for datastore: ", datastore.name

        print error

        pass

def main():

    args = GetArgs()

    try:

        si = connect.ConnectNoSSL(args.host, 443, args.user, args.password)

        atexit.register(Disconnect, si)

        content = si.RetrieveContent()

        for datacenter in content.rootFolder.childEntity:

            print "##################################################"

            print "##################################################"

            print "### datacenter : " + datacenter.name

            print "##################################################"

            if printDatastore:

                datastores = datacenter.datastore

                for ds in datastores:

                    printDatastoreInformation(ds)

            if printHost:

                if hasattr(datacenter.vmFolder, 'childEntity'):

                    hostFolder = datacenter.hostFolder

                    computeResourceList = hostFolder.childEntity

                    for computeResource in computeResourceList:

                        printComputeResourceInformation(computeResource)

    except vmodl.MethodFault as error:

        print "Caught vmodl fault : " + error.msg

        return -1

    return 0

if __name__ == "__main__":

    main()

I cut some lines that doesn't matter to me. I am working at an check plugin for esxi servers with an output to icinga2

View solution in original post

0 Kudos
5 Replies
JohnBrown11
Contributor
Contributor
Jump to solution

bump

defaultUser0815
Contributor
Contributor
Jump to solution

OK, if someone is interested i found a way to list every metric over the api

0 Kudos
JohnBrown11
Contributor
Contributor
Jump to solution

yes please Smiley Happy

Currently, I'm only using ESXi (no vCentre or vCSA) but I think i'm going to have to install vCSA to use the API

0 Kudos
defaultUser0815
Contributor
Contributor
Jump to solution

ok, so this script helped me a lot to understand the basics for make queries to the vsphere api through pyvmomi, have fun with it :smileygrin:

import pyVmomi

import argparse

import atexit

import itertools

from pyVim import connect

from pyVmomi import vim, vmodl

from pyVim.connect import SmartConnect, Disconnect

import humanize

MBFACTOR = float(1 << 20)

printVM = True

printDatastore = True

printHost = True

def GetArgs():

    parser = argparse.ArgumentParser(

        description='Process args for retrieving all the Virtual Machines')

    parser.add_argument('-s', '--host', required=True, action='store',

                        help='Remote host to connect to')

    parser.add_argument('-o', '--port', type=int, default=443, action='store',

                        help='Port to connect on')

    parser.add_argument('-u', '--user', required=True, action='store',

                        help='User name to use when connecting to host')

    parser.add_argument('-p', '--password', required=False, action='store',

                        help='Password to use when connecting to host')

    args = parser.parse_args()

    return args

def printHostInformation(host):

    try:

        summary = host.summary

        stats = summary.quickStats

        hardware = host.hardware

        cpuCapacityMhz = (host.hardware.cpuInfo.hz * host.hardware.cpuInfo.numCpuCores) / 1000 / 1000

        cpuUsage = stats.overallCpuUsage

        memoryCapacity = hardware.memorySize

        memoryCapacityInMB = hardware.memorySize/MBFACTOR

        memoryUsage = stats.overallMemoryUsage

        freeMemoryPercentage = 100 - (

            (float(memoryUsage) / memoryCapacityInMB) * 100

        )

        print "--------------------------------------------------"

        print "Host name: ", host.name

        print "Host CPU capacity: ", cpuCapacityMhz, " Mhz"

        print "Host CPU usage: ", cpuUsage

        print "Host memory capacity: ", humanize.naturalsize(memoryCapacity,

                                                             binary=True)

        print "Host memory usage: ", memoryUsage / 1024, "GiB"

        print "Free memory percentage: " + str(freeMemoryPercentage) + "%"

        print "--------------------------------------------------"

    except Exception as error:

        print "Unable to access information for host: ", host.name

        print error

        pass

def printComputeResourceInformation(computeResource):

    try:

        hostList = computeResource.host

        for host in hostList:

            printHostInformation(host)

    except Exception as error:

        print "Unable to access information for compute resource: ",

        computeResource.name

        print error

        pass

def printDatastoreInformation(datastore):

    try:

        summary = datastore.summary

        capacity = summary.capacity

        freeSpace = summary.freeSpace

        uncommittedSpace = summary.uncommitted

        freeSpacePercentage = (float(freeSpace) / capacity) * 100

        print "##################################################"

        print "Datastore name: ", summary.name

        print "Capacity: ", humanize.naturalsize(capacity, binary=True)

        if uncommittedSpace is not None:

            provisionedSpace = (capacity - freeSpace) + uncommittedSpace

            print "Provisioned space: ", humanize.naturalsize(provisionedSpace,

                                                              binary=True)

        print "Free space: ", humanize.naturalsize(freeSpace, binary=True)

        print "Free space percentage: " + str(freeSpacePercentage) + "%"

        print "##################################################"

    except Exception as error:

        print "Unable to access summary for datastore: ", datastore.name

        print error

        pass

def main():

    args = GetArgs()

    try:

        si = connect.ConnectNoSSL(args.host, 443, args.user, args.password)

        atexit.register(Disconnect, si)

        content = si.RetrieveContent()

        for datacenter in content.rootFolder.childEntity:

            print "##################################################"

            print "##################################################"

            print "### datacenter : " + datacenter.name

            print "##################################################"

            if printDatastore:

                datastores = datacenter.datastore

                for ds in datastores:

                    printDatastoreInformation(ds)

            if printHost:

                if hasattr(datacenter.vmFolder, 'childEntity'):

                    hostFolder = datacenter.hostFolder

                    computeResourceList = hostFolder.childEntity

                    for computeResource in computeResourceList:

                        printComputeResourceInformation(computeResource)

    except vmodl.MethodFault as error:

        print "Caught vmodl fault : " + error.msg

        return -1

    return 0

if __name__ == "__main__":

    main()

I cut some lines that doesn't matter to me. I am working at an check plugin for esxi servers with an output to icinga2

0 Kudos
defaultUser0815
Contributor
Contributor
Jump to solution

If anyone is interested in an Icinga check plugin for checking the ESXI Resources, here you have Smiley Happy

nagios-plugins/check_esxi_resources.py at master · defaultUser0816/nagios-plugins · GitHub

It do not use SSL at the moment but the modification for that is not much work. It is designed for check a single esxi with only one datastore. Dont forget the parameter --host, --user, --password. Host is for the IP-Address, user & pw from ESXI login. Output looks like this:

OK - 65% CPU usage, 38% Free Memory, 27% Free Disk space

0 Kudos