Transforms Tip: Transforms based on Time

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

In some cases, you may want to have certain actions kick off based on the time that the event comes in. Here are some quick examples of how you could do this...

Example 01

In the following example, we want to drop an event for the device named "ottawa" between 3am and 5am:

 import time
 if evt.device == 'ottawa':
     if time.localtime()[3] >= 3 and time.localtime()[3] < 5:
         evt._action = 'drop'

Example 02

If we wanted to do the same thing, but for 3pm to 5pm we would do:

 import time
 if evt.device == 'ottawa':
     if time.localtime()[3] >= 15 and time.localtime()[3] < 17:
         evt._action = 'drop'

Example 03

If we want to specify a timezone (Note that by default this is necessary in Zenoss version 5 or higher, as the docker container's timezone is UTC by default. This can be adjusted at the container level by adding a line like TZ='America/New_York' to /etc/default/serviced on each host where serviced is running, then restarting serviced)...

 import os
 import time
 os.environ['TZ'] = 'Europe/Rome'
 if evt.device == 'ottawa':
     if time.localtime()[3] >= 15 and time.localtime()[3] < 17:
         evt._action = 'drop'

You can find a list of timezones here: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones

The following are the values which can be accessed:

Current Year: time.localtime()[0]
Current Month: time.localtime()[1]
Current Day: time.localtime()[2]
Current Hour: time.localtime()[3]
Current Minute: time.localtime()[4]
Current Second: time.localtime()[5]
Current Day of Week: time.localtime()[6]

Day of the week index:

0: Monday
1: Tuesday
2: Wednesday
3: Thursday
4: Friday
5: Saturday
6: Sunday