|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Collect system state info. Save it to a JSON file, |
| 4 | +if file already exists, compare it first and report deltas. |
| 5 | +""" |
| 6 | + |
| 7 | +import json |
| 8 | +import os |
| 9 | +import subprocess |
| 10 | +import sys |
| 11 | + |
| 12 | + |
| 13 | +def run_cmd_text(cmd): |
| 14 | + """Execute a shell command and return its output as text.""" |
| 15 | + result = subprocess.run(cmd, shell=True, check=False, |
| 16 | + stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 17 | + universal_newlines=True) |
| 18 | + return result.stdout |
| 19 | + |
| 20 | + |
| 21 | +def run_cmd_json(cmd): |
| 22 | + """Execute a shell command and return its output parsed as JSON.""" |
| 23 | + result = subprocess.run(cmd, shell=True, check=False, |
| 24 | + stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 25 | + universal_newlines=True) |
| 26 | + if result.returncode != 0: |
| 27 | + return {"error": result.stderr.strip()} |
| 28 | + |
| 29 | + ret = json.loads(result.stdout) |
| 30 | + # "decapsulate" the one element arrays that ip and ethtool like return |
| 31 | + if isinstance(ret, list) and len(ret) == 1: |
| 32 | + ret = ret[0] |
| 33 | + return ret |
| 34 | + |
| 35 | + |
| 36 | +def collect_system_state(): |
| 37 | + """Collect network interface information.""" |
| 38 | + state = { |
| 39 | + "links": {}, |
| 40 | + "chans": {}, |
| 41 | + "feat": {}, |
| 42 | + "rings": {}, |
| 43 | + "rss": {}, |
| 44 | + "ntuple": {}, |
| 45 | + } |
| 46 | + |
| 47 | + interfaces = run_cmd_json("ip -j -d link show") |
| 48 | + |
| 49 | + for iface in interfaces: |
| 50 | + ifname = iface['ifname'] |
| 51 | + |
| 52 | + state["links"][ifname] = iface |
| 53 | + |
| 54 | + state["chans"][ifname] = run_cmd_json(f"ethtool -j -l {ifname}") |
| 55 | + state["feat" ][ifname] = run_cmd_json(f"ethtool -j -k {ifname}") |
| 56 | + state["rings"][ifname] = run_cmd_json(f"ethtool -j -g {ifname}") |
| 57 | + state["rss" ][ifname] = run_cmd_json(f"ethtool -j -x {ifname}") |
| 58 | + if "rss-hash-key" in state["rss"][ifname]: |
| 59 | + del state["rss"][ifname]["rss-hash-key"] |
| 60 | + state["ntuple"][ifname] = run_cmd_text(f"ethtool -n {ifname}") |
| 61 | + |
| 62 | + return state |
| 63 | + |
| 64 | + |
| 65 | +def compare_states(current, saved, path=""): |
| 66 | + """Compare current system state with saved state.""" |
| 67 | + |
| 68 | + ret = 0 |
| 69 | + |
| 70 | + if isinstance(current, dict) and isinstance(saved, dict): |
| 71 | + for k in current.keys() | saved.keys(): |
| 72 | + if k in current and k in saved: |
| 73 | + ret |= compare_states(current[k], saved[k], path=f"{path}.{k}") |
| 74 | + else: |
| 75 | + print(f"Saved {path}.{k}:", saved.get(k)) |
| 76 | + print(f"Current {path}.{k}:", current.get(k)) |
| 77 | + ret = 1 |
| 78 | + else: |
| 79 | + if current != saved: |
| 80 | + print(f"Saved {path}:", saved) |
| 81 | + print(f"Current {path}:", current) |
| 82 | + ret = 1 |
| 83 | + |
| 84 | + return ret |
| 85 | + |
| 86 | + |
| 87 | +def main(): |
| 88 | + """Main function to collect and compare network interface states.""" |
| 89 | + output_file = "/tmp/nipa-env-state.json" |
| 90 | + if len(sys.argv) > 1: |
| 91 | + output_file = sys.argv[1] |
| 92 | + |
| 93 | + # Collect current system state |
| 94 | + current_state = collect_system_state() |
| 95 | + exit_code = 0 |
| 96 | + |
| 97 | + # Check if the file already exists |
| 98 | + if os.path.exists(output_file): |
| 99 | + print("Comparing to existing state file: ", end="") |
| 100 | + try: |
| 101 | + with open(output_file, 'r', encoding='utf-8') as f: |
| 102 | + saved_state = json.load(f) |
| 103 | + |
| 104 | + # Compare states |
| 105 | + exit_code = compare_states(current_state, saved_state) |
| 106 | + if exit_code == 0: |
| 107 | + print("no differences detected.") |
| 108 | + except (json.JSONDecodeError, IOError, OSError) as e: |
| 109 | + print("Error loading or comparing:") |
| 110 | + print(e) |
| 111 | + # Save current state to file |
| 112 | + with open(output_file, 'w', encoding='utf-8') as f: |
| 113 | + json.dump(current_state, f, indent=2) |
| 114 | + print(f"Current system state saved to {output_file}") |
| 115 | + |
| 116 | + sys.exit(exit_code) |
| 117 | + |
| 118 | + |
| 119 | +if __name__ == "__main__": |
| 120 | + main() |
0 commit comments