VMware Cloud Community
GrafDaniel
Contributor
Contributor

Using pyvmomi to get vCenter alarms and its description

Hello everybody,

I need to get all alarms from the root of the vCenter inventory (please see https://vdc-download.vmware.com/vmwb-repository/dcr-public/6b586ed2-655c-49d9-9029-bc416323cb22/fa0b...). I've tried to query the alarms with this code:

#!/usr/bin/env python3

import sys
import atexit
import ssl
from pyVim.connect import SmartConnect, Disconnect
from pyVmomi import vim

context = None
if sys.version_info[:3] > (2,7,8):
  context = ssl.create_default_context()
  context.check_hostname = False
  context.verify_mode = ssl.CERT_NONE

vSphereConnection = SmartConnect( host='vcenter',
                                  user='administrator@vsphere.local',
                                  pwd='password',
                                  sslContext=context )

vCenterObject = vSphereConnection.RetrieveContent()

View = vCenterObject.viewManager.CreateContainerView(
  vCenterObject.rootFolder,
  [vim.ServiceInstance],
  True
)

for Entry in View.triggeredAlarmState:
  print( Entry.alarm )

But all I get is this error message:

Traceback (most recent call last):
  File "./case.py", line 25, in <module>
    True
  File "/usr/local/lib/python3.6/site-packages/pyVmomi/VmomiSupport.py", line 706, in <lambda>
    self.f(*(self.args + (obj,) + args), **kwargs)
  File "/usr/local/lib/python3.6/site-packages/pyVmomi/VmomiSupport.py", line 512, in _InvokeMethod
    return self._stub.InvokeMethod(self, info, args)
  File "/usr/local/lib/python3.6/site-packages/pyVmomi/SoapAdapter.py", line 1397, in InvokeMethod
    raise obj # pylint: disable-msg=E0702
pyVmomi.VmomiSupport.InvalidArgument: (vmodl.fault.InvalidArgument) {
   dynamicType = <unset>,
   dynamicProperty = (vmodl.DynamicProperty) [],
   msg = 'A specified parameter was not correct: type',
   faultCause = <unset>,
   faultMessage = (vmodl.LocalizableMessage) [],
   invalidProperty = 'type'
}

It looks like vim.ServiceInstance isn't a valid Parameter for vCenterObject.viewManager.CreateContainterView(), isn't it? The code itself works if I use vim.HostSystemfor example.

  (vim.alarm.AlarmState) {
   dynamicType = <unset>,
   dynamicProperty = (vmodl.DynamicProperty) [],
   key = 'alarm-1209.host-9923',
   entity = 'vim.HostSystem:host-9923',
   alarm = 'vim.alarm.Alarm:alarm-1209',
   overallStatus = 'red',
   time = 2020-06-26T13:59:24.355Z,
   acknowledged = true,
   acknowledgedByUser = 'LABDR\\Administrator',
   acknowledgedTime = 2020-10-07T13:32:23.454999Z,
   eventKey = 7252519,
   disabled = <unset>
}

But who can I map the alarm (vim.alarm.Alarm:alarm-1209) to a human readable alarm? I've also tried to query vim.AlarmManager with vCenterObject.viewManager.CreateContainterView(), but it also fails.

Thanks in advance and best regards,

Daniel

0 Kudos
2 Replies
StjepanCD
Contributor
Contributor

Hi,

I found your post yesterday as I was trying to achieve the same goal. I was banging my head on this and found a solution. I have replaced your code after vSphereConnection definition with this code block:

vCenterObject = vSphereConnection.content
alarms = vCenterObject.rootFolder.triggeredAlarmState

for alarm in alarms:
    print( alarm )

Disconnect(vSphereConnection)

It works for me quite well, it returns all alarms that appear at vCenter top level, which was the desired result. I looked at the MOB and understood that I could call content/rootFolder properties directly, rather than creating a view.

Hope this helps, regards

Stjepan

0 Kudos
evgmol
Contributor
Contributor

The code that helped me to get top level Triggered Alarms was:

from pyVim.connect import SmartConnect, Disconnect, SmartConnection
from pyVmomi import vim, vmodl
import ssl
 
context = None
context = ssl.create_default_context()
context.check_hostname = False
context.verify_mode = ssl.CERT_NONE

user = "username"
pwd = "password"

vcs = ["vcenter"]

for vc in vcs:
    si = SmartConnect(host=vc, user=user, pwd=pwd, port=int('443'),sslContext=context)
    content = si.RetrieveContent()
    for ta in content.rootFolder.triggeredAlarmState:
        print(ta.alarm.info.name+','+ta.entity.name+','+ta.overallStatus+','+str(ta.time))
Disconnect(si)
 
Hope that helps.
This post also gives a great explanation how to use API browser in case you find yourself lost in data structures someday: https://www.vcloudnine.de/first-steps-with-python-and-pyvmomi-vsphere-sdk-for-python/