| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #include "../proc.plugin/plugin_proc.h" |
| 4 | |
| 5 | #define PLUGIN_DISKSPACE_NAME "diskspace.plugin" |
| 6 | |
| 7 | #define DEFAULT_EXCLUDED_PATHS "/dev /dev/shm /proc/* /sys/* /var/run/user/* /run/lock /run/user/* /snap/* /var/lib/docker/* /var/lib/containers/storage/* /run/credentials/* /run/containerd/* /rpool /rpool/*" |
| 8 | #define DEFAULT_EXCLUDED_FILESYSTEMS "*gvfs *gluster* *s3fs *ipfs *davfs2 *httpfs *sshfs *gdfs *moosefs fusectl autofs cgroup cgroup2 hugetlbfs devtmpfs fuse.lxcfs" |
| 9 | #define DEFAULT_EXCLUDED_FILESYSTEMS_INODES "msdosfs msdos vfat overlayfs aufs* *unionfs" |
| 10 | #define CONFIG_SECTION_DISKSPACE "plugin:proc:diskspace" |
| 11 | |
| 12 | #define RRDFUNCTIONS_DISKSPACE_HELP "Displays filesystem mount points with space utilization, available capacity, and inode usage statistics." |
| 13 | |
| 14 | #define MAX_STAT_USEC 10000LU |
| 15 | #define SLOW_UPDATE_EVERY 5 |
| 16 | |
| 17 | static ND_THREAD *diskspace_slow_thread = NULL; |
| 18 | |
| 19 | static struct mountinfo *disk_mountinfo_root = NULL; |
| 20 | static int check_for_new_mountpoints_every = 15; |
| 21 | static int cleanup_mount_points = 1; |
| 22 | |
| 23 | // ---------------------------------------------------------------------------- |
| 24 | // ZFS dataset deduplication |
| 25 | // |
| 26 | // ZFS datasets without quotas report the same capacity as their parent pool, |
| 27 | // causing duplicate metrics and alert floods. This heuristic excludes datasets |
| 28 | // that mirror pool capacity while keeping datasets with quotas. |
| 29 | // |
| 30 | // To avoid calling statvfs() on every dataset on every collection cycle, |
| 31 | // we cache both pool capacities and dataset exclusion decisions. |
| 32 | // Cache entries are refreshed every ZFS_DATASET_RECHECK_SECONDS. |
| 33 | |
| 34 | #define ZFS_DATASET_RECHECK_SECONDS 300 // recheck every 5 minutes |
| 35 | |
| 36 | // Combined cache for ZFS pools and datasets |
| 37 | // Key: mount_source (e.g., "tank" for pool, "tank/home" for dataset) |
| 38 | struct zfs_cache_entry { |
| 39 | time_t last_checked; // when this entry was last checked |
| 40 | bool is_pool; // true = pool, false = dataset |
| 41 | uint64_t pool_capacity; // only for pools: capacity in bytes |
| 42 | bool excluded; // only for datasets: true if excluded from monitoring |
| 43 | }; |
| 44 | |
| 45 | static DICTIONARY *zfs_cache = NULL; |
| 46 | static SIMPLE_PATTERN *excluded_zfs_datasets_pattern = NULL; |
| 47 | static int zfs_datasets_heuristic = CONFIG_BOOLEAN_YES; |
| 48 | |
| 49 | static SIMPLE_PATTERN *excluded_mountpoints = NULL; |
| 50 | static SIMPLE_PATTERN *excluded_filesystems = NULL; |
| 51 | static SIMPLE_PATTERN *excluded_filesystems_inodes = NULL; |
| 52 | |
| 53 | static inline void mountinfo_reload(int force) { |
| 54 | static time_t last_loaded = 0; |
| 55 | time_t now = now_realtime_sec(); |
| 56 | |
| 57 | if(force || now - last_loaded >= check_for_new_mountpoints_every) { |
| 58 | // mountinfo_free_all() can be called with NULL disk_mountinfo_root |
| 59 | mountinfo_free_all(disk_mountinfo_root); |
| 60 | |
| 61 | // re-read mountinfo in case something changed |
| 62 | disk_mountinfo_root = mountinfo_read(0); |
| 63 | |
| 64 | last_loaded = now; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | // Data to be stored in DICTIONARY dict_mountpoints used by do_disk_space_stats(). |
| 69 | // This DICTIONARY is used to lookup the settings of the mount point on each iteration. |
| 70 | struct mount_point_metadata { |
| 71 | int do_space; |
| 72 | int do_inodes; |
| 73 | |
| 74 | bool shown_error; |
| 75 | bool updated; |
| 76 | bool slow; |
| 77 | |
| 78 | STRING *filesystem; |
| 79 | STRING *mountroot; |
| 80 | |
| 81 | RRDLABELS *chart_labels; |
| 82 | |
| 83 | size_t collected; // the number of times this has been collected |
| 84 | |
| 85 | RRDSET *st_space; |
| 86 | RRDDIM *rd_space_used; |
| 87 | RRDDIM *rd_space_avail; |
| 88 | RRDDIM *rd_space_reserved; |
| 89 | |
| 90 | RRDSET *st_inodes; |
| 91 | RRDDIM *rd_inodes_used; |
| 92 | RRDDIM *rd_inodes_avail; |
| 93 | RRDDIM *rd_inodes_reserved; |
| 94 | }; |
| 95 | |
| 96 | static DICTIONARY *dict_mountpoints = NULL; |
| 97 | |
| 98 | #define rrdset_obsolete_and_pointer_null(st) do { if(st) { rrdset_is_obsolete___safe_from_collector_thread(st); (st) = NULL; } } while(st) |
| 99 | |
| 100 | static void mountpoint_delete_cb(const DICTIONARY_ITEM *item __maybe_unused, void *entry, void *data __maybe_unused); |
| 101 | |
| 102 | static void diskspace_mountpoints_init(void) { |
| 103 | if(dict_mountpoints) |
| 104 | return; |
| 105 | |
| 106 | SIMPLE_PREFIX_MODE mode = SIMPLE_PATTERN_EXACT; |
| 107 | |
| 108 | if(inicfg_move(&netdata_config, "plugin:proc:/proc/diskstats", "exclude space metrics on paths", CONFIG_SECTION_DISKSPACE, "exclude space metrics on paths") != -1) { |
| 109 | // old configuration, enable backwards compatibility |
| 110 | mode = SIMPLE_PATTERN_PREFIX; |
| 111 | } |
| 112 | |
| 113 | excluded_mountpoints = simple_pattern_create( |
| 114 | inicfg_get(&netdata_config, CONFIG_SECTION_DISKSPACE, "exclude space metrics on paths", DEFAULT_EXCLUDED_PATHS), |
| 115 | NULL, |
| 116 | mode, |
| 117 | true); |
| 118 | |
| 119 | excluded_filesystems = simple_pattern_create( |
| 120 | inicfg_get(&netdata_config, CONFIG_SECTION_DISKSPACE, "exclude space metrics on filesystems", DEFAULT_EXCLUDED_FILESYSTEMS), |
| 121 | NULL, |
| 122 | SIMPLE_PATTERN_EXACT, |
| 123 | true); |
| 124 | |
| 125 | excluded_filesystems_inodes = simple_pattern_create( |
| 126 | inicfg_get(&netdata_config, CONFIG_SECTION_DISKSPACE, "exclude inode metrics on filesystems", DEFAULT_EXCLUDED_FILESYSTEMS_INODES), |
| 127 | NULL, |
| 128 | SIMPLE_PATTERN_EXACT, |
| 129 | true); |
| 130 | |
| 131 | // ZFS dataset exclusion pattern (used when heuristic is disabled) |
| 132 | // Always create so the option appears in config for users to customize. |
| 133 | excluded_zfs_datasets_pattern = simple_pattern_create( |
| 134 | inicfg_get(&netdata_config, CONFIG_SECTION_DISKSPACE, "exclude zfs datasets on paths", "!*"), |
| 135 | NULL, |
| 136 | SIMPLE_PATTERN_EXACT, |
| 137 | true); |
| 138 | |
| 139 | dict_mountpoints = dictionary_create_advanced(DICT_OPTION_FIXED_SIZE, &dictionary_stats_category_collectors, sizeof(struct mount_point_metadata)); |
| 140 | dictionary_register_delete_callback(dict_mountpoints, mountpoint_delete_cb, NULL); |
| 141 | } |
| 142 | |
| 143 | static void mount_points_cleanup(bool slow) { |
| 144 | struct mount_point_metadata *mp; |
| 145 | dfe_start_write(dict_mountpoints, mp) { |
| 146 | if(mp->slow != slow) continue; |
| 147 | |
| 148 | if(mp->updated) |
| 149 | mp->updated = false; |
| 150 | else if(cleanup_mount_points) |
| 151 | dictionary_del(dict_mountpoints, mp_dfe.name); |
| 152 | } |
| 153 | dfe_done(mp); |
| 154 | |
| 155 | dictionary_garbage_collect(dict_mountpoints); |
| 156 | } |
| 157 | |
| 158 | static void mountpoint_delete_cb(const DICTIONARY_ITEM *item __maybe_unused, void *entry, void *data __maybe_unused) { |
| 159 | struct mount_point_metadata *mp = (struct mount_point_metadata *)entry; |
| 160 | |
| 161 | mp->collected = 0; |
| 162 | mp->updated = false; |
| 163 | mp->shown_error = false; |
| 164 | |
| 165 | string_freez(mp->filesystem); |
| 166 | mp->filesystem = NULL; |
| 167 | |
| 168 | string_freez(mp->mountroot); |
| 169 | mp->mountroot = NULL; |
| 170 | |
| 171 | // Free the labels if they exist |
| 172 | rrdlabels_destroy(mp->chart_labels); |
| 173 | mp->chart_labels = NULL; |
| 174 | |
| 175 | rrdset_obsolete_and_pointer_null(mp->st_space); |
| 176 | rrdset_obsolete_and_pointer_null(mp->st_inodes); |
| 177 | |
| 178 | mp->rd_space_avail = NULL; |
| 179 | mp->rd_space_used = NULL; |
| 180 | mp->rd_space_reserved = NULL; |
| 181 | |
| 182 | mp->rd_inodes_avail = NULL; |
| 183 | mp->rd_inodes_used = NULL; |
| 184 | mp->rd_inodes_reserved = NULL; |
| 185 | } |
| 186 | |
| 187 | // a copy of basic mountinfo fields |
| 188 | struct basic_mountinfo { |
| 189 | char *persistent_id; |
| 190 | char *root; |
| 191 | char *mount_point_stat_path; |
| 192 | char *mount_point; |
| 193 | char *mount_source; |
| 194 | char *filesystem; |
| 195 | |
| 196 | struct basic_mountinfo *next; |
| 197 | }; |
| 198 | |
| 199 | static struct basic_mountinfo *slow_mountinfo_tmp_root = NULL; |
| 200 | static netdata_mutex_t slow_mountinfo_mutex; |
| 201 | |
| 202 | static struct basic_mountinfo *basic_mountinfo_create_and_copy(struct mountinfo* mi) |
| 203 | { |
| 204 | struct basic_mountinfo *bmi = callocz(1, sizeof(struct basic_mountinfo)); |
| 205 | |
| 206 | if (mi) { |
| 207 | bmi->persistent_id = strdupz(mi->persistent_id); |
| 208 | bmi->root = strdupz(mi->root); |
| 209 | bmi->mount_point_stat_path = strdupz(mi->mount_point_stat_path); |
| 210 | bmi->mount_point = strdupz(mi->mount_point); |
| 211 | bmi->mount_source = mi->mount_source ? strdupz(mi->mount_source) : NULL; |
| 212 | bmi->filesystem = mi->filesystem ? strdupz(mi->filesystem) : NULL; |
| 213 | } |
| 214 | |
| 215 | return bmi; |
| 216 | } |
| 217 | |
| 218 | static void add_basic_mountinfo(struct basic_mountinfo **root, struct mountinfo *mi) |
| 219 | { |
| 220 | if (!root) |
| 221 | return; |
| 222 | |
| 223 | struct basic_mountinfo *bmi = basic_mountinfo_create_and_copy(mi); |
| 224 | |
| 225 | bmi->next = *root; |
| 226 | *root = bmi; |
| 227 | } |
| 228 | |
| 229 | static void free_basic_mountinfo(struct basic_mountinfo *bmi) |
| 230 | { |
| 231 | if (bmi) { |
| 232 | freez(bmi->persistent_id); |
| 233 | freez(bmi->root); |
| 234 | freez(bmi->mount_point_stat_path); |
| 235 | freez(bmi->mount_point); |
| 236 | freez(bmi->mount_source); |
| 237 | freez(bmi->filesystem); |
| 238 | |
| 239 | freez(bmi); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | static void free_basic_mountinfo_list(struct basic_mountinfo *root) |
| 244 | { |
| 245 | struct basic_mountinfo *bmi = root, *next; |
| 246 | |
| 247 | while (bmi) { |
| 248 | next = bmi->next; |
| 249 | free_basic_mountinfo(bmi); |
| 250 | bmi = next; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | static void calculate_values_and_show_charts( |
| 255 | struct basic_mountinfo *mi, |
| 256 | struct mount_point_metadata *m, |
| 257 | struct statvfs *buff_statvfs, |
| 258 | int update_every) |
| 259 | { |
| 260 | const char *family = mi->mount_point; |
| 261 | const char *disk = mi->persistent_id; |
| 262 | |
| 263 | // logic found at get_fs_usage() in coreutils |
| 264 | unsigned long bsize = (buff_statvfs->f_frsize) ? buff_statvfs->f_frsize : buff_statvfs->f_bsize; |
| 265 | |
| 266 | fsblkcnt_t bavail = buff_statvfs->f_bavail; |
| 267 | fsblkcnt_t btotal = buff_statvfs->f_blocks; |
| 268 | fsblkcnt_t bavail_root = buff_statvfs->f_bfree; |
| 269 | fsblkcnt_t breserved_root = bavail_root - bavail; |
| 270 | fsblkcnt_t bused = likely(btotal >= bavail_root) ? btotal - bavail_root : bavail_root - btotal; |
| 271 | |
| 272 | #ifdef NETDATA_INTERNAL_CHECKS |
| 273 | if(unlikely(btotal != bavail + breserved_root + bused)) |
| 274 | collector_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); |
| 275 | #endif |
| 276 | |
| 277 | // -------------------------------------------------------------------------- |
| 278 | |
| 279 | fsfilcnt_t favail = buff_statvfs->f_favail; |
| 280 | fsfilcnt_t ftotal = buff_statvfs->f_files; |
| 281 | fsfilcnt_t favail_root = buff_statvfs->f_ffree; |
| 282 | fsfilcnt_t freserved_root = favail_root - favail; |
| 283 | fsfilcnt_t fused = ftotal - favail_root; |
| 284 | |
| 285 | if(m->do_inodes == CONFIG_BOOLEAN_AUTO && favail == (fsfilcnt_t)-1) { |
| 286 | // this file system does not support inodes reporting |
| 287 | // eg. cephfs |
| 288 | m->do_inodes = CONFIG_BOOLEAN_NO; |
| 289 | } |
| 290 | |
| 291 | #ifdef NETDATA_INTERNAL_CHECKS |
| 292 | if(unlikely(btotal != bavail + breserved_root + bused)) |
| 293 | collector_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); |
| 294 | #endif |
| 295 | |
| 296 | int rendered = 0; |
| 297 | |
| 298 | if (m->do_space == CONFIG_BOOLEAN_YES || m->do_space == CONFIG_BOOLEAN_AUTO) { |
| 299 | if(unlikely(!m->st_space) || m->st_space->update_every != update_every) { |
| 300 | m->do_space = CONFIG_BOOLEAN_YES; |
| 301 | m->st_space = rrdset_find_active_bytype_localhost("disk_space", disk); |
| 302 | if(unlikely(!m->st_space || m->st_space->update_every != update_every)) { |
| 303 | char title[4096 + 1]; |
| 304 | snprintfz(title, sizeof(title) - 1, "Disk Space Usage"); |
| 305 | m->st_space = rrdset_create_localhost( |
| 306 | "disk_space" |
| 307 | , disk |
| 308 | , NULL |
| 309 | , family |
| 310 | , "disk.space" |
| 311 | , title |
| 312 | , "GiB" |
| 313 | , PLUGIN_DISKSPACE_NAME |
| 314 | , NULL |
| 315 | , NETDATA_CHART_PRIO_DISKSPACE_SPACE |
| 316 | , update_every |
| 317 | , RRDSET_TYPE_STACKED |
| 318 | ); |
| 319 | } |
| 320 | |
| 321 | rrdset_update_rrdlabels(m->st_space, m->chart_labels); |
| 322 | |
| 323 | m->rd_space_avail = rrddim_add(m->st_space, "avail", NULL, (collected_number)bsize, 1024 * 1024 * 1024, RRD_ALGORITHM_ABSOLUTE); |
| 324 | m->rd_space_used = rrddim_add(m->st_space, "used", NULL, (collected_number)bsize, 1024 * 1024 * 1024, RRD_ALGORITHM_ABSOLUTE); |
| 325 | m->rd_space_reserved = rrddim_add(m->st_space, "reserved_for_root", "reserved for root", (collected_number)bsize, 1024 * 1024 * 1024, RRD_ALGORITHM_ABSOLUTE); |
| 326 | } |
| 327 | |
| 328 | rrddim_set_by_pointer(m->st_space, m->rd_space_avail, (collected_number)bavail); |
| 329 | rrddim_set_by_pointer(m->st_space, m->rd_space_used, (collected_number)bused); |
| 330 | rrddim_set_by_pointer(m->st_space, m->rd_space_reserved, (collected_number)breserved_root); |
| 331 | rrdset_done(m->st_space); |
| 332 | |
| 333 | rendered++; |
| 334 | } |
| 335 | |
| 336 | if (m->do_inodes == CONFIG_BOOLEAN_YES || m->do_inodes == CONFIG_BOOLEAN_AUTO) { |
| 337 | if(unlikely(!m->st_inodes) || m->st_inodes->update_every != update_every) { |
| 338 | m->do_inodes = CONFIG_BOOLEAN_YES; |
| 339 | m->st_inodes = rrdset_find_active_bytype_localhost("disk_inodes", disk); |
| 340 | if(unlikely(!m->st_inodes) || m->st_inodes->update_every != update_every) { |
| 341 | char title[4096 + 1]; |
| 342 | snprintfz(title, sizeof(title) - 1, "Disk Files (inodes) Usage"); |
| 343 | m->st_inodes = rrdset_create_localhost( |
| 344 | "disk_inodes" |
| 345 | , disk |
| 346 | , NULL |
| 347 | , family |
| 348 | , "disk.inodes" |
| 349 | , title |
| 350 | , "inodes" |
| 351 | , PLUGIN_DISKSPACE_NAME |
| 352 | , NULL |
| 353 | , NETDATA_CHART_PRIO_DISKSPACE_INODES |
| 354 | , update_every |
| 355 | , RRDSET_TYPE_STACKED |
| 356 | ); |
| 357 | } |
| 358 | |
| 359 | rrdset_update_rrdlabels(m->st_inodes, m->chart_labels); |
| 360 | |
| 361 | m->rd_inodes_avail = rrddim_add(m->st_inodes, "avail", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE); |
| 362 | m->rd_inodes_used = rrddim_add(m->st_inodes, "used", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE); |
| 363 | m->rd_inodes_reserved = rrddim_add(m->st_inodes, "reserved_for_root", "reserved for root", 1, 1, RRD_ALGORITHM_ABSOLUTE); |
| 364 | } |
| 365 | |
| 366 | rrddim_set_by_pointer(m->st_inodes, m->rd_inodes_avail, (collected_number)favail); |
| 367 | rrddim_set_by_pointer(m->st_inodes, m->rd_inodes_used, (collected_number)fused); |
| 368 | rrddim_set_by_pointer(m->st_inodes, m->rd_inodes_reserved, (collected_number)freserved_root); |
| 369 | rrdset_done(m->st_inodes); |
| 370 | |
| 371 | rendered++; |
| 372 | } |
| 373 | |
| 374 | if(likely(rendered)) |
| 375 | m->collected++; |
| 376 | } |
| 377 | |
| 378 | // ---------------------------------------------------------------------------- |
| 379 | // ZFS helper functions |
| 380 | |
| 381 | // Extract pool name from ZFS mount_source |
| 382 | // "tank" → "tank", "tank/data/set" → "tank" |
| 383 | static const char *extract_zfs_pool_name(const char *mount_source, char *buf, size_t buf_size) { |
| 384 | if (!mount_source || !mount_source[0] || buf_size == 0) |
| 385 | return NULL; |
| 386 | |
| 387 | const char *slash = strchr(mount_source, '/'); |
| 388 | if (slash) { |
| 389 | size_t len = slash - mount_source; |
| 390 | if (len >= buf_size) |
| 391 | len = buf_size - 1; |
| 392 | memcpy(buf, mount_source, len); |
| 393 | buf[len] = '\0'; |
| 394 | } else { |
| 395 | strncpyz(buf, mount_source, buf_size - 1); |
| 396 | } |
| 397 | |
| 398 | return buf; |
| 399 | } |
| 400 | |
| 401 | // LXC detection result – set once at startup in diskspace_main, never changes at runtime |
| 402 | static bool zfs_inside_lxc_container = false; |
| 403 | |
| 404 | // Cache the capacity of a ZFS pool mount into zfs_cache. |
| 405 | // Called from do_disk_space_stats / do_slow_disk_space_stats after their existing statvfs() |
| 406 | // succeeds, so no extra blocking call is introduced. |
| 407 | static void zfs_cache_pool_capacity(const char *filesystem, const char *mount_source, |
| 408 | const struct statvfs *buff) |
| 409 | { |
| 410 | if (!zfs_datasets_heuristic || !zfs_cache) |
| 411 | return; |
| 412 | if (!filesystem || strcmp(filesystem, "zfs") != 0) |
| 413 | return; |
| 414 | // Only pool mounts – datasets have '/' in mount_source |
| 415 | if (!mount_source || !mount_source[0] || strchr(mount_source, '/')) |
| 416 | return; |
| 417 | |
| 418 | // Skip if the cached entry is still fresh |
| 419 | time_t now = now_realtime_sec(); |
| 420 | const DICTIONARY_ITEM *existing = dictionary_get_and_acquire_item(zfs_cache, mount_source); |
| 421 | if (existing) { |
| 422 | struct zfs_cache_entry *entry = dictionary_acquired_item_value(existing); |
| 423 | bool fresh = entry->is_pool && (now - entry->last_checked < ZFS_DATASET_RECHECK_SECONDS); |
| 424 | dictionary_acquired_item_release(zfs_cache, existing); |
| 425 | if (fresh) |
| 426 | return; |
| 427 | } |
| 428 | |
| 429 | unsigned long bsize = buff->f_frsize ? buff->f_frsize : buff->f_bsize; |
| 430 | uint64_t capacity = (uint64_t)buff->f_blocks * bsize; |
| 431 | |
| 432 | struct zfs_cache_entry new_entry = { |
| 433 | .last_checked = now, |
| 434 | .is_pool = true, |
| 435 | .pool_capacity = capacity, |
| 436 | .excluded = false |
| 437 | }; |
| 438 | dictionary_set(zfs_cache, mount_source, &new_entry, sizeof(new_entry)); |
| 439 | } |
| 440 | |
| 441 | // Check if a ZFS dataset has a cached exclusion decision that's still valid |
| 442 | // Returns: true if cached as excluded (skip statvfs), false otherwise |
| 443 | static bool zfs_dataset_cached_excluded(const char *filesystem, const char *mount_point, const char *mount_source) { |
| 444 | // Quick checks that don't need cache |
| 445 | if (!filesystem || strcmp(filesystem, "zfs") != 0) |
| 446 | return false; |
| 447 | if (!zfs_datasets_heuristic || !zfs_cache) |
| 448 | return false; |
| 449 | if (zfs_inside_lxc_container) |
| 450 | return false; |
| 451 | if (mount_point && strcmp(mount_point, "/") == 0) |
| 452 | return false; |
| 453 | if (!mount_source || !mount_source[0] || !strchr(mount_source, '/')) |
| 454 | return false; // not a dataset |
| 455 | |
| 456 | const DICTIONARY_ITEM *item = dictionary_get_and_acquire_item(zfs_cache, mount_source); |
| 457 | if (!item) |
| 458 | return false; // not in cache |
| 459 | |
| 460 | struct zfs_cache_entry *entry = dictionary_acquired_item_value(item); |
| 461 | |
| 462 | // Only use cache for datasets (not pools), and only if still fresh |
| 463 | time_t now = now_realtime_sec(); |
| 464 | bool cached_excluded = !entry->is_pool && |
| 465 | entry->excluded && |
| 466 | (now - entry->last_checked < ZFS_DATASET_RECHECK_SECONDS); |
| 467 | |
| 468 | dictionary_acquired_item_release(zfs_cache, item); |
| 469 | |
| 470 | return cached_excluded; |
| 471 | } |
| 472 | |
| 473 | // Determine if a ZFS mount should be excluded and cache the decision |
| 474 | // Returns: true if should exclude, false if should keep |
| 475 | static bool should_exclude_zfs(const char *filesystem, const char *mount_point, const char *mount_source, struct statvfs *buff) { |
| 476 | // 1. Not ZFS → keep |
| 477 | if (!filesystem || strcmp(filesystem, "zfs") != 0) |
| 478 | return false; |
| 479 | |
| 480 | // 2. Root mount → always keep |
| 481 | if (mount_point && strcmp(mount_point, "/") == 0) |
| 482 | return false; |
| 483 | |
| 484 | // 3. Inside LXC container → keep all (container sees virtualized mounts) |
| 485 | if (zfs_inside_lxc_container) |
| 486 | return false; |
| 487 | |
| 488 | // 4. Not a dataset (it's a pool) → keep |
| 489 | // Dataset = has '/' in mount_source, Pool = no '/' |
| 490 | if (!mount_source || !mount_source[0] || !strchr(mount_source, '/')) |
| 491 | return false; |
| 492 | |
| 493 | // 5. Heuristic disabled → use pattern matching (default "!*" excludes nothing) |
| 494 | if (!zfs_datasets_heuristic) |
| 495 | return excluded_zfs_datasets_pattern && simple_pattern_matches(excluded_zfs_datasets_pattern, mount_point); |
| 496 | |
| 497 | if (!zfs_cache) |
| 498 | return false; |
| 499 | |
| 500 | // 6. Heuristic enabled - use capacity logic |
| 501 | char pool_name_buf[256]; |
| 502 | const char *pool_name = extract_zfs_pool_name(mount_source, pool_name_buf, sizeof(pool_name_buf)); |
| 503 | if (!pool_name || !pool_name[0]) |
| 504 | return false; // can't determine pool → keep |
| 505 | |
| 506 | // Get pool info from cache |
| 507 | const DICTIONARY_ITEM *pool_item = dictionary_get_and_acquire_item(zfs_cache, pool_name); |
| 508 | if (!pool_item) |
| 509 | return false; // unknown pool → keep |
| 510 | |
| 511 | struct zfs_cache_entry *pool_entry = dictionary_acquired_item_value(pool_item); |
| 512 | |
| 513 | // 7. Verify it's a pool entry with valid capacity |
| 514 | if (!pool_entry->is_pool || !pool_entry->pool_capacity) { |
| 515 | dictionary_acquired_item_release(zfs_cache, pool_item); |
| 516 | return false; |
| 517 | } |
| 518 | |
| 519 | // 8. Check if pool cache entry is still fresh (pool may have been unmounted) |
| 520 | // Use 2x interval: if entry missed one refresh cycle, pool is likely unmounted |
| 521 | time_t now = now_realtime_sec(); |
| 522 | if (now - pool_entry->last_checked >= ZFS_DATASET_RECHECK_SECONDS * 2) { |
| 523 | dictionary_acquired_item_release(zfs_cache, pool_item); |
| 524 | return false; |
| 525 | } |
| 526 | |
| 527 | uint64_t pool_capacity = pool_entry->pool_capacity; |
| 528 | dictionary_acquired_item_release(zfs_cache, pool_item); |
| 529 | |
| 530 | // 9. Calculate this dataset's capacity |
| 531 | unsigned long bsize = buff->f_frsize ? buff->f_frsize : buff->f_bsize; |
| 532 | uint64_t dataset_capacity = (uint64_t)buff->f_blocks * bsize; |
| 533 | |
| 534 | // 10. Determine exclusion: dataset capacity >= pool capacity → no quota → exclude |
| 535 | // Datasets without quotas report same capacity as pool. |
| 536 | // Datasets with quotas report smaller capacity (capped by quota). |
| 537 | bool excluded = (dataset_capacity >= pool_capacity); |
| 538 | |
| 539 | // 11. Cache the decision for this dataset |
| 540 | struct zfs_cache_entry dataset_entry = { |
| 541 | .last_checked = now, |
| 542 | .is_pool = false, |
| 543 | .pool_capacity = 0, |
| 544 | .excluded = excluded |
| 545 | }; |
| 546 | dictionary_set(zfs_cache, mount_source, &dataset_entry, sizeof(dataset_entry)); |
| 547 | |
| 548 | return excluded; |
| 549 | } |
| 550 | |
| 551 | static inline void do_disk_space_stats(struct mountinfo *mi, int update_every) { |
| 552 | const char *disk = mi->persistent_id; |
| 553 | |
| 554 | usec_t slow_timeout = MAX_STAT_USEC * update_every; |
| 555 | |
| 556 | int do_space, do_inodes; |
| 557 | |
| 558 | if(unlikely(!dict_mountpoints)) |
| 559 | diskspace_mountpoints_init(); |
| 560 | |
| 561 | const DICTIONARY_ITEM *item = dictionary_get_and_acquire_item(dict_mountpoints, mi->mount_point); |
| 562 | if(unlikely(!item)) { |
| 563 | bool slow = false; |
| 564 | |
| 565 | int def_space = inicfg_get_boolean_ondemand(&netdata_config, CONFIG_SECTION_DISKSPACE, "space usage for all disks", CONFIG_BOOLEAN_AUTO); |
| 566 | int def_inodes = inicfg_get_boolean_ondemand(&netdata_config, CONFIG_SECTION_DISKSPACE, "inodes usage for all disks", CONFIG_BOOLEAN_AUTO); |
| 567 | |
| 568 | if(unlikely(simple_pattern_matches(excluded_mountpoints, mi->mount_point))) { |
| 569 | def_space = CONFIG_BOOLEAN_NO; |
| 570 | def_inodes = CONFIG_BOOLEAN_NO; |
| 571 | } |
| 572 | |
| 573 | if(unlikely(simple_pattern_matches(excluded_filesystems, mi->filesystem))) { |
| 574 | def_space = CONFIG_BOOLEAN_NO; |
| 575 | def_inodes = CONFIG_BOOLEAN_NO; |
| 576 | } |
| 577 | if (unlikely(simple_pattern_matches(excluded_filesystems_inodes, mi->filesystem))) { |
| 578 | def_inodes = CONFIG_BOOLEAN_NO; |
| 579 | } |
| 580 | |
| 581 | // check if the mount point is a directory #2407 |
| 582 | // but only when it is enabled by default #4491 |
| 583 | if(def_space != CONFIG_BOOLEAN_NO || def_inodes != CONFIG_BOOLEAN_NO) { |
| 584 | usec_t start_time = now_monotonic_high_precision_usec(); |
| 585 | struct stat bs; |
| 586 | |
| 587 | if(stat(mi->mount_point_stat_path, &bs) == -1) { |
| 588 | collector_error("DISKSPACE: Cannot stat() mount point '%s' (disk '%s', filesystem '%s', root '%s')." |
| 589 | , mi->mount_point_stat_path |
| 590 | , disk |
| 591 | , mi->filesystem?mi->filesystem:"" |
| 592 | , mi->root?mi->root:"" |
| 593 | ); |
| 594 | def_space = CONFIG_BOOLEAN_NO; |
| 595 | def_inodes = CONFIG_BOOLEAN_NO; |
| 596 | } |
| 597 | else { |
| 598 | if((bs.st_mode & S_IFMT) != S_IFDIR) { |
| 599 | collector_error("DISKSPACE: Mount point '%s' (disk '%s', filesystem '%s', root '%s') is not a directory." |
| 600 | , mi->mount_point_stat_path |
| 601 | , disk |
| 602 | , mi->filesystem?mi->filesystem:"" |
| 603 | , mi->root?mi->root:"" |
| 604 | ); |
| 605 | def_space = CONFIG_BOOLEAN_NO; |
| 606 | def_inodes = CONFIG_BOOLEAN_NO; |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | if ((now_monotonic_high_precision_usec() - start_time) > slow_timeout) |
| 611 | slow = true; |
| 612 | } |
| 613 | |
| 614 | char var_name[4096 + 1]; |
| 615 | snprintfz(var_name, 4096, "plugin:proc:diskspace:%s", mi->mount_point); |
| 616 | |
| 617 | do_space = def_space; |
| 618 | do_inodes = def_inodes; |
| 619 | |
| 620 | if (inicfg_exists(&netdata_config, var_name, "space usage")) |
| 621 | do_space = inicfg_get_boolean_ondemand(&netdata_config, var_name, "space usage", def_space); |
| 622 | if (inicfg_exists(&netdata_config, var_name, "inodes usage")) |
| 623 | do_inodes = inicfg_get_boolean_ondemand(&netdata_config, var_name, "inodes usage", def_inodes); |
| 624 | |
| 625 | struct mount_point_metadata mp = { |
| 626 | .do_space = do_space, |
| 627 | .do_inodes = do_inodes, |
| 628 | .shown_error = false, |
| 629 | .updated = false, |
| 630 | .slow = slow, |
| 631 | |
| 632 | .collected = 0, |
| 633 | .filesystem = string_strdupz(mi->filesystem), |
| 634 | .mountroot = string_strdupz(mi->root), |
| 635 | .chart_labels = rrdlabels_create(), |
| 636 | |
| 637 | .st_space = NULL, |
| 638 | .rd_space_avail = NULL, |
| 639 | .rd_space_used = NULL, |
| 640 | .rd_space_reserved = NULL, |
| 641 | |
| 642 | .st_inodes = NULL, |
| 643 | .rd_inodes_avail = NULL, |
| 644 | .rd_inodes_used = NULL, |
| 645 | .rd_inodes_reserved = NULL |
| 646 | }; |
| 647 | |
| 648 | rrdlabels_add(mp.chart_labels, "mount_point", mi->mount_point, RRDLABEL_SRC_AUTO); |
| 649 | rrdlabels_add(mp.chart_labels, "filesystem", mi->filesystem, RRDLABEL_SRC_AUTO); |
| 650 | rrdlabels_add(mp.chart_labels, "mount_root", mi->root, RRDLABEL_SRC_AUTO); |
| 651 | |
| 652 | item = dictionary_set_and_acquire_item(dict_mountpoints, mi->mount_point, &mp, sizeof(struct mount_point_metadata)); |
| 653 | } |
| 654 | |
| 655 | struct mount_point_metadata *m = dictionary_acquired_item_value(item); |
| 656 | if (m->slow) { |
| 657 | add_basic_mountinfo(&slow_mountinfo_tmp_root, mi); |
| 658 | goto cleanup; |
| 659 | } |
| 660 | |
| 661 | m->updated = true; |
| 662 | |
| 663 | if(unlikely(m->do_space == CONFIG_BOOLEAN_NO && m->do_inodes == CONFIG_BOOLEAN_NO)) { |
| 664 | goto cleanup; |
| 665 | } |
| 666 | |
| 667 | if (unlikely( |
| 668 | mi->flags & MOUNTINFO_READONLY && |
| 669 | !(mi->flags & MOUNTINFO_IS_IN_SYSD_PROTECTED_LIST) && |
| 670 | !m->collected && |
| 671 | m->do_space != CONFIG_BOOLEAN_YES && |
| 672 | m->do_inodes != CONFIG_BOOLEAN_YES)) { |
| 673 | goto cleanup; |
| 674 | } |
| 675 | |
| 676 | // Check if this ZFS dataset has a cached exclusion decision (skip statvfs entirely) |
| 677 | if (zfs_dataset_cached_excluded(mi->filesystem, mi->mount_point, mi->mount_source)) |
| 678 | goto cleanup; |
| 679 | |
| 680 | usec_t start_time = now_monotonic_high_precision_usec(); |
| 681 | struct statvfs buff_statvfs; |
| 682 | |
| 683 | if (statvfs(mi->mount_point_stat_path, &buff_statvfs) < 0) { |
| 684 | if(!m->shown_error) { |
| 685 | collector_error("DISKSPACE: failed to statvfs() mount point '%s' (disk '%s', filesystem '%s', root '%s')" |
| 686 | , mi->mount_point_stat_path |
| 687 | , disk |
| 688 | , mi->filesystem?mi->filesystem:"" |
| 689 | , mi->root?mi->root:"" |
| 690 | ); |
| 691 | m->shown_error = true; |
| 692 | } |
| 693 | goto cleanup; |
| 694 | } |
| 695 | |
| 696 | if ((now_monotonic_high_precision_usec() - start_time) > slow_timeout) |
| 697 | m->slow = true; |
| 698 | |
| 699 | // Cache ZFS pool capacity so dataset exclusion heuristic can work next cycle |
| 700 | zfs_cache_pool_capacity(mi->filesystem, mi->mount_source, &buff_statvfs); |
| 701 | |
| 702 | // Check if this ZFS mount should be excluded (capacity-based heuristic) |
| 703 | if (should_exclude_zfs(mi->filesystem, mi->mount_point, mi->mount_source, &buff_statvfs)) |
| 704 | goto cleanup; |
| 705 | |
| 706 | m->shown_error = false; |
| 707 | |
| 708 | struct basic_mountinfo bmi; |
| 709 | bmi.mount_point = mi->mount_point; |
| 710 | bmi.persistent_id = mi->persistent_id; |
| 711 | bmi.filesystem = mi->filesystem; |
| 712 | bmi.root = mi->root; |
| 713 | |
| 714 | calculate_values_and_show_charts(&bmi, m, &buff_statvfs, update_every); |
| 715 | |
| 716 | cleanup: |
| 717 | dictionary_acquired_item_release(dict_mountpoints, item); |
| 718 | } |
| 719 | |
| 720 | static inline void do_slow_disk_space_stats(struct basic_mountinfo *mi, int update_every) { |
| 721 | const DICTIONARY_ITEM *item = dictionary_get_and_acquire_item(dict_mountpoints, mi->mount_point); |
| 722 | if(!item) return; |
| 723 | |
| 724 | struct mount_point_metadata *m = dictionary_acquired_item_value(item); |
| 725 | m->updated = true; |
| 726 | |
| 727 | struct statvfs buff_statvfs; |
| 728 | if (statvfs(mi->mount_point_stat_path, &buff_statvfs) < 0) { |
| 729 | if(!m->shown_error) { |
| 730 | collector_error("DISKSPACE: failed to statvfs() mount point '%s' (disk '%s', filesystem '%s', root '%s')" |
| 731 | , mi->mount_point_stat_path |
| 732 | , mi->persistent_id |
| 733 | , mi->filesystem?mi->filesystem:"" |
| 734 | , mi->root?mi->root:"" |
| 735 | ); |
| 736 | m->shown_error = true; |
| 737 | } |
| 738 | goto cleanup; |
| 739 | } |
| 740 | m->shown_error = false; |
| 741 | |
| 742 | // Cache ZFS pool capacity so dataset exclusion heuristic can work next cycle |
| 743 | zfs_cache_pool_capacity(mi->filesystem, mi->mount_source, &buff_statvfs); |
| 744 | |
| 745 | // Check if this ZFS mount should be excluded (same logic as fast path) |
| 746 | if (should_exclude_zfs(mi->filesystem, mi->mount_point, mi->mount_source, &buff_statvfs)) |
| 747 | goto cleanup; |
| 748 | |
| 749 | calculate_values_and_show_charts(mi, m, &buff_statvfs, update_every); |
| 750 | |
| 751 | cleanup: |
| 752 | dictionary_acquired_item_release(dict_mountpoints, item); |
| 753 | } |
| 754 | |
| 755 | #define WORKER_JOB_SLOW_MOUNTPOINT 0 |
| 756 | #define WORKER_JOB_SLOW_CLEANUP 1 |
| 757 | |
| 758 | struct slow_worker_data { |
| 759 | int update_every; |
| 760 | }; |
| 761 | |
| 762 | void diskspace_slow_worker(void *ptr) |
| 763 | { |
| 764 | struct slow_worker_data *data = (struct slow_worker_data *)ptr; |
| 765 | |
| 766 | worker_register("DISKSPACE_SLOW"); |
| 767 | worker_register_job_name(WORKER_JOB_SLOW_MOUNTPOINT, "mountpoint"); |
| 768 | worker_register_job_name(WORKER_JOB_SLOW_CLEANUP, "cleanup"); |
| 769 | |
| 770 | struct basic_mountinfo *slow_mountinfo_root = NULL; |
| 771 | |
| 772 | int slow_update_every = data->update_every > SLOW_UPDATE_EVERY ? data->update_every : SLOW_UPDATE_EVERY; |
| 773 | |
| 774 | usec_t step = slow_update_every * USEC_PER_SEC; |
| 775 | usec_t real_step = USEC_PER_SEC; |
| 776 | heartbeat_t hb; |
| 777 | heartbeat_init(&hb, USEC_PER_SEC); |
| 778 | |
| 779 | while(service_running(SERVICE_COLLECTORS)) { |
| 780 | worker_is_idle(); |
| 781 | heartbeat_next(&hb); |
| 782 | |
| 783 | if (real_step < step) { |
| 784 | real_step += USEC_PER_SEC; |
| 785 | continue; |
| 786 | } |
| 787 | real_step = USEC_PER_SEC; |
| 788 | |
| 789 | usec_t start_time = now_monotonic_high_precision_usec(); |
| 790 | |
| 791 | if (!dict_mountpoints) |
| 792 | continue; |
| 793 | |
| 794 | if(unlikely(!service_running(SERVICE_COLLECTORS))) break; |
| 795 | |
| 796 | // -------------------------------------------------------------------------- |
| 797 | // disk space metrics |
| 798 | |
| 799 | worker_is_busy(WORKER_JOB_SLOW_MOUNTPOINT); |
| 800 | |
| 801 | netdata_mutex_lock(&slow_mountinfo_mutex); |
| 802 | free_basic_mountinfo_list(slow_mountinfo_root); |
| 803 | slow_mountinfo_root = slow_mountinfo_tmp_root; |
| 804 | slow_mountinfo_tmp_root = NULL; |
| 805 | netdata_mutex_unlock(&slow_mountinfo_mutex); |
| 806 | |
| 807 | struct basic_mountinfo *bmi; |
| 808 | for(bmi = slow_mountinfo_root; bmi; bmi = bmi->next) { |
| 809 | do_slow_disk_space_stats(bmi, slow_update_every); |
| 810 | |
| 811 | if(unlikely(!service_running(SERVICE_COLLECTORS))) break; |
| 812 | } |
| 813 | |
| 814 | if(unlikely(!service_running(SERVICE_COLLECTORS))) break; |
| 815 | |
| 816 | if(dict_mountpoints) { |
| 817 | worker_is_busy(WORKER_JOB_SLOW_CLEANUP); |
| 818 | mount_points_cleanup(true); |
| 819 | } |
| 820 | |
| 821 | usec_t dt = now_monotonic_high_precision_usec() - start_time; |
| 822 | if (dt > step) { |
| 823 | slow_update_every = (dt / USEC_PER_SEC) * 3 / 2; |
| 824 | if (slow_update_every % SLOW_UPDATE_EVERY) |
| 825 | slow_update_every += SLOW_UPDATE_EVERY - slow_update_every % SLOW_UPDATE_EVERY; |
| 826 | step = slow_update_every * USEC_PER_SEC; |
| 827 | } |
| 828 | } |
| 829 | |
| 830 | // cleanup |
| 831 | netdata_mutex_lock(&slow_mountinfo_mutex); |
| 832 | free_basic_mountinfo_list(slow_mountinfo_root); |
| 833 | netdata_mutex_unlock(&slow_mountinfo_mutex); |
| 834 | |
| 835 | worker_unregister(); |
| 836 | } |
| 837 | |
| 838 | static void diskspace_main_cleanup(void *ptr) { |
| 839 | struct netdata_static_thread *static_thread = ptr; |
| 840 | if(!static_thread) return; |
| 841 | |
| 842 | static_thread->enabled = NETDATA_MAIN_THREAD_EXITING; |
| 843 | |
| 844 | if (diskspace_slow_thread) |
| 845 | nd_thread_join(diskspace_slow_thread); |
| 846 | |
| 847 | netdata_mutex_lock(&slow_mountinfo_mutex); |
| 848 | free_basic_mountinfo_list(slow_mountinfo_tmp_root); |
| 849 | netdata_mutex_unlock(&slow_mountinfo_mutex); |
| 850 | |
| 851 | // Free the mountpoints dictionary |
| 852 | dictionary_destroy(dict_mountpoints); |
| 853 | dict_mountpoints = NULL; |
| 854 | |
| 855 | simple_pattern_free(excluded_mountpoints); |
| 856 | excluded_mountpoints = NULL; |
| 857 | |
| 858 | simple_pattern_free(excluded_filesystems); |
| 859 | excluded_filesystems = NULL; |
| 860 | |
| 861 | simple_pattern_free(excluded_filesystems_inodes); |
| 862 | excluded_filesystems_inodes = NULL; |
| 863 | |
| 864 | // Free ZFS deduplication resources |
| 865 | dictionary_destroy(zfs_cache); |
| 866 | zfs_cache = NULL; |
| 867 | |
| 868 | simple_pattern_free(excluded_zfs_datasets_pattern); |
| 869 | excluded_zfs_datasets_pattern = NULL; |
| 870 | |
| 871 | rrd_collector_finished(); |
| 872 | worker_unregister(); |
| 873 | |
| 874 | static_thread->enabled = NETDATA_MAIN_THREAD_EXITED; |
| 875 | } |
| 876 | |
| 877 | #define WORKER_JOB_MOUNTINFO 0 |
| 878 | #define WORKER_JOB_MOUNTPOINT 1 |
| 879 | #define WORKER_JOB_CLEANUP 2 |
| 880 | |
| 881 | #if WORKER_UTILIZATION_MAX_JOB_TYPES < 3 |
| 882 | #error WORKER_UTILIZATION_MAX_JOB_TYPES has to be at least 3 |
| 883 | #endif |
| 884 | |
| 885 | static int diskspace_function_mount_points(BUFFER *wb, const char *function __maybe_unused, BUFFER *payload __maybe_unused, const char *source __maybe_unused) { |
| 886 | netdata_mutex_lock(&slow_mountinfo_mutex); |
| 887 | |
| 888 | buffer_flush(wb); |
| 889 | wb->content_type = CT_APPLICATION_JSON; |
| 890 | buffer_json_initialize(wb, "\"", "\"", 0, true, BUFFER_JSON_OPTIONS_DEFAULT); |
| 891 | |
| 892 | buffer_json_member_add_string(wb, "hostname", rrdhost_hostname(localhost)); |
| 893 | buffer_json_member_add_uint64(wb, "status", HTTP_RESP_OK); |
| 894 | buffer_json_member_add_string(wb, "type", "table"); |
| 895 | buffer_json_member_add_time_t(wb, "update_every", 1); |
| 896 | buffer_json_member_add_boolean(wb, "has_history", false); |
| 897 | buffer_json_member_add_string(wb, "help", RRDFUNCTIONS_DISKSPACE_HELP); |
| 898 | buffer_json_member_add_array(wb, "data"); |
| 899 | |
| 900 | double max_space_util = 0.0; |
| 901 | double max_space_avail = 0.0; |
| 902 | double max_space_used = 0.0; |
| 903 | double max_space_reserved = 0.0; |
| 904 | |
| 905 | double max_inodes_util = 0.0; |
| 906 | double max_inodes_avail = 0.0; |
| 907 | double max_inodes_used = 0.0; |
| 908 | double max_inodes_reserved = 0.0; |
| 909 | |
| 910 | struct mount_point_metadata *mp; |
| 911 | dfe_start_read(dict_mountpoints, mp) { |
| 912 | if (!mp->collected) |
| 913 | continue; |
| 914 | |
| 915 | buffer_json_add_array_item_array(wb); |
| 916 | |
| 917 | buffer_json_add_array_item_string(wb, mp_dfe.name); |
| 918 | buffer_json_add_array_item_string(wb, string2str(mp->filesystem)); |
| 919 | buffer_json_add_array_item_string(wb, string2str(mp->mountroot)); |
| 920 | |
| 921 | double space_avail = rrddim_get_last_stored_value(mp->rd_space_avail, &max_space_avail, 1.0); |
| 922 | double space_used = rrddim_get_last_stored_value(mp->rd_space_used, &max_space_used, 1.0); |
| 923 | double space_reserved = rrddim_get_last_stored_value(mp->rd_space_reserved, &max_space_reserved, 1.0); |
| 924 | double inodes_avail = rrddim_get_last_stored_value(mp->rd_inodes_avail, &max_inodes_avail, 1.0); |
| 925 | double inodes_used = rrddim_get_last_stored_value(mp->rd_inodes_used, &max_inodes_used, 1.0); |
| 926 | double inodes_reserved = rrddim_get_last_stored_value(mp->rd_inodes_reserved, &max_inodes_reserved, 1.0); |
| 927 | |
| 928 | double space_util = NAN; |
| 929 | if (!isnan(space_avail) && !isnan(space_used)) { |
| 930 | space_util = space_avail + space_used > 0 ? space_used * 100.0 / (space_avail + space_used) : 0; |
| 931 | max_space_util = MAX(max_space_util, space_util); |
| 932 | } |
| 933 | double inodes_util = NAN; |
| 934 | if (!isnan(inodes_avail) && !isnan(inodes_used)) { |
| 935 | inodes_util = inodes_avail + inodes_used > 0 ? inodes_used * 100.0 / (inodes_avail + inodes_used) : 0; |
| 936 | max_inodes_util = MAX(max_inodes_util, inodes_util); |
| 937 | } |
| 938 | |
| 939 | buffer_json_add_array_item_double(wb, space_util); |
| 940 | buffer_json_add_array_item_double(wb, space_avail); |
| 941 | buffer_json_add_array_item_double(wb, space_used); |
| 942 | buffer_json_add_array_item_double(wb, space_reserved); |
| 943 | |
| 944 | buffer_json_add_array_item_double(wb, inodes_util); |
| 945 | buffer_json_add_array_item_double(wb, inodes_avail); |
| 946 | buffer_json_add_array_item_double(wb, inodes_used); |
| 947 | buffer_json_add_array_item_double(wb, inodes_reserved); |
| 948 | |
| 949 | buffer_json_array_close(wb); |
| 950 | } |
| 951 | dfe_done(mp); |
| 952 | |
| 953 | buffer_json_array_close(wb); // data |
| 954 | buffer_json_member_add_object(wb, "columns"); |
| 955 | { |
| 956 | size_t field_id = 0; |
| 957 | |
| 958 | buffer_rrdf_table_add_field(wb, field_id++, "Mountpoint", "Mountpoint Name", |
| 959 | RRDF_FIELD_TYPE_STRING, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE, |
| 960 | 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL, |
| 961 | RRDF_FIELD_SUMMARY_COUNT, RRDF_FIELD_FILTER_MULTISELECT, |
| 962 | RRDF_FIELD_OPTS_VISIBLE | RRDF_FIELD_OPTS_UNIQUE_KEY | RRDF_FIELD_OPTS_STICKY | RRDF_FIELD_OPTS_FULL_WIDTH, |
| 963 | NULL); |
| 964 | buffer_rrdf_table_add_field(wb, field_id++, "Filesystem", "Mountpoint Filesystem", |
| 965 | RRDF_FIELD_TYPE_STRING, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE, |
| 966 | 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL, |
| 967 | RRDF_FIELD_SUMMARY_COUNT, RRDF_FIELD_FILTER_MULTISELECT, |
| 968 | RRDF_FIELD_OPTS_VISIBLE | RRDF_FIELD_OPTS_UNIQUE_KEY, |
| 969 | NULL); |
| 970 | buffer_rrdf_table_add_field(wb, field_id++, "Root", "Mountpoint Root", |
| 971 | RRDF_FIELD_TYPE_STRING, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE, |
| 972 | 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL, |
| 973 | RRDF_FIELD_SUMMARY_COUNT, RRDF_FIELD_FILTER_MULTISELECT, |
| 974 | RRDF_FIELD_OPTS_UNIQUE_KEY, |
| 975 | NULL); |
| 976 | |
| 977 | buffer_rrdf_table_add_field(wb, field_id++, "Used%", "Space Utilization", |
| 978 | RRDF_FIELD_TYPE_BAR_WITH_INTEGER, RRDF_FIELD_VISUAL_BAR, RRDF_FIELD_TRANSFORM_NUMBER, |
| 979 | 2, "%", max_space_util, RRDF_FIELD_SORT_DESCENDING, NULL, |
| 980 | RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_NONE, |
| 981 | RRDF_FIELD_OPTS_VISIBLE, |
| 982 | NULL); |
| 983 | buffer_rrdf_table_add_field(wb, field_id++, "Avail", "Space Avail", |
| 984 | RRDF_FIELD_TYPE_BAR_WITH_INTEGER, RRDF_FIELD_VISUAL_BAR, RRDF_FIELD_TRANSFORM_NUMBER, |
| 985 | 2, "GiB", max_space_avail, RRDF_FIELD_SORT_DESCENDING, NULL, |
| 986 | RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_NONE, |
| 987 | RRDF_FIELD_OPTS_VISIBLE, |
| 988 | NULL); |
| 989 | buffer_rrdf_table_add_field(wb, field_id++, "Used", "Space Used", |
| 990 | RRDF_FIELD_TYPE_BAR_WITH_INTEGER, RRDF_FIELD_VISUAL_BAR, RRDF_FIELD_TRANSFORM_NUMBER, |
| 991 | 2, "GiB", max_space_used, RRDF_FIELD_SORT_DESCENDING, NULL, |
| 992 | RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_NONE, |
| 993 | RRDF_FIELD_OPTS_VISIBLE, |
| 994 | NULL); |
| 995 | buffer_rrdf_table_add_field(wb, field_id++, "Reserved", "Space Reserved for root", |
| 996 | RRDF_FIELD_TYPE_BAR_WITH_INTEGER, RRDF_FIELD_VISUAL_BAR, RRDF_FIELD_TRANSFORM_NUMBER, |
| 997 | 2, "GiB", max_space_reserved, RRDF_FIELD_SORT_DESCENDING, NULL, |
| 998 | RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_NONE, |
| 999 | RRDF_FIELD_OPTS_VISIBLE, |
| 1000 | NULL); |
| 1001 | |
| 1002 | buffer_rrdf_table_add_field(wb, field_id++, "iUsed%", "Inodes Utilization", |
| 1003 | RRDF_FIELD_TYPE_BAR_WITH_INTEGER, RRDF_FIELD_VISUAL_BAR, RRDF_FIELD_TRANSFORM_NUMBER, |
| 1004 | 2, "%", max_inodes_util, RRDF_FIELD_SORT_DESCENDING, NULL, |
| 1005 | RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_NONE, |
| 1006 | RRDF_FIELD_OPTS_NONE, |
| 1007 | NULL); |
| 1008 | buffer_rrdf_table_add_field(wb, field_id++, "iAvail", "Inodes Avail", |
| 1009 | RRDF_FIELD_TYPE_BAR_WITH_INTEGER, RRDF_FIELD_VISUAL_BAR, RRDF_FIELD_TRANSFORM_NUMBER, |
| 1010 | 2, "inodes", max_inodes_avail, RRDF_FIELD_SORT_DESCENDING, NULL, |
| 1011 | RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_NONE, |
| 1012 | RRDF_FIELD_OPTS_NONE, |
| 1013 | NULL); |
| 1014 | buffer_rrdf_table_add_field(wb, field_id++, "iUsed", "Inodes Used", |
| 1015 | RRDF_FIELD_TYPE_BAR_WITH_INTEGER, RRDF_FIELD_VISUAL_BAR, RRDF_FIELD_TRANSFORM_NUMBER, |
| 1016 | 2, "inodes", max_inodes_used, RRDF_FIELD_SORT_DESCENDING, NULL, |
| 1017 | RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_NONE, |
| 1018 | RRDF_FIELD_OPTS_NONE, |
| 1019 | NULL); |
| 1020 | buffer_rrdf_table_add_field(wb, field_id++, "iReserved", "Inodes Reserved for root", |
| 1021 | RRDF_FIELD_TYPE_BAR_WITH_INTEGER, RRDF_FIELD_VISUAL_BAR, RRDF_FIELD_TRANSFORM_NUMBER, |
| 1022 | 2, "inodes", max_inodes_reserved, RRDF_FIELD_SORT_DESCENDING, NULL, |
| 1023 | RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_NONE, |
| 1024 | RRDF_FIELD_OPTS_NONE, |
| 1025 | NULL); |
| 1026 | } |
| 1027 | |
| 1028 | buffer_json_object_close(wb); // columns |
| 1029 | buffer_json_member_add_string(wb, "default_sort_column", "Used%"); |
| 1030 | |
| 1031 | buffer_json_member_add_object(wb, "charts"); |
| 1032 | { |
| 1033 | buffer_json_member_add_object(wb, "Utilization"); |
| 1034 | { |
| 1035 | buffer_json_member_add_string(wb, "name", "Utilization"); |
| 1036 | buffer_json_member_add_string(wb, "type", "stacked-bar"); |
| 1037 | buffer_json_member_add_array(wb, "columns"); |
| 1038 | { |
| 1039 | buffer_json_add_array_item_string(wb, "Used%"); |
| 1040 | } |
| 1041 | buffer_json_array_close(wb); |
| 1042 | } |
| 1043 | buffer_json_object_close(wb); |
| 1044 | |
| 1045 | buffer_json_member_add_object(wb, "Usage"); |
| 1046 | { |
| 1047 | buffer_json_member_add_string(wb, "name", "Usage"); |
| 1048 | buffer_json_member_add_string(wb, "type", "stacked-bar"); |
| 1049 | buffer_json_member_add_array(wb, "columns"); |
| 1050 | { |
| 1051 | buffer_json_add_array_item_string(wb, "Avail"); |
| 1052 | buffer_json_add_array_item_string(wb, "Used"); |
| 1053 | buffer_json_add_array_item_string(wb, "Reserved"); |
| 1054 | } |
| 1055 | buffer_json_array_close(wb); |
| 1056 | } |
| 1057 | buffer_json_object_close(wb); |
| 1058 | |
| 1059 | buffer_json_member_add_object(wb, "Inodes"); |
| 1060 | { |
| 1061 | buffer_json_member_add_string(wb, "name", "Inodes"); |
| 1062 | buffer_json_member_add_string(wb, "type", "stacked-bar"); |
| 1063 | buffer_json_member_add_array(wb, "columns"); |
| 1064 | { |
| 1065 | buffer_json_add_array_item_string(wb, "iAvail"); |
| 1066 | buffer_json_add_array_item_string(wb, "iUsed"); |
| 1067 | buffer_json_add_array_item_string(wb, "iReserved"); |
| 1068 | } |
| 1069 | buffer_json_array_close(wb); |
| 1070 | } |
| 1071 | buffer_json_object_close(wb); |
| 1072 | } |
| 1073 | buffer_json_object_close(wb); // charts |
| 1074 | |
| 1075 | buffer_json_member_add_array(wb, "default_charts"); |
| 1076 | { |
| 1077 | buffer_json_add_array_item_array(wb); |
| 1078 | buffer_json_add_array_item_string(wb, "Utilization"); |
| 1079 | buffer_json_add_array_item_string(wb, "Mountpoint"); |
| 1080 | buffer_json_array_close(wb); |
| 1081 | |
| 1082 | buffer_json_add_array_item_array(wb); |
| 1083 | buffer_json_add_array_item_string(wb, "Usage"); |
| 1084 | buffer_json_add_array_item_string(wb, "Mountpoint"); |
| 1085 | buffer_json_array_close(wb); |
| 1086 | } |
| 1087 | buffer_json_array_close(wb); |
| 1088 | |
| 1089 | buffer_json_member_add_time_t(wb, "expires", now_realtime_sec() + 1); |
| 1090 | buffer_json_finalize(wb); |
| 1091 | |
| 1092 | netdata_mutex_unlock(&slow_mountinfo_mutex); |
| 1093 | return HTTP_RESP_OK; |
| 1094 | } |
| 1095 | |
| 1096 | void diskspace_main(void *ptr) { |
| 1097 | worker_register("DISKSPACE"); |
| 1098 | worker_register_job_name(WORKER_JOB_MOUNTINFO, "mountinfo"); |
| 1099 | worker_register_job_name(WORKER_JOB_MOUNTPOINT, "mountpoint"); |
| 1100 | worker_register_job_name(WORKER_JOB_CLEANUP, "cleanup"); |
| 1101 | |
| 1102 | // Initialize shared state before publishing the function endpoint, so that |
| 1103 | // diskspace_function_mount_points cannot fire against an uninitialized |
| 1104 | // mutex or a NULL dict_mountpoints. |
| 1105 | netdata_mutex_init(&slow_mountinfo_mutex); |
| 1106 | diskspace_mountpoints_init(); |
| 1107 | |
| 1108 | rrd_function_add_inline(localhost, NULL, "mount-points", 10, |
| 1109 | RRDFUNCTIONS_PRIORITY_DEFAULT, RRDFUNCTIONS_VERSION_DEFAULT, |
| 1110 | RRDFUNCTIONS_DISKSPACE_HELP, |
| 1111 | "top", HTTP_ACCESS_ANONYMOUS_DATA, |
| 1112 | diskspace_function_mount_points); |
| 1113 | |
| 1114 | cleanup_mount_points = inicfg_get_boolean(&netdata_config, CONFIG_SECTION_DISKSPACE, "remove charts of unmounted disks" , cleanup_mount_points); |
| 1115 | |
| 1116 | int update_every = (int)inicfg_get_duration_seconds(&netdata_config, CONFIG_SECTION_DISKSPACE, "update every", localhost->rrd_update_every); |
| 1117 | if(update_every < localhost->rrd_update_every) { |
| 1118 | update_every = localhost->rrd_update_every; |
| 1119 | inicfg_set_duration_seconds(&netdata_config, CONFIG_SECTION_DISKSPACE, "update every", update_every); |
| 1120 | } |
| 1121 | |
| 1122 | check_for_new_mountpoints_every = (int)inicfg_get_duration_seconds(&netdata_config, CONFIG_SECTION_DISKSPACE, "check for new mount points every", check_for_new_mountpoints_every); |
| 1123 | if(check_for_new_mountpoints_every < update_every) |
| 1124 | check_for_new_mountpoints_every = update_every; |
| 1125 | |
| 1126 | // ZFS dataset deduplication configuration |
| 1127 | zfs_datasets_heuristic = inicfg_get_boolean( |
| 1128 | &netdata_config, |
| 1129 | CONFIG_SECTION_DISKSPACE, |
| 1130 | "zfs datasets heuristic", |
| 1131 | CONFIG_BOOLEAN_YES); |
| 1132 | |
| 1133 | if (zfs_datasets_heuristic) { |
| 1134 | // Heuristic mode: create cache for pool capacities and dataset exclusion decisions |
| 1135 | zfs_cache = dictionary_create_advanced( |
| 1136 | DICT_OPTION_FIXED_SIZE, |
| 1137 | &dictionary_stats_category_collectors, |
| 1138 | sizeof(struct zfs_cache_entry)); |
| 1139 | } |
| 1140 | |
| 1141 | struct slow_worker_data slow_worker_data = { .update_every = update_every }; |
| 1142 | |
| 1143 | diskspace_slow_thread = nd_thread_create( |
| 1144 | "P[diskspace slow]", |
| 1145 | NETDATA_THREAD_OPTION_DEFAULT, |
| 1146 | diskspace_slow_worker, |
| 1147 | &slow_worker_data); |
| 1148 | |
| 1149 | // LXC detection – done once; virtualised mounts inside LXC bypass the ZFS exclusion heuristic |
| 1150 | zfs_inside_lxc_container = is_lxcfs_proc_mounted(); |
| 1151 | |
| 1152 | heartbeat_t hb; |
| 1153 | heartbeat_init(&hb, update_every * USEC_PER_SEC); |
| 1154 | while(service_running(SERVICE_COLLECTORS)) { |
| 1155 | worker_is_idle(); |
| 1156 | /* usec_t hb_dt = */ heartbeat_next(&hb); |
| 1157 | |
| 1158 | if(unlikely(!service_running(SERVICE_COLLECTORS))) break; |
| 1159 | |
| 1160 | // -------------------------------------------------------------------------- |
| 1161 | // this is smart enough not to reload it every time |
| 1162 | |
| 1163 | worker_is_busy(WORKER_JOB_MOUNTINFO); |
| 1164 | mountinfo_reload(0); |
| 1165 | |
| 1166 | // -------------------------------------------------------------------------- |
| 1167 | // disk space metrics |
| 1168 | |
| 1169 | netdata_mutex_lock(&slow_mountinfo_mutex); |
| 1170 | free_basic_mountinfo_list(slow_mountinfo_tmp_root); |
| 1171 | slow_mountinfo_tmp_root = NULL; |
| 1172 | |
| 1173 | struct mountinfo *mi; |
| 1174 | for(mi = disk_mountinfo_root; mi; mi = mi->next) { |
| 1175 | if(unlikely(mi->flags & (MOUNTINFO_IS_DUMMY | MOUNTINFO_IS_BIND))) |
| 1176 | continue; |
| 1177 | |
| 1178 | // exclude mounts made by ProtectHome and ProtectSystem systemd hardening options |
| 1179 | // https://github.com/netdata/netdata/issues/11498#issuecomment-950982878 |
| 1180 | if(mi->flags & MOUNTINFO_READONLY && mi->flags & MOUNTINFO_IS_IN_SYSD_PROTECTED_LIST && !strcmp(mi->root, mi->mount_point)) |
| 1181 | continue; |
| 1182 | |
| 1183 | worker_is_busy(WORKER_JOB_MOUNTPOINT); |
| 1184 | do_disk_space_stats(mi, update_every); |
| 1185 | if(unlikely(!service_running(SERVICE_COLLECTORS))) break; |
| 1186 | } |
| 1187 | netdata_mutex_unlock(&slow_mountinfo_mutex); |
| 1188 | |
| 1189 | if(unlikely(!service_running(SERVICE_COLLECTORS))) break; |
| 1190 | |
| 1191 | if(dict_mountpoints) { |
| 1192 | worker_is_busy(WORKER_JOB_CLEANUP); |
| 1193 | mount_points_cleanup(false); |
| 1194 | } |
| 1195 | } |
| 1196 | |
| 1197 | // cleanup |
| 1198 | diskspace_main_cleanup(ptr); |
| 1199 | } |