Files
SnarfCode/scripts/probe_all_node_disks.py

64 lines
1.9 KiB
Python

import paramiko
import json
SSH_KEY = r"C:\Users\Snarf\.ssh\id_ed25519_snarfnet"
nodes = {
"snaisvr44hrvstr01": "140.44.4.81",
"snaisvr44hrvstr02": "140.44.4.82",
"snaisvr44hrvstr03": "140.44.4.83",
"snaisvr44hrvstr04": "140.44.4.84"
}
scan_script = """python3 - << 'PYEOF'
import os
import subprocess
import json
# Get blkid
out = subprocess.check_output("blkid", shell=True).decode()
disks = {}
for line in out.splitlines():
if not line.startswith("/dev/sd"): continue
dev = line.split(":")[0]
if "sda" in dev: continue # skip OS disk
uuid = ""
for part in line.split():
if part.startswith("UUID="):
uuid = part.split('"')[1]
disks[dev] = uuid
print(f"Disks found: {len(disks)}")
results = []
for dev, uuid in disks.items():
tmp_mount = f"/tmp/probe_{os.path.basename(dev)}"
os.makedirs(tmp_mount, exist_ok=True)
try:
subprocess.check_call(f"mount {dev} {tmp_mount}", shell=True)
cfg_path = os.path.join(tmp_mount, "longhorn-disk.cfg")
cfg_data = ""
if os.path.exists(cfg_path):
with open(cfg_path) as fp:
cfg_data = fp.read().strip()
results.append({"dev": dev, "uuid": uuid, "cfg": cfg_data})
subprocess.call(f"umount {tmp_mount}", shell=True)
except Exception as e:
results.append({"dev": dev, "uuid": uuid, "error": str(e)})
print(json.dumps(results, indent=2))
PYEOF
"""
for hname, ip in nodes.items():
print(f"\n=================== {hname} ({ip}) ===================")
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
c.connect(ip, username="rancher", key_filename=SSH_KEY, timeout=10)
_, o, e = c.exec_command(f"echo Katzir476! | sudo -S {scan_script}")
print(o.read().decode())
c.close()
except Exception as e:
print("Error:", e)