def getSession(self, rule, host): _mdb = self._connect() cursor = _mdb.cursor() query = "SELECT * FROM cm_alert_sessions WHERE rule_id=%(rule_id)s and fixed_ip=%(fixed_ip)s and is_active=1 order by id desc limit 1" cursor.execute(query, {'rule_id': rule, 'fixed_ip':host}) row = cursor.fetchone() _ses = None if row is not None: sesinfo = dict(zip(cursor.column_names, row)) _ses = AlertSession() _ses.id = sesinfo['id'] _ses.rule = sesinfo['rule_id'] _ses.host = sesinfo['fixed_ip'] if isinstance(sesinfo['last_alert'], datetime): temp_time = sesinfo['last_alert'].utctimetuple() elif isinstance(sesinfo['last_alert'], str): temp_time = time.strptime(sesinfo['last_alert'], '%Y-%m-%d %H:%M:%S') else: temp_time = None if temp_time is not None: _ses.lastAlert = int(time.mktime(temp_time)) else: _ses.lastAlert = None _ses.alertTimes = sesinfo['alert_times'] cursor.close() _mdb.close() return _ses
def newSession(self, rule, host, time): nsession = AlertSession() nsession.id = host nsession.host = host nsession.lastAlert = time nsession.alertTimes = 1 _sessions[host] = nsession