Files
SnarfCode/scripts/check_storage_and_drives.py

59 lines
2.4 KiB
Python

import paramiko, json
# 1. Check Spaceport & Aerodrome disks
for name, ip in [('Aerodrome', '140.44.4.11'), ('Spaceport', '140.44.4.41')]:
try:
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(ip, username='root', key_filename=r'C:\Users\Snarf\.ssh\id_ed25519_snarfnet', timeout=10)
_, o, _ = c.exec_command('kubectl get nodes.longhorn.io -n longhorn-system -o json')
data = json.loads(o.read().decode())
total_max = 0
total_avail = 0
num_disks = 0
for n in data['items']:
disks = n.get('status', {}).get('diskStatus', {})
for d_name, d_val in disks.items():
num_disks += 1
total_max += d_val.get('storageMaximum', 0)
total_avail += d_val.get('storageAvailable', 0)
tib = total_max / (1024**4)
gib = total_max / (1024**3)
avail_gib = total_avail / (1024**3)
print(f"{name} Longhorn -> Disks: {num_disks}, Total Storage: {tib:.2f} TiB ({gib:.0f} GiB), Available: {avail_gib:.0f} GiB")
c.close()
except Exception as e:
print(f"{name} -> error: {e}")
# 2. Query Zabbix for NAS storage & drives
import urllib.request, ssl
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
ZABBIX_URL = "http://140.44.4.60:8888/api_jsonrpc.php"
def zbx(method, params, auth=None):
payload = {"jsonrpc": "2.0", "method": method, "params": params, "id": 1}
headers = {"Content-Type": "application/json-rpc"}
if auth:
headers["Authorization"] = f"Bearer {auth}"
req = urllib.request.Request(ZABBIX_URL, data=json.dumps(payload).encode('utf-8'), headers=headers, method="POST")
with urllib.request.urlopen(req, timeout=10, context=ctx) as r:
resp = json.loads(r.read().decode())
return resp.get("result", {})
try:
token = zbx("user.login", {"username": "Admin", "password": "zabbix"})
hosts = zbx("host.get", {"filter": {"name": ["SN-NAS (DS920+)", "SnarfNAS (DS1821+)", "SN-NAS", "SnarfNAS"]}}, token)
print("\nZabbix NAS Hosts:")
for h in hosts:
print(h["hostid"], h["host"], h["name"])
# Get items for storage / disks
items = zbx("item.get", {"hostids": h["hostid"], "search": {"name": "space"}}, token)
for it in items[:5]:
print(" ", it["name"], it.get("lastvalue"))
except Exception as e:
print("Zabbix error:", e)