VMware {code}

 View Only

 How to fetch an ESXi host maintenance mode history via any vSphere SDK?

Subeesh KK's profile image
Subeesh KK posted Apr 17, 2025 06:27 AM

Hello All,

I have a requirement to fetch the history details of an esx host maintenance mode details - like how many times the host went on maintenance mode, etc - via vSphere SDK or any other API . Is it possible? 

I tried using vmwarephp library as follows :

                $vhostcon = new \Vmwarephp\Vhost($this->provider->ip . ':443', $this->provider->user, $this->provider->pwd);
                $vhost = $vhostcon->getService();
 
                // 1. Get EventManager ManagedObject Reference
                $eventManagerRef = $vhost->getServiceContent()->eventManager;
 
                // 2. Define the time range
                $beginTime = new \DateTime('-30 days');  // Or your custom start time
                $endTime = new \DateTime();              // Up to now
 
                // 3. Find all hosts
                $hosts = $vhost->findAllManagedObjects('HostSystem', array('name',));
 
                 // 4. Loop over each host and fetch maintenance events
                foreach ($hosts as $host) {
                    $hostName = $host->name;
 
                    // Build EventFilterSpec
                    $eventFilterSpec = new stdClass();
 
                    // Time filter
                    $eventFilterSpec->time = new stdClass();
                    $eventFilterSpec->time->beginTime = $beginTime->format(DATE_ATOM); // ISO 8601
                    $eventFilterSpec->time->endTime = $endTime->format(DATE_ATOM);
 
                    // Entity filter (the host)
                    $eventFilterSpec->entity = new stdClass();
                    $eventFilterSpec->entity->entity = $host->toReference(); // Returns ManagedObjectReference
                    $eventFilterSpec->entity->recursion = 'self'; // Only events directly related to the host
 
                    // Event types filter
                    $eventFilterSpec->eventTypeId = [
                        'EnteredMaintenanceModeEvent',
                        'ExitedMaintenanceModeEvent',
                    ];
 
                    // Wrapper for EventFilterSpec
                    $filterWrapper = new stdClass();
                    $filterWrapper->_this = $eventManagerRef->toReference();     // IMPORTANT!
                    $filterWrapper->filter = $eventFilterSpec;
                    $filterWrapper->maxCount = 100; // Or null to get all
                    $filterWrapper->delayed = false;
                   // Query events - NOT WORKING .. ???
                    $events = $vhost->getServiceContent()->eventManager->QueryEvents($eventManagerRef,$filterWrapper);
 
                    // Display results
                    if (!empty($events)) {
                        echo "=== Events for host: $hostName ===\n";
                        foreach ($events as $event) {
                            echo "[" . $event->createdTime . "] " . $event->_ . "\n"; // fullFormattedMessage
                        }
                        echo "\n";
                    }
                }

The problem is with calling QueryEvents . Can someone help me here please as I am not very experienced in this side. 

Thank you,

Subeesh