]> arthur.barton.de Git - netdata.git/blob - src/plugin_proc_diskspace.c
ab-debian 0.20170311.01-0ab1, upstream v1.5.0-573-g0fba967b
[netdata.git] / src / plugin_proc_diskspace.c
1 #include "common.h"
2
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"
6
7 static struct mountinfo *disk_mountinfo_root = NULL;
8 static int check_for_new_mountpoints_every = 15;
9
10 static inline void mountinfo_reload(int force) {
11     static time_t last_loaded = 0;
12     time_t now = now_realtime_sec();
13
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);
17
18         // re-read mountinfo in case something changed
19         disk_mountinfo_root = mountinfo_read(0);
20
21         last_loaded = now;
22     }
23 }
24
25 // Data to be stored in DICTIONARY dict_mountpoints 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 {
28     int do_space;
29     int do_inodes;
30     int shown_error;
31     int updated;
32
33     size_t collected; // the number of times this has been collected
34
35     RRDSET *st_space;
36     RRDDIM *rd_space_used;
37     RRDDIM *rd_space_avail;
38     RRDDIM *rd_space_reserved;
39
40     RRDSET *st_inodes;
41     RRDDIM *rd_inodes_used;
42     RRDDIM *rd_inodes_avail;
43     RRDDIM *rd_inodes_reserved;
44 };
45
46 static DICTIONARY *dict_mountpoints = NULL;
47
48 #define rrdset_obsolete_and_pointer_null(st) do { if(st) { rrdset_flag_set(st, RRDSET_FLAG_OBSOLETE); st = NULL; } } while(st)
49
50 int mount_point_cleanup(void *entry, void *data) {
51     (void)data;
52
53     struct mount_point_metadata *mp = (struct mount_point_metadata *)entry;
54     if(!mp) return 0;
55
56     if(likely(mp->updated)) {
57         mp->updated = 0;
58         return 0;
59     }
60
61     if(likely(mp->collected)) {
62         mp->collected = 0;
63         mp->updated = 0;
64         mp->shown_error = 0;
65
66         mp->rd_space_avail = NULL;
67         mp->rd_space_used = NULL;
68         mp->rd_space_reserved = NULL;
69
70         mp->rd_inodes_avail = NULL;
71         mp->rd_inodes_used = NULL;
72         mp->rd_inodes_reserved = NULL;
73
74         rrdset_obsolete_and_pointer_null(mp->st_space);
75         rrdset_obsolete_and_pointer_null(mp->st_inodes);
76     }
77
78     return 0;
79 }
80
81 static inline void do_disk_space_stats(struct mountinfo *mi, int update_every) {
82     const char *family = mi->mount_point;
83     const char *disk = mi->persistent_id;
84
85     static SIMPLE_PATTERN *excluded_mountpoints = NULL;
86     static SIMPLE_PATTERN *excluded_filesystems = NULL;
87     int do_space, do_inodes;
88
89     if(unlikely(!dict_mountpoints)) {
90         SIMPLE_PREFIX_MODE mode = SIMPLE_PATTERN_EXACT;
91
92         if(config_move("plugin:proc:/proc/diskstats", "exclude space metrics on paths", CONFIG_SECTION_DISKSPACE, "exclude space metrics on paths") != -1) {
93             // old configuration, enable backwards compatibility
94             mode = SIMPLE_PATTERN_PREFIX;
95         }
96
97         excluded_mountpoints = simple_pattern_create(
98                 config_get(CONFIG_SECTION_DISKSPACE, "exclude space metrics on paths", DELAULT_EXLUDED_PATHS),
99                 mode
100         );
101
102         excluded_filesystems = simple_pattern_create(
103                 config_get(CONFIG_SECTION_DISKSPACE, "exclude space metrics on filesystems", DEFAULT_EXCLUDED_FILESYSTEMS),
104                 SIMPLE_PATTERN_EXACT
105         );
106
107         dict_mountpoints = dictionary_create(DICTIONARY_FLAG_SINGLE_THREADED);
108     }
109
110     struct mount_point_metadata *m = dictionary_get(dict_mountpoints, mi->mount_point);
111     if(unlikely(!m)) {
112         char var_name[4096 + 1];
113         snprintfz(var_name, 4096, "plugin:proc:diskspace:%s", mi->mount_point);
114
115         int def_space = config_get_boolean_ondemand(CONFIG_SECTION_DISKSPACE, "space usage for all disks", CONFIG_BOOLEAN_AUTO);
116         int def_inodes = config_get_boolean_ondemand(CONFIG_SECTION_DISKSPACE, "inodes usage for all disks", CONFIG_BOOLEAN_AUTO);
117
118         if(unlikely(simple_pattern_matches(excluded_mountpoints, mi->mount_point))) {
119             def_space = CONFIG_BOOLEAN_NO;
120             def_inodes = CONFIG_BOOLEAN_NO;
121         }
122
123         if(unlikely(simple_pattern_matches(excluded_filesystems, mi->filesystem))) {
124             def_space = CONFIG_BOOLEAN_NO;
125             def_inodes = CONFIG_BOOLEAN_NO;
126         }
127
128         do_space = config_get_boolean_ondemand(var_name, "space usage", def_space);
129         do_inodes = config_get_boolean_ondemand(var_name, "inodes usage", def_inodes);
130
131         struct mount_point_metadata mp = {
132                 .do_space = do_space,
133                 .do_inodes = do_inodes,
134                 .shown_error = 0,
135                 .updated = 0,
136
137                 .collected = 0,
138
139                 .st_space = NULL,
140                 .rd_space_avail = NULL,
141                 .rd_space_used = NULL,
142                 .rd_space_reserved = NULL,
143
144                 .st_inodes = NULL,
145                 .rd_inodes_avail = NULL,
146                 .rd_inodes_used = NULL,
147                 .rd_inodes_reserved = NULL
148         };
149
150         m = dictionary_set(dict_mountpoints, mi->mount_point, &mp, sizeof(struct mount_point_metadata));
151     }
152
153     m->updated = 1;
154
155     if(unlikely(m->do_space == CONFIG_BOOLEAN_NO && m->do_inodes == CONFIG_BOOLEAN_NO))
156         return;
157
158     if(unlikely(mi->flags & MOUNTINFO_READONLY && !m->collected))
159         return;
160
161     struct statvfs buff_statvfs;
162     if (statvfs(mi->mount_point, &buff_statvfs) < 0) {
163         if(!m->shown_error) {
164             error("Failed statvfs() for '%s' (disk '%s', filesystem '%s', root '%s')"
165                   , mi->mount_point
166                   , disk
167                   , mi->filesystem?mi->filesystem:""
168                   , mi->root?mi->root:""
169             );
170             m->shown_error = 1;
171         }
172         return;
173     }
174     m->shown_error = 0;
175
176     // logic found at get_fs_usage() in coreutils
177     unsigned long bsize = (buff_statvfs.f_frsize) ? buff_statvfs.f_frsize : buff_statvfs.f_bsize;
178
179     fsblkcnt_t bavail         = buff_statvfs.f_bavail;
180     fsblkcnt_t btotal         = buff_statvfs.f_blocks;
181     fsblkcnt_t bavail_root    = buff_statvfs.f_bfree;
182     fsblkcnt_t breserved_root = bavail_root - bavail;
183     fsblkcnt_t bused;
184     if(likely(btotal >= bavail_root))
185         bused = btotal - bavail_root;
186     else
187         bused = bavail_root - btotal;
188
189 #ifdef NETDATA_INTERNAL_CHECKS
190     if(unlikely(btotal != bavail + breserved_root + bused))
191         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);
192 #endif
193
194     // --------------------------------------------------------------------------
195
196     fsfilcnt_t favail         = buff_statvfs.f_favail;
197     fsfilcnt_t ftotal         = buff_statvfs.f_files;
198     fsfilcnt_t favail_root    = buff_statvfs.f_ffree;
199     fsfilcnt_t freserved_root = favail_root - favail;
200     fsfilcnt_t fused          = ftotal - favail_root;
201
202 #ifdef NETDATA_INTERNAL_CHECKS
203     if(unlikely(btotal != bavail + breserved_root + bused))
204         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);
205 #endif
206
207     // --------------------------------------------------------------------------
208
209     int rendered = 0;
210
211     if(m->do_space == CONFIG_BOOLEAN_YES || (m->do_space == CONFIG_BOOLEAN_AUTO && (bavail || breserved_root || bused))) {
212         if(unlikely(!m->st_space)) {
213             m->do_space = CONFIG_BOOLEAN_YES;
214             m->st_space = rrdset_find_bytype_localhost("disk_space", disk);
215             if(unlikely(!m->st_space)) {
216                 char title[4096 + 1];
217                 snprintfz(title, 4096, "Disk Space Usage for %s [%s]", family, mi->mount_source);
218                 m->st_space = rrdset_create_localhost(
219                         "disk_space"
220                         , disk
221                         , NULL
222                         , family
223                         , "disk.space"
224                         , title
225                         , "GB"
226                         , 2023
227                         , update_every
228                         , RRDSET_TYPE_STACKED
229                 );
230             }
231
232             m->rd_space_avail    = rrddim_add(m->st_space, "avail", NULL, (collected_number)bsize, 1024 * 1024 * 1024, RRD_ALGORITHM_ABSOLUTE);
233             m->rd_space_used     = rrddim_add(m->st_space, "used", NULL, (collected_number)bsize, 1024 * 1024 * 1024, RRD_ALGORITHM_ABSOLUTE);
234             m->rd_space_reserved = rrddim_add(m->st_space, "reserved_for_root", "reserved for root", (collected_number)bsize, 1024 * 1024 * 1024, RRD_ALGORITHM_ABSOLUTE);
235         }
236         else
237             rrdset_next(m->st_space);
238
239         rrddim_set_by_pointer(m->st_space, m->rd_space_avail,    (collected_number)bavail);
240         rrddim_set_by_pointer(m->st_space, m->rd_space_used,     (collected_number)bused);
241         rrddim_set_by_pointer(m->st_space, m->rd_space_reserved, (collected_number)breserved_root);
242         rrdset_done(m->st_space);
243
244         rendered++;
245     }
246
247     // --------------------------------------------------------------------------
248
249     if(m->do_inodes == CONFIG_BOOLEAN_YES || (m->do_inodes == CONFIG_BOOLEAN_AUTO && (favail || freserved_root || fused))) {
250         if(unlikely(!m->st_inodes)) {
251             m->do_inodes = CONFIG_BOOLEAN_YES;
252             m->st_inodes = rrdset_find_bytype_localhost("disk_inodes", disk);
253             if(unlikely(!m->st_inodes)) {
254                 char title[4096 + 1];
255                 snprintfz(title, 4096, "Disk Files (inodes) Usage for %s [%s]", family, mi->mount_source);
256                 m->st_inodes = rrdset_create_localhost(
257                         "disk_inodes"
258                         , disk
259                         , NULL
260                         , family
261                         , "disk.inodes"
262                         , title
263                         , "Inodes"
264                         , 2024
265                         , update_every
266                         , RRDSET_TYPE_STACKED
267                 );
268             }
269
270             m->rd_inodes_avail    = rrddim_add(m->st_inodes, "avail", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
271             m->rd_inodes_used     = rrddim_add(m->st_inodes, "used", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
272             m->rd_inodes_reserved = rrddim_add(m->st_inodes, "reserved_for_root", "reserved for root", 1, 1, RRD_ALGORITHM_ABSOLUTE);
273         }
274         else
275             rrdset_next(m->st_inodes);
276
277         rrddim_set_by_pointer(m->st_inodes, m->rd_inodes_avail,    (collected_number)favail);
278         rrddim_set_by_pointer(m->st_inodes, m->rd_inodes_used,     (collected_number)fused);
279         rrddim_set_by_pointer(m->st_inodes, m->rd_inodes_reserved, (collected_number)freserved_root);
280         rrdset_done(m->st_inodes);
281
282         rendered++;
283     }
284
285     // --------------------------------------------------------------------------
286
287     if(likely(rendered))
288         m->collected++;
289 }
290
291 void *proc_diskspace_main(void *ptr) {
292     struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
293
294     info("DISKSPACE thread created with task id %d", gettid());
295
296     if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
297         error("Cannot set pthread cancel type to DEFERRED.");
298
299     if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
300         error("Cannot set pthread cancel state to ENABLE.");
301
302     int vdo_cpu_netdata = config_get_boolean("plugin:proc", "netdata server resources", 1);
303
304     int update_every = (int)config_get_number(CONFIG_SECTION_DISKSPACE, "update every", localhost->rrd_update_every);
305     if(update_every < localhost->rrd_update_every)
306         update_every = localhost->rrd_update_every;
307
308     check_for_new_mountpoints_every = (int)config_get_number(CONFIG_SECTION_DISKSPACE, "check for new mount points every", check_for_new_mountpoints_every);
309     if(check_for_new_mountpoints_every < update_every)
310         check_for_new_mountpoints_every = update_every;
311
312     struct rusage thread;
313
314     usec_t duration = 0;
315     usec_t step = update_every * USEC_PER_SEC;
316     heartbeat_t hb;
317     heartbeat_init(&hb);
318     for(;;) {
319         duration = heartbeat_dt_usec(&hb);
320         /* usec_t hb_dt = */ heartbeat_next(&hb, step);
321
322         if(unlikely(netdata_exit)) break;
323
324
325         // --------------------------------------------------------------------------
326         // this is smart enough not to reload it every time
327
328         mountinfo_reload(0);
329
330
331         // --------------------------------------------------------------------------
332         // disk space metrics
333
334         struct mountinfo *mi;
335         for(mi = disk_mountinfo_root; mi; mi = mi->next) {
336
337             if(unlikely(mi->flags & (MOUNTINFO_IS_DUMMY | MOUNTINFO_IS_BIND | MOUNTINFO_IS_SAME_DEV | MOUNTINFO_NO_STAT | MOUNTINFO_NO_SIZE)))
338                 continue;
339
340             do_disk_space_stats(mi, update_every);
341             if(unlikely(netdata_exit)) break;
342         }
343
344         if(unlikely(netdata_exit)) break;
345
346         dictionary_get_all(dict_mountpoints, mount_point_cleanup, NULL);
347
348         if(vdo_cpu_netdata) {
349             static RRDSET *stcpu_thread = NULL, *st_duration = NULL;
350             static RRDDIM *rd_user = NULL, *rd_system = NULL, *rd_duration = NULL;
351
352             // ----------------------------------------------------------------
353
354             getrusage(RUSAGE_THREAD, &thread);
355
356             if(!stcpu_thread) {
357                 stcpu_thread = rrdset_find_localhost("netdata.plugin_diskspace");
358                 if(!stcpu_thread)
359                     stcpu_thread = rrdset_create_localhost(
360                             "netdata"
361                             , "plugin_diskspace"
362                             , NULL
363                             , "diskspace"
364                             , NULL
365                             , "NetData Disk Space Plugin CPU usage"
366                             , "milliseconds/s"
367                             , 132020
368                             , update_every
369                             , RRDSET_TYPE_STACKED
370                     );
371
372                 rd_user   = rrddim_add(stcpu_thread, "user", NULL, 1, 1000, RRD_ALGORITHM_INCREMENTAL);
373                 rd_system = rrddim_add(stcpu_thread, "system", NULL, 1, 1000, RRD_ALGORITHM_INCREMENTAL);
374             }
375             else
376                 rrdset_next(stcpu_thread);
377
378             rrddim_set_by_pointer(stcpu_thread, rd_user, thread.ru_utime.tv_sec * 1000000ULL + thread.ru_utime.tv_usec);
379             rrddim_set_by_pointer(stcpu_thread, rd_system, thread.ru_stime.tv_sec * 1000000ULL + thread.ru_stime.tv_usec);
380             rrdset_done(stcpu_thread);
381
382             // ----------------------------------------------------------------
383
384             if(!st_duration) {
385                 st_duration = rrdset_find_localhost("netdata.plugin_diskspace_dt");
386                 if(!st_duration)
387                     st_duration = rrdset_create_localhost(
388                             "netdata"
389                             , "plugin_diskspace_dt"
390                             , NULL
391                             , "diskspace"
392                             , NULL
393                             , "NetData Disk Space Plugin Duration"
394                             , "milliseconds/run"
395                             , 132021
396                             , update_every
397                             , RRDSET_TYPE_AREA
398                     );
399
400                 rd_duration = rrddim_add(st_duration, "duration", NULL, 1, 1000, RRD_ALGORITHM_ABSOLUTE);
401             }
402             else
403                 rrdset_next(st_duration);
404
405             rrddim_set_by_pointer(st_duration, rd_duration, duration);
406             rrdset_done(st_duration);
407
408             // ----------------------------------------------------------------
409
410             if(unlikely(netdata_exit)) break;
411         }
412     }
413
414     info("DISKSPACE thread exiting");
415
416     static_thread->enabled = 0;
417     pthread_exit(NULL);
418     return NULL;
419 }