75 lines
2.3 KiB
Python
75 lines
2.3 KiB
Python
import re
|
|
import sys
|
|
|
|
def parse_file(file_path):
|
|
results = []
|
|
|
|
with open(file_path, "r", encoding="utf-8", errors="ignore") as f:
|
|
for line in f:
|
|
|
|
# Look for lines containing recap info
|
|
if "ok=" in line and "failed=" in line and "unreachable=" in line:
|
|
|
|
# Extract host (first non-time token before :)
|
|
host_match = re.search(r"(?:\d{2}:\d{2}:\d{2}\s+)?(\S+)\s+:", line)
|
|
if not host_match:
|
|
continue
|
|
|
|
host = host_match.group(1)
|
|
|
|
# Extract values individually (order-independent)
|
|
def get_value(field):
|
|
match = re.search(rf"{field}=(\d+)", line)
|
|
return int(match.group(1)) if match else 0
|
|
|
|
ok = get_value("ok")
|
|
changed = get_value("changed")
|
|
unreachable = get_value("unreachable")
|
|
failed = get_value("failed")
|
|
|
|
status = (
|
|
"UNREACHABLE" if unreachable > 0
|
|
else "FAILED" if failed > 0
|
|
else "OK"
|
|
)
|
|
|
|
results.append({
|
|
"host": host,
|
|
"ok": ok,
|
|
"changed": changed,
|
|
"unreachable": unreachable,
|
|
"failed": failed,
|
|
"status": status
|
|
})
|
|
|
|
return results
|
|
|
|
|
|
def print_report(results):
|
|
unreachable_hosts = []
|
|
failed_hosts = []
|
|
|
|
print("\n=== FULL HOST REPORT ===")
|
|
for r in results:
|
|
print(f"{r['host']} -> {r['status']} "
|
|
f"(ok={r['ok']}, changed={r['changed']}, "
|
|
f"failed={r['failed']}, unreachable={r['unreachable']})")
|
|
|
|
if r["status"] == "UNREACHABLE":
|
|
unreachable_hosts.append(r["host"])
|
|
elif r["status"] == "FAILED":
|
|
failed_hosts.append(r["host"])
|
|
|
|
print("\n=== SUMMARY ===")
|
|
|
|
print("\nUNREACHABLE HOSTS:")
|
|
print("\n".join(unreachable_hosts) if unreachable_hosts else " None")
|
|
|
|
print("\nFAILED HOSTS:")
|
|
print("\n".join(failed_hosts) if failed_hosts else " None")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
results = parse_file(sys.argv[1])
|
|
print_report(results)
|