@cryptotaxi247 / netdata-1 / commits / 6f7779516

Add disk space and inodes utilization charts to macOS plugin

Vladimir Kobal committed Jan 13, 2017 at 05:26 UTC 6f777951680eb7a2031b4d9c20de918448e990fd
1 file changed +83 -1
src/macos_fw.c
+83 -1
@@ -3,16 +3,24 @@
3 #include <IOKit/IOKitLib.h>
4 #include <IOKit/storage/IOBlockStorageDriver.h>
5 #include <IOKit/IOBSD.h>
6 +// NEEDED BY do_space, do_inodes
7 +#include <sys/mount.h>
8
9 #define MAXDRIVENAME 31
10
11 +#define KILO_FACTOR 1024
12 +#define MEGA_FACTOR 1048576 // 1024 * 1024
13 +#define GIGA_FACTOR 1073741824 // 1024 * 1024 * 1024
14 +
15 int do_macos_iokit(int update_every, usec_t dt) {
16 (void)dt;
17
12 - static int do_io = -1;
18 + static int do_io = -1, do_space = -1, do_inodes = -1;
19
20 if (unlikely(do_io == -1)) {
21 do_io = config_get_boolean("plugin:macos:iokit", "disk i/o", 1);
22 + do_space = config_get_boolean("plugin:macos:sysctl", "space usage for all disks", 1);
23 + do_inodes = config_get_boolean("plugin:macos:sysctl", "inodes usage for all disks", 1);
24 }
25
26 RRDSET *st;
@@ -52,6 +60,12 @@ int do_macos_iokit(int update_every, usec_t dt) {
60 collected_number busy_time_ns;
61 } prev_diskstat;
62
63 + // NEEDED BY: do_space, do_inodes
64 + struct statfs *mntbuf;
65 + int mntsize, i;
66 + char mntonname[MNAMELEN + 1];
67 + char title[4096 + 1];
68 +
69 /* Get ports and services for drive statistics. */
70 if (unlikely(IOMasterPort(bootstrap_port, &master_port))) {
71 error("MACOS: IOMasterPort() failed");
@@ -290,5 +304,73 @@ int do_macos_iokit(int update_every, usec_t dt) {
304 rrdset_done(st);
305 }
306
307 + // --------------------------------------------------------------------------
308 +
309 + if (likely(do_space || do_inodes)) {
310 + // there is no mount info in sysctl MIBs
311 + if (unlikely(!(mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)))) {
312 + error("FREEBSD: getmntinfo() failed");
313 + do_space = 0;
314 + error("DISABLED: disk_space.X");
315 + do_inodes = 0;
316 + error("DISABLED: disk_inodes.X");
317 + } else {
318 + for (i = 0; i < mntsize; i++) {
319 + if (mntbuf[i].f_flags == MNT_RDONLY ||
320 + mntbuf[i].f_blocks == 0 ||
321 + // taken from gnulib/mountlist.c and shortened to FreeBSD related fstypes
322 + strcmp(mntbuf[i].f_fstypename, "autofs") == 0 ||
323 + strcmp(mntbuf[i].f_fstypename, "procfs") == 0 ||
324 + strcmp(mntbuf[i].f_fstypename, "subfs") == 0 ||
325 + strcmp(mntbuf[i].f_fstypename, "devfs") == 0 ||
326 + strcmp(mntbuf[i].f_fstypename, "none") == 0)
327 + continue;
328 +
329 + // --------------------------------------------------------------------------
330 +
331 + if (likely(do_space)) {
332 + st = rrdset_find_bytype("disk_space", mntbuf[i].f_mntonname);
333 + if (unlikely(!st)) {
334 + snprintfz(title, 4096, "Disk Space Usage for %s [%s]", mntbuf[i].f_mntonname, mntbuf[i].f_mntfromname);
335 + st = rrdset_create("disk_space", mntbuf[i].f_mntonname, NULL, mntbuf[i].f_mntonname, "disk.space", title, "GB", 2023,
336 + update_every,
337 + RRDSET_TYPE_STACKED);
338 +
339 + rrddim_add(st, "avail", NULL, mntbuf[i].f_bsize, GIGA_FACTOR, RRDDIM_ABSOLUTE);
340 + rrddim_add(st, "used", NULL, mntbuf[i].f_bsize, GIGA_FACTOR, RRDDIM_ABSOLUTE);
341 + rrddim_add(st, "reserved_for_root", "reserved for root", mntbuf[i].f_bsize, GIGA_FACTOR,
342 + RRDDIM_ABSOLUTE);
343 + } else
344 + rrdset_next(st);
345 +
346 + rrddim_set(st, "avail", (collected_number) mntbuf[i].f_bavail);
347 + rrddim_set(st, "used", (collected_number) (mntbuf[i].f_blocks - mntbuf[i].f_bfree));
348 + rrddim_set(st, "reserved_for_root", (collected_number) (mntbuf[i].f_bfree - mntbuf[i].f_bavail));
349 + rrdset_done(st);
350 + }
351 +
352 + // --------------------------------------------------------------------------
353 +
354 + if (likely(do_inodes)) {
355 + st = rrdset_find_bytype("disk_inodes", mntbuf[i].f_mntonname);
356 + if (unlikely(!st)) {
357 + snprintfz(title, 4096, "Disk Files (inodes) Usage for %s [%s]", mntbuf[i].f_mntonname, mntbuf[i].f_mntfromname);
358 + st = rrdset_create("disk_inodes", mntbuf[i].f_mntonname, NULL, mntbuf[i].f_mntonname, "disk.inodes", title, "Inodes", 2024,
359 + update_every, RRDSET_TYPE_STACKED);
360 +
361 + rrddim_add(st, "avail", NULL, 1, 1, RRDDIM_ABSOLUTE);
362 + rrddim_add(st, "used", NULL, 1, 1, RRDDIM_ABSOLUTE);
363 + rrddim_add(st, "reserved_for_root", "reserved for root", 1, 1, RRDDIM_ABSOLUTE);
364 + } else
365 + rrdset_next(st);
366 +
367 + rrddim_set(st, "avail", (collected_number) mntbuf[i].f_ffree);
368 + rrddim_set(st, "used", (collected_number) (mntbuf[i].f_files - mntbuf[i].f_ffree));
369 + rrdset_done(st);
370 + }
371 + }
372 + }
373 + }
374 +
375 return 0;
376 }