I am trying to write some code that detects “orphaned” VMs - VMs that have been removed in vCenter but are still sitting as Instances and Servers in Morpheus. The only thing I thought I could use in the API to figure this out, was the powerState. I figured it would be on or off if the instance was in vCenter, but it would be set to unknown if it was not found. I figured I could trigger a cloud sync, then look for this value after waiting some length of time. But, I have an instance that has been deleted in vCenter, and the power state is still set to “on” after quite a lengthy period of time, so I am not questioning the reliability of this method of ascertaining the existence of the VM. Any guidance or suggestions on this?
I would delete discovered vm from morpheus if the status is unknown or failed. If they do exist in vmware, they would be synced again with correct info, if not then it was orphaned and removed from morpheus
example script: change the query params
Here is a snippet of the code I am using, which is done in Python because I’m Powershell Ignorant.
# JSON Output of results pprint.pprint ("Listing Possible Orphaned VMs...") ctr = 0 for object in results['servers']: if object["powerState"] == "unknown": ctr = ctr+1 pprint.pprint("----------------------------------------------------") pprint.pprint (object["name"]+" on Cloud "+object["zone"]["name"]+" managed: "+str(object["computeServerType"]["managed"])) #pprint.pprint (object["computeServerType"]["managed"]) remove = input("Remove from Morpheus (Y/N/Q)?: ") if "y" == str.lower(remove): confirm = input(" Are you sure (Y/N/Q)?: ") if "y" == str.lower(confirm): pprint.pprint("Removing " + object["name"] + ".") append = "servers" + "/" + str(object["id"]) options = [('removeResources','on'),('removeInstances','on'),('force','on')] results = morpheus.call("delete", append, options=options) pprint.pprint("DEL RESULTS") pprint.pprint(results)
The problem I am facing, is that the powerState is set to “on”, and never seems to change to “unknown”. All the VM stats are also fully populated, leaving me in a situation where I can’t really tell if the VM is there or not. I also called getInstances also, and same thing there (so at least they’re aligned, which is good).
We have some guys on the other side of the house that use VRA, and I checked on what VRA does. It sets a field to “missing”. I think semantically speaking, that’s a bit more clear. The other challenge, is in figuring out how long to wait for a result, since you have to kind of poll for the result. You can force the cloud sync in the API, but there is no “notification” of when that cloud sync has finished (right?). So you need to wait on a timer for x length of time, then go back to the well to see if the value has changed.