Developer's Guide

  • Docs Home
  • Community Home

3. Adding an Event Class

Event classes can be added easily through the UI. If you need to use an event class internally, however, you need to make sure that class will always be available, which involves several more steps.

3.1. Add to ZenEventClasses

Add a definition of the name of your new event class to Products/ZenEvents/ZenEventClasses:

...
My_New_Class = "/My/New/Class"

Now your event class is centralized and can be imported wherever you need to use it, e.g.:

...
from Products.ZenEvents.ZenEventClasses import My_New_Class
...
if thing.evclass == My_New_Class:
...

3.2. Add the class to the import XML

Several event classes are imported from XML by zenload just after the ZODB is created. To include your new event class in this import, add an <object> element describing it to Products/ZenModel/data/events.xml. Be sure to nest it inside the classes that already exist, if appropriate. For example, if your new class is "/Status/NewClass", you would add it inside the <object id='Status'> that already exists:

...
<object id='Status' module='Products.ZenEvents.EventClass' class='EventClass'>
<!--This event exists already -->
...
<object id='NewClass' module='Products.ZenEvents.EventClass' class='EventClass'>
<!--This is your new event -->
</object>
></object>

3.3. Write a migrate script

Since your code is no longer backwards-compatible, you need to add the new event class to databases that have already been created by writing a migrate script. (See the section on migrating for more detailed information). Create a new script in Products/ZenModel/migrate with an unique name (here neweventclasses.py). Here's an example:

__doc__="""Add new classes to EventManager
"""
...
import Migrate
...
class NewEventClasses(Migrate.Step):
	version = Migrate.Version(1, 1, 0) # Replace this with the correct version
	def cutover(self, dmd):
		dmd.Events.createOrganizer("/My/Event/Class")
		dmd.Events.createOrganizer("/My/Event/Class2") # Add multiple new classes in the same migrate script
		dmd.ZenEventManager.buildRelations()

NewEventClasses()

Next, add your migrate script to Products/ZenModel/migrate/__init__.py:

...
import neweventclasses

Now

zenmigrate --dont-commit

to make sure your class is created properly.

Once you're satisfied with your changes, make the changes permanent with zenmigrate.

$ zenmigrate