Files
SnarfCode/scripts/query_synology_dsm_storage.py

63 lines
3.1 KiB
Python

import urllib.request, urllib.parse, ssl, json
ctx = ssl._create_unverified_context()
for ip, nas_name in [('14.10.10.71', 'SN-NAS (RS810+)'), ('14.10.10.72', 'SnarfNAS (DS416play)')]:
print(f"\n=== Querying {nas_name} ({ip}) ===")
logged_in = False
for user in ['snarf', 'admin']:
params = urllib.parse.urlencode({
'api': 'SYNO.API.Auth',
'version': '3',
'method': 'login',
'account': user,
'passwd': 'Katzir476!',
'session': 'Storage',
'format': 'sid'
})
url = f'https://{ip}:5001/webapi/auth.cgi?{params}'
req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
try:
with urllib.request.urlopen(req, context=ctx, timeout=4) as r:
res = json.loads(r.read().decode())
if res.get('success'):
sid = res['data']['sid']
print(f"Logged in successfully as {user}!")
logged_in = True
st_url = f'https://{ip}:5001/webapi/entry.cgi?api=SYNO.Storage.CGI.Storage&version=1&method=load_info&_sid={sid}'
req2 = urllib.request.Request(st_url, headers={'User-Agent': 'Mozilla/5.0'})
with urllib.request.urlopen(req2, context=ctx, timeout=6) as r2:
st_data = json.loads(r2.read().decode())
data = st_data.get('data', {})
vols = data.get('volumes', [])
disks = data.get('disks', [])
storage_pools = data.get('storagePools', [])
print(f"Disks count: {len(disks)}")
total_raw_tib = 0
for d in disks:
sz = int(d.get('size_total', 0)) / (1024**4)
total_raw_tib += sz
print(f" Drive {d.get('name') or d.get('id')}: {d.get('model', 'Unknown')}, Size: {sz:.2f} TiB, Status: {d.get('status')}")
print(f"Total Raw Drive Capacity: {total_raw_tib:.2f} TiB")
print(f"Volumes count: {len(vols)}")
total_usable_tib = 0
total_used_tib = 0
for v in vols:
v_tot = int(v.get('size_total', 0)) / (1024**4)
v_usd = int(v.get('size_used', 0)) / (1024**4)
total_usable_tib += v_tot
total_used_tib += v_usd
print(f" Volume {v.get('name', 'vol')}: Usable: {v_tot:.2f} TiB, Used: {v_usd:.2f} TiB, FS: {v.get('fs_type')}")
print(f"Total Usable Storage: {total_usable_tib:.2f} TiB (Used: {total_used_tib:.2f} TiB)")
break
else:
print(f"Login failed for {user}: {res}")
except Exception as e:
print(f"Error connecting as {user}: {e}")
if not logged_in:
print(f"Could not log into {ip}")