Difference between revisions of "Transforms Tip: Transforms based on Time"

From Zenoss Wiki
Jump to: navigation, search
Line 20: Line 20:
 
         evt._action = 'drop'
 
         evt._action = 'drop'
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
== Example 02 ==
 +
If we want to specify a timezone...
 +
 +
<syntaxhighlight lang=python>
 +
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'
 +
</syntaxhighlight>
 +
 +
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: ====
 
==== The following are the values which can be accessed: ====

Revision as of 13:47, 13 October 2017

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 02

If we want to specify a timezone...

 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