Forum Discussion

Shack's avatar
Shack
Icon for Advisor rankAdvisor
7 months ago

ESX Host Services?

I'm looking for a way to monitor ESX Host Services.  Right now I have a script that I believed to be working but after spot checking a few hosts it seems to not be totally accurate.  Or, I just don't fully understand the backend on a host as far as the services go vs the data I am collecting.  

My datasource is reporting services to be Running on hosts that clearly state certain services to be Stopped.  

Active Discovery

import com.santaba.agent.groovyapi.esx.ESX
import com.vmware.vim25.mo.InventoryNavigator

def hostname = hostProps.get("system.hostname")
def user = hostProps.get("esx.user")
def pass = hostProps.get("esx.pass")
def custom_url = hostProps.get("esx.url")
def display_name = hostProps.get("system.displayname")

// Connect to the ESX service
def url = custom_url ?: "https://${hostname}/sdk"
def svc = new ESX()
svc.open(url, user, pass, 10 * 1000) // Timeout in 10 seconds

// Get the service instance and root folder
def si = svc.getServiceInstance()
def rootFolder = si.getRootFolder()

// Search for managed entities of type 'HostSystem'
def hosts = new InventoryNavigator(rootFolder).searchManagedEntities("HostSystem")

// Iterate over each host system
hosts.each { host ->
    // Get the services for the current host
    def services = host.getConfig().getService()
    
    // Iterate over each service
    services.each { service ->
        // Get the label and key of each service
        def labels = service?.service?.label
        def keys = service?.service?.key
        def running = service?.service?.running ? "Running" : "Stopped"
        
        // If label is an array, print each element on its own line
        if (labels instanceof List) {
            labels.eachWithIndex { singleLabel, index ->
                println "${keys[index]}##${keys[index]}##${singleLabel}"
            }
        } else {
            // If label is not an array, print it normally
            //println "Service Label: ${labels}, Key: ${keys}, Status: ${running}"
        }
    }
}

// Close the ESX service connection
svc.close()

 

Data Collection

import com.santaba.agent.groovyapi.esx.ESX
import com.vmware.vim25.mo.InventoryNavigator
 
def hostname = hostProps.get("system.hostname")
def user = hostProps.get("esx.user")
def pass = hostProps.get("esx.pass")
def custom_url = hostProps.get("esx.url")
def display_name = hostProps.get("system.displayname")
 
// Connect to the ESX service
def url = custom_url ?: "https://${hostname}/sdk"
def svc = new ESX()
svc.open(url, user, pass, 10 * 1000) // Timeout in 10 seconds
 
// Get the service instance and root folder
def si = svc.getServiceInstance()
def rootFolder = si.getRootFolder()
 
// Search for managed entities of type 'HostSystem'
def hosts = new InventoryNavigator(rootFolder).searchManagedEntities("HostSystem")
 
// Iterate over each host system
hosts.each { host ->
    // Get the services for the current host
    def services = host.getConfig().getService()
    
    // Iterate over each service
    services.each { service ->
        // Get the label and key of each service
        def labels = service?.service?.label
        def keys = service?.service?.key
        //def running = service?.service?.running ? "Running" : "Stopped"
        def status = service?.service?.running ? "1" : "0"
        
        // If label is an array, print each element on its own line
        if (labels instanceof List) {
            labels.eachWithIndex { singleLabel, index ->
def wildvalue = "${keys[index]}"
                //println "Service Label: ${singleLabel}, Key: ${keys[index]}, Status: ${running}"
println "${wildvalue}.status=${status}";
           }
        } else {
            // If label is not an array, print it normally
            //println "Service Label: ${labels}, Key: ${keys}, Status: ${running}"
        }
    }
}
 
// Close the ESX service connection
svc.close()
  • Think I figured out my problem and it was here when printing the status I wasn't including the index.  However, doing this breaks the evaluation of the status using 1 or 0.  in the part of the script where status is defined.  

    println "${wildvalue}.status=${status[index]}"

    • Shack's avatar
      Shack
      Icon for Advisor rankAdvisor

      I was able to get this figured out by moving a couple of things around.  

      Instead of using the value of 1 or 0 on line 1 I moved it to line 2

      def status = service?.service?.running //? 1 : 0
      def runningstate = status[index] ? 1 : 0

      Then for my println 

      println "${wildvalue}.status:${runningstate}"

      Everything works

  • I'm not sure what I am doing wrong and would love another set of eyes.