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