ZenDMD Tip - Manipulate Events

From Zenoss Wiki
This is the approved revision of this page, as well as being the most recent.
Jump to: navigation, search

Zenoss 4 has a new events system that is quite different from the system that was present in previous versions.

The following is an example script that contains several of the new event functions.

WARNING: The ZEP facade should never, ever be used within event transforms. It is only safe to use this for external scripts. DO NOT put any of this code in event transforms. It can/will cause problems with event processing.

#!/usr/bin/env python
 
###########################
#   ZEP Facade Example    #
###########################
# Created by: Ryan Matte  #
# Updated by: Shane Scott #
###########################
 
# import the stuff that zendmd needs and create the dmd context
import Globals
from Products.ZenUtils.ZenScriptBase import ZenScriptBase
from transaction import commit
dmd = ZenScriptBase(connect=True, noopts=True).dmd
 
# import the stuff that zep needs
from Products.Zuul import getFacade, listFacades
from zenoss.protocols.jsonformat import from_dict
from zenoss.protocols.protobufs.zep_pb2 import EventSummary
from Products.ZenEvents.events2.proxy import EventSummaryProxy
 
# create a context for the sync function
sync = dmd.zport._p_jar.sync
 
# create the zep context
zep = getFacade('zep')
 
# create an event filter to only fetch events with
# a status of 0 or 1 (new or acknowledged)
zep_filter = zep.createEventFilter(status=[0,1])
 
# some possible statuses are...
#
# 0 new
# 1 acknowledged
# 2 suppressed
# 3 closed
# 4 cleared
# 5 aged
 
# Other filter options are possible....
#
# zep_filter = zep.createEventFilter(severity=5, details={'zenoss.device.production_state':1000})
 
# Filter on a specific device name / device title...
#
# zep_filter = zep.createEventFilter(element_identifier=("yourDevice"))
# zep_filter = zep.createEventFilter(element_title=("yourDevice"))
 
# Or a specific component name / component title...
#
# zep_filter = zep.createEventFilter(element_sub_identifier=("yourComponent"))
# zep_filter = zep.createEventFilter(element_sub_title=("yourComponent"))
 
# Examples of creating logic using operator=zep.AND, operator=zep.OR and by nesting filters with subfilter=anotherZepFilter
# logic below: ((event is new OR acknowledged) AND severity is critical) AND (device production state is production OR device priority is 1)
 
# zep_filterA = zep.createEventFilter(status=[0,1], operator=zep.AND, severity=[5])
# zep_filterB = zep.createEventFilter(subfilter=zep_filterA, details={'zenoss.device.production_state':1000})
# zep_filterC = zep.createEventFilter(subfilter=zep_filterB, details={'zenoss.device.priority':1}, operator=zep.OR)
 
# See $ZENHOME/Products/Zuul/facades/zepfacade.py for more information
 
# fetch	the events from	zep using the filter we just created
for summary in zep.getEventSummariesGenerator(filter=zep_filter):
 
    # synchronize each cycle for good measure
    sync()
 
    # create the event context so that the event can be
    # queried/manipulated as you would in an event transform
    # this is done using an event proxy
    evt = EventSummaryProxy(from_dict(EventSummary, summary))
 
    # print some information about the event
    print 'Device: %s' % (evt.device)
    print 'Summary: %s' % (evt.summary)
 
    # fetch all of the details of the event as a list (including event notes):
    zep.getEventSummary(evt.evid)
 
    # specifically fetch event notes as a list
    zep.getEventSummary(evt.evid)['notes']
 
    # create an event filter to use when acknowledging,
    # closing, or reopening a single event (set based on evt.evid)
    evid_filter = zep.createEventFilter(uuid=[evt.evid])
 
    # acknowledge the event
    zep.acknowledgeEventSummaries(eventFilter=evid_filter, userName='admin')
 
    # close the event
    zep.closeEventSummaries(eventFilter=evid_filter, userName='admin')
 
    # reopen the event
    zep.reopenEventSummaries(eventFilter=evid_filter, userName='admin')
 
    # add a note to the event
    zep.addNote(evt.evid, 'My Note', userName='admin')