-
Notifications
You must be signed in to change notification settings - Fork 1
/
fs_info.c
73 lines (57 loc) · 2.27 KB
/
fs_info.c
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
#include "pgsampler.h"
char* fs_info(void) {
char* result;
FILE *fp;
struct mntent *fs;
struct statvfs vfs;
StringInfoData resultbuf;
char strbuf[30];
initStringInfo(&resultbuf);
appendStringInfo(&resultbuf, "FSINFO;");
fp = setmntent("/etc/mtab", "r"); /* read only */
if (fp == NULL) {
elog(LOG, "%s: could not open: %s\n", "/etc/mtab", strerror(errno));
result = palloc(2);
strcpy(result, "");
return result;
}
while ((fs = getmntent(fp)) != NULL) {
if (fs->mnt_fsname[0] == '/') {
if (statvfs(fs->mnt_dir, & vfs) != 0) {
//elog(LOG, "SKIPPING %s: statvfs failed: %s\n", fs->mnt_dir, strerror(errno));
} else {
/* Filesystem Info */
appendStringInfoString(&resultbuf, fs->mnt_fsname);
appendStringInfo(&resultbuf, FIELD_DELIMIT);
appendStringInfoString(&resultbuf, fs->mnt_dir);
appendStringInfo(&resultbuf, FIELD_DELIMIT);
appendStringInfoString(&resultbuf, fs->mnt_type);
appendStringInfo(&resultbuf, FIELD_DELIMIT);
appendStringInfoString(&resultbuf, fs->mnt_opts);
appendStringInfo(&resultbuf, FIELD_DELIMIT);
/* Filesystem Stats */
sprintf(strbuf, "%ld", (unsigned long) vfs.f_bsize);
appendStringInfoString(&resultbuf, strbuf);
appendStringInfo(&resultbuf, FIELD_DELIMIT);
sprintf(strbuf, "%ld", (unsigned long) vfs.f_frsize);
appendStringInfoString(&resultbuf, strbuf);
appendStringInfo(&resultbuf, FIELD_DELIMIT);
sprintf(strbuf, "%ld", (unsigned long) vfs.f_blocks);
appendStringInfoString(&resultbuf, strbuf);
appendStringInfo(&resultbuf, FIELD_DELIMIT);
sprintf(strbuf, "%ld", (unsigned long) vfs.f_bfree);
appendStringInfoString(&resultbuf, strbuf);
appendStringInfo(&resultbuf, FIELD_DELIMIT);
sprintf(strbuf, "%ld", (unsigned long) vfs.f_bavail);
appendStringInfoString(&resultbuf, strbuf);
appendStringInfo(&resultbuf, FIELD_DELIMIT);
appendStringInfo(&resultbuf,REC_DELIMIT);
}
}
}
endmntent(fp);
// elog(LOG, "FSINFO: %s", resultbuf.data); //print the command to send, debugging
appendStringInfoString(&resultbuf, CDELIMIT);
result = pstrdup(resultbuf.data);
return result;
}