def process_rules(self): """ Process string from firewall-cmd application output on EL7, matches ANY source/dest address rule specifications Note: the expected input is line separated <port>/<proto> pairs, this will give any explicitly allowed rules added by IML and other applications to the default zone but will not list named services enabled in firewalld Empty string is a valid input indicating no explicitly added port/proto rules :return: None on success/valid input, error string otherwise """ result = self.remote_access_func(self.address, self.firewall_list_cmd) if result.rc != 0: from iml_common.lib.shell import Shell raise RuntimeError( """process_rules(): remote shell command failed unexpectedly (%s), is firewall-cmd running? (%s) (%s) systemctl status firewalld: %s systemctl status polkit: %s journalctl -n 100: %s""" % ( result.rc, result.stdout, result.stderr, Shell.run(["systemctl", "status", "firewalld"]).stdout, Shell.run(["systemctl", "status", "polkit"]).stdout, Shell.run(["journalctl", "-n", "100"]).stdout, ) ) if result.stdout.strip() == "": return None # handle output separated by either new-line chars, spaces, or both tokens = [token for token in result.stdout.replace("\n", " ").split() if token] # as we are reading in a new firewall table content, reset 'rules' member list self.rules = [] index = 0 while index < len(tokens): match = re.search("(?P<port>\d+)\/(?P<proto>\w+)", tokens[index]) if match: # Note: because we are uncertain about the command used to obtain the input string, assume persist=False self.rules.append(self.firewall_rule(match.group("port"), match.group("proto"))) else: raise RuntimeError( 'process_rules(): "%s" command output contains unexpected firewall-cmd output' % self.firewall_list_cmd ) index += 1 return None
def host_test(address, issue_num): def print_result(r): return "rc: %s\n\nstdout:\n%s\n\nstderr:\n%s" % \ (r.rc, r.stdout, r.stderr) ping_result1 = Shell.run(['ping', '-c', '1', '-W', '1', address]) ping_result2_report = "" ip_addr_result = Shell.run(['ip', 'addr', 'ls']) ip_route_ls_result = Shell.run(['ip', 'route', 'ls']) try: gw = [ l for l in ip_route_ls_result.stdout.split('\n') if l.startswith("default ") ][0].split()[2] ping_gw_result = Shell.run(['ping', '-c', '1', '-W', '1', gw]) ping_gw_report = "\nping gateway (%s): %s" % \ (gw, print_result(ping_gw_result)) except: ping_gw_report = "\nUnable to ping gatewy. " \ "No gateway could be found in:\n" % \ ip_route_ls_result.stdout if ping_result1.rc != 0: time.sleep(30) ping_result2 = Shell.run( ['ping', '-c', '1', '-W', '1', address]) ping_result2_report = "\n30s later ping: %s" % \ print_result(ping_result2) msg = "Error connecting to %s: %s.\n" \ "Please add the following to " \ "https://github.com/intel-hpdd/intel-manager-for-lustre/issues/%s\n" \ "Performing some diagnostics...\n" \ "ping: %s\n" \ "ifconfig -a: %s\n" \ "ip route ls: %s" \ "%s" \ "%s" % \ (address, e, issue_num, print_result(ping_result1), print_result(ip_addr_result), print_result(ip_route_ls_result), ping_gw_report, ping_result2_report) logger.error(msg) DEVNULL = open(os.devnull, 'wb') p = subprocess.Popen(['sendmail', '-t'], stdin=subprocess.PIPE, stdout=DEVNULL, stderr=DEVNULL) p.communicate(input=b'To: [email protected]\n' 'Subject: GH#%s\n\n' % issue_num + msg) p.wait() DEVNULL.close()
def host_test(address, issue_num): def print_result(r): return "rc: %s\n\nstdout:\n%s\n\nstderr:\n%s" % (r.rc, r.stdout, r.stderr) ping_result1 = Shell.run(["ping", "-c", "1", "-W", "1", address]) ping_result2_report = "" ip_addr_result = Shell.run(["ip", "addr", "ls"]) ip_route_ls_result = Shell.run(["ip", "route", "ls"]) try: gw = [l for l in ip_route_ls_result.stdout.split("\n") if l.startswith("default ")][0].split()[2] ping_gw_result = Shell.run(["ping", "-c", "1", "-W", "1", gw]) ping_gw_report = "\nping gateway (%s): %s" % (gw, print_result(ping_gw_result)) except: ping_gw_report = ( "\nUnable to ping gatewy. " "No gateway could be found in:\n" % ip_route_ls_result.stdout ) if ping_result1.rc != 0: time.sleep(30) ping_result2 = Shell.run(["ping", "-c", "1", "-W", "1", address]) ping_result2_report = "\n30s later ping: %s" % print_result(ping_result2) msg = ( "Error connecting to %s: %s.\n" "Please add the following to " "https://github.com/whamcloud/integrated-manager-for-lustre/issues/%s\n" "Performing some diagnostics...\n" "ping: %s\n" "ifconfig -a: %s\n" "ip route ls: %s" "%s" "%s" % ( address, e, issue_num, print_result(ping_result1), print_result(ip_addr_result), print_result(ip_route_ls_result), ping_gw_report, ping_result2_report, ) ) logger.error(msg) DEVNULL = open(os.devnull, "wb") p = subprocess.Popen(["sendmail", "-t"], stdin=subprocess.PIPE, stdout=DEVNULL, stderr=DEVNULL) p.communicate(input=b"To: [email protected]\n" b"Subject: GH#%s\n\n" % issue_num + msg) p.wait() DEVNULL.close()