ZenDMD Tip - Manipulate Events

From Zenoss Wiki
Revision as of 13:29, 2 December 2013 by Hackman238 (Talk | contribs)$7

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...

#!/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
# create logic using operator=zep.AND, operator=zep.OR and by nesting filters with subfilter=anotherZepFilter
# Below logic: ((Event is New OR Ack) AND severity is Critical) and (Device production state is Production OR Device priority is One)
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)
 
# some possible statuses are...
#
# 0 new
# 1 acknowledged
# 2 suppressed
# 3 closed
# 4 cleared
# 5 aged
 
# 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_filterC):     # 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')