]> arthur.barton.de Git - netdata.git/blob - src/plugin_proc_diskspace.c
Merge pull request #1589 from l2isbad/varnish_plugin
[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
5 static struct mountinfo *disk_mountinfo_root = NULL;
6 static int check_for_new_mountpoints_every = 15;
7
8 static inline void mountinfo_reload(int force) {
9     static time_t last_loaded = 0;
10     time_t now = now_realtime_sec();
11
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);
15
16         // re-read mountinfo in case something changed
17         disk_mountinfo_root = mountinfo_read(1);
18
19         last_loaded = now;
20     }
21 }
22
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 {
26     int do_space;
27     int do_inodes;
28 };
29
30 static inline void do_disk_space_stats(struct mountinfo *mi, int update_every) {
31     const char *family = mi->mount_point;
32     const char *disk = mi->persistent_id;
33
34     static DICTIONARY *mount_points = NULL;
35     static SIMPLE_PATTERN *excluded_mountpoints = NULL;
36     int do_space, do_inodes;
37
38     if(unlikely(!mount_points)) {
39         const char *s;
40         SIMPLE_PREFIX_MODE mode = SIMPLE_PATTERN_EXACT;
41
42         if(config_exists("plugin:proc:/proc/diskstats", "exclude space metrics on paths") && !config_exists("plugin:proc:diskspace", "exclude space metrics on paths")) {
43             // the config exists in the old section
44             s = config_get("plugin:proc:/proc/diskstats", "exclude space metrics on paths", DELAULT_EXLUDED_PATHS);
45             mode = SIMPLE_PATTERN_PREFIX;
46         }
47         else
48             s = config_get("plugin:proc:diskspace", "exclude space metrics on paths", DELAULT_EXLUDED_PATHS);
49
50         mount_points = dictionary_create(DICTIONARY_FLAG_SINGLE_THREADED);
51         excluded_mountpoints = simple_pattern_create(s, mode);
52     }
53
54     struct mount_point_metadata *m = dictionary_get(mount_points, mi->mount_point);
55     if(unlikely(!m)) {
56         char var_name[4096 + 1];
57         snprintfz(var_name, 4096, "plugin:proc:diskspace:%s", mi->mount_point);
58
59         int def_space = config_get_boolean_ondemand("plugin:proc:diskspace", "space usage for all disks", CONFIG_ONDEMAND_ONDEMAND);
60         int def_inodes = config_get_boolean_ondemand("plugin:proc:diskspace", "inodes usage for all disks", CONFIG_ONDEMAND_ONDEMAND);
61
62         if(unlikely(simple_pattern_matches(excluded_mountpoints, mi->mount_point))) {
63             def_space = CONFIG_ONDEMAND_NO;
64             def_inodes = CONFIG_ONDEMAND_NO;
65         }
66
67         do_space = config_get_boolean_ondemand(var_name, "space usage", def_space);
68         do_inodes = config_get_boolean_ondemand(var_name, "inodes usage", def_inodes);
69
70         struct mount_point_metadata mp = {
71                 .do_space = do_space,
72                 .do_inodes = do_inodes
73         };
74
75         dictionary_set(mount_points, mi->mount_point, &mp, sizeof(struct mount_point_metadata));
76     }
77     else {
78         do_space = m->do_space;
79         do_inodes = m->do_inodes;
80     }
81
82     if(unlikely(do_space == CONFIG_ONDEMAND_NO && do_inodes == CONFIG_ONDEMAND_NO))
83         return;
84
85     struct statvfs buff_statvfs;
86     if (statvfs(mi->mount_point, &buff_statvfs) < 0) {
87         error("Failed statvfs() for '%s' (disk '%s')", mi->mount_point, disk);
88         return;
89     }
90
91     // taken from get_fs_usage() found in coreutils
92     unsigned long bsize = (buff_statvfs.f_frsize) ? buff_statvfs.f_frsize : buff_statvfs.f_bsize;
93
94     fsblkcnt_t bavail         = buff_statvfs.f_bavail;
95     fsblkcnt_t btotal         = buff_statvfs.f_blocks;
96     fsblkcnt_t bavail_root    = buff_statvfs.f_bfree;
97     fsblkcnt_t breserved_root = bavail_root - bavail;
98     fsblkcnt_t bused;
99     if(likely(btotal >= bavail_root))
100         bused = btotal - bavail_root;
101     else
102         bused = bavail_root - btotal;
103
104 #ifdef NETDATA_INTERNAL_CHECKS
105     if(unlikely(btotal != bavail + breserved_root + bused))
106         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);
107 #endif
108
109     // --------------------------------------------------------------------------
110
111     fsfilcnt_t favail         = buff_statvfs.f_favail;
112     fsfilcnt_t ftotal         = buff_statvfs.f_files;
113     fsfilcnt_t favail_root    = buff_statvfs.f_ffree;
114     fsfilcnt_t freserved_root = favail_root - favail;
115     fsfilcnt_t fused          = ftotal - favail_root;
116
117 #ifdef NETDATA_INTERNAL_CHECKS
118     if(unlikely(btotal != bavail + breserved_root + bused))
119         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);
120 #endif
121
122     // --------------------------------------------------------------------------
123
124     RRDSET *st;
125
126     if(do_space == CONFIG_ONDEMAND_YES || (do_space == CONFIG_ONDEMAND_ONDEMAND && (bavail || breserved_root || bused))) {
127         st = rrdset_find_bytype("disk_space", disk);
128         if(unlikely(!st)) {
129             char title[4096 + 1];
130             snprintfz(title, 4096, "Disk Space Usage for %s [%s]", family, mi->mount_source);
131             st = rrdset_create("disk_space", disk, NULL, family, "disk.space", title, "GB", 2023, update_every, RRDSET_TYPE_STACKED);
132
133             rrddim_add(st, "avail", NULL, bsize, 1024*1024*1024, RRDDIM_ABSOLUTE);
134             rrddim_add(st, "used" , NULL, bsize, 1024*1024*1024, RRDDIM_ABSOLUTE);
135             rrddim_add(st, "reserved_for_root", "reserved for root", bsize, 1024*1024*1024, RRDDIM_ABSOLUTE);
136         }
137         else rrdset_next(st);
138
139         rrddim_set(st, "avail", (collected_number)bavail);
140         rrddim_set(st, "used", (collected_number)bused);
141         rrddim_set(st, "reserved_for_root", (collected_number)breserved_root);
142         rrdset_done(st);
143     }
144
145     // --------------------------------------------------------------------------
146
147     if(do_inodes == CONFIG_ONDEMAND_YES || (do_inodes == CONFIG_ONDEMAND_ONDEMAND && (favail || freserved_root || fused))) {
148         st = rrdset_find_bytype("disk_inodes", disk);
149         if(unlikely(!st)) {
150             char title[4096 + 1];
151             snprintfz(title, 4096, "Disk Files (inodes) Usage for %s [%s]", family, mi->mount_source);
152             st = rrdset_create("disk_inodes", disk, NULL, family, "disk.inodes", title, "Inodes", 2024, update_every, RRDSET_TYPE_STACKED);
153
154             rrddim_add(st, "avail", NULL, 1, 1, RRDDIM_ABSOLUTE);
155             rrddim_add(st, "used" , NULL, 1, 1, RRDDIM_ABSOLUTE);
156             rrddim_add(st, "reserved_for_root", "reserved for root", 1, 1, RRDDIM_ABSOLUTE);
157         }
158         else rrdset_next(st);
159
160         rrddim_set(st, "avail", (collected_number)favail);
161         rrddim_set(st, "used", (collected_number)fused);
162         rrddim_set(st, "reserved_for_root", (collected_number)freserved_root);
163         rrdset_done(st);
164     }
165 }
166
167 void *proc_diskspace_main(void *ptr) {
168     struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
169
170     info("DISKSPACE thread created with task id %d", gettid());
171
172     if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
173         error("Cannot set pthread cancel type to DEFERRED.");
174
175     if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
176         error("Cannot set pthread cancel state to ENABLE.");
177
178     int vdo_cpu_netdata = config_get_boolean("plugin:proc", "netdata server resources", 1);
179
180     int update_every = (int)config_get_number("plugin:proc:diskspace", "update every", rrd_update_every);
181     if(update_every < rrd_update_every)
182         update_every = rrd_update_every;
183
184     check_for_new_mountpoints_every = (int)config_get_number("plugin:proc:diskspace", "check for new mount points every", check_for_new_mountpoints_every);
185     if(check_for_new_mountpoints_every < update_every)
186         check_for_new_mountpoints_every = update_every;
187
188     RRDSET *stcpu_thread = NULL, *st_duration = NULL;
189     RRDDIM *rd_user = NULL, *rd_system = NULL, *rd_duration = NULL;
190     struct rusage thread;
191
192     usec_t last = 0, dt = 0;
193     usec_t step = update_every * USEC_PER_SEC;
194     for(;;) {
195         usec_t now = now_monotonic_usec();
196         usec_t next = now - (now % step) + step;
197
198         dt = (last)?now - last:0;
199
200         while(now < next) {
201             sleep_usec(next - now);
202             now = now_monotonic_usec();
203         }
204
205         last = now;
206
207         if(unlikely(netdata_exit)) break;
208
209
210         // --------------------------------------------------------------------------
211         // this is smart enough not to reload it every time
212
213         mountinfo_reload(0);
214
215
216         // --------------------------------------------------------------------------
217         // disk space metrics
218
219         struct mountinfo *mi;
220         for(mi = disk_mountinfo_root; mi; mi = mi->next) {
221
222             if(unlikely(mi->flags &
223                         (MOUNTINFO_IS_DUMMY | MOUNTINFO_IS_BIND | MOUNTINFO_IS_SAME_DEV | MOUNTINFO_NO_STAT |
224                          MOUNTINFO_NO_SIZE | MOUNTINFO_READONLY)))
225                 continue;
226
227             do_disk_space_stats(mi, update_every);
228             if(unlikely(netdata_exit)) break;
229         }
230
231         if(unlikely(netdata_exit)) break;
232
233         if(vdo_cpu_netdata) {
234             // ----------------------------------------------------------------
235
236             getrusage(RUSAGE_THREAD, &thread);
237
238             if(!stcpu_thread) {
239                 stcpu_thread = rrdset_find("netdata.plugin_diskspace");
240                 if(!stcpu_thread) stcpu_thread = rrdset_create("netdata", "plugin_diskspace", NULL, "diskspace", NULL
241                                                  , "NetData Disk Space Plugin CPU usage", "milliseconds/s", 132020
242                                                  , update_every, RRDSET_TYPE_STACKED);
243
244                 rd_user = rrddim_add(stcpu_thread, "user", NULL, 1, 1000, RRDDIM_INCREMENTAL);
245                 rd_system = rrddim_add(stcpu_thread, "system", NULL, 1, 1000, RRDDIM_INCREMENTAL);
246             }
247             else
248                 rrdset_next(stcpu_thread);
249
250             rrddim_set_by_pointer(stcpu_thread, rd_user, thread.ru_utime.tv_sec * 1000000ULL + thread.ru_utime.tv_usec);
251             rrddim_set_by_pointer(stcpu_thread, rd_system, thread.ru_stime.tv_sec * 1000000ULL + thread.ru_stime.tv_usec);
252             rrdset_done(stcpu_thread);
253
254             // ----------------------------------------------------------------
255
256             if(!st_duration) {
257                 st_duration = rrdset_find("netdata.plugin_diskspace_dt");
258                 if(!st_duration) st_duration = rrdset_create("netdata", "plugin_diskspace_dt", NULL, "diskspace", NULL
259                                                  , "NetData Disk Space Plugin Duration", "milliseconds/run", 132021
260                                                  , update_every, RRDSET_TYPE_AREA);
261
262                 rd_duration = rrddim_add(st_duration, "duration", NULL, 1, 1000, RRDDIM_ABSOLUTE);
263             }
264             else
265                 rrdset_next(st_duration);
266
267             rrddim_set_by_pointer(st_duration, rd_duration, dt);
268             rrdset_done(st_duration);
269
270             // ----------------------------------------------------------------
271
272             if(unlikely(netdata_exit)) break;
273         }
274     }
275
276     info("DISKSPACE thread exiting");
277
278     static_thread->enabled = 0;
279     pthread_exit(NULL);
280     return NULL;
281 }