From 48323d6f667a646a43a764c01aad6458ecf44aba Mon Sep 17 00:00:00 2001 From: Michael Meli Date: Mon, 22 Aug 2022 15:07:00 -0400 Subject: [PATCH] add disk utilization, read, and write metrics --- README.rst | 3 ++ src/synology_dsm/api/core/utilization.py | 42 ++++++++++++++++++++++++ tests/test_synology_dsm.py | 10 ++++++ 3 files changed, 55 insertions(+) diff --git a/README.rst b/README.rst index 753ab9a0..43aef6c2 100644 --- a/README.rst +++ b/README.rst @@ -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 ===") diff --git a/src/synology_dsm/api/core/utilization.py b/src/synology_dsm/api/core/utilization.py index d25946b4..749813d3 100644 --- a/src/synology_dsm/api/core/utilization.py +++ b/src/synology_dsm/api/core/utilization.py @@ -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 diff --git a/tests/test_synology_dsm.py b/tests/test_synology_dsm.py index b7b475a6..957be5e5 100644 --- a/tests/test_synology_dsm.py +++ b/tests/test_synology_dsm.py @@ -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)