3 #define DELAULT_EXLUDED_PATHS "/proc/* /sys/* /var/run/user/* /run/user/*"
5 static struct mountinfo *disk_mountinfo_root = NULL;
6 static int check_for_new_mountpoints_every = 15;
8 static inline void mountinfo_reload(int force) {
9 static time_t last_loaded = 0;
10 time_t now = now_realtime_sec();
12 if(force || now - last_loaded >= check_for_new_mountpoints_every) {
13 // mountinfo_free() can be called with NULL disk_mountinfo_root
14 mountinfo_free(disk_mountinfo_root);
16 // re-read mountinfo in case something changed
17 disk_mountinfo_root = mountinfo_read(1);
23 // Data to be stored in DICTIONARY mount_points used by do_disk_space_stats().
24 // This DICTIONARY is used to lookup the settings of the mount point on each iteration.
25 struct mount_point_metadata {
29 size_t collected; // the number of times this has been collected
32 RRDDIM *rd_space_used;
33 RRDDIM *rd_space_avail;
34 RRDDIM *rd_space_reserved;
37 RRDDIM *rd_inodes_used;
38 RRDDIM *rd_inodes_avail;
39 RRDDIM *rd_inodes_reserved;
42 static inline void do_disk_space_stats(struct mountinfo *mi, int update_every) {
43 const char *family = mi->mount_point;
44 const char *disk = mi->persistent_id;
46 static DICTIONARY *mount_points = NULL;
47 static SIMPLE_PATTERN *excluded_mountpoints = NULL;
48 int do_space, do_inodes;
50 if(unlikely(!mount_points)) {
52 SIMPLE_PREFIX_MODE mode = SIMPLE_PATTERN_EXACT;
54 if(config_exists("plugin:proc:/proc/diskstats", "exclude space metrics on paths") && !config_exists("plugin:proc:diskspace", "exclude space metrics on paths")) {
55 // the config exists in the old section
56 s = config_get("plugin:proc:/proc/diskstats", "exclude space metrics on paths", DELAULT_EXLUDED_PATHS);
57 mode = SIMPLE_PATTERN_PREFIX;
60 s = config_get("plugin:proc:diskspace", "exclude space metrics on paths", DELAULT_EXLUDED_PATHS);
62 mount_points = dictionary_create(DICTIONARY_FLAG_SINGLE_THREADED);
63 excluded_mountpoints = simple_pattern_create(s, mode);
66 struct mount_point_metadata *m = dictionary_get(mount_points, mi->mount_point);
68 char var_name[4096 + 1];
69 snprintfz(var_name, 4096, "plugin:proc:diskspace:%s", mi->mount_point);
71 int def_space = config_get_boolean_ondemand("plugin:proc:diskspace", "space usage for all disks", CONFIG_ONDEMAND_ONDEMAND);
72 int def_inodes = config_get_boolean_ondemand("plugin:proc:diskspace", "inodes usage for all disks", CONFIG_ONDEMAND_ONDEMAND);
74 if(unlikely(simple_pattern_matches(excluded_mountpoints, mi->mount_point))) {
75 def_space = CONFIG_ONDEMAND_NO;
76 def_inodes = CONFIG_ONDEMAND_NO;
79 do_space = config_get_boolean_ondemand(var_name, "space usage", def_space);
80 do_inodes = config_get_boolean_ondemand(var_name, "inodes usage", def_inodes);
82 struct mount_point_metadata mp = {
84 .do_inodes = do_inodes,
89 .rd_space_avail = NULL,
90 .rd_space_used = NULL,
91 .rd_space_reserved = NULL,
94 .rd_inodes_avail = NULL,
95 .rd_inodes_used = NULL,
96 .rd_inodes_reserved = NULL
99 m = dictionary_set(mount_points, mi->mount_point, &mp, sizeof(struct mount_point_metadata));
102 do_space = m->do_space;
103 do_inodes = m->do_inodes;
106 if(unlikely(do_space == CONFIG_ONDEMAND_NO && do_inodes == CONFIG_ONDEMAND_NO))
109 if(unlikely(mi->flags & MOUNTINFO_READONLY && !m->collected))
112 struct statvfs buff_statvfs;
113 if (statvfs(mi->mount_point, &buff_statvfs) < 0) {
114 error("Failed statvfs() for '%s' (disk '%s')", mi->mount_point, disk);
118 // logic found at get_fs_usage() in coreutils
119 unsigned long bsize = (buff_statvfs.f_frsize) ? buff_statvfs.f_frsize : buff_statvfs.f_bsize;
121 fsblkcnt_t bavail = buff_statvfs.f_bavail;
122 fsblkcnt_t btotal = buff_statvfs.f_blocks;
123 fsblkcnt_t bavail_root = buff_statvfs.f_bfree;
124 fsblkcnt_t breserved_root = bavail_root - bavail;
126 if(likely(btotal >= bavail_root))
127 bused = btotal - bavail_root;
129 bused = bavail_root - btotal;
131 #ifdef NETDATA_INTERNAL_CHECKS
132 if(unlikely(btotal != bavail + breserved_root + bused))
133 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);
136 // --------------------------------------------------------------------------
138 fsfilcnt_t favail = buff_statvfs.f_favail;
139 fsfilcnt_t ftotal = buff_statvfs.f_files;
140 fsfilcnt_t favail_root = buff_statvfs.f_ffree;
141 fsfilcnt_t freserved_root = favail_root - favail;
142 fsfilcnt_t fused = ftotal - favail_root;
144 #ifdef NETDATA_INTERNAL_CHECKS
145 if(unlikely(btotal != bavail + breserved_root + bused))
146 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);
149 // --------------------------------------------------------------------------
153 if(do_space == CONFIG_ONDEMAND_YES || (do_space == CONFIG_ONDEMAND_ONDEMAND && (bavail || breserved_root || bused))) {
154 if(unlikely(!m->st_space)) {
155 m->do_space = CONFIG_ONDEMAND_YES;
156 m->st_space = rrdset_find_bytype("disk_space", disk);
157 if(unlikely(!m->st_space)) {
158 char title[4096 + 1];
159 snprintfz(title, 4096, "Disk Space Usage for %s [%s]", family, mi->mount_source);
160 m->st_space = rrdset_create("disk_space", disk, NULL, family, "disk.space", title, "GB", 2023, update_every, RRDSET_TYPE_STACKED);
163 m->rd_space_avail = rrddim_add(m->st_space, "avail", NULL, bsize, 1024 * 1024 * 1024, RRDDIM_ABSOLUTE);
164 m->rd_space_used = rrddim_add(m->st_space, "used", NULL, bsize, 1024 * 1024 * 1024, RRDDIM_ABSOLUTE);
165 m->rd_space_reserved = rrddim_add(m->st_space, "reserved_for_root", "reserved for root", bsize, 1024 * 1024 * 1024, RRDDIM_ABSOLUTE);
168 rrdset_next(m->st_space);
170 rrddim_set_by_pointer(m->st_space, m->rd_space_avail, (collected_number)bavail);
171 rrddim_set_by_pointer(m->st_space, m->rd_space_used, (collected_number)bused);
172 rrddim_set_by_pointer(m->st_space, m->rd_space_reserved, (collected_number)breserved_root);
173 rrdset_done(m->st_space);
178 // --------------------------------------------------------------------------
180 if(do_inodes == CONFIG_ONDEMAND_YES || (do_inodes == CONFIG_ONDEMAND_ONDEMAND && (favail || freserved_root || fused))) {
181 if(unlikely(!m->st_inodes)) {
182 m->do_inodes = CONFIG_ONDEMAND_YES;
183 m->st_inodes = rrdset_find_bytype("disk_inodes", disk);
184 if(unlikely(!m->st_inodes)) {
185 char title[4096 + 1];
186 snprintfz(title, 4096, "Disk Files (inodes) Usage for %s [%s]", family, mi->mount_source);
187 m->st_inodes = rrdset_create("disk_inodes", disk, NULL, family, "disk.inodes", title, "Inodes", 2024, update_every, RRDSET_TYPE_STACKED);
190 m->rd_inodes_avail = rrddim_add(m->st_inodes, "avail", NULL, 1, 1, RRDDIM_ABSOLUTE);
191 m->rd_inodes_used = rrddim_add(m->st_inodes, "used", NULL, 1, 1, RRDDIM_ABSOLUTE);
192 m->rd_inodes_reserved = rrddim_add(m->st_inodes, "reserved_for_root", "reserved for root", 1, 1, RRDDIM_ABSOLUTE);
195 rrdset_next(m->st_inodes);
197 rrddim_set_by_pointer(m->st_inodes, m->rd_inodes_avail, (collected_number)favail);
198 rrddim_set_by_pointer(m->st_inodes, m->rd_inodes_used, (collected_number)fused);
199 rrddim_set_by_pointer(m->st_inodes, m->rd_inodes_reserved, (collected_number)freserved_root);
200 rrdset_done(m->st_inodes);
205 // --------------------------------------------------------------------------
211 void *proc_diskspace_main(void *ptr) {
212 struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
214 info("DISKSPACE thread created with task id %d", gettid());
216 if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
217 error("Cannot set pthread cancel type to DEFERRED.");
219 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
220 error("Cannot set pthread cancel state to ENABLE.");
222 int vdo_cpu_netdata = config_get_boolean("plugin:proc", "netdata server resources", 1);
224 int update_every = (int)config_get_number("plugin:proc:diskspace", "update every", rrd_update_every);
225 if(update_every < rrd_update_every)
226 update_every = rrd_update_every;
228 check_for_new_mountpoints_every = (int)config_get_number("plugin:proc:diskspace", "check for new mount points every", check_for_new_mountpoints_every);
229 if(check_for_new_mountpoints_every < update_every)
230 check_for_new_mountpoints_every = update_every;
232 struct rusage thread;
234 usec_t last = 0, dt = 0;
235 usec_t step = update_every * USEC_PER_SEC;
237 usec_t now = now_monotonic_usec();
238 usec_t next = now - (now % step) + step;
240 dt = (last)?now - last:0;
243 sleep_usec(next - now);
244 now = now_monotonic_usec();
249 if(unlikely(netdata_exit)) break;
252 // --------------------------------------------------------------------------
253 // this is smart enough not to reload it every time
258 // --------------------------------------------------------------------------
259 // disk space metrics
261 struct mountinfo *mi;
262 for(mi = disk_mountinfo_root; mi; mi = mi->next) {
264 if(unlikely(mi->flags & (MOUNTINFO_IS_DUMMY | MOUNTINFO_IS_BIND | MOUNTINFO_IS_SAME_DEV | MOUNTINFO_NO_STAT | MOUNTINFO_NO_SIZE)))
267 do_disk_space_stats(mi, update_every);
268 if(unlikely(netdata_exit)) break;
271 if(unlikely(netdata_exit)) break;
273 if(vdo_cpu_netdata) {
274 static RRDSET *stcpu_thread = NULL, *st_duration = NULL;
275 static RRDDIM *rd_user = NULL, *rd_system = NULL, *rd_duration = NULL;
277 // ----------------------------------------------------------------
279 getrusage(RUSAGE_THREAD, &thread);
282 stcpu_thread = rrdset_find("netdata.plugin_diskspace");
283 if(!stcpu_thread) stcpu_thread = rrdset_create("netdata", "plugin_diskspace", NULL, "diskspace", NULL
284 , "NetData Disk Space Plugin CPU usage", "milliseconds/s", 132020
285 , update_every, RRDSET_TYPE_STACKED);
287 rd_user = rrddim_add(stcpu_thread, "user", NULL, 1, 1000, RRDDIM_INCREMENTAL);
288 rd_system = rrddim_add(stcpu_thread, "system", NULL, 1, 1000, RRDDIM_INCREMENTAL);
291 rrdset_next(stcpu_thread);
293 rrddim_set_by_pointer(stcpu_thread, rd_user, thread.ru_utime.tv_sec * 1000000ULL + thread.ru_utime.tv_usec);
294 rrddim_set_by_pointer(stcpu_thread, rd_system, thread.ru_stime.tv_sec * 1000000ULL + thread.ru_stime.tv_usec);
295 rrdset_done(stcpu_thread);
297 // ----------------------------------------------------------------
300 st_duration = rrdset_find("netdata.plugin_diskspace_dt");
301 if(!st_duration) st_duration = rrdset_create("netdata", "plugin_diskspace_dt", NULL, "diskspace", NULL
302 , "NetData Disk Space Plugin Duration", "milliseconds/run", 132021
303 , update_every, RRDSET_TYPE_AREA);
305 rd_duration = rrddim_add(st_duration, "duration", NULL, 1, 1000, RRDDIM_ABSOLUTE);
308 rrdset_next(st_duration);
310 rrddim_set_by_pointer(st_duration, rd_duration, dt);
311 rrdset_done(st_duration);
313 // ----------------------------------------------------------------
315 if(unlikely(netdata_exit)) break;
319 info("DISKSPACE thread exiting");
321 static_thread->enabled = 0;