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