2015-06-28 22:32:17 +02:00
|
|
|
#!/usr/bin/python
|
2015-07-31 19:50:27 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2015-06-28 22:32:17 +02:00
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
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
|
|
|
|
|
|
2016-10-03 11:56:27 +02:00
|
|
|
from includes import globalVars # Global variables
|
2015-06-28 22:32:17 +02:00
|
|
|
|
|
|
|
|
#
|
2015-06-29 17:15:20 +02:00
|
|
|
# ListStructure [0..n] = (ID, TimeStamp, msg)
|
2015-06-28 22:32:17 +02:00
|
|
|
#
|
2015-07-31 19:50:27 +02:00
|
|
|
doubleList = []
|
|
|
|
|
|
2015-06-28 22:32:17 +02:00
|
|
|
|
2015-06-29 17:15:20 +02:00
|
|
|
def checkID(typ, id, msg=""):
|
2015-06-28 22:32:17 +02:00
|
|
|
"""
|
|
|
|
|
check if id was called in the last x sec and n entries
|
2015-07-02 09:02:49 +02:00
|
|
|
|
2015-06-28 22:32:17 +02:00
|
|
|
@requires: Configuration has to be set in the config.ini
|
2015-07-02 09:02:49 +02:00
|
|
|
|
2015-06-28 22:32:17 +02:00
|
|
|
@return: True if check was OK
|
|
|
|
|
@return: False if double was found
|
|
|
|
|
"""
|
2015-07-31 19:50:27 +02:00
|
|
|
global doubleList
|
2015-07-02 09:02:49 +02:00
|
|
|
timestamp = int(time.time()) # Get Timestamp
|
2015-06-28 22:32:17 +02:00
|
|
|
|
2015-07-29 18:43:25 +02:00
|
|
|
logging.debug("checkID: %s (%s)", id, msg)
|
2015-07-31 19:50:27 +02:00
|
|
|
for i in range(len(doubleList)):
|
|
|
|
|
(xID, xTimestamp, xMsg) = doubleList[i]
|
2015-06-28 22:32:17 +02:00
|
|
|
# given ID found?
|
2015-06-28 22:37:01 +02:00
|
|
|
# return False if the first entry in double_ignore_time is found, we will not check for younger ones...
|
2015-06-29 17:15:20 +02:00
|
|
|
if id == xID and timestamp < xTimestamp + globals.config.getint("BOSWatch", "doubleFilter_ignore_time"):
|
2015-07-29 18:43:25 +02:00
|
|
|
logging.debug("-- previous id %s is within doubleFilter_ignore_time (%ss)", xID, globals.config.getint("BOSWatch", "doubleFilter_ignore_time"))
|
2015-07-02 09:02:49 +02:00
|
|
|
# if wanted, we have to check the msg additional
|
2015-06-29 17:15:20 +02:00
|
|
|
if "POC" in typ and globals.config.getint("BOSWatch", "doubleFilter_check_msg"):
|
2015-07-29 18:43:25 +02:00
|
|
|
logging.debug("-- compare msg:")
|
|
|
|
|
logging.debug("---- current msg: (%s)", msg.strip())
|
|
|
|
|
logging.debug("---- previous msg: (%s)", xMsg)
|
2015-06-29 17:15:20 +02:00
|
|
|
# if msg is a substring of xMsg we found a double
|
2015-07-29 18:43:25 +02:00
|
|
|
if msg.strip() in xMsg:
|
2015-06-29 17:15:20 +02:00
|
|
|
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
|
2015-06-28 22:32:17 +02:00
|
|
|
return True
|
2015-07-02 09:02:49 +02:00
|
|
|
|
2015-06-28 22:32:17 +02:00
|
|
|
|
2015-06-29 17:15:20 +02:00
|
|
|
def newEntry(id, msg = ""):
|
2015-06-28 22:32:17 +02:00
|
|
|
"""
|
2015-06-28 22:37:01 +02:00
|
|
|
new entry in double alarm list
|
2015-07-02 09:02:49 +02:00
|
|
|
|
2015-06-28 22:32:17 +02:00
|
|
|
@return: nothing
|
|
|
|
|
"""
|
2015-07-31 19:50:27 +02:00
|
|
|
global doubleList
|
2015-07-02 09:02:49 +02:00
|
|
|
timestamp = int(time.time()) # Get Timestamp
|
2015-07-31 19:50:27 +02:00
|
|
|
doubleList.append((id, timestamp, msg.strip()))
|
2015-07-02 09:02:49 +02:00
|
|
|
|
2015-06-30 08:55:46 +02:00
|
|
|
logging.debug("Added %s to doubleList", id)
|
2015-07-02 09:02:49 +02:00
|
|
|
|
2015-06-28 22:32:17 +02:00
|
|
|
# now check if list has more than n entries:
|
2015-07-31 19:50:27 +02:00
|
|
|
if len(doubleList) > globals.config.getint("BOSWatch", "doubleFilter_ignore_entries"):
|
2015-06-28 22:32:17 +02:00
|
|
|
# we have to kill the oldest one
|
2015-07-31 19:50:27 +02:00
|
|
|
doubleList.pop(0)
|