-
Notifications
You must be signed in to change notification settings - Fork 9
/
mounts.cpp
99 lines (77 loc) · 2.12 KB
/
mounts.cpp
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
96
97
98
99
#include <sys/types.h>
#include <sys/mntctl.h>
#include <sys/vmount.h>
#include <sys/statvfs.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#include <iostream>
#include "node_exporter_aix.hpp"
std::vector<mountpoint> list_mounts() {
std::vector<mountpoint> output;
char *buffer, *current;
current = buffer = (char *)malloc(102400);
memset(buffer, 0, 102400);
int rc;
rc = mntctl(MCTL_QUERY, 102400, buffer);
if(rc<0) {
perror("Error getting initial");
exit(0);
}
struct vmount *mounts;
for(int i=0; i<rc; i++) {
mounts = (struct vmount *)current;
if(mounts->vmt_gfstype == MNT_J2 || mounts->vmt_gfstype == MNT_JFS) {
if(vmt2datasize(mounts, VMT_STUB) && vmt2datasize(mounts, VMT_OBJECT)) {
struct mountpoint mp;
mp.device = vmt2dataptr(mounts, VMT_OBJECT);
mp.mountpoint = vmt2dataptr(mounts, VMT_STUB);
if (mounts->vmt_gfstype == MNT_J2) {
mp.fstype = "jfs2";
} else if (mounts->vmt_gfstype == MNT_JFS) {
mp.fstype = "jfs";
} else {
mp.fstype = "unknown";
}
output.push_back(mp);
}
}
current = current + mounts->vmt_length;
}
free(buffer);
return output;
}
std::vector<filesystem> stat_filesystems(std::vector<mountpoint> mounts) {
std::vector<filesystem> output;
for(auto it = mounts.begin(); it != mounts.end(); it++) {
struct statvfs64 s;
int rc = statvfs64((*it).mountpoint.c_str(), &s);
if(rc < 0) {
perror("Error getting statvfs64: ");
exit(1);
}
struct filesystem fs;
fs.mountpoint = (*it).mountpoint;
fs.device = (*it).device;
fs.fstype = (*it).fstype;
fs.avail_bytes = s.f_bavail * s.f_bsize;
fs.size_bytes = s.f_blocks * s.f_bsize;
fs.free_bytes = s.f_bfree * s.f_bsize;
fs.files = s.f_files;
fs.files_free = s.f_ffree;
fs.files_avail = s.f_favail;
output.push_back(fs);
}
return output;
}
#ifdef TESTING
int main() {
auto fs = stat_filesystems(list_mounts());
for(auto it = fs.begin(); it != fs.end(); it++) {
std::cout << (*it).mountpoint << " " << (*it).fstype << " " << (*it).size_bytes/1024 << " " << (*it).free_bytes/1024 << std::endl;
}
return 0;
}
#endif