WindowsStorageMixin implements napalm-device-types' HostStorageMixin, so the host reports its storage and stays a host: - get_disks: Get-PhysicalDisk with medium, bus, Windows' health and the Storage Spaces pool a disk is in; a Storage Spaces virtual disk is no physical disk. - get_disk_smart: Get-StorageReliabilityCounter per disk (temperature, wear, power-on hours, uncorrected read and write errors). A counter that cannot be read is no counter, not an error; disks that report nothing are left out. - get_disk_pools: Storage Spaces' pools. - get_volumes: fixed volumes mounted at a drive letter or in a folder, with file system and health. A disk whose medium Windows cannot tell and whose counters are empty (a virtual disk, a RAID controller's logical disk) reads temperature -1 and health "unknown". Every script probes its cmdlets first. The fixtures are synthetic; the scripts are in harvest.py for the first real host.
74 lines
2.4 KiB
Python
74 lines
2.4 KiB
Python
"""Record what the driver's PowerShell scripts emit on a real Windows host.
|
|
|
|
python tools/harvest.py <host> <user> <label> [--port 5985] [--insecure]
|
|
|
|
Asks for the password, runs every getter script over one WinRM connection and
|
|
writes the raw JSON to tools/harvest-out/<label>/<fixture>.json — the same file
|
|
names as tests/fixtures/synthetic/. harvest-out/ is gitignored: scrub serials,
|
|
MAC and IP addresses and host names before copying anything into tests/.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import getpass
|
|
import pathlib
|
|
|
|
from napalm_windows import WindowsDriver, scheduled_tasks, storage, time_sync
|
|
from napalm_windows import windows as mod
|
|
|
|
SCRIPTS = {
|
|
"facts": mod._PS_FACTS,
|
|
"interfaces": mod._PS_INTERFACES,
|
|
"interfaces_ip": mod._PS_INTERFACES_IP,
|
|
"arp": mod._PS_ARP,
|
|
"routes": mod._PS_ROUTES,
|
|
"services": mod._PS_SERVICES,
|
|
"listening": mod._PS_LISTENING,
|
|
"scheduled_tasks": scheduled_tasks._PS_SCHEDULED_TASKS,
|
|
"time_sync": time_sync._PS_TIME_SYNC,
|
|
"disks": storage._PS_DISKS,
|
|
"storage_pools": storage._PS_STORAGE_POOLS,
|
|
"volumes": storage._PS_VOLUMES,
|
|
}
|
|
|
|
OUT = pathlib.Path(__file__).parent / "harvest-out"
|
|
|
|
|
|
def main() -> None:
|
|
ap = argparse.ArgumentParser(description=__doc__.splitlines()[0])
|
|
ap.add_argument("host")
|
|
ap.add_argument("user")
|
|
ap.add_argument("label", help="subdirectory name, e.g. server2022 or win11")
|
|
ap.add_argument("--port", type=int, default=5986)
|
|
ap.add_argument("--insecure", action="store_true", help="skip certificate validation")
|
|
args = ap.parse_args()
|
|
|
|
driver = WindowsDriver(
|
|
args.host,
|
|
args.user,
|
|
getpass.getpass(f"Password for {args.user}@{args.host}: "),
|
|
optional_args={"port": args.port, "ssl_verify": not args.insecure},
|
|
)
|
|
target = OUT / args.label
|
|
target.mkdir(parents=True, exist_ok=True)
|
|
|
|
driver.open()
|
|
try:
|
|
for name, script in SCRIPTS.items():
|
|
try:
|
|
raw = driver._run_raw(script)
|
|
except Exception as exc: # record the failure, keep harvesting
|
|
raw = ""
|
|
print(f" {name}: FAILED — {exc}")
|
|
else:
|
|
print(f" {name}: {len(raw)} bytes")
|
|
(target / f"{name}.json").write_text(raw + "\n")
|
|
finally:
|
|
driver.close()
|
|
print(f"written to {target}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|