def is_alarm(self,k,result): """ 判断是否进行告警提示,相同告警进行拦截,相同告警在过了间隔时间后才会再次预警 :return: 返回True,则可以进行告警,反之则否 """ history_path = os.path.join(BASE_DIR, '.history') f = FileModify(history_path, autocreate=True) data = self.get_history_data(k) now = int(time.time()) if data and isinstance(data,tuple): alarm_data_3 = eval(data[2]).get('alarm_data').get('3') if alarm_data_3 == result.get('alarm_data').get('3'): time_diff = now - int(data[1]) if time_diff > 3600 * CONFIG.ISOLATE_TIME: f.replace(data[1],now) return True else: return False else: f.replace('^{}.*'.format(k),'{}||{}||{}'.format(k,now,result)) return True f.add('{}||{}||{}'.format(k,now,result)) return True
def set_hosts(ip, hostname): path = '/etc/hosts' f = FileModify(path) f.add('{ip} {hostname}'.format(ip=ip, hostname=hostname))