-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathtest_storage.cpp
More file actions
95 lines (80 loc) · 3.57 KB
/
test_storage.cpp
File metadata and controls
95 lines (80 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <sys/statvfs.h>
#include <mntent.h>
// Test our storage_path structure
struct storage_path {
std::string path = std::string("");
uint64_t total_size = 0; // Total size in bytes
uint64_t used_size = 0; // Used size in bytes
uint64_t free_size = 0; // Free size in bytes
uint32_t usage_percent = 0; // Usage percentage (0-100)
std::string filesystem = std::string(""); // Filesystem type
};
// Test our storage update function
bool update_storage_info(std::vector<storage_path>& storage_paths) {
std::vector<storage_path> new_storage_paths;
// Open /etc/mtab or /proc/mounts to get mounted filesystems
FILE *mtab = setmntent("/proc/mounts", "r");
if (!mtab) {
return false;
}
struct mntent *entry;
while ((entry = getmntent(mtab)) != NULL) {
// Skip non-local filesystems and special filesystems
if (strcmp(entry->mnt_type, "proc") == 0 ||
strcmp(entry->mnt_type, "sysfs") == 0 ||
strcmp(entry->mnt_type, "devpts") == 0 ||
strcmp(entry->mnt_type, "tmpfs") == 0 ||
strcmp(entry->mnt_type, "devtmpfs") == 0 ||
strcmp(entry->mnt_type, "cgroup") == 0 ||
strncmp(entry->mnt_type, "fuse.", 5) == 0) {
continue;
}
struct statvfs vfs;
if (statvfs(entry->mnt_dir, &vfs) == 0) {
storage_path path_info;
path_info.path = std::string(entry->mnt_dir);
path_info.filesystem = std::string(entry->mnt_type);
// Calculate sizes in bytes
uint64_t block_size = vfs.f_frsize ? vfs.f_frsize : vfs.f_bsize;
path_info.total_size = (uint64_t)vfs.f_blocks * block_size;
path_info.free_size = (uint64_t)vfs.f_bavail * block_size;
path_info.used_size = path_info.total_size - path_info.free_size;
// Calculate usage percentage
if (path_info.total_size > 0) {
path_info.usage_percent = (uint32_t)((path_info.used_size * 100) / path_info.total_size);
} else {
path_info.usage_percent = 0;
}
new_storage_paths.push_back(path_info);
}
}
endmntent(mtab);
// Update the storage paths
storage_paths = new_storage_paths;
return true;
}
int main() {
std::vector<storage_path> storage_paths;
std::cout << "Testing storage monitoring implementation..." << std::endl;
if (update_storage_info(storage_paths)) {
std::cout << "Successfully retrieved storage information for " << storage_paths.size() << " mount points:" << std::endl;
for (const auto& path : storage_paths) {
std::cout << " Path: " << path.path << std::endl;
std::cout << " Filesystem: " << path.filesystem << std::endl;
std::cout << " Total: " << (path.total_size / (1024*1024*1024)) << " GB" << std::endl;
std::cout << " Used: " << (path.used_size / (1024*1024*1024)) << " GB" << std::endl;
std::cout << " Free: " << (path.free_size / (1024*1024*1024)) << " GB" << std::endl;
std::cout << " Usage: " << path.usage_percent << "%" << std::endl;
std::cout << std::endl;
}
} else {
std::cout << "Failed to retrieve storage information" << std::endl;
return 1;
}
std::cout << "Storage monitoring implementation test completed successfully!" << std::endl;
return 0;
}