From cecb8e589474d08819bbf5484536a61a0b867cd9 Mon Sep 17 00:00:00 2001 From: Toni500git Date: Sat, 7 Sep 2024 13:59:41 +0200 Subject: [PATCH] query: disk: add mountdir and device members --- include/query.hpp | 4 +++ src/main.cpp | 7 +++-- src/parse.cpp | 4 ++- src/query/unix/disk.cpp | 59 ++++++++++++++++++++++++++++------------- 4 files changed, 53 insertions(+), 21 deletions(-) diff --git a/include/query.hpp b/include/query.hpp index cb03360..67ecbd2 100644 --- a/include/query.hpp +++ b/include/query.hpp @@ -205,6 +205,8 @@ class Disk float free_amount = 0; float used_amount = 0; std::string typefs; + std::string device; + std::string mountponit; }; Disk(const std::string_view path, std::vector& paths); @@ -213,6 +215,8 @@ class Disk float& free_amount(); float& used_amount(); std::string& typefs(); + std::string& device(); + std::string& mountponit(); private: static struct statvfs m_statvfs; diff --git a/src/main.cpp b/src/main.cpp index 8f13591..d334e87 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -176,8 +176,9 @@ swap free_perc : percentage of available amount of the swapfile in total [6.71%] # same comments as RAM (above) -# note: I mean literally /path/to/fs -# e.g disk(/) +# note: the module can have either a device path +# or a filesystem path +# e.g disk(/) or disk(/dev/sda5) disk(/path/to/fs) disk : used and total amount of disk space (auto) with type of filesystem [360.02 GiB / 438.08 GiB - ext4] used : used amount of disk space (auto) [360.02 GiB] @@ -186,6 +187,8 @@ disk(/path/to/fs) used_perc : percentage of used amount of the disk in total [82.18%] free_perc : percentage of available amount of the disk in total [17.82%] fs : type of filesystem [ext4] + device : path to device [/dev/sda5] + mountdir : path to the device mount point [/] # usually people have 1 GPU in their host, # but if you got more than 1 and want to query it, diff --git a/src/parse.cpp b/src/parse.cpp index ea68d2e..a420506 100644 --- a/src/parse.cpp +++ b/src/parse.cpp @@ -1008,7 +1008,9 @@ void addValueFromModule(systemInfo_t& sysInfo, const std::string& moduleName, co switch (moduleMember_hash) { - case "fs"_fnv1a16: SYSINFO_INSERT(query_disk.typefs()); break; + case "fs"_fnv1a16: SYSINFO_INSERT(query_disk.typefs()); break; + case "device"_fnv1a16: SYSINFO_INSERT(query_disk.device()); break; + case "mountdir"_fnv1a16: SYSINFO_INSERT(query_disk.mountponit()); break; // clang-format off case "disk"_fnv1a16: diff --git a/src/query/unix/disk.cpp b/src/query/unix/disk.cpp index 3b3ca02..4baf44a 100644 --- a/src/query/unix/disk.cpp +++ b/src/query/unix/disk.cpp @@ -1,5 +1,7 @@ +#include #include #include +#include #include "query.hpp" #include "util.hpp" @@ -9,42 +11,57 @@ using namespace Query; Disk::Disk(const std::string_view path, std::vector& paths) { if (std::find(paths.begin(), paths.end(), path) == paths.end()) - { debug("DISK: adding {} to disk paths", path); paths.push_back(path); } + paths.push_back(path); else return; - if (statvfs(path.data(), &m_statvfs) != 0) + if (!std::filesystem::exists(path)) { - error("stat() failed: "); - perror(""); - error("Failed to get disk info"); - memset(&m_statvfs, 0, sizeof(struct statvfs)); + // if user is using disk.disk or disk.fs + // then let's just "try" to remove it + m_disk_infos.typefs = MAGIC_LINE; + m_disk_infos.device = MAGIC_LINE; + m_disk_infos.mountponit = MAGIC_LINE; + return; } - m_disk_infos.total_amount = static_cast(m_statvfs.f_blocks * m_statvfs.f_frsize); - m_disk_infos.used_amount = - static_cast((m_statvfs.f_blocks * m_statvfs.f_frsize) - - (m_statvfs.f_bavail * m_statvfs.f_frsize)); - - m_disk_infos.free_amount = static_cast(m_statvfs.f_bfree * m_statvfs.f_frsize); - FILE* mountsFile = setmntent("/proc/mounts", "r"); if (mountsFile == NULL) - die("setmntent() failed. Could not get disk info"); + { + perror("setmntent"); + error("setmntent() failed. Could not get disk info"); + return; + } struct mntent* pDevice; while ((pDevice = getmntent(mountsFile))) { - debug("pDevice->mnt_dir = {}", pDevice->mnt_dir); - if (path == pDevice->mnt_dir) + debug("pDevice->mnt_dir = {} && pDevice->mnt_fsname = {}", pDevice->mnt_dir, pDevice->mnt_fsname); + if (path == pDevice->mnt_dir || path == pDevice->mnt_fsname) { m_disk_infos.typefs = pDevice->mnt_type; break; } } + + m_disk_infos.device = pDevice->mnt_fsname; + m_disk_infos.mountponit = pDevice->mnt_dir; + + const std::string_view statpath = (hasStart(path, "/dev") ? pDevice->mnt_dir : path); + + if (statvfs(statpath.data(), &m_statvfs) != 0) + { + perror("statvfs"); + error("Failed to get disk info"); + return; + } + + m_disk_infos.total_amount = static_cast(m_statvfs.f_blocks * m_statvfs.f_frsize); + m_disk_infos.free_amount = static_cast(m_statvfs.f_bfree * m_statvfs.f_frsize); + m_disk_infos.used_amount = m_disk_infos.total_amount - m_disk_infos.free_amount; + + endmntent(mountsFile); - if (mountsFile) - fclose(mountsFile); } // clang-format off @@ -59,3 +76,9 @@ float& Disk::free_amount() std::string& Disk::typefs() { return m_disk_infos.typefs; } + +std::string& Disk::mountponit() +{ return m_disk_infos.mountponit; } + +std::string& Disk::device() +{ return m_disk_infos.device; }