Difference between revisions of "ZenDMD Tip - Manipulate Events"

From Zenoss Wiki
Jump to: navigation, search
Line 31: Line 31:
  
 
# create an event filter to only fetch events with
 
# create an event filter to only fetch events with
# a status of 0 or 1 (new or acknowledged)
+
# create logic using operator=zep.AND, operator=zep.OR and by nesting filters with subfilter=anotherZepFilter
zep_filter = zep.createEventFilter(status=[0,1])
+
# 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...
 
# some possible statuses are...
Line 43: Line 46:
 
# 5 aged
 
# 5 aged
  
# Other filter options are possible....
+
# See $ZENHOME/Products/Zuul/facades/zepfacade.py for more information
#
+
# zep_filter = zep.createEventFilter(severity=5, details={'zenoss.device.production_state':1000})
+
  
 
# fetch the events from zep using the filter we just created
 
# fetch the events from zep using the filter we just created
for summary in zep.getEventSummariesGenerator(filter=zep_filter):
+
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)
  
    # synchronize each cycle for good measure
+
     # fetch all of the details of the event as a list (including event notes):   zep.getEventSummary(evt.evid)
    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
 
     # specifically fetch event notes as a list
Line 85: Line 73:
  
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
--Shane Scott (Hackman238)
  
 
[[Category:Tips]]
 
[[Category:Tips]]
 
[[Category:ZenDMD]]
 
[[Category:ZenDMD]]

Revision as of 13:28, 2 December 2013

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 #
##########################
 
# 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')

--Shane Scott (Hackman238)