Discussion:
Testing for Event Class Key in transform
jshardlow
2012-10-29 15:52:47 UTC
Permalink
jshardlow [http://community.zenoss.org/people/jshardlow] created the discussion

"Testing for Event Class Key in transform"

To view the discussion, visit: http://community.zenoss.org/message/69604#69604

--------------------------------------------------------------
Hi,

I'm after a bit of help with my Python in testing if an event has an Event Class Key via a defaultmapping (long story). Basically my snippet of code used to work in 2.5.2 (Python 2.4.3), but in 4 (Python 2.6.6) it doesn't with rather disasterous effects.

# In case there is no Event Class Key
try:
  evt.eventClassKey
except Exception:
  eck = 'UNKNOWN'
else:
  eck = evt.eventClassKey
...

if ( "gtr" in eck ) or ( "GTR" in eck ) or ( "exp" in evt.device ):


The problem I have is that sometimes an event comes in with no Event Class Key, the exception doesn't get it and the transform bombs out at the if statement. This in turn throws out thousands of errors:

zeneventd.log:
2012-10-29 15:44:23,119 WARNING zen.Events: Error processing transform/mapping on Event Class /instances/DEFAULTMAPPING


And from the actual event that appears thousands of times:
messageProblem with line 28: if ( "gtr" in eck ) or ( "GTR" in eck ) or ( "exp" in evt.device ):
exceptionTypeError: argument of type 'NoneType' is not iterable


Can anyone help point me in the right direction on how to deal with this? Should I be using exception AttributeError or exception TypeError or even something else? Thanks in advance!
--------------------------------------------------------------

Reply to this message by replying to this email -or- go to the discussion on Zenoss Community
[http://community.zenoss.org/message/69604#69604]

Start a new discussion in zenoss-users by email
[discussions-community-forums-zenoss--***@community.zenoss.org] -or- at Zenoss Community
[http://community.zenoss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2003]
pwarren
2012-10-30 01:01:29 UTC
Permalink
pwarren [http://community.zenoss.org/people/pwarren] created the discussion

"Re: Testing for Event Class Key in transform"

To view the discussion, visit: http://community.zenoss.org/message/69606#69606

--------------------------------------------------------------
Try this:

try:
  eck = evt.eventClassKey if evt.eventClassKey is not None else ''
except Exception:
  eck = 'UNKNOWN'

if ( "gtr" in eck ) or ( "GTR" in eck ) or ( "exp" in evt.device ):

I think the issue you are running into is that eventClassKey is returning None instead of the expected AttributeError. The above code will work around this and set 'eck' to the empty string if it is not None, which should make the 'in' tests below work.

-- Philip Warren
--------------------------------------------------------------

Reply to this message by replying to this email -or- go to the discussion on Zenoss Community
[http://community.zenoss.org/message/69606#69606]

Start a new discussion in zenoss-users by email
[discussions-community-forums-zenoss--***@community.zenoss.org] -or- at Zenoss Community
[http://community.zenoss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2003]
jshardlow
2012-11-02 07:51:56 UTC
Permalink
jshardlow [http://community.zenoss.org/people/jshardlow] created the discussion

"Re: Testing for Event Class Key in transform"

To view the discussion, visit: http://community.zenoss.org/message/69681#69681

--------------------------------------------------------------
I think that may have done the trick. Thanks. I need to do some more testing to make sure, and I can never quite tell when the problem occurs.
--------------------------------------------------------------

Reply to this message by replying to this email -or- go to the discussion on Zenoss Community
[http://community.zenoss.org/message/69681#69681]

Start a new discussion in zenoss-users by email
[discussions-community-forums-zenoss--***@community.zenoss.org] -or- at Zenoss Community
[http://community.zenoss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2003]
Loading...