Skip to content

Commit

Permalink
query: disk: add mountdir and device members
Browse files Browse the repository at this point in the history
  • Loading branch information
Toni500github committed Sep 7, 2024
1 parent 784acc6 commit cecb8e5
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 21 deletions.
4 changes: 4 additions & 0 deletions include/query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string_view>& paths);
Expand All @@ -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;
Expand Down
7 changes: 5 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion src/parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
59 changes: 41 additions & 18 deletions src/query/unix/disk.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include <mntent.h>
#include <algorithm>
#include <cstdio>
#include <filesystem>

#include "query.hpp"
#include "util.hpp"
Expand All @@ -9,42 +11,57 @@ using namespace Query;
Disk::Disk(const std::string_view path, std::vector<std::string_view>& 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<float>(m_statvfs.f_blocks * m_statvfs.f_frsize);
m_disk_infos.used_amount =
static_cast<float>((m_statvfs.f_blocks * m_statvfs.f_frsize) -
(m_statvfs.f_bavail * m_statvfs.f_frsize));

m_disk_infos.free_amount = static_cast<float>(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<float>(m_statvfs.f_blocks * m_statvfs.f_frsize);
m_disk_infos.free_amount = static_cast<float>(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
Expand All @@ -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; }

0 comments on commit cecb8e5

Please sign in to comment.