Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add disk utilization, read, and write metrics #238

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ The ``SynologyDSM`` class can also ``update()`` all APIs at once.
print("Memory Use: " + str(api.utilisation.memory_real_usage) + " %")
print("Net Up: " + str(api.utilisation.network_up()))
print("Net Down: " + str(api.utilisation.network_down()))
print("Disk Util: " + str(api.utilisation.disk_utilization()) + " %")
print("Disk Read: " + str(api.utilisation.disk_read()))
print("Disk Write: " + str(api.utilisation.disk_write()))
print("--")

print("=== Storage ===")
Expand Down
42 changes: 42 additions & 0 deletions src/synology_dsm/api/core/utilization.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,45 @@ def network_down(self, human_readable=False):
return SynoFormatHelper.bytes_to_readable(return_data)
return return_data
return None

@property
def disk(self):
"""Gets disk utilization."""
return self._data.get("disk", {})

def _get_disk(self, disk_id):
"""Function to get specific disk (sata1, total, etc)."""
if disk_id == "total":
return self.disk.get("total", None)
else:
for disk in self.disk.get("disk", []):
if disk["device"] == disk_id:
return disk
return None

def disk_utilization(self):
"""Total percent of disk being used."""
disk = self._get_disk("total")
if disk:
return int(disk["utilization"])
return None

def disk_read(self, human_readable=False):
"""Total bytes being read from disk."""
disk = self._get_disk("total")
if disk:
return_data = int(disk["read_byte"])
if human_readable:
return SynoFormatHelper.bytes_to_readable(return_data)
return return_data
return None

def disk_write(self, human_readable=False):
"""Total bytes being written to disk."""
disk = self._get_disk("total")
if disk:
return_data = int(disk["write_byte"])
if human_readable:
return SynoFormatHelper.bytes_to_readable(return_data)
return return_data
return None
10 changes: 10 additions & 0 deletions tests/test_synology_dsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,3 +583,13 @@ def test_utilisation_network(self, dsm):
assert dsm.utilisation.network_up(True)
assert dsm.utilisation.network_down()
assert dsm.utilisation.network_down(True)

def test_utilisation_disk(self, dsm):
"""Test utilisation disk."""
dsm.utilisation.update()
assert dsm.utilisation.disk
assert dsm.utilisation.disk_utilization()
assert dsm.utilisation.disk_read()
assert dsm.utilisation.disk_read(True)
assert dsm.utilisation.disk_write()
assert dsm.utilisation.disk_write(True)