def exit_check(self): for table in TABLES["check_table"]: cmd = "ip route show table %s" % table p, stdout, stderr = external_cmd(cmd) if stdout == "" or stdout is None: default_gateway = self.default_route[table]["gateway"] default_dev = self.default_route[table]["device"] add_cmd = "ip route add default via %s dev %s table %s" % (default_gateway, default_dev, table) p, stdout, stderr = external_cmd(add_cmd) if p.returncode == 0: logger.info("table %s restore the defualt gateway %s" % (table, default_gateway)) else: logger.info("table %s has gateway ,will exit")
def add_route(self, ip, dev, table): cmd = "ip route add default via %s dev %s table %s" % (ip, dev, table) p, stdout, stderr = external_cmd(cmd) logger.info("will execute the cmd %s" % cmd) if p.returncode != 0: return False return True
def switch_gateway(self, dst_gateway, src_gateway, table, current_gateway, current_dev): src_dev = self.gate_map[src_gateway] dst_dev = self.gate_map[dst_gateway] del_cmd = "ip route del default via %s dev %s table %s" % (src_gateway, src_dev, table) p, stdout, stderr = external_cmd(del_cmd) logger.info("del route :%s, stdout is:%s , stderr is:%s" % (del_cmd, stdout, stderr)) if p.returncode != 0: raise Exception(stderr) add_cmd = "ip route add default via %s dev %s table %s" % (dst_gateway, dst_dev, table) p, stdout, stderr = external_cmd(add_cmd) logger.info("add route :%s, stdout is:%s , stderr is:%s" % (add_cmd, stdout, stderr)) if p.returncode != 0: raise Exception(stderr) send_switch_mail(del_cmd, add_cmd, self.detect_dict, current_gateway, current_dev, table, self.ip_map)
def get_current_gateway(self, table): cmd = "ip route show table %s" % table p, stdout, stderr = external_cmd(cmd) if p.returncode != 0: raise ExecException("get default table gateway failed") gateway = re.findall("default via\s(\d+\.\d+\.\d+\.\d+)\s", stdout) dev = re.findall("default via\s\d+\.\d+\.\d+\.\d+\sdev\s(\w+)", stdout) return gateway[0], dev[0]
def check_ip_gateway(self): # 三线ip 的三条路由检查 ''' 1,检查三线ip 是否有默认网关 2,检查detect_list 和 jump_list 是否有rule 以上如果没有默认都将添加 ''' for ip, table in self.ip_map.items(): if table == "100": map_table = "111" elif table == "200": map_table = "112" else: map_table = "113" cmd = "ip route show table %s" % map_table p, stdout, stderr = external_cmd(cmd) if stdout == "" or stdout is None: gate_ip = self.default_route[table]["gateway"] gate_dev = self.default_route[table]["device"] res = self.add_route(gate_ip, gate_dev, map_table) logger.info("check and ip add route ,execute %s" % cmd) if res == False: msg = "three for ip table add default gateway failed " send_except_mail(msg, self.ip_map) logger.error(msg) exit(1) for to_ip in DETECT_LIST: rule_cmd = "ip rule show |grep %s |grep %s" % (to_ip, map_table) rule_p, rule_stdout, rule_stderr = external_cmd(rule_cmd) if rule_stdout == "" or rule_stdout is None: res = self.add_rule(ip, to_ip, map_table) logger.info("check and ip add rule ,execute %s" % rule_cmd) if res == False: msg = "three for ip rule add default gateway failed " send_except_mail(msg, self.ip_map) logger.error(msg) exit(1)
def check_ping(cmd): """ :return success_nums int: success ping_cost int: time cost """ p, stdout, stderr = external_cmd(cmd) if stdout is None: return 0, 0 success_nums = len(re.findall("64 bytes from\s", stdout)) ping_cost_str = re.findall("\stime=(\d*)", stdout) ping_cost = sum([int(i) for i in ping_cost_str]) return success_nums, ping_cost
def check_three_table(self): for table in TABLES["check_table"]: cmd = "ip route show table %s" % table p, stdout, stderr = external_cmd(cmd) if stdout == "" or stdout is None: raise LoseDefaultGateway("table lose gateway ,will exit...")
def add_rule(self, from_ip, to_ip, table): cmd = "ip rule add from %s to %s table %s pref 2500" % (from_ip, to_ip, table) p, stdout, stderr = external_cmd(cmd) if p.returncode != 0: return False return True