3 #define DELAULT_EXLUDED_PATHS "/proc/* /sys/* /var/run/user/* /run/user/*"
4 #define DEFAULT_EXCLUDED_FILESYSTEMS ""
5 #define CONFIG_SECTION_DISKSPACE "plugin:proc:diskspace"
7 static struct mountinfo *disk_mountinfo_root = NULL;
8 static int check_for_new_mountpoints_every = 15;
10 static inline void mountinfo_reload(int force) {
11 static time_t last_loaded = 0;
12 time_t now = now_realtime_sec();
14 if(force || now - last_loaded >= check_for_new_mountpoints_every) {
15 // mountinfo_free() can be called with NULL disk_mountinfo_root
16 mountinfo_free(disk_mountinfo_root);
18 // re-read mountinfo in case something changed
19 disk_mountinfo_root = mountinfo_read(0);
25 // Data to be stored in DICTIONARY mount_points used by do_disk_space_stats().
26 // This DICTIONARY is used to lookup the settings of the mount point on each iteration.
27 struct mount_point_metadata {
31 size_t collected; // the number of times this has been collected
34 RRDDIM *rd_space_used;
35 RRDDIM *rd_space_avail;
36 RRDDIM *rd_space_reserved;
39 RRDDIM *rd_inodes_used;
40 RRDDIM *rd_inodes_avail;
41 RRDDIM *rd_inodes_reserved;
44 static inline void do_disk_space_stats(struct mountinfo *mi, int update_every) {
45 const char *family = mi->mount_point;
46 const char *disk = mi->persistent_id;
48 static DICTIONARY *mount_points = NULL;
49 static SIMPLE_PATTERN *excluded_mountpoints = NULL;
50 static SIMPLE_PATTERN *excluded_filesystems = NULL;
51 int do_space, do_inodes;
53 if(unlikely(!mount_points)) {
54 SIMPLE_PREFIX_MODE mode = SIMPLE_PATTERN_EXACT;
56 if(config_move("plugin:proc:/proc/diskstats", "exclude space metrics on paths", CONFIG_SECTION_DISKSPACE, "exclude space metrics on paths") != -1) {
57 // old configuration, enable backwards compatibility
58 mode = SIMPLE_PATTERN_PREFIX;
61 excluded_mountpoints = simple_pattern_create(
62 config_get(CONFIG_SECTION_DISKSPACE, "exclude space metrics on paths", DELAULT_EXLUDED_PATHS),
66 excluded_filesystems = simple_pattern_create(
67 config_get(CONFIG_SECTION_DISKSPACE, "exclude space metrics on filesystems", DEFAULT_EXCLUDED_FILESYSTEMS),
71 mount_points = dictionary_create(DICTIONARY_FLAG_SINGLE_THREADED);
74 struct mount_point_metadata *m = dictionary_get(mount_points, mi->mount_point);
76 char var_name[4096 + 1];
77 snprintfz(var_name, 4096, "plugin:proc:diskspace:%s", mi->mount_point);
79 int def_space = config_get_boolean_ondemand(CONFIG_SECTION_DISKSPACE, "space usage for all disks", CONFIG_BOOLEAN_AUTO);
80 int def_inodes = config_get_boolean_ondemand(CONFIG_SECTION_DISKSPACE, "inodes usage for all disks", CONFIG_BOOLEAN_AUTO);
82 if(unlikely(simple_pattern_matches(excluded_mountpoints, mi->mount_point))) {
83 def_space = CONFIG_BOOLEAN_NO;
84 def_inodes = CONFIG_BOOLEAN_NO;
87 if(unlikely(simple_pattern_matches(excluded_filesystems, mi->filesystem))) {
88 def_space = CONFIG_BOOLEAN_NO;
89 def_inodes = CONFIG_BOOLEAN_NO;
92 do_space = config_get_boolean_ondemand(var_name, "space usage", def_space);
93 do_inodes = config_get_boolean_ondemand(var_name, "inodes usage", def_inodes);
95 struct mount_point_metadata mp = {
97 .do_inodes = do_inodes,
102 .rd_space_avail = NULL,
103 .rd_space_used = NULL,
104 .rd_space_reserved = NULL,
107 .rd_inodes_avail = NULL,
108 .rd_inodes_used = NULL,
109 .rd_inodes_reserved = NULL
112 m = dictionary_set(mount_points, mi->mount_point, &mp, sizeof(struct mount_point_metadata));
115 do_space = m->do_space;
116 do_inodes = m->do_inodes;
119 if(unlikely(do_space == CONFIG_BOOLEAN_NO && do_inodes == CONFIG_BOOLEAN_NO))
122 if(unlikely(mi->flags & MOUNTINFO_READONLY && !m->collected))
125 struct statvfs buff_statvfs;
126 if (statvfs(mi->mount_point, &buff_statvfs) < 0) {
127 error("Failed statvfs() for '%s' (disk '%s')", mi->mount_point, disk);
131 // logic found at get_fs_usage() in coreutils
132 unsigned long bsize = (buff_statvfs.f_frsize) ? buff_statvfs.f_frsize : buff_statvfs.f_bsize;
134 fsblkcnt_t bavail = buff_statvfs.f_bavail;
135 fsblkcnt_t btotal = buff_statvfs.f_blocks;
136 fsblkcnt_t bavail_root = buff_statvfs.f_bfree;
137 fsblkcnt_t breserved_root = bavail_root - bavail;
139 if(likely(btotal >= bavail_root))
140 bused = btotal - bavail_root;
142 bused = bavail_root - btotal;
144 #ifdef NETDATA_INTERNAL_CHECKS
145 if(unlikely(btotal != bavail + breserved_root + bused))
146 error("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);
149 // --------------------------------------------------------------------------
151 fsfilcnt_t favail = buff_statvfs.f_favail;
152 fsfilcnt_t ftotal = buff_statvfs.f_files;
153 fsfilcnt_t favail_root = buff_statvfs.f_ffree;
154 fsfilcnt_t freserved_root = favail_root - favail;
155 fsfilcnt_t fused = ftotal - favail_root;
157 #ifdef NETDATA_INTERNAL_CHECKS
158 if(unlikely(btotal != bavail + breserved_root + bused))
159 error("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);
162 // --------------------------------------------------------------------------
166 if(do_space == CONFIG_BOOLEAN_YES || (do_space == CONFIG_BOOLEAN_AUTO && (bavail || breserved_root || bused))) {
167 if(unlikely(!m->st_space)) {
168 m->do_space = CONFIG_BOOLEAN_YES;
169 m->st_space = rrdset_find_bytype_localhost("disk_space", disk);
170 if(unlikely(!m->st_space)) {
171 char title[4096 + 1];
172 snprintfz(title, 4096, "Disk Space Usage for %s [%s]", family, mi->mount_source);
173 m->st_space = rrdset_create_localhost(
183 , RRDSET_TYPE_STACKED
187 m->rd_space_avail = rrddim_add(m->st_space, "avail", NULL, (collected_number)bsize, 1024 * 1024 * 1024, RRD_ALGORITHM_ABSOLUTE);
188 m->rd_space_used = rrddim_add(m->st_space, "used", NULL, (collected_number)bsize, 1024 * 1024 * 1024, RRD_ALGORITHM_ABSOLUTE);
189 m->rd_space_reserved = rrddim_add(m->st_space, "reserved_for_root", "reserved for root", (collected_number)bsize, 1024 * 1024 * 1024, RRD_ALGORITHM_ABSOLUTE);
192 rrdset_next(m->st_space);
194 rrddim_set_by_pointer(m->st_space, m->rd_space_avail, (collected_number)bavail);
195 rrddim_set_by_pointer(m->st_space, m->rd_space_used, (collected_number)bused);
196 rrddim_set_by_pointer(m->st_space, m->rd_space_reserved, (collected_number)breserved_root);
197 rrdset_done(m->st_space);
202 // --------------------------------------------------------------------------
204 if(do_inodes == CONFIG_BOOLEAN_YES || (do_inodes == CONFIG_BOOLEAN_AUTO && (favail || freserved_root || fused))) {
205 if(unlikely(!m->st_inodes)) {
206 m->do_inodes = CONFIG_BOOLEAN_YES;
207 m->st_inodes = rrdset_find_bytype_localhost("disk_inodes", disk);
208 if(unlikely(!m->st_inodes)) {
209 char title[4096 + 1];
210 snprintfz(title, 4096, "Disk Files (inodes) Usage for %s [%s]", family, mi->mount_source);
211 m->st_inodes = rrdset_create_localhost(
221 , RRDSET_TYPE_STACKED
225 m->rd_inodes_avail = rrddim_add(m->st_inodes, "avail", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
226 m->rd_inodes_used = rrddim_add(m->st_inodes, "used", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
227 m->rd_inodes_reserved = rrddim_add(m->st_inodes, "reserved_for_root", "reserved for root", 1, 1, RRD_ALGORITHM_ABSOLUTE);
230 rrdset_next(m->st_inodes);
232 rrddim_set_by_pointer(m->st_inodes, m->rd_inodes_avail, (collected_number)favail);
233 rrddim_set_by_pointer(m->st_inodes, m->rd_inodes_used, (collected_number)fused);
234 rrddim_set_by_pointer(m->st_inodes, m->rd_inodes_reserved, (collected_number)freserved_root);
235 rrdset_done(m->st_inodes);
240 // --------------------------------------------------------------------------
246 void *proc_diskspace_main(void *ptr) {
247 struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
249 info("DISKSPACE thread created with task id %d", gettid());
251 if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
252 error("Cannot set pthread cancel type to DEFERRED.");
254 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
255 error("Cannot set pthread cancel state to ENABLE.");
257 int vdo_cpu_netdata = config_get_boolean("plugin:proc", "netdata server resources", 1);
259 int update_every = (int)config_get_number(CONFIG_SECTION_DISKSPACE, "update every", localhost->rrd_update_every);
260 if(update_every < localhost->rrd_update_every)
261 update_every = localhost->rrd_update_every;
263 check_for_new_mountpoints_every = (int)config_get_number(CONFIG_SECTION_DISKSPACE, "check for new mount points every", check_for_new_mountpoints_every);
264 if(check_for_new_mountpoints_every < update_every)
265 check_for_new_mountpoints_every = update_every;
267 struct rusage thread;
270 usec_t step = update_every * USEC_PER_SEC;
274 duration = heartbeat_dt_usec(&hb);
275 /* usec_t hb_dt = */ heartbeat_next(&hb, step);
277 if(unlikely(netdata_exit)) break;
280 // --------------------------------------------------------------------------
281 // this is smart enough not to reload it every time
286 // --------------------------------------------------------------------------
287 // disk space metrics
289 struct mountinfo *mi;
290 for(mi = disk_mountinfo_root; mi; mi = mi->next) {
292 if(unlikely(mi->flags & (MOUNTINFO_IS_DUMMY | MOUNTINFO_IS_BIND | MOUNTINFO_IS_SAME_DEV | MOUNTINFO_NO_STAT | MOUNTINFO_NO_SIZE)))
295 do_disk_space_stats(mi, update_every);
296 if(unlikely(netdata_exit)) break;
299 if(unlikely(netdata_exit)) break;
301 if(vdo_cpu_netdata) {
302 static RRDSET *stcpu_thread = NULL, *st_duration = NULL;
303 static RRDDIM *rd_user = NULL, *rd_system = NULL, *rd_duration = NULL;
305 // ----------------------------------------------------------------
307 getrusage(RUSAGE_THREAD, &thread);
310 stcpu_thread = rrdset_find_localhost("netdata.plugin_diskspace");
312 stcpu_thread = rrdset_create_localhost(
318 , "NetData Disk Space Plugin CPU usage"
322 , RRDSET_TYPE_STACKED
325 rd_user = rrddim_add(stcpu_thread, "user", NULL, 1, 1000, RRD_ALGORITHM_INCREMENTAL);
326 rd_system = rrddim_add(stcpu_thread, "system", NULL, 1, 1000, RRD_ALGORITHM_INCREMENTAL);
329 rrdset_next(stcpu_thread);
331 rrddim_set_by_pointer(stcpu_thread, rd_user, thread.ru_utime.tv_sec * 1000000ULL + thread.ru_utime.tv_usec);
332 rrddim_set_by_pointer(stcpu_thread, rd_system, thread.ru_stime.tv_sec * 1000000ULL + thread.ru_stime.tv_usec);
333 rrdset_done(stcpu_thread);
335 // ----------------------------------------------------------------
338 st_duration = rrdset_find_localhost("netdata.plugin_diskspace_dt");
340 st_duration = rrdset_create_localhost(
342 , "plugin_diskspace_dt"
346 , "NetData Disk Space Plugin Duration"
353 rd_duration = rrddim_add(st_duration, "duration", NULL, 1, 1000, RRD_ALGORITHM_ABSOLUTE);
356 rrdset_next(st_duration);
358 rrddim_set_by_pointer(st_duration, rd_duration, duration);
359 rrdset_done(st_duration);
361 // ----------------------------------------------------------------
363 if(unlikely(netdata_exit)) break;
367 info("DISKSPACE thread exiting");
369 static_thread->enabled = 0;