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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
|
// SPDX-License-Identifier: GPL-3.0-or-later
#include "../proc.plugin/plugin_proc.h"
#define PLUGIN_DISKSPACE_NAME "diskspace.plugin"
#define THREAD_DISKSPACE_SLOW_NAME "PLUGIN[diskspace slow]"
#define DEFAULT_EXCLUDED_PATHS "/proc/* /sys/* /var/run/user/* /run/user/* /snap/* /var/lib/docker/*"
#define DEFAULT_EXCLUDED_FILESYSTEMS "*gvfs *gluster* *s3fs *ipfs *davfs2 *httpfs *sshfs *gdfs *moosefs fusectl autofs"
#define CONFIG_SECTION_DISKSPACE "plugin:proc:diskspace"
#define MAX_STAT_USEC 10000LU
#define SLOW_UPDATE_EVERY 5
static netdata_thread_t *diskspace_slow_thread = NULL;
static struct mountinfo *disk_mountinfo_root = NULL;
static int check_for_new_mountpoints_every = 15;
static int cleanup_mount_points = 1;
static inline void mountinfo_reload(int force) {
static time_t last_loaded = 0;
time_t now = now_realtime_sec();
if(force || now - last_loaded >= check_for_new_mountpoints_every) {
// mountinfo_free_all() can be called with NULL disk_mountinfo_root
mountinfo_free_all(disk_mountinfo_root);
// re-read mountinfo in case something changed
disk_mountinfo_root = mountinfo_read(0);
last_loaded = now;
}
}
// Data to be stored in DICTIONARY dict_mountpoints used by do_disk_space_stats().
// This DICTIONARY is used to lookup the settings of the mount point on each iteration.
struct mount_point_metadata {
int do_space;
int do_inodes;
int shown_error;
int updated;
int slow;
size_t collected; // the number of times this has been collected
RRDSET *st_space;
RRDDIM *rd_space_used;
RRDDIM *rd_space_avail;
RRDDIM *rd_space_reserved;
RRDSET *st_inodes;
RRDDIM *rd_inodes_used;
RRDDIM *rd_inodes_avail;
RRDDIM *rd_inodes_reserved;
};
static DICTIONARY *dict_mountpoints = NULL;
#define rrdset_obsolete_and_pointer_null(st) do { if(st) { rrdset_is_obsolete(st); (st) = NULL; } } while(st)
int mount_point_cleanup(const char *name, void *entry, int slow) {
(void)name;
struct mount_point_metadata *mp = (struct mount_point_metadata *)entry;
if(!mp) return 0;
if (slow != mp->slow)
return 0;
if(likely(mp->updated)) {
mp->updated = 0;
return 0;
}
if(likely(cleanup_mount_points && mp->collected)) {
mp->collected = 0;
mp->updated = 0;
mp->shown_error = 0;
mp->rd_space_avail = NULL;
mp->rd_space_used = NULL;
mp->rd_space_reserved = NULL;
mp->rd_inodes_avail = NULL;
mp->rd_inodes_used = NULL;
mp->rd_inodes_reserved = NULL;
rrdset_obsolete_and_pointer_null(mp->st_space);
rrdset_obsolete_and_pointer_null(mp->st_inodes);
}
return 0;
}
int mount_point_cleanup_cb(const char *name, void *entry, void *data) {
UNUSED(data);
return mount_point_cleanup(name, (struct mount_point_metadata *)entry, 0);
}
// for the full list of protected mount points look at
// https://github.com/systemd/systemd/blob/1eb3ef78b4df28a9e9f464714208f2682f957e36/src/core/namespace.c#L142-L149
// https://github.com/systemd/systemd/blob/1eb3ef78b4df28a9e9f464714208f2682f957e36/src/core/namespace.c#L180-L194
static const char *systemd_protected_mount_points[] = {
"/home",
"/root",
"/usr",
"/boot",
"/efi",
"/etc",
NULL
};
int mount_point_is_protected(char *mount_point)
{
for (size_t i = 0; systemd_protected_mount_points[i] != NULL; i++)
if (!strcmp(mount_point, systemd_protected_mount_points[i]))
return 1;
return 0;
}
// a copy of basic mountinfo fields
struct basic_mountinfo {
char *persistent_id;
char *root;
char *mount_point;
char *filesystem;
struct basic_mountinfo *next;
};
static struct basic_mountinfo *slow_mountinfo_tmp_root = NULL;
static netdata_mutex_t slow_mountinfo_mutex;
static struct basic_mountinfo *basic_mountinfo_create_and_copy(struct mountinfo* mi)
{
struct basic_mountinfo *bmi = callocz(1, sizeof(struct basic_mountinfo));
if (mi) {
bmi->persistent_id = strdupz(mi->persistent_id);
bmi->root = strdupz(mi->root);
bmi->mount_point = strdupz(mi->mount_point);
bmi->filesystem = strdupz(mi->filesystem);
}
return bmi;
}
static void add_basic_mountinfo(struct basic_mountinfo **root, struct mountinfo *mi)
{
if (!root)
return;
struct basic_mountinfo *bmi = basic_mountinfo_create_and_copy(mi);
bmi->next = *root;
*root = bmi;
};
static void free_basic_mountinfo(struct basic_mountinfo *bmi)
{
if (bmi) {
freez(bmi->persistent_id);
freez(bmi->root);
freez(bmi->mount_point);
freez(bmi->filesystem);
freez(bmi);
}
};
static void free_basic_mountinfo_list(struct basic_mountinfo *root)
{
struct basic_mountinfo *bmi = root, *next;
while (bmi) {
next = bmi->next;
free_basic_mountinfo(bmi);
bmi = next;
}
}
static void calculate_values_and_show_charts(
struct basic_mountinfo *mi,
struct mount_point_metadata *m,
struct statvfs *buff_statvfs,
int update_every)
{
const char *family = mi->mount_point;
const char *disk = mi->persistent_id;
// logic found at get_fs_usage() in coreutils
unsigned long bsize = (buff_statvfs->f_frsize) ? buff_statvfs->f_frsize : buff_statvfs->f_bsize;
fsblkcnt_t bavail = buff_statvfs->f_bavail;
fsblkcnt_t btotal = buff_statvfs->f_blocks;
fsblkcnt_t bavail_root = buff_statvfs->f_bfree;
fsblkcnt_t breserved_root = bavail_root - bavail;
fsblkcnt_t bused = likely(btotal >= bavail_root) ? btotal - bavail_root : bavail_root - btotal;
#ifdef NETDATA_INTERNAL_CHECKS
if(unlikely(btotal != bavail + breserved_root + bused))
error("DISKSPACE: disk block statistics for '%s' (disk '%s') do not sum up: total = %llu, available = %llu, reserved = %llu, used = %llu", mi->mount_point, disk, (unsigned long long)btotal, (unsigned long long)bavail, (unsigned long long)breserved_root, (unsigned long long)bused);
#endif
// --------------------------------------------------------------------------
fsfilcnt_t favail = buff_statvfs->f_favail;
fsfilcnt_t ftotal = buff_statvfs->f_files;
fsfilcnt_t favail_root = buff_statvfs->f_ffree;
fsfilcnt_t freserved_root = favail_root - favail;
fsfilcnt_t fused = ftotal - favail_root;
if(m->do_inodes == CONFIG_BOOLEAN_AUTO && favail == (fsfilcnt_t)-1) {
// this file system does not support inodes reporting
// eg. cephfs
m->do_inodes = CONFIG_BOOLEAN_NO;
}
#ifdef NETDATA_INTERNAL_CHECKS
if(unlikely(btotal != bavail + breserved_root + bused))
error("DISKSPACE: disk inode statistics for '%s' (disk '%s') do not sum up: total = %llu, available = %llu, reserved = %llu, used = %llu", mi->mount_point, disk, (unsigned long long)ftotal, (unsigned long long)favail, (unsigned long long)freserved_root, (unsigned long long)fused);
#endif
// --------------------------------------------------------------------------
int rendered = 0;
if(m->do_space == CONFIG_BOOLEAN_YES || (m->do_space == CONFIG_BOOLEAN_AUTO &&
(bavail || breserved_root || bused ||
netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES))) {
if(unlikely(!m->st_space) || m->st_space->update_every != update_every) {
m->do_space = CONFIG_BOOLEAN_YES;
m->st_space = rrdset_find_active_bytype_localhost("disk_space", disk);
if(unlikely(!m->st_space || m->st_space->update_every != update_every)) {
char title[4096 + 1];
snprintfz(title, 4096, "Disk Space Usage");
m->st_space = rrdset_create_localhost(
"disk_space"
, disk
, NULL
, family
, "disk.space"
, title
, "GiB"
, PLUGIN_DISKSPACE_NAME
, NULL
, NETDATA_CHART_PRIO_DISKSPACE_SPACE
, update_every
, RRDSET_TYPE_STACKED
);
}
m->rd_space_avail = rrddim_add(m->st_space, "avail", NULL, (collected_number)bsize, 1024 * 1024 * 1024, RRD_ALGORITHM_ABSOLUTE);
m->rd_space_used = rrddim_add(m->st_space, "used", NULL, (collected_number)bsize, 1024 * 1024 * 1024, RRD_ALGORITHM_ABSOLUTE);
m->rd_space_reserved = rrddim_add(m->st_space, "reserved_for_root", "reserved for root", (collected_number)bsize, 1024 * 1024 * 1024, RRD_ALGORITHM_ABSOLUTE);
}
else
rrdset_next(m->st_space);
rrddim_set_by_pointer(m->st_space, m->rd_space_avail, (collected_number)bavail);
rrddim_set_by_pointer(m->st_space, m->rd_space_used, (collected_number)bused);
rrddim_set_by_pointer(m->st_space, m->rd_space_reserved, (collected_number)breserved_root);
rrdset_done(m->st_space);
rendered++;
}
// --------------------------------------------------------------------------
if(m->do_inodes == CONFIG_BOOLEAN_YES || (m->do_inodes == CONFIG_BOOLEAN_AUTO &&
(favail || freserved_root || fused ||
netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES))) {
if(unlikely(!m->st_inodes) || m->st_inodes->update_every != update_every) {
m->do_inodes = CONFIG_BOOLEAN_YES;
m->st_inodes = rrdset_find_active_bytype_localhost("disk_inodes", disk);
if(unlikely(!m->st_inodes) || m->st_inodes->update_every != update_every) {
char title[4096 + 1];
snprintfz(title, 4096, "Disk Files (inodes) Usage");
m->st_inodes = rrdset_create_localhost(
"disk_inodes"
, disk
, NULL
, family
, "disk.inodes"
, title
, "inodes"
, PLUGIN_DISKSPACE_NAME
, NULL
, NETDATA_CHART_PRIO_DISKSPACE_INODES
, update_every
, RRDSET_TYPE_STACKED
);
}
m->rd_inodes_avail = rrddim_add(m->st_inodes, "avail", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
m->rd_inodes_used = rrddim_add(m->st_inodes, "used", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
m->rd_inodes_reserved = rrddim_add(m->st_inodes, "reserved_for_root", "reserved for root", 1, 1, RRD_ALGORITHM_ABSOLUTE);
}
else
rrdset_next(m->st_inodes);
rrddim_set_by_pointer(m->st_inodes, m->rd_inodes_avail, (collected_number)favail);
rrddim_set_by_pointer(m->st_inodes, m->rd_inodes_used, (collected_number)fused);
rrddim_set_by_pointer(m->st_inodes, m->rd_inodes_reserved, (collected_number)freserved_root);
rrdset_done(m->st_inodes);
rendered++;
}
// --------------------------------------------------------------------------
if(likely(rendered))
m->collected++;
}
static inline void do_disk_space_stats(struct mountinfo *mi, int update_every) {
const char *disk = mi->persistent_id;
static SIMPLE_PATTERN *excluded_mountpoints = NULL;
static SIMPLE_PATTERN *excluded_filesystems = NULL;
usec_t slow_timeout = MAX_STAT_USEC * update_every;
int do_space, do_inodes;
if(unlikely(!dict_mountpoints)) {
SIMPLE_PREFIX_MODE mode = SIMPLE_PATTERN_EXACT;
if(config_move("plugin:proc:/proc/diskstats", "exclude space metrics on paths", CONFIG_SECTION_DISKSPACE, "exclude space metrics on paths") != -1) {
// old configuration, enable backwards compatibility
mode = SIMPLE_PATTERN_PREFIX;
}
excluded_mountpoints = simple_pattern_create(
config_get(CONFIG_SECTION_DISKSPACE, "exclude space metrics on paths", DEFAULT_EXCLUDED_PATHS)
, NULL
, mode
);
excluded_filesystems = simple_pattern_create(
config_get(CONFIG_SECTION_DISKSPACE, "exclude space metrics on filesystems", DEFAULT_EXCLUDED_FILESYSTEMS)
, NULL
, SIMPLE_PATTERN_EXACT
);
dict_mountpoints = dictionary_create(DICTIONARY_FLAG_SINGLE_THREADED);
}
struct mount_point_metadata *m = dictionary_get(dict_mountpoints, mi->mount_point);
if(unlikely(!m)) {
int slow = 0;
char var_name[4096 + 1];
snprintfz(var_name, 4096, "plugin:proc:diskspace:%s", mi->mount_point);
int def_space = config_get_boolean_ondemand(CONFIG_SECTION_DISKSPACE, "space usage for all disks", CONFIG_BOOLEAN_AUTO);
int def_inodes = config_get_boolean_ondemand(CONFIG_SECTION_DISKSPACE, "inodes usage for all disks", CONFIG_BOOLEAN_AUTO);
if(unlikely(simple_pattern_matches(excluded_mountpoints, mi->mount_point))) {
def_space = CONFIG_BOOLEAN_NO;
def_inodes = CONFIG_BOOLEAN_NO;
}
if(unlikely(simple_pattern_matches(excluded_filesystems, mi->filesystem))) {
def_space = CONFIG_BOOLEAN_NO;
def_inodes = CONFIG_BOOLEAN_NO;
}
// check if the mount point is a directory #2407
// but only when it is enabled by default #4491
if(def_space != CONFIG_BOOLEAN_NO || def_inodes != CONFIG_BOOLEAN_NO) {
usec_t start_time = now_monotonic_high_precision_usec();
struct stat bs;
if(stat(mi->mount_point, &bs) == -1) {
error("DISKSPACE: Cannot stat() mount point '%s' (disk '%s', filesystem '%s', root '%s')."
, mi->mount_point
, disk
, mi->filesystem?mi->filesystem:""
, mi->root?mi->root:""
);
def_space = CONFIG_BOOLEAN_NO;
def_inodes = CONFIG_BOOLEAN_NO;
}
else {
if((bs.st_mode & S_IFMT) != S_IFDIR) {
error("DISKSPACE: Mount point '%s' (disk '%s', filesystem '%s', root '%s') is not a directory."
, mi->mount_point
, disk
, mi->filesystem?mi->filesystem:""
, mi->root?mi->root:""
);
def_space = CONFIG_BOOLEAN_NO;
def_inodes = CONFIG_BOOLEAN_NO;
}
}
if ((now_monotonic_high_precision_usec() - start_time) > slow_timeout)
slow = 1;
}
do_space = config_get_boolean_ondemand(var_name, "space usage", def_space);
do_inodes = config_get_boolean_ondemand(var_name, "inodes usage", def_inodes);
struct mount_point_metadata mp = {
.do_space = do_space,
.do_inodes = do_inodes,
.shown_error = 0,
.updated = 0,
.slow = 0,
.collected = 0,
.st_space = NULL,
.rd_space_avail = NULL,
.rd_space_used = NULL,
.rd_space_reserved = NULL,
.st_inodes = NULL,
.rd_inodes_avail = NULL,
.rd_inodes_used = NULL,
.rd_inodes_reserved = NULL
};
m = dictionary_set(dict_mountpoints, mi->mount_point, &mp, sizeof(struct mount_point_metadata));
m->slow = slow;
}
if (m->slow) {
add_basic_mountinfo(&slow_mountinfo_tmp_root, mi);
return;
}
m->updated = 1;
if(unlikely(m->do_space == CONFIG_BOOLEAN_NO && m->do_inodes == CONFIG_BOOLEAN_NO))
return;
if (unlikely(
mi->flags & MOUNTINFO_READONLY &&
!mount_point_is_protected(mi->mount_point) &&
!m->collected &&
m->do_space != CONFIG_BOOLEAN_YES &&
m->do_inodes != CONFIG_BOOLEAN_YES))
return;
usec_t start_time = now_monotonic_high_precision_usec();
struct statvfs buff_statvfs;
if (statvfs(mi->mount_point, &buff_statvfs) < 0) {
if(!m->shown_error) {
error("DISKSPACE: failed to statvfs() mount point '%s' (disk '%s', filesystem '%s', root '%s')"
, mi->mount_point
, disk
, mi->filesystem?mi->filesystem:""
, mi->root?mi->root:""
);
m->shown_error = 1;
}
return;
}
if ((now_monotonic_high_precision_usec() - start_time) > slow_timeout)
m->slow = 1;
m->shown_error = 0;
struct basic_mountinfo bmi;
bmi.mount_point = mi->mount_point;
bmi.persistent_id = mi->persistent_id;
bmi.filesystem = mi->filesystem;
bmi.root = mi->root;
calculate_values_and_show_charts(&bmi, m, &buff_statvfs, update_every);
}
static inline void do_slow_disk_space_stats(struct basic_mountinfo *mi, int update_every) {
struct mount_point_metadata *m = dictionary_get(dict_mountpoints, mi->mount_point);
m->updated = 1;
struct statvfs buff_statvfs;
if (statvfs(mi->mount_point, &buff_statvfs) < 0) {
if(!m->shown_error) {
error("DISKSPACE: failed to statvfs() mount point '%s' (disk '%s', filesystem '%s', root '%s')"
, mi->mount_point
, mi->persistent_id
, mi->filesystem?mi->filesystem:""
, mi->root?mi->root:""
);
m->shown_error = 1;
}
return;
}
m->shown_error = 0;
calculate_values_and_show_charts(mi, m, &buff_statvfs, update_every);
}
static void diskspace_slow_worker_cleanup(void *ptr)
{
UNUSED(ptr);
info("cleaning up...");
worker_unregister();
}
#define WORKER_JOB_SLOW_MOUNTPOINT 0
#define WORKER_JOB_SLOW_CLEA
|