plugin_diskspace.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "../proc.plugin/plugin_proc.h"
  3. #define PLUGIN_DISKSPACE_NAME "diskspace.plugin"
  4. #define DEFAULT_EXCLUDED_PATHS "/proc/* /sys/* /var/run/user/* /run/user/* /snap/* /var/lib/docker/*"
  5. #define DEFAULT_EXCLUDED_FILESYSTEMS "*gvfs *gluster* *s3fs *ipfs *davfs2 *httpfs *sshfs *gdfs *moosefs fusectl autofs"
  6. #define CONFIG_SECTION_DISKSPACE "plugin:proc:diskspace"
  7. static struct mountinfo *disk_mountinfo_root = NULL;
  8. static int check_for_new_mountpoints_every = 15;
  9. static int cleanup_mount_points = 1;
  10. static inline void mountinfo_reload(int force) {
  11. static time_t last_loaded = 0;
  12. time_t now = now_realtime_sec();
  13. if(force || now - last_loaded >= check_for_new_mountpoints_every) {
  14. // mountinfo_free_all() can be called with NULL disk_mountinfo_root
  15. mountinfo_free_all(disk_mountinfo_root);
  16. // re-read mountinfo in case something changed
  17. disk_mountinfo_root = mountinfo_read(0);
  18. last_loaded = now;
  19. }
  20. }
  21. // Data to be stored in DICTIONARY dict_mountpoints used by do_disk_space_stats().
  22. // This DICTIONARY is used to lookup the settings of the mount point on each iteration.
  23. struct mount_point_metadata {
  24. int do_space;
  25. int do_inodes;
  26. int shown_error;
  27. int updated;
  28. size_t collected; // the number of times this has been collected
  29. RRDSET *st_space;
  30. RRDDIM *rd_space_used;
  31. RRDDIM *rd_space_avail;
  32. RRDDIM *rd_space_reserved;
  33. RRDSET *st_inodes;
  34. RRDDIM *rd_inodes_used;
  35. RRDDIM *rd_inodes_avail;
  36. RRDDIM *rd_inodes_reserved;
  37. };
  38. static DICTIONARY *dict_mountpoints = NULL;
  39. #define rrdset_obsolete_and_pointer_null(st) do { if(st) { rrdset_is_obsolete(st); (st) = NULL; } } while(st)
  40. int mount_point_cleanup(const char *name, void *entry, void *data) {
  41. (void)name;
  42. (void)data;
  43. struct mount_point_metadata *mp = (struct mount_point_metadata *)entry;
  44. if(!mp) return 0;
  45. if(likely(mp->updated)) {
  46. mp->updated = 0;
  47. return 0;
  48. }
  49. if(likely(cleanup_mount_points && mp->collected)) {
  50. mp->collected = 0;
  51. mp->updated = 0;
  52. mp->shown_error = 0;
  53. mp->rd_space_avail = NULL;
  54. mp->rd_space_used = NULL;
  55. mp->rd_space_reserved = NULL;
  56. mp->rd_inodes_avail = NULL;
  57. mp->rd_inodes_used = NULL;
  58. mp->rd_inodes_reserved = NULL;
  59. rrdset_obsolete_and_pointer_null(mp->st_space);
  60. rrdset_obsolete_and_pointer_null(mp->st_inodes);
  61. }
  62. return 0;
  63. }
  64. // for the full list of protected mount points look at
  65. // https://github.com/systemd/systemd/blob/1eb3ef78b4df28a9e9f464714208f2682f957e36/src/core/namespace.c#L142-L149
  66. // https://github.com/systemd/systemd/blob/1eb3ef78b4df28a9e9f464714208f2682f957e36/src/core/namespace.c#L180-L194
  67. static const char *systemd_protected_mount_points[] = {
  68. "/home",
  69. "/root",
  70. "/usr",
  71. "/boot",
  72. "/efi",
  73. "/etc",
  74. NULL
  75. };
  76. int mount_point_is_protected(char *mount_point)
  77. {
  78. for (size_t i = 0; systemd_protected_mount_points[i] != NULL; i++)
  79. if (!strcmp(mount_point, systemd_protected_mount_points[i]))
  80. return 1;
  81. return 0;
  82. }
  83. static inline void do_disk_space_stats(struct mountinfo *mi, int update_every) {
  84. const char *family = mi->mount_point;
  85. const char *disk = mi->persistent_id;
  86. static SIMPLE_PATTERN *excluded_mountpoints = NULL;
  87. static SIMPLE_PATTERN *excluded_filesystems = NULL;
  88. int do_space, do_inodes;
  89. if(unlikely(!dict_mountpoints)) {
  90. SIMPLE_PREFIX_MODE mode = SIMPLE_PATTERN_EXACT;
  91. if(config_move("plugin:proc:/proc/diskstats", "exclude space metrics on paths", CONFIG_SECTION_DISKSPACE, "exclude space metrics on paths") != -1) {
  92. // old configuration, enable backwards compatibility
  93. mode = SIMPLE_PATTERN_PREFIX;
  94. }
  95. excluded_mountpoints = simple_pattern_create(
  96. config_get(CONFIG_SECTION_DISKSPACE, "exclude space metrics on paths", DEFAULT_EXCLUDED_PATHS)
  97. , NULL
  98. , mode
  99. );
  100. excluded_filesystems = simple_pattern_create(
  101. config_get(CONFIG_SECTION_DISKSPACE, "exclude space metrics on filesystems", DEFAULT_EXCLUDED_FILESYSTEMS)
  102. , NULL
  103. , SIMPLE_PATTERN_EXACT
  104. );
  105. dict_mountpoints = dictionary_create(DICTIONARY_FLAG_SINGLE_THREADED);
  106. }
  107. struct mount_point_metadata *m = dictionary_get(dict_mountpoints, mi->mount_point);
  108. if(unlikely(!m)) {
  109. char var_name[4096 + 1];
  110. snprintfz(var_name, 4096, "plugin:proc:diskspace:%s", mi->mount_point);
  111. int def_space = config_get_boolean_ondemand(CONFIG_SECTION_DISKSPACE, "space usage for all disks", CONFIG_BOOLEAN_AUTO);
  112. int def_inodes = config_get_boolean_ondemand(CONFIG_SECTION_DISKSPACE, "inodes usage for all disks", CONFIG_BOOLEAN_AUTO);
  113. if(unlikely(simple_pattern_matches(excluded_mountpoints, mi->mount_point))) {
  114. def_space = CONFIG_BOOLEAN_NO;
  115. def_inodes = CONFIG_BOOLEAN_NO;
  116. }
  117. if(unlikely(simple_pattern_matches(excluded_filesystems, mi->filesystem))) {
  118. def_space = CONFIG_BOOLEAN_NO;
  119. def_inodes = CONFIG_BOOLEAN_NO;
  120. }
  121. // check if the mount point is a directory #2407
  122. // but only when it is enabled by default #4491
  123. if(def_space != CONFIG_BOOLEAN_NO || def_inodes != CONFIG_BOOLEAN_NO) {
  124. struct stat bs;
  125. if(stat(mi->mount_point, &bs) == -1) {
  126. error("DISKSPACE: Cannot stat() mount point '%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. def_space = CONFIG_BOOLEAN_NO;
  133. def_inodes = CONFIG_BOOLEAN_NO;
  134. }
  135. else {
  136. if((bs.st_mode & S_IFMT) != S_IFDIR) {
  137. error("DISKSPACE: Mount point '%s' (disk '%s', filesystem '%s', root '%s') is not a directory."
  138. , mi->mount_point
  139. , disk
  140. , mi->filesystem?mi->filesystem:""
  141. , mi->root?mi->root:""
  142. );
  143. def_space = CONFIG_BOOLEAN_NO;
  144. def_inodes = CONFIG_BOOLEAN_NO;
  145. }
  146. }
  147. }
  148. do_space = config_get_boolean_ondemand(var_name, "space usage", def_space);
  149. do_inodes = config_get_boolean_ondemand(var_name, "inodes usage", def_inodes);
  150. struct mount_point_metadata mp = {
  151. .do_space = do_space,
  152. .do_inodes = do_inodes,
  153. .shown_error = 0,
  154. .updated = 0,
  155. .collected = 0,
  156. .st_space = NULL,
  157. .rd_space_avail = NULL,
  158. .rd_space_used = NULL,
  159. .rd_space_reserved = NULL,
  160. .st_inodes = NULL,
  161. .rd_inodes_avail = NULL,
  162. .rd_inodes_used = NULL,
  163. .rd_inodes_reserved = NULL
  164. };
  165. m = dictionary_set(dict_mountpoints, mi->mount_point, &mp, sizeof(struct mount_point_metadata));
  166. }
  167. m->updated = 1;
  168. if(unlikely(m->do_space == CONFIG_BOOLEAN_NO && m->do_inodes == CONFIG_BOOLEAN_NO))
  169. return;
  170. if (unlikely(
  171. mi->flags & MOUNTINFO_READONLY &&
  172. !mount_point_is_protected(mi->mount_point) &&
  173. !m->collected &&
  174. m->do_space != CONFIG_BOOLEAN_YES &&
  175. m->do_inodes != CONFIG_BOOLEAN_YES))
  176. return;
  177. struct statvfs buff_statvfs;
  178. if (statvfs(mi->mount_point, &buff_statvfs) < 0) {
  179. if(!m->shown_error) {
  180. error("DISKSPACE: failed to statvfs() mount point '%s' (disk '%s', filesystem '%s', root '%s')"
  181. , mi->mount_point
  182. , disk
  183. , mi->filesystem?mi->filesystem:""
  184. , mi->root?mi->root:""
  185. );
  186. m->shown_error = 1;
  187. }
  188. return;
  189. }
  190. m->shown_error = 0;
  191. // logic found at get_fs_usage() in coreutils
  192. unsigned long bsize = (buff_statvfs.f_frsize) ? buff_statvfs.f_frsize : buff_statvfs.f_bsize;
  193. fsblkcnt_t bavail = buff_statvfs.f_bavail;
  194. fsblkcnt_t btotal = buff_statvfs.f_blocks;
  195. fsblkcnt_t bavail_root = buff_statvfs.f_bfree;
  196. fsblkcnt_t breserved_root = bavail_root - bavail;
  197. fsblkcnt_t bused;
  198. if(likely(btotal >= bavail_root))
  199. bused = btotal - bavail_root;
  200. else
  201. bused = bavail_root - btotal;
  202. #ifdef NETDATA_INTERNAL_CHECKS
  203. if(unlikely(btotal != bavail + breserved_root + bused))
  204. error("DISKSPACE: 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);
  205. #endif
  206. // --------------------------------------------------------------------------
  207. fsfilcnt_t favail = buff_statvfs.f_favail;
  208. fsfilcnt_t ftotal = buff_statvfs.f_files;
  209. fsfilcnt_t favail_root = buff_statvfs.f_ffree;
  210. fsfilcnt_t freserved_root = favail_root - favail;
  211. fsfilcnt_t fused = ftotal - favail_root;
  212. if(m->do_inodes == CONFIG_BOOLEAN_AUTO && favail == (fsfilcnt_t)-1) {
  213. // this file system does not support inodes reporting
  214. // eg. cephfs
  215. m->do_inodes = CONFIG_BOOLEAN_NO;
  216. }
  217. #ifdef NETDATA_INTERNAL_CHECKS
  218. if(unlikely(btotal != bavail + breserved_root + bused))
  219. error("DISKSPACE: 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);
  220. #endif
  221. // --------------------------------------------------------------------------
  222. int rendered = 0;
  223. if(m->do_space == CONFIG_BOOLEAN_YES || (m->do_space == CONFIG_BOOLEAN_AUTO &&
  224. (bavail || breserved_root || bused ||
  225. netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES))) {
  226. if(unlikely(!m->st_space)) {
  227. m->do_space = CONFIG_BOOLEAN_YES;
  228. m->st_space = rrdset_find_active_bytype_localhost("disk_space", disk);
  229. if(unlikely(!m->st_space)) {
  230. char title[4096 + 1];
  231. snprintfz(title, 4096, "Disk Space Usage");
  232. m->st_space = rrdset_create_localhost(
  233. "disk_space"
  234. , disk
  235. , NULL
  236. , family
  237. , "disk.space"
  238. , title
  239. , "GiB"
  240. , PLUGIN_DISKSPACE_NAME
  241. , NULL
  242. , NETDATA_CHART_PRIO_DISKSPACE_SPACE
  243. , update_every
  244. , RRDSET_TYPE_STACKED
  245. );
  246. }
  247. m->rd_space_avail = rrddim_add(m->st_space, "avail", NULL, (collected_number)bsize, 1024 * 1024 * 1024, RRD_ALGORITHM_ABSOLUTE);
  248. m->rd_space_used = rrddim_add(m->st_space, "used", NULL, (collected_number)bsize, 1024 * 1024 * 1024, RRD_ALGORITHM_ABSOLUTE);
  249. m->rd_space_reserved = rrddim_add(m->st_space, "reserved_for_root", "reserved for root", (collected_number)bsize, 1024 * 1024 * 1024, RRD_ALGORITHM_ABSOLUTE);
  250. }
  251. else
  252. rrdset_next(m->st_space);
  253. rrddim_set_by_pointer(m->st_space, m->rd_space_avail, (collected_number)bavail);
  254. rrddim_set_by_pointer(m->st_space, m->rd_space_used, (collected_number)bused);
  255. rrddim_set_by_pointer(m->st_space, m->rd_space_reserved, (collected_number)breserved_root);
  256. rrdset_done(m->st_space);
  257. rendered++;
  258. }
  259. // --------------------------------------------------------------------------
  260. if(m->do_inodes == CONFIG_BOOLEAN_YES || (m->do_inodes == CONFIG_BOOLEAN_AUTO &&
  261. (favail || freserved_root || fused ||
  262. netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES))) {
  263. if(unlikely(!m->st_inodes)) {
  264. m->do_inodes = CONFIG_BOOLEAN_YES;
  265. m->st_inodes = rrdset_find_active_bytype_localhost("disk_inodes", disk);
  266. if(unlikely(!m->st_inodes)) {
  267. char title[4096 + 1];
  268. snprintfz(title, 4096, "Disk Files (inodes) Usage");
  269. m->st_inodes = rrdset_create_localhost(
  270. "disk_inodes"
  271. , disk
  272. , NULL
  273. , family
  274. , "disk.inodes"
  275. , title
  276. , "inodes"
  277. , PLUGIN_DISKSPACE_NAME
  278. , NULL
  279. , NETDATA_CHART_PRIO_DISKSPACE_INODES
  280. , update_every
  281. , RRDSET_TYPE_STACKED
  282. );
  283. }
  284. m->rd_inodes_avail = rrddim_add(m->st_inodes, "avail", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
  285. m->rd_inodes_used = rrddim_add(m->st_inodes, "used", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
  286. m->rd_inodes_reserved = rrddim_add(m->st_inodes, "reserved_for_root", "reserved for root", 1, 1, RRD_ALGORITHM_ABSOLUTE);
  287. }
  288. else
  289. rrdset_next(m->st_inodes);
  290. rrddim_set_by_pointer(m->st_inodes, m->rd_inodes_avail, (collected_number)favail);
  291. rrddim_set_by_pointer(m->st_inodes, m->rd_inodes_used, (collected_number)fused);
  292. rrddim_set_by_pointer(m->st_inodes, m->rd_inodes_reserved, (collected_number)freserved_root);
  293. rrdset_done(m->st_inodes);
  294. rendered++;
  295. }
  296. // --------------------------------------------------------------------------
  297. if(likely(rendered))
  298. m->collected++;
  299. }
  300. static void diskspace_main_cleanup(void *ptr) {
  301. worker_unregister();
  302. struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
  303. static_thread->enabled = NETDATA_MAIN_THREAD_EXITING;
  304. info("cleaning up...");
  305. static_thread->enabled = NETDATA_MAIN_THREAD_EXITED;
  306. }
  307. #define WORKER_JOB_MOUNTINFO 0
  308. #define WORKER_JOB_MOUNTPOINT 1
  309. #define WORKER_JOB_CLEANUP 2
  310. #if WORKER_UTILIZATION_MAX_JOB_TYPES < 3
  311. #error WORKER_UTILIZATION_MAX_JOB_TYPES has to be at least 3
  312. #endif
  313. void *diskspace_main(void *ptr) {
  314. worker_register("DISKSPACE");
  315. worker_register_job_name(WORKER_JOB_MOUNTINFO, "mountinfo");
  316. worker_register_job_name(WORKER_JOB_MOUNTPOINT, "mountpoint");
  317. worker_register_job_name(WORKER_JOB_CLEANUP, "cleanup");
  318. netdata_thread_cleanup_push(diskspace_main_cleanup, ptr);
  319. cleanup_mount_points = config_get_boolean(CONFIG_SECTION_DISKSPACE, "remove charts of unmounted disks" , cleanup_mount_points);
  320. int update_every = (int)config_get_number(CONFIG_SECTION_DISKSPACE, "update every", localhost->rrd_update_every);
  321. if(update_every < localhost->rrd_update_every)
  322. update_every = localhost->rrd_update_every;
  323. check_for_new_mountpoints_every = (int)config_get_number(CONFIG_SECTION_DISKSPACE, "check for new mount points every", check_for_new_mountpoints_every);
  324. if(check_for_new_mountpoints_every < update_every)
  325. check_for_new_mountpoints_every = update_every;
  326. usec_t step = update_every * USEC_PER_SEC;
  327. heartbeat_t hb;
  328. heartbeat_init(&hb);
  329. while(!netdata_exit) {
  330. worker_is_idle();
  331. /* usec_t hb_dt = */ heartbeat_next(&hb, step);
  332. if(unlikely(netdata_exit)) break;
  333. // --------------------------------------------------------------------------
  334. // this is smart enough not to reload it every time
  335. worker_is_busy(WORKER_JOB_MOUNTINFO);
  336. mountinfo_reload(0);
  337. // --------------------------------------------------------------------------
  338. // disk space metrics
  339. struct mountinfo *mi;
  340. for(mi = disk_mountinfo_root; mi; mi = mi->next) {
  341. if(unlikely(mi->flags & (MOUNTINFO_IS_DUMMY | MOUNTINFO_IS_BIND)))
  342. continue;
  343. // exclude mounts made by ProtectHome and ProtectSystem systemd hardening options
  344. if(mi->flags & MOUNTINFO_READONLY && !strcmp(mi->root, mi->mount_point))
  345. continue;
  346. worker_is_busy(WORKER_JOB_MOUNTPOINT);
  347. do_disk_space_stats(mi, update_every);
  348. if(unlikely(netdata_exit)) break;
  349. }
  350. if(unlikely(netdata_exit)) break;
  351. if(dict_mountpoints) {
  352. worker_is_busy(WORKER_JOB_CLEANUP);
  353. dictionary_walkthrough_read(dict_mountpoints, mount_point_cleanup, NULL);
  354. }
  355. }
  356. worker_unregister();
  357. netdata_thread_cleanup_pop(1);
  358. return NULL;
  359. }