This commit is contained in:
Sukru Senli 2025-11-04 10:22:51 +01:00
parent a8daca910b
commit 2d9bbfc05e
2 changed files with 66 additions and 39 deletions

View file

@ -280,7 +280,7 @@ DMOBJ tDeviceInfoObj[] = {
#endif
#ifdef SYSMNGR_DISK_STATUS
{CUSTOM_PREFIX"DiskStatus", &DMREAD, NULL, NULL, NULL, NULL, NULL, NULL, tDeviceInfoDiskStatusObj, tDeviceInfoDiskStatusParams, NULL, BBFDM_BOTH},
{CUSTOM_PREFIX"Storage", &DMREAD, NULL, NULL, NULL, NULL, NULL, NULL, tDeviceInfoDiskStatusObj, tDeviceInfoDiskStatusParams, NULL, BBFDM_BOTH},
#endif
#ifdef SYSMNGR_PROCESS_STATUS

View file

@ -396,6 +396,30 @@ static void enumerate_physical_disks(void)
disk_count, total_partitions);
}
static int get_disk_stats(const char *mount_point, unsigned long long *total,
unsigned long long *used, unsigned long long *available)
{
struct statvfs statbuf;
if (statvfs(mount_point, &statbuf) != 0) {
syslog(LOG_ERR, "DiskStatus: Failed to get stats for %s", mount_point);
return -1;
}
unsigned long long blksize, blocks, availblks;
// Use f_frsize (fragment size) if available, otherwise f_bsize
blksize = statbuf.f_frsize ? statbuf.f_frsize : statbuf.f_bsize;
blocks = statbuf.f_blocks;
availblks = statbuf.f_bavail; // IMPORTANT: Available to non-root users
*total = blocks * blksize;
*available = availblks * blksize;
*used = *total - *available; // Used = Total - Available (to non-root)
return 0;
}
/*************************************************************
* GET & SET PARAM - DiskStatus.Disk.{i}. (Mounted Filesystems)
**************************************************************/
@ -430,10 +454,9 @@ static int get_disk_instance_mount_point(char *refparam, struct dmctx *ctx, void
static int get_disk_instance_total_space(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value)
{
partition_entry *entry = (partition_entry *)((struct dm_data *)data)->additional_data;
struct statvfs stat;
unsigned long long total, used, available;
if (entry && statvfs(entry->mount_point, &stat) == 0) {
unsigned long long total = (unsigned long long)stat.f_blocks * stat.f_frsize;
if (entry && get_disk_stats(entry->mount_point, &total, &used, &available) == 0) {
dmasprintf(value, "%llu", total);
} else {
*value = dmstrdup("0");
@ -445,12 +468,9 @@ static int get_disk_instance_total_space(char *refparam, struct dmctx *ctx, void
static int get_disk_instance_used_space(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value)
{
partition_entry *entry = (partition_entry *)((struct dm_data *)data)->additional_data;
struct statvfs stat;
unsigned long long total, used, available;
if (entry && statvfs(entry->mount_point, &stat) == 0) {
unsigned long long total = (unsigned long long)stat.f_blocks * stat.f_frsize;
unsigned long long free_space = (unsigned long long)stat.f_bfree * stat.f_frsize;
unsigned long long used = total - free_space;
if (entry && get_disk_stats(entry->mount_point, &total, &used, &available) == 0) {
dmasprintf(value, "%llu", used);
} else {
*value = dmstrdup("0");
@ -462,10 +482,9 @@ static int get_disk_instance_used_space(char *refparam, struct dmctx *ctx, void
static int get_disk_instance_available_space(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value)
{
partition_entry *entry = (partition_entry *)((struct dm_data *)data)->additional_data;
struct statvfs stat;
unsigned long long total, used, available;
if (entry && statvfs(entry->mount_point, &stat) == 0) {
unsigned long long available = (unsigned long long)stat.f_bavail * stat.f_frsize;
if (entry && get_disk_stats(entry->mount_point, &total, &used, &available) == 0) {
dmasprintf(value, "%llu", available);
} else {
*value = dmstrdup("0");
@ -477,13 +496,9 @@ static int get_disk_instance_available_space(char *refparam, struct dmctx *ctx,
static int get_disk_instance_used_percentage(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value)
{
partition_entry *entry = (partition_entry *)((struct dm_data *)data)->additional_data;
struct statvfs stat;
unsigned long long total, used, available;
if (entry && statvfs(entry->mount_point, &stat) == 0) {
unsigned long long total = (unsigned long long)stat.f_blocks * stat.f_frsize;
unsigned long long free_space = (unsigned long long)stat.f_bfree * stat.f_frsize;
unsigned long long used = total - free_space;
if (entry && get_disk_stats(entry->mount_point, &total, &used, &available) == 0) {
unsigned int percentage = 0;
if (total > 0) {
percentage = (unsigned int)((used * 100) / total);
@ -617,12 +632,20 @@ static int get_partition_status(char *refparam, struct dmctx *ctx, void *data, c
static int get_partition_capacity(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value)
{
partition_entry *entry = (partition_entry *)((struct dm_data *)data)->additional_data;
struct statvfs stat;
partition_info *entry = (partition_info *)((struct dm_data *)data)->additional_data;
unsigned long long total, used, available;
if (entry && entry->mount_point[0] != '\0' && statvfs(entry->mount_point, &stat) == 0) {
unsigned long long total = (unsigned long long)stat.f_blocks * stat.f_frsize;
dmasprintf(value, "%llu", total);
// If mounted, get actual filesystem capacity
if (entry && entry->is_mounted && entry->mount_point[0] != '\0') {
if (get_disk_stats(entry->mount_point, &total, &used, &available) == 0) {
dmasprintf(value, "%llu", total);
return 0;
}
}
// If not mounted, return partition size from geometry
if (entry) {
dmasprintf(value, "%llu", entry->size_bytes);
} else {
*value = dmstrdup("0");
}
@ -632,33 +655,37 @@ static int get_partition_capacity(char *refparam, struct dmctx *ctx, void *data,
static int get_partition_used_space(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value)
{
partition_entry *entry = (partition_entry *)((struct dm_data *)data)->additional_data;
struct statvfs stat;
partition_info *entry = (partition_info *)((struct dm_data *)data)->additional_data;
unsigned long long total, used, available;
if (entry && entry->mount_point[0] != '\0' && statvfs(entry->mount_point, &stat) == 0) {
unsigned long long total = (unsigned long long)stat.f_blocks * stat.f_frsize;
unsigned long long free_space = (unsigned long long)stat.f_bfree * stat.f_frsize;
unsigned long long used = total - free_space;
dmasprintf(value, "%llu", used);
} else {
*value = dmstrdup("0");
// Only return used space if mounted
if (entry && entry->is_mounted && entry->mount_point[0] != '\0') {
if (get_disk_stats(entry->mount_point, &total, &used, &available) == 0) {
dmasprintf(value, "%llu", used);
return 0;
}
}
// Not mounted or error
*value = dmstrdup("0");
return 0;
}
static int get_partition_available_space(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value)
{
partition_entry *entry = (partition_entry *)((struct dm_data *)data)->additional_data;
struct statvfs stat;
partition_info *entry = (partition_info *)((struct dm_data *)data)->additional_data;
unsigned long long total, used, available;
if (entry && entry->mount_point[0] != '\0' && statvfs(entry->mount_point, &stat) == 0) {
unsigned long long available = (unsigned long long)stat.f_bavail * stat.f_frsize;
dmasprintf(value, "%llu", available);
} else {
*value = dmstrdup("0");
// Only return available space if mounted
if (entry && entry->is_mounted && entry->mount_point[0] != '\0') {
if (get_disk_stats(entry->mount_point, &total, &used, &available) == 0) {
dmasprintf(value, "%llu", available);
return 0;
}
}
// Not mounted or error
*value = dmstrdup("0");
return 0;
}