forked from Schrolli91/BOSWatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
doubleFilter.py
72 lines (56 loc) · 2.3 KB
/
doubleFilter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
doubleFilter is the central function to filter out double alarms.
You can set the number of historical entries the filter will check
and the time ignoring the id in case of a double alarm
@author: Jens Herrmann
@requires: Configuration has to be set in the config.ini
"""
import logging # Global logger
import time # timestamp for doublealarm
from includes import globalVars # Global variables
#
# ListStructure [0..n] = (Data, TimeStamp, msg)
#
doubleList = []
def checkID(typ, data, msg=""):
"""
check if id was called in the last x sec and n entries
@requires: Configuration has to be set in the config.ini
@return: True if check was OK
@return: False if double was found
"""
timestamp = int(time.time()) # Get Timestamp
logging.debug("checkID: %s (%s)", data, msg)
for (xID, xTimestamp, xMsg) in doubleList:
# given ID found?
# return False if the first entry in double_ignore_time is found, we will not check for younger ones...
if data == xID and timestamp < xTimestamp + globalVars.config.getint("BOSWatch", "doubleFilter_ignore_time"):
logging.debug("-- previous id %s is within doubleFilter_ignore_time (%ss)", xID, globalVars.config.getint("BOSWatch", "doubleFilter_ignore_time"))
# if wanted, we have to check the msg additional
if "POC" in typ and globalVars.config.getint("BOSWatch", "doubleFilter_check_msg"):
logging.debug("-- compare msg:")
logging.debug("---- current msg: (%s)", msg.strip())
logging.debug("---- previous msg: (%s)", xMsg)
# if msg is a substring of xMsg we found a double
if msg.strip() in xMsg:
logging.info("%s double alarm (id+msg): %s within %s second(s)", typ, xID, timestamp-xTimestamp)
return False
else:
logging.info("%s double alarm (id): %s within %s second(s)", typ, xID, timestamp-xTimestamp)
return False
return True
def newEntry(data, msg = ""):
"""
new entry in double alarm list
@return: nothing
"""
global doubleList
timestamp = int(time.time()) # Get Timestamp
doubleList.append((data, timestamp, msg.strip()))
logging.debug("Added %s to doubleList", data)
# now check if list has more than n entries:
if len(doubleList) > globalVars.config.getint("BOSWatch", "doubleFilter_ignore_entries"):
# we have to kill the oldest one
doubleList.pop(0)