Пример #1
0
class Alert(object):
    """description of class"""
    def __init__(self, config):
        self.config_=config
        self.statsStateDict_=SortedDict()
        self.monitorStateDict_=SortedDict()
        return
    def displayAlerts(self):
        for time in self.statsStateDict_:
            if self.statsStateDict_[time].printed_ == bool(False):
                print(f'stats for last {self.config_.statsTime_} secs for {time} is {self.statsStateDict_[time]}')
        for time in self.monitorStateDict_:
            if self.monitorStateDict_[time].printed_ == bool(False):
                print(f'{self.monitorStateDict_[time]}')
        
        return
    def updateAlertState(self,logState):
        if not logState.keys():
            return

        lastInsertedTime = logState.keys()[-1]
        if lastInsertedTime in self.statsStateDict_.keys():
            logline = logState.getElem(int(lastInsertedTime))[-1]#last inserted element in the list
            self.statsStateDict_[lastInsertedTime].increment_total_hits()
            self.statsStateDict_[lastInsertedTime].increment_hits_per_host(logline.getRemoteHost())
            self.statsStateDict_[lastInsertedTime].increment_hits_per_user(logline.getUser())
            self.statsStateDict_[lastInsertedTime].increment_hits_per_request(logline.getRequest())
            self.statsStateDict_[lastInsertedTime].increment_hits_per_status(logline.getStatus())
            self.statsStateDict_[lastInsertedTime].increment_hits_per_section(logline.getRequest())
        else:
            statsTimeBack = logState.getLowerBound(int(lastInsertedTime)-int(self.config_.statsTime_))
            diffStartTime = statsTimeBack[0]
            if int(lastInsertedTime)-int(diffStartTime) == self.config_.statsTime_:
                count=0
                currentStatsState=StatsState()
                for time in statsTimeBack:
                    temp = logState.getElem(time)
                    count += len(temp)
                    for logline in temp:
                        currentStatsState.increment_hits_per_host(logline.getRemoteHost())
                        currentStatsState.increment_hits_per_user(logline.getUser())
                        currentStatsState.increment_hits_per_request(logline.getRequest())
                        currentStatsState.increment_hits_per_status(logline.getStatus())
                        currentStatsState.increment_hits_per_section(logline.getRequest())
                currentStatsState.set_total_hits(count)
                self.statsStateDict_._setitem(lastInsertedTime,currentStatsState)
   
        if int(lastInsertedTime) not in self.monitorStateDict_.keys():
            MonitorTimeBack = logState.getLowerBound(int(lastInsertedTime)-int(self.config_.monitorTime_))
            diffStartTime = MonitorTimeBack[0]
            if int(lastInsertedTime) - int(diffStartTime) == int(self.config_.monitorTime_):
                count =0
                currentMonitorState=MonitorState(int(lastInsertedTime),count,self.config_)
                for time in MonitorTimeBack:
                    temp = logState.getElem(time)
                    count += len(temp)
                currentMonitorState.totalHits_=count
                self.monitorStateDict_[lastInsertedTime]=currentMonitorState
        else:
            self.monitorStateDict_.get(int(lastInsertedTime)).totalHits_ +=1
            self.monitorStateDict_.get(int(lastInsertedTime)).printed_=False