mirror of
https://github.com/netdata/netdata.git
synced 2025-04-06 14:35:32 +00:00

* rrdset - in progress * rrdset optimal constructor; rrdset conflict * rrdset final touches * re-organization of rrdset object members * prevent use-after-free * dictionary dfe supports also counting of iterations * rrddim managed by dictionary * rrd.h cleanup * DICTIONARY_ITEM now is referencing actual dictionary items in the code * removed rrdset linked list * Revert "removed rrdset linked list" This reverts commit 690d6a588b4b99619c2c5e10f84e8f868ae6def5. * removed rrdset linked list * added comments * Switch chart uuid to static allocation in rrdset Remove unused functions * rrdset_archive() and friends... * always create rrdfamily * enable ml_free_dimension * rrddim_foreach done with dfe * most custom rrddim loops replaced with rrddim_foreach * removed accesses to rrddim->dimensions * removed locks that are no longer needed * rrdsetvar is now managed by the dictionary * set rrdset is rrdsetvar, fixes https://github.com/netdata/netdata/pull/13646#issuecomment-1242574853 * conflict callback of rrdsetvar now properly checks if it has to reset the variable * dictionary registered callbacks accept as first parameter the DICTIONARY_ITEM * dictionary dfe now uses internal counter to report; avoided excess variables defined with dfe * dictionary walkthrough callbacks get dictionary acquired items * dictionary reference counters that can be dupped from zero * added advanced functions for get and del * rrdvar managed by dictionaries * thread safety for rrdsetvar * faster rrdvar initialization * rrdvar string lengths should match in all add, del, get functions * rrdvar internals hidden from the rest of the world * rrdvar is now acquired throughout netdata * hide the internal structures of rrdsetvar * rrdsetvar is now acquired through out netdata * rrddimvar managed by dictionary; rrddimvar linked list removed; rrddimvar structures hidden from the rest of netdata * better error handling * dont create variables if not initialized for health * dont create variables if not initialized for health again * rrdfamily is now managed by dictionaries; references of it are acquired dictionary items * type checking on acquired objects * rrdcalc renaming of functions * type checking for rrdfamily_acquired * rrdcalc managed by dictionaries * rrdcalc double free fix * host rrdvars is always needed * attempt to fix deadlock 1 * attempt to fix deadlock 2 * Remove unused variable * attempt to fix deadlock 3 * snprintfz * rrdcalc index in rrdset fix * Stop storing active charts and computing chart hashes * Remove store active chart function * Remove compute chart hash function * Remove sql_store_chart_hash function * Remove store_active_dimension function * dictionary delayed destruction * formatting and cleanup * zero dictionary base on rrdsetvar * added internal error to log delayed destructions of dictionaries * typo in rrddimvar * added debugging info to dictionary * debug info * fix for rrdcalc keys being empty * remove forgotten unlock * remove deadlock * Switch to metadata version 5 and drop chart_hash chart_hash_map chart_active dimension_active v_chart_hash * SQL cosmetic changes * do not busy wait while destroying a referenced dictionary * remove deadlock * code cleanup; re-organization; * fast cleanup and flushing of dictionaries * number formatting fixes * do not delete configured alerts when archiving a chart * rrddim obsolete linked list management outside dictionaries * removed duplicate contexts call * fix crash when rrdfamily is not initialized * dont keep rrddimvar referenced * properly cleanup rrdvar * removed some locks * Do not attempt to cleanup chart_hash / chart_hash_map * rrdcalctemplate managed by dictionary * register callbacks on the right dictionary * removed some more locks * rrdcalc secondary index replaced with linked-list; rrdcalc labels updates are now executed by health thread * when looking up for an alarm look using both chart id and chart name * host initialization a bit more modular * init rrdlabels on host update * preparation for dictionary views * improved comment * unused variables without internal checks * service threads isolation and worker info * more worker info in service thread * thread cancelability debugging with internal checks * strings data races addressed; fixes https://github.com/netdata/netdata/issues/13647 * dictionary modularization * Remove unused SQL statement definition * unit-tested thread safety of dictionaries; removed data race conditions on dictionaries and strings; dictionaries now can detect if the caller is holds a write lock and automatically all the calls become their unsafe versions; all direct calls to unsafe version is eliminated * remove worker_is_idle() from the exit of service functions, because we lose the lock time between loops * rewritten dictionary to have 2 separate locks, one for indexing and another for traversal * Update collectors/cgroups.plugin/sys_fs_cgroup.c Co-authored-by: Vladimir Kobal <vlad@prokk.net> * Update collectors/cgroups.plugin/sys_fs_cgroup.c Co-authored-by: Vladimir Kobal <vlad@prokk.net> * Update collectors/proc.plugin/proc_net_dev.c Co-authored-by: Vladimir Kobal <vlad@prokk.net> * fix memory leak in rrdset cache_dir * minor dictionary changes * dont use index locks in single threaded * obsolete dict option * rrddim options and flags separation; rrdset_done() optimization to keep array of reference pointers to rrddim; * fix jump on uninitialized value in dictionary; remove double free of cache_dir * addressed codacy findings * removed debugging code * use the private refcount on dictionaries * make dictionary item desctructors work on dictionary destruction; strictier control on dictionary API; proper cleanup sequence on rrddim; * more dictionary statistics * global statistics about dictionary operations, memory, items, callbacks * dictionary support for views - missing the public API * removed warning about unused parameter * chart and context name for cloud * chart and context name for cloud, again * dictionary statistics fixed; first implementation of dictionary views - not currently used * only the master can globally delete an item * context needs netdata prefix * fix context and chart it of spins * fix for host variables when health is not enabled * run garbage collector on item insert too * Fix info message; remove extra "using" * update dict unittest for new placement of garbage collector * we need RRDHOST->rrdvars for maintaining custom host variables * Health initialization needs the host->host_uuid * split STRING to its own files; no code changes other than that * initialize health unconditionally * unit tests do not pollute the global scope with their variables * Skip initialization when creating archived hosts on startup. When a child connects it will initialize properly Co-authored-by: Stelios Fragkakis <52996999+stelfrag@users.noreply.github.com> Co-authored-by: Vladimir Kobal <vlad@prokk.net>
747 lines
30 KiB
C
747 lines
30 KiB
C
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
#include "plugin_proc.h"
|
|
|
|
#define PLUGIN_PROC_MODULE_BTRFS_NAME "/sys/fs/btrfs"
|
|
|
|
typedef struct btrfs_disk {
|
|
char *name;
|
|
uint32_t hash;
|
|
int exists;
|
|
|
|
char *size_filename;
|
|
char *hw_sector_size_filename;
|
|
unsigned long long size;
|
|
unsigned long long hw_sector_size;
|
|
|
|
struct btrfs_disk *next;
|
|
} BTRFS_DISK;
|
|
|
|
typedef struct btrfs_node {
|
|
int exists;
|
|
int logged_error;
|
|
|
|
char *id;
|
|
uint32_t hash;
|
|
|
|
char *label;
|
|
|
|
// unsigned long long int sectorsize;
|
|
// unsigned long long int nodesize;
|
|
// unsigned long long int quota_override;
|
|
|
|
#define declare_btrfs_allocation_section_field(SECTION, FIELD) \
|
|
char *allocation_ ## SECTION ## _ ## FIELD ## _filename; \
|
|
unsigned long long int allocation_ ## SECTION ## _ ## FIELD;
|
|
|
|
#define declare_btrfs_allocation_field(FIELD) \
|
|
char *allocation_ ## FIELD ## _filename; \
|
|
unsigned long long int allocation_ ## FIELD;
|
|
|
|
RRDSET *st_allocation_disks;
|
|
RRDDIM *rd_allocation_disks_unallocated;
|
|
RRDDIM *rd_allocation_disks_data_used;
|
|
RRDDIM *rd_allocation_disks_data_free;
|
|
RRDDIM *rd_allocation_disks_metadata_used;
|
|
RRDDIM *rd_allocation_disks_metadata_free;
|
|
RRDDIM *rd_allocation_disks_system_used;
|
|
RRDDIM *rd_allocation_disks_system_free;
|
|
unsigned long long all_disks_total;
|
|
|
|
RRDSET *st_allocation_data;
|
|
RRDDIM *rd_allocation_data_free;
|
|
RRDDIM *rd_allocation_data_used;
|
|
declare_btrfs_allocation_section_field(data, total_bytes)
|
|
declare_btrfs_allocation_section_field(data, bytes_used)
|
|
declare_btrfs_allocation_section_field(data, disk_total)
|
|
declare_btrfs_allocation_section_field(data, disk_used)
|
|
|
|
RRDSET *st_allocation_metadata;
|
|
RRDDIM *rd_allocation_metadata_free;
|
|
RRDDIM *rd_allocation_metadata_used;
|
|
RRDDIM *rd_allocation_metadata_reserved;
|
|
declare_btrfs_allocation_section_field(metadata, total_bytes)
|
|
declare_btrfs_allocation_section_field(metadata, bytes_used)
|
|
declare_btrfs_allocation_section_field(metadata, disk_total)
|
|
declare_btrfs_allocation_section_field(metadata, disk_used)
|
|
//declare_btrfs_allocation_field(global_rsv_reserved)
|
|
declare_btrfs_allocation_field(global_rsv_size)
|
|
|
|
RRDSET *st_allocation_system;
|
|
RRDDIM *rd_allocation_system_free;
|
|
RRDDIM *rd_allocation_system_used;
|
|
declare_btrfs_allocation_section_field(system, total_bytes)
|
|
declare_btrfs_allocation_section_field(system, bytes_used)
|
|
declare_btrfs_allocation_section_field(system, disk_total)
|
|
declare_btrfs_allocation_section_field(system, disk_used)
|
|
|
|
BTRFS_DISK *disks;
|
|
|
|
struct btrfs_node *next;
|
|
} BTRFS_NODE;
|
|
|
|
static BTRFS_NODE *nodes = NULL;
|
|
|
|
static inline void btrfs_free_disk(BTRFS_DISK *d) {
|
|
freez(d->name);
|
|
freez(d->size_filename);
|
|
freez(d->hw_sector_size_filename);
|
|
freez(d);
|
|
}
|
|
|
|
static inline void btrfs_free_node(BTRFS_NODE *node) {
|
|
// info("BTRFS: destroying '%s'", node->id);
|
|
|
|
if(node->st_allocation_disks)
|
|
rrdset_is_obsolete(node->st_allocation_disks);
|
|
|
|
if(node->st_allocation_data)
|
|
rrdset_is_obsolete(node->st_allocation_data);
|
|
|
|
if(node->st_allocation_metadata)
|
|
rrdset_is_obsolete(node->st_allocation_metadata);
|
|
|
|
if(node->st_allocation_system)
|
|
rrdset_is_obsolete(node->st_allocation_system);
|
|
|
|
freez(node->allocation_data_bytes_used_filename);
|
|
freez(node->allocation_data_total_bytes_filename);
|
|
|
|
freez(node->allocation_metadata_bytes_used_filename);
|
|
freez(node->allocation_metadata_total_bytes_filename);
|
|
|
|
freez(node->allocation_system_bytes_used_filename);
|
|
freez(node->allocation_system_total_bytes_filename);
|
|
|
|
while(node->disks) {
|
|
BTRFS_DISK *d = node->disks;
|
|
node->disks = node->disks->next;
|
|
btrfs_free_disk(d);
|
|
}
|
|
|
|
freez(node->label);
|
|
freez(node->id);
|
|
freez(node);
|
|
}
|
|
|
|
static inline int find_btrfs_disks(BTRFS_NODE *node, const char *path) {
|
|
char filename[FILENAME_MAX + 1];
|
|
|
|
node->all_disks_total = 0;
|
|
|
|
BTRFS_DISK *d;
|
|
for(d = node->disks ; d ; d = d->next)
|
|
d->exists = 0;
|
|
|
|
DIR *dir = opendir(path);
|
|
if (!dir) {
|
|
if(!node->logged_error) {
|
|
error("BTRFS: Cannot open directory '%s'.", path);
|
|
node->logged_error = 1;
|
|
}
|
|
return 1;
|
|
}
|
|
node->logged_error = 0;
|
|
|
|
struct dirent *de = NULL;
|
|
while ((de = readdir(dir))) {
|
|
if (de->d_type != DT_LNK
|
|
|| !strcmp(de->d_name, ".")
|
|
|| !strcmp(de->d_name, "..")
|
|
) {
|
|
// info("BTRFS: ignoring '%s'", de->d_name);
|
|
continue;
|
|
}
|
|
|
|
uint32_t hash = simple_hash(de->d_name);
|
|
|
|
// --------------------------------------------------------------------
|
|
// search for it
|
|
|
|
for(d = node->disks ; d ; d = d->next) {
|
|
if(hash == d->hash && !strcmp(de->d_name, d->name))
|
|
break;
|
|
}
|
|
|
|
// --------------------------------------------------------------------
|
|
// did we find it?
|
|
|
|
if(!d) {
|
|
d = callocz(sizeof(BTRFS_DISK), 1);
|
|
|
|
d->name = strdupz(de->d_name);
|
|
d->hash = simple_hash(d->name);
|
|
|
|
snprintfz(filename, FILENAME_MAX, "%s/%s/size", path, de->d_name);
|
|
d->size_filename = strdupz(filename);
|
|
|
|
// for bcache
|
|
snprintfz(filename, FILENAME_MAX, "%s/%s/bcache/../queue/hw_sector_size", path, de->d_name);
|
|
struct stat sb;
|
|
if(stat(filename, &sb) == -1) {
|
|
// for disks
|
|
snprintfz(filename, FILENAME_MAX, "%s/%s/queue/hw_sector_size", path, de->d_name);
|
|
if(stat(filename, &sb) == -1)
|
|
// for partitions
|
|
snprintfz(filename, FILENAME_MAX, "%s/%s/../queue/hw_sector_size", path, de->d_name);
|
|
}
|
|
|
|
d->hw_sector_size_filename = strdupz(filename);
|
|
|
|
// link it
|
|
d->next = node->disks;
|
|
node->disks = d;
|
|
}
|
|
|
|
d->exists = 1;
|
|
|
|
|
|
// --------------------------------------------------------------------
|
|
// update the values
|
|
|
|
if(read_single_number_file(d->size_filename, &d->size) != 0) {
|
|
error("BTRFS: failed to read '%s'", d->size_filename);
|
|
d->exists = 0;
|
|
continue;
|
|
}
|
|
|
|
if(read_single_number_file(d->hw_sector_size_filename, &d->hw_sector_size) != 0) {
|
|
error("BTRFS: failed to read '%s'", d->hw_sector_size_filename);
|
|
d->exists = 0;
|
|
continue;
|
|
}
|
|
|
|
node->all_disks_total += d->size * d->hw_sector_size;
|
|
}
|
|
closedir(dir);
|
|
|
|
// ------------------------------------------------------------------------
|
|
// cleanup
|
|
|
|
BTRFS_DISK *last = NULL;
|
|
d = node->disks;
|
|
|
|
while(d) {
|
|
if(unlikely(!d->exists)) {
|
|
if(unlikely(node->disks == d)) {
|
|
node->disks = d->next;
|
|
btrfs_free_disk(d);
|
|
d = node->disks;
|
|
last = NULL;
|
|
}
|
|
else {
|
|
last->next = d->next;
|
|
btrfs_free_disk(d);
|
|
d = last->next;
|
|
}
|
|
|
|
continue;
|
|
}
|
|
|
|
last = d;
|
|
d = d->next;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
static inline int find_all_btrfs_pools(const char *path) {
|
|
static int logged_error = 0;
|
|
char filename[FILENAME_MAX + 1];
|
|
|
|
BTRFS_NODE *node;
|
|
for(node = nodes ; node ; node = node->next)
|
|
node->exists = 0;
|
|
|
|
DIR *dir = opendir(path);
|
|
if (!dir) {
|
|
if(!logged_error) {
|
|
error("BTRFS: Cannot open directory '%s'.", path);
|
|
logged_error = 1;
|
|
}
|
|
return 1;
|
|
}
|
|
logged_error = 0;
|
|
|
|
struct dirent *de = NULL;
|
|
while ((de = readdir(dir))) {
|
|
if(de->d_type != DT_DIR
|
|
|| !strcmp(de->d_name, ".")
|
|
|| !strcmp(de->d_name, "..")
|
|
|| !strcmp(de->d_name, "features")
|
|
) {
|
|
// info("BTRFS: ignoring '%s'", de->d_name);
|
|
continue;
|
|
}
|
|
|
|
uint32_t hash = simple_hash(de->d_name);
|
|
|
|
// search for it
|
|
for(node = nodes ; node ; node = node->next) {
|
|
if(hash == node->hash && !strcmp(de->d_name, node->id))
|
|
break;
|
|
}
|
|
|
|
// did we find it?
|
|
if(node) {
|
|
// info("BTRFS: already exists '%s'", de->d_name);
|
|
node->exists = 1;
|
|
|
|
// update the disk sizes
|
|
snprintfz(filename, FILENAME_MAX, "%s/%s/devices", path, de->d_name);
|
|
find_btrfs_disks(node, filename);
|
|
|
|
continue;
|
|
}
|
|
|
|
// info("BTRFS: adding '%s'", de->d_name);
|
|
|
|
// not found, create it
|
|
node = callocz(sizeof(BTRFS_NODE), 1);
|
|
|
|
node->id = strdupz(de->d_name);
|
|
node->hash = simple_hash(node->id);
|
|
node->exists = 1;
|
|
|
|
{
|
|
char label[FILENAME_MAX + 1] = "";
|
|
|
|
snprintfz(filename, FILENAME_MAX, "%s/%s/label", path, de->d_name);
|
|
if(read_file(filename, label, FILENAME_MAX) != 0) {
|
|
error("BTRFS: failed to read '%s'", filename);
|
|
btrfs_free_node(node);
|
|
continue;
|
|
}
|
|
|
|
char *s = label;
|
|
if (s[0])
|
|
s = trim(label);
|
|
|
|
if(s && s[0])
|
|
node->label = strdupz(s);
|
|
else
|
|
node->label = strdupz(node->id);
|
|
}
|
|
|
|
//snprintfz(filename, FILENAME_MAX, "%s/%s/sectorsize", path, de->d_name);
|
|
//if(read_single_number_file(filename, &node->sectorsize) != 0) {
|
|
// error("BTRFS: failed to read '%s'", filename);
|
|
// btrfs_free_node(node);
|
|
// continue;
|
|
//}
|
|
|
|
//snprintfz(filename, FILENAME_MAX, "%s/%s/nodesize", path, de->d_name);
|
|
//if(read_single_number_file(filename, &node->nodesize) != 0) {
|
|
// error("BTRFS: failed to read '%s'", filename);
|
|
// btrfs_free_node(node);
|
|
// continue;
|
|
//}
|
|
|
|
//snprintfz(filename, FILENAME_MAX, "%s/%s/quota_override", path, de->d_name);
|
|
//if(read_single_number_file(filename, &node->quota_override) != 0) {
|
|
// error("BTRFS: failed to read '%s'", filename);
|
|
// btrfs_free_node(node);
|
|
// continue;
|
|
//}
|
|
|
|
// --------------------------------------------------------------------
|
|
// macros to simplify our life
|
|
|
|
#define init_btrfs_allocation_field(FIELD) {\
|
|
snprintfz(filename, FILENAME_MAX, "%s/%s/allocation/" #FIELD, path, de->d_name); \
|
|
if(read_single_number_file(filename, &node->allocation_ ## FIELD) != 0) {\
|
|
error("BTRFS: failed to read '%s'", filename);\
|
|
btrfs_free_node(node);\
|
|
continue;\
|
|
}\
|
|
if(!node->allocation_ ## FIELD ## _filename)\
|
|
node->allocation_ ## FIELD ## _filename = strdupz(filename);\
|
|
}
|
|
|
|
#define init_btrfs_allocation_section_field(SECTION, FIELD) {\
|
|
snprintfz(filename, FILENAME_MAX, "%s/%s/allocation/" #SECTION "/" #FIELD, path, de->d_name); \
|
|
if(read_single_number_file(filename, &node->allocation_ ## SECTION ## _ ## FIELD) != 0) {\
|
|
error("BTRFS: failed to read '%s'", filename);\
|
|
btrfs_free_node(node);\
|
|
continue;\
|
|
}\
|
|
if(!node->allocation_ ## SECTION ## _ ## FIELD ## _filename)\
|
|
node->allocation_ ## SECTION ## _ ## FIELD ## _filename = strdupz(filename);\
|
|
}
|
|
|
|
// --------------------------------------------------------------------
|
|
// allocation/data
|
|
|
|
init_btrfs_allocation_section_field(data, total_bytes);
|
|
init_btrfs_allocation_section_field(data, bytes_used);
|
|
init_btrfs_allocation_section_field(data, disk_total);
|
|
init_btrfs_allocation_section_field(data, disk_used);
|
|
|
|
|
|
// --------------------------------------------------------------------
|
|
// allocation/metadata
|
|
|
|
init_btrfs_allocation_section_field(metadata, total_bytes);
|
|
init_btrfs_allocation_section_field(metadata, bytes_used);
|
|
init_btrfs_allocation_section_field(metadata, disk_total);
|
|
init_btrfs_allocation_section_field(metadata, disk_used);
|
|
|
|
init_btrfs_allocation_field(global_rsv_size);
|
|
// init_btrfs_allocation_field(global_rsv_reserved);
|
|
|
|
|
|
// --------------------------------------------------------------------
|
|
// allocation/system
|
|
|
|
init_btrfs_allocation_section_field(system, total_bytes);
|
|
init_btrfs_allocation_section_field(system, bytes_used);
|
|
init_btrfs_allocation_section_field(system, disk_total);
|
|
init_btrfs_allocation_section_field(system, disk_used);
|
|
|
|
|
|
// --------------------------------------------------------------------
|
|
// find all disks related to this node
|
|
// and collect their sizes
|
|
|
|
snprintfz(filename, FILENAME_MAX, "%s/%s/devices", path, de->d_name);
|
|
find_btrfs_disks(node, filename);
|
|
|
|
|
|
// --------------------------------------------------------------------
|
|
// link it
|
|
|
|
// info("BTRFS: linking '%s'", node->id);
|
|
node->next = nodes;
|
|
nodes = node;
|
|
}
|
|
closedir(dir);
|
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
// cleanup
|
|
|
|
BTRFS_NODE *last = NULL;
|
|
node = nodes;
|
|
|
|
while(node) {
|
|
if(unlikely(!node->exists)) {
|
|
if(unlikely(nodes == node)) {
|
|
nodes = node->next;
|
|
btrfs_free_node(node);
|
|
node = nodes;
|
|
last = NULL;
|
|
}
|
|
else {
|
|
last->next = node->next;
|
|
btrfs_free_node(node);
|
|
node = last->next;
|
|
}
|
|
|
|
continue;
|
|
}
|
|
|
|
last = node;
|
|
node = node->next;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static void add_labels_to_btrfs(BTRFS_NODE *n, RRDSET *st) {
|
|
rrdlabels_add(st->rrdlabels, "device", n->id, RRDLABEL_SRC_AUTO);
|
|
rrdlabels_add(st->rrdlabels, "device_label", n->label, RRDLABEL_SRC_AUTO);
|
|
}
|
|
|
|
int do_sys_fs_btrfs(int update_every, usec_t dt) {
|
|
static int initialized = 0
|
|
, do_allocation_disks = CONFIG_BOOLEAN_AUTO
|
|
, do_allocation_system = CONFIG_BOOLEAN_AUTO
|
|
, do_allocation_data = CONFIG_BOOLEAN_AUTO
|
|
, do_allocation_metadata = CONFIG_BOOLEAN_AUTO;
|
|
|
|
static usec_t refresh_delta = 0, refresh_every = 60 * USEC_PER_SEC;
|
|
static char *btrfs_path = NULL;
|
|
|
|
(void)dt;
|
|
|
|
if(unlikely(!initialized)) {
|
|
initialized = 1;
|
|
|
|
char filename[FILENAME_MAX + 1];
|
|
snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/sys/fs/btrfs");
|
|
btrfs_path = config_get("plugin:proc:/sys/fs/btrfs", "path to monitor", filename);
|
|
|
|
refresh_every = config_get_number("plugin:proc:/sys/fs/btrfs", "check for btrfs changes every", refresh_every / USEC_PER_SEC) * USEC_PER_SEC;
|
|
refresh_delta = refresh_every;
|
|
|
|
do_allocation_disks = config_get_boolean_ondemand("plugin:proc:/sys/fs/btrfs", "physical disks allocation", do_allocation_disks);
|
|
do_allocation_data = config_get_boolean_ondemand("plugin:proc:/sys/fs/btrfs", "data allocation", do_allocation_data);
|
|
do_allocation_metadata = config_get_boolean_ondemand("plugin:proc:/sys/fs/btrfs", "metadata allocation", do_allocation_metadata);
|
|
do_allocation_system = config_get_boolean_ondemand("plugin:proc:/sys/fs/btrfs", "system allocation", do_allocation_system);
|
|
}
|
|
|
|
refresh_delta += dt;
|
|
if(refresh_delta >= refresh_every) {
|
|
refresh_delta = 0;
|
|
find_all_btrfs_pools(btrfs_path);
|
|
}
|
|
|
|
BTRFS_NODE *node;
|
|
for(node = nodes; node ; node = node->next) {
|
|
// --------------------------------------------------------------------
|
|
// allocation/system
|
|
|
|
#define collect_btrfs_allocation_field(FIELD) \
|
|
read_single_number_file(node->allocation_ ## FIELD ## _filename, &node->allocation_ ## FIELD)
|
|
|
|
#define collect_btrfs_allocation_section_field(SECTION, FIELD) \
|
|
read_single_number_file(node->allocation_ ## SECTION ## _ ## FIELD ## _filename, &node->allocation_ ## SECTION ## _ ## FIELD)
|
|
|
|
if(do_allocation_disks != CONFIG_BOOLEAN_NO) {
|
|
if( collect_btrfs_allocation_section_field(data, disk_total) != 0
|
|
|| collect_btrfs_allocation_section_field(data, disk_used) != 0
|
|
|| collect_btrfs_allocation_section_field(metadata, disk_total) != 0
|
|
|| collect_btrfs_allocation_section_field(metadata, disk_used) != 0
|
|
|| collect_btrfs_allocation_section_field(system, disk_total) != 0
|
|
|| collect_btrfs_allocation_section_field(system, disk_used) != 0) {
|
|
error("BTRFS: failed to collect physical disks allocation for '%s'", node->id);
|
|
// make it refresh btrfs at the next iteration
|
|
refresh_delta = refresh_every;
|
|
continue;
|
|
}
|
|
}
|
|
|
|
if(do_allocation_data != CONFIG_BOOLEAN_NO) {
|
|
if (collect_btrfs_allocation_section_field(data, total_bytes) != 0
|
|
|| collect_btrfs_allocation_section_field(data, bytes_used) != 0) {
|
|
error("BTRFS: failed to collect allocation/data for '%s'", node->id);
|
|
// make it refresh btrfs at the next iteration
|
|
refresh_delta = refresh_every;
|
|
continue;
|
|
}
|
|
}
|
|
|
|
if(do_allocation_metadata != CONFIG_BOOLEAN_NO) {
|
|
if (collect_btrfs_allocation_section_field(metadata, total_bytes) != 0
|
|
|| collect_btrfs_allocation_section_field(metadata, bytes_used) != 0
|
|
|| collect_btrfs_allocation_field(global_rsv_size) != 0
|
|
) {
|
|
error("BTRFS: failed to collect allocation/metadata for '%s'", node->id);
|
|
// make it refresh btrfs at the next iteration
|
|
refresh_delta = refresh_every;
|
|
continue;
|
|
}
|
|
}
|
|
|
|
if(do_allocation_system != CONFIG_BOOLEAN_NO) {
|
|
if (collect_btrfs_allocation_section_field(system, total_bytes) != 0
|
|
|| collect_btrfs_allocation_section_field(system, bytes_used) != 0) {
|
|
error("BTRFS: failed to collect allocation/system for '%s'", node->id);
|
|
// make it refresh btrfs at the next iteration
|
|
refresh_delta = refresh_every;
|
|
continue;
|
|
}
|
|
}
|
|
|
|
// --------------------------------------------------------------------
|
|
// allocation/disks
|
|
|
|
if(do_allocation_disks == CONFIG_BOOLEAN_YES || (do_allocation_disks == CONFIG_BOOLEAN_AUTO &&
|
|
((node->all_disks_total && node->allocation_data_disk_total) ||
|
|
netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES))) {
|
|
do_allocation_disks = CONFIG_BOOLEAN_YES;
|
|
|
|
if(unlikely(!node->st_allocation_disks)) {
|
|
char id[RRD_ID_LENGTH_MAX + 1], name[RRD_ID_LENGTH_MAX + 1], title[200 + 1];
|
|
|
|
snprintf(id, RRD_ID_LENGTH_MAX, "disk_%s", node->id);
|
|
snprintf(name, RRD_ID_LENGTH_MAX, "disk_%s", node->label);
|
|
snprintf(title, 200, "BTRFS Physical Disk Allocation");
|
|
|
|
netdata_fix_chart_id(id);
|
|
netdata_fix_chart_name(name);
|
|
|
|
node->st_allocation_disks = rrdset_create_localhost(
|
|
"btrfs"
|
|
, id
|
|
, name
|
|
, node->label
|
|
, "btrfs.disk"
|
|
, title
|
|
, "MiB"
|
|
, PLUGIN_PROC_NAME
|
|
, PLUGIN_PROC_MODULE_BTRFS_NAME
|
|
, NETDATA_CHART_PRIO_BTRFS_DISK
|
|
, update_every
|
|
, RRDSET_TYPE_STACKED
|
|
);
|
|
|
|
node->rd_allocation_disks_unallocated = rrddim_add(node->st_allocation_disks, "unallocated", NULL, 1, 1024 * 1024, RRD_ALGORITHM_ABSOLUTE);
|
|
node->rd_allocation_disks_data_free = rrddim_add(node->st_allocation_disks, "data_free", "data free", 1, 1024 * 1024, RRD_ALGORITHM_ABSOLUTE);
|
|
node->rd_allocation_disks_data_used = rrddim_add(node->st_allocation_disks, "data_used", "data used", 1, 1024 * 1024, RRD_ALGORITHM_ABSOLUTE);
|
|
node->rd_allocation_disks_metadata_free = rrddim_add(node->st_allocation_disks, "meta_free", "meta free", 1, 1024 * 1024, RRD_ALGORITHM_ABSOLUTE);
|
|
node->rd_allocation_disks_metadata_used = rrddim_add(node->st_allocation_disks, "meta_used", "meta used", 1, 1024 * 1024, RRD_ALGORITHM_ABSOLUTE);
|
|
node->rd_allocation_disks_system_free = rrddim_add(node->st_allocation_disks, "sys_free", "sys free", 1, 1024 * 1024, RRD_ALGORITHM_ABSOLUTE);
|
|
node->rd_allocation_disks_system_used = rrddim_add(node->st_allocation_disks, "sys_used", "sys used", 1, 1024 * 1024, RRD_ALGORITHM_ABSOLUTE);
|
|
|
|
add_labels_to_btrfs(node, node->st_allocation_disks);
|
|
}
|
|
else rrdset_next(node->st_allocation_disks);
|
|
|
|
// unsigned long long disk_used = node->allocation_data_disk_used + node->allocation_metadata_disk_used + node->allocation_system_disk_used;
|
|
unsigned long long disk_total = node->allocation_data_disk_total + node->allocation_metadata_disk_total + node->allocation_system_disk_total;
|
|
unsigned long long disk_unallocated = node->all_disks_total - disk_total;
|
|
|
|
rrddim_set_by_pointer(node->st_allocation_disks, node->rd_allocation_disks_unallocated, disk_unallocated);
|
|
rrddim_set_by_pointer(node->st_allocation_disks, node->rd_allocation_disks_data_used, node->allocation_data_disk_used);
|
|
rrddim_set_by_pointer(node->st_allocation_disks, node->rd_allocation_disks_data_free, node->allocation_data_disk_total - node->allocation_data_disk_used);
|
|
rrddim_set_by_pointer(node->st_allocation_disks, node->rd_allocation_disks_metadata_used, node->allocation_metadata_disk_used);
|
|
rrddim_set_by_pointer(node->st_allocation_disks, node->rd_allocation_disks_metadata_free, node->allocation_metadata_disk_total - node->allocation_metadata_disk_used);
|
|
rrddim_set_by_pointer(node->st_allocation_disks, node->rd_allocation_disks_system_used, node->allocation_system_disk_used);
|
|
rrddim_set_by_pointer(node->st_allocation_disks, node->rd_allocation_disks_system_free, node->allocation_system_disk_total - node->allocation_system_disk_used);
|
|
rrdset_done(node->st_allocation_disks);
|
|
}
|
|
|
|
|
|
// --------------------------------------------------------------------
|
|
// allocation/data
|
|
|
|
if(do_allocation_data == CONFIG_BOOLEAN_YES || (do_allocation_data == CONFIG_BOOLEAN_AUTO &&
|
|
(node->allocation_data_total_bytes ||
|
|
netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES))) {
|
|
do_allocation_data = CONFIG_BOOLEAN_YES;
|
|
|
|
if(unlikely(!node->st_allocation_data)) {
|
|
char id[RRD_ID_LENGTH_MAX + 1], name[RRD_ID_LENGTH_MAX + 1], title[200 + 1];
|
|
|
|
snprintf(id, RRD_ID_LENGTH_MAX, "data_%s", node->id);
|
|
snprintf(name, RRD_ID_LENGTH_MAX, "data_%s", node->label);
|
|
snprintf(title, 200, "BTRFS Data Allocation");
|
|
|
|
netdata_fix_chart_id(id);
|
|
netdata_fix_chart_name(name);
|
|
|
|
node->st_allocation_data = rrdset_create_localhost(
|
|
"btrfs"
|
|
, id
|
|
, name
|
|
, node->label
|
|
, "btrfs.data"
|
|
, title
|
|
, "MiB"
|
|
, PLUGIN_PROC_NAME
|
|
, PLUGIN_PROC_MODULE_BTRFS_NAME
|
|
, NETDATA_CHART_PRIO_BTRFS_DATA
|
|
, update_every
|
|
, RRDSET_TYPE_STACKED
|
|
);
|
|
|
|
node->rd_allocation_data_free = rrddim_add(node->st_allocation_data, "free", NULL, 1, 1024 * 1024, RRD_ALGORITHM_ABSOLUTE);
|
|
node->rd_allocation_data_used = rrddim_add(node->st_allocation_data, "used", NULL, 1, 1024 * 1024, RRD_ALGORITHM_ABSOLUTE);
|
|
|
|
add_labels_to_btrfs(node, node->st_allocation_data);
|
|
}
|
|
else rrdset_next(node->st_allocation_data);
|
|
|
|
rrddim_set_by_pointer(node->st_allocation_data, node->rd_allocation_data_free, node->allocation_data_total_bytes - node->allocation_data_bytes_used);
|
|
rrddim_set_by_pointer(node->st_allocation_data, node->rd_allocation_data_used, node->allocation_data_bytes_used);
|
|
rrdset_done(node->st_allocation_data);
|
|
}
|
|
|
|
// --------------------------------------------------------------------
|
|
// allocation/metadata
|
|
|
|
if(do_allocation_metadata == CONFIG_BOOLEAN_YES || (do_allocation_metadata == CONFIG_BOOLEAN_AUTO &&
|
|
(node->allocation_metadata_total_bytes ||
|
|
netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES))) {
|
|
do_allocation_metadata = CONFIG_BOOLEAN_YES;
|
|
|
|
if(unlikely(!node->st_allocation_metadata)) {
|
|
char id[RRD_ID_LENGTH_MAX + 1], name[RRD_ID_LENGTH_MAX + 1], title[200 + 1];
|
|
|
|
snprintf(id, RRD_ID_LENGTH_MAX, "metadata_%s", node->id);
|
|
snprintf(name, RRD_ID_LENGTH_MAX, "metadata_%s", node->label);
|
|
snprintf(title, 200, "BTRFS Metadata Allocation");
|
|
|
|
netdata_fix_chart_id(id);
|
|
netdata_fix_chart_name(name);
|
|
|
|
node->st_allocation_metadata = rrdset_create_localhost(
|
|
"btrfs"
|
|
, id
|
|
, name
|
|
, node->label
|
|
, "btrfs.metadata"
|
|
, title
|
|
, "MiB"
|
|
, PLUGIN_PROC_NAME
|
|
, PLUGIN_PROC_MODULE_BTRFS_NAME
|
|
, NETDATA_CHART_PRIO_BTRFS_METADATA
|
|
, update_every
|
|
, RRDSET_TYPE_STACKED
|
|
);
|
|
|
|
node->rd_allocation_metadata_free = rrddim_add(node->st_allocation_metadata, "free", NULL, 1, 1024 * 1024, RRD_ALGORITHM_ABSOLUTE);
|
|
node->rd_allocation_metadata_used = rrddim_add(node->st_allocation_metadata, "used", NULL, 1, 1024 * 1024, RRD_ALGORITHM_ABSOLUTE);
|
|
node->rd_allocation_metadata_reserved = rrddim_add(node->st_allocation_metadata, "reserved", NULL, 1, 1024 * 1024, RRD_ALGORITHM_ABSOLUTE);
|
|
|
|
add_labels_to_btrfs(node, node->st_allocation_metadata);
|
|
}
|
|
else rrdset_next(node->st_allocation_metadata);
|
|
|
|
rrddim_set_by_pointer(node->st_allocation_metadata, node->rd_allocation_metadata_free, node->allocation_metadata_total_bytes - node->allocation_metadata_bytes_used - node->allocation_global_rsv_size);
|
|
rrddim_set_by_pointer(node->st_allocation_metadata, node->rd_allocation_metadata_used, node->allocation_metadata_bytes_used);
|
|
rrddim_set_by_pointer(node->st_allocation_metadata, node->rd_allocation_metadata_reserved, node->allocation_global_rsv_size);
|
|
rrdset_done(node->st_allocation_metadata);
|
|
}
|
|
|
|
// --------------------------------------------------------------------
|
|
// allocation/system
|
|
|
|
if(do_allocation_system == CONFIG_BOOLEAN_YES || (do_allocation_system == CONFIG_BOOLEAN_AUTO &&
|
|
(node->allocation_system_total_bytes ||
|
|
netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES))) {
|
|
do_allocation_system = CONFIG_BOOLEAN_YES;
|
|
|
|
if(unlikely(!node->st_allocation_system)) {
|
|
char id[RRD_ID_LENGTH_MAX + 1], name[RRD_ID_LENGTH_MAX + 1], title[200 + 1];
|
|
|
|
snprintf(id, RRD_ID_LENGTH_MAX, "system_%s", node->id);
|
|
snprintf(name, RRD_ID_LENGTH_MAX, "system_%s", node->label);
|
|
snprintf(title, 200, "BTRFS System Allocation");
|
|
|
|
netdata_fix_chart_id(id);
|
|
netdata_fix_chart_name(name);
|
|
|
|
node->st_allocation_system = rrdset_create_localhost(
|
|
"btrfs"
|
|
, id
|
|
, name
|
|
, node->label
|
|
, "btrfs.system"
|
|
, title
|
|
, "MiB"
|
|
, PLUGIN_PROC_NAME
|
|
, PLUGIN_PROC_MODULE_BTRFS_NAME
|
|
, NETDATA_CHART_PRIO_BTRFS_SYSTEM
|
|
, update_every
|
|
, RRDSET_TYPE_STACKED
|
|
);
|
|
|
|
node->rd_allocation_system_free = rrddim_add(node->st_allocation_system, "free", NULL, 1, 1024 * 1024, RRD_ALGORITHM_ABSOLUTE);
|
|
node->rd_allocation_system_used = rrddim_add(node->st_allocation_system, "used", NULL, 1, 1024 * 1024, RRD_ALGORITHM_ABSOLUTE);
|
|
|
|
add_labels_to_btrfs(node, node->st_allocation_system);
|
|
}
|
|
else rrdset_next(node->st_allocation_system);
|
|
|
|
rrddim_set_by_pointer(node->st_allocation_system, node->rd_allocation_system_free, node->allocation_system_total_bytes - node->allocation_system_bytes_used);
|
|
rrddim_set_by_pointer(node->st_allocation_system, node->rd_allocation_system_used, node->allocation_system_bytes_used);
|
|
rrdset_done(node->st_allocation_system);
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|