plugin_diskspace.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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(void *entry, void *data) {
  41. (void)data;
  42. struct mount_point_metadata *mp = (struct mount_point_metadata *)entry;
  43. if(!mp) return 0;
  44. if(likely(mp->updated)) {
  45. mp->updated = 0;
  46. return 0;
  47. }
  48. if(likely(cleanup_mount_points && mp->collected)) {
  49. mp->collected = 0;
  50. mp->updated = 0;
  51. mp->shown_error = 0;
  52. mp->rd_space_avail = NULL;
  53. mp->rd_space_used = NULL;
  54. mp->rd_space_reserved = NULL;
  55. mp->rd_inodes_avail = NULL;
  56. mp->rd_inodes_used = NULL;
  57. mp->rd_inodes_reserved = NULL;
  58. rrdset_obsolete_and_pointer_null(mp->st_space);
  59. rrdset_obsolete_and_pointer_null(mp->st_inodes);
  60. }
  61. return 0;
  62. }
  63. // for the full list of protected mount points look at
  64. // https://github.com/systemd/systemd/blob/1eb3ef78b4df28a9e9f464714208f2682f957e36/src/core/namespace.c#L142-L149
  65. // https://github.com/systemd/systemd/blob/1eb3ef78b4df28a9e9f464714208f2682f957e36/src/core/namespace.c#L180-L194
  66. static const char *systemd_protected_mount_points[] = {
  67. "/home",
  68. "/root",
  69. "/usr",
  70. "/boot",
  71. "/efi",
  72. "/etc",
  73. NULL
  74. };
  75. int mount_point_is_protected(char *mount_point)
  76. {
  77. for (size_t i = 0; systemd_protected_mount_points[i] != NULL; i++)
  78. if (!strcmp(mount_point, systemd_protected_mount_points[i]))
  79. return 1;
  80. return 0;
  81. }
  82. static inline void do_disk_space_stats(struct mountinfo *mi, int update_every) {
  83. const char *family = mi->mount_point;
  84. const char *disk = mi->persistent_id;
  85. static SIMPLE_PATTERN *excluded_mountpoints = NULL;
  86. static SIMPLE_PATTERN *excluded_filesystems = NULL;
  87. int do_space, do_inodes;
  88. if(unlikely(!dict_mountpoints)) {
  89. SIMPLE_PREFIX_MODE mode = SIMPLE_PATTERN_EXACT;
  90. if(config_move("plugin:proc:/proc/diskstats", "exclude space metrics on paths", CONFIG_SECTION_DISKSPACE, "exclude space metrics on paths") != -1) {
  91. // old configuration, enable backwards compatibility
  92. mode = SIMPLE_PATTERN_PREFIX;
  93. }
  94. excluded_mountpoints = simple_pattern_create(
  95. config_get(CONFIG_SECTION_DISKSPACE, "exclude space metrics on paths", DEFAULT_EXCLUDED_PATHS)
  96. , NULL
  97. , mode
  98. );
  99. excluded_filesystems = simple_pattern_create(
  100. config_get(CONFIG_SECTION_DISKSPACE, "exclude space metrics on filesystems", DEFAULT_EXCLUDED_FILESYSTEMS)
  101. , NULL
  102. , SIMPLE_PATTERN_EXACT
  103. );
  104. dict_mountpoints = dictionary_create(DICTIONARY_FLAG_SINGLE_THREADED);
  105. }
  106. struct mount_point_metadata *m = dictionary_get(dict_mountpoints, mi->mount_point);
  107. if(unlikely(!m)) {
  108. char var_name[4096 + 1];
  109. snprintfz(var_name, 4096, "plugin:proc:diskspace:%s", mi->mount_point);
  110. int def_space = config_get_boolean_ondemand(CONFIG_SECTION_DISKSPACE, "space usage for all disks", CONFIG_BOOLEAN_AUTO);
  111. int def_inodes = config_get_boolean_ondemand(CONFIG_SECTION_DISKSPACE, "inodes usage for all disks", CONFIG_BOOLEAN_AUTO);
  112. if(unlikely(simple_pattern_matches(excluded_mountpoints, mi->mount_point))) {
  113. def_space = CONFIG_BOOLEAN_NO;
  114. def_inodes = CONFIG_BOOLEAN_NO;
  115. }
  116. if(unlikely(simple_pattern_matches(excluded_filesystems, mi->filesystem))) {
  117. def_space = CONFIG_BOOLEAN_NO;
  118. def_inodes = CONFIG_BOOLEAN_NO;
  119. }
  120. // check if the mount point is a directory #2407
  121. // but only when it is enabled by default #4491
  122. if(def_space != CONFIG_BOOLEAN_NO || def_inodes != CONFIG_BOOLEAN_NO) {
  123. struct stat bs;
  124. if(stat(mi->mount_point, &bs) == -1) {
  125. error("DISKSPACE: Cannot stat() mount point '%s' (disk '%s', filesystem '%s', root '%s')."
  126. , mi->mount_point
  127. , disk
  128. , mi->filesystem?mi->filesystem:""
  129. , mi->root?mi->root:""
  130. );
  131. def_space = CONFIG_BOOLEAN_NO;
  132. def_inodes = CONFIG_BOOLEAN_NO;
  133. }
  134. else {
  135. if((bs.st_mode & S_IFMT) != S_IFDIR) {
  136. error("DISKSPACE: Mount point '%s' (disk '%s', filesystem '%s', root '%s') is not a directory."
  137. , mi->mount_point
  138. , disk
  139. , mi->filesystem?mi->filesystem:""
  140. , mi->root?mi->root:""
  141. );
  142. def_space = CONFIG_BOOLEAN_NO;
  143. def_inodes = CONFIG_BOOLEAN_NO;
  144. }
  145. }
  146. }
  147. do_space = config_get_boolean_ondemand(var_name, "space usage", def_space);
  148. do_inodes = config_get_boolean_ondemand(var_name, "inodes usage", def_inodes);
  149. struct mount_point_metadata mp = {
  150. .do_space = do_space,
  151. .do_inodes = do_inodes,
  152. .shown_error = 0,
  153. .updated = 0,
  154. .collected = 0,
  155. .st_space = NULL,
  156. .rd_space_avail = NULL,
  157. .rd_space_used = NULL,
  158. .rd_space_reserved = NULL,
  159. .st_inodes = NULL,
  160. .rd_inodes_avail = NULL,
  161. .rd_inodes_used = NULL,
  162. .rd_inodes_reserved = NULL
  163. };
  164. m = dictionary_set(dict_mountpoints, mi->mount_point, &mp, sizeof(struct mount_point_metadata));
  165. }
  166. m->updated = 1;
  167. if(unlikely(m->do_space == CONFIG_BOOLEAN_NO && m->do_inodes == CONFIG_BOOLEAN_NO))
  168. return;
  169. if (unlikely(
  170. mi->flags & MOUNTINFO_READONLY &&
  171. !mount_point_is_protected(mi->mount_point) &&
  172. !m->collected &&
  173. m->do_space != CONFIG_BOOLEAN_YES &&
  174. m->do_inodes != CONFIG_BOOLEAN_YES))
  175. return;
  176. struct statvfs buff_statvfs;
  177. if (statvfs(mi->mount_point, &buff_statvfs) < 0) {
  178. if(!m->shown_error) {
  179. error("DISKSPACE: failed to statvfs() mount point '%s' (disk '%s', filesystem '%s', root '%s')"
  180. , mi->mount_point
  181. , disk
  182. , mi->filesystem?mi->filesystem:""
  183. , mi->root?mi->root:""
  184. );
  185. m->shown_error = 1;
  186. }
  187. return;
  188. }
  189. m->shown_error = 0;
  190. // logic found at get_fs_usage() in coreutils
  191. unsigned long bsize = (buff_statvfs.f_frsize) ? buff_statvfs.f_frsize : buff_statvfs.f_bsize;
  192. fsblkcnt_t bavail = buff_statvfs.f_bavail;
  193. fsblkcnt_t btotal = buff_statvfs.f_blocks;
  194. fsblkcnt_t bavail_root = buff_statvfs.f_bfree;
  195. fsblkcnt_t breserved_root = bavail_root - bavail;
  196. fsblkcnt_t bused;
  197. if(likely(btotal >= bavail_root))
  198. bused = btotal - bavail_root;
  199. else
  200. bused = bavail_root - btotal;
  201. #ifdef NETDATA_INTERNAL_CHECKS
  202. if(unlikely(btotal != bavail + breserved_root + bused))
  203. 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);
  204. #endif
  205. // --------------------------------------------------------------------------
  206. fsfilcnt_t favail = buff_statvfs.f_favail;
  207. fsfilcnt_t ftotal = buff_statvfs.f_files;
  208. fsfilcnt_t favail_root = buff_statvfs.f_ffree;
  209. fsfilcnt_t freserved_root = favail_root - favail;
  210. fsfilcnt_t fused = ftotal - favail_root;
  211. if(m->do_inodes == CONFIG_BOOLEAN_AUTO && favail == (fsfilcnt_t)-1) {
  212. // this file system does not support inodes reporting
  213. // eg. cephfs
  214. m->do_inodes = CONFIG_BOOLEAN_NO;
  215. }
  216. #ifdef NETDATA_INTERNAL_CHECKS
  217. if(unlikely(btotal != bavail + breserved_root + bused))
  218. 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);
  219. #endif
  220. // --------------------------------------------------------------------------
  221. int rendered = 0;
  222. if(m->do_space == CONFIG_BOOLEAN_YES || (m->do_space == CONFIG_BOOLEAN_AUTO &&
  223. (bavail || breserved_root || bused ||
  224. netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES))) {
  225. if(unlikely(!m->st_space)) {
  226. m->do_space = CONFIG_BOOLEAN_YES;
  227. m->st_space = rrdset_find_active_bytype_localhost("disk_space", disk);
  228. if(unlikely(!m->st_space)) {
  229. char title[4096 + 1];
  230. snprintfz(title, 4096, "Disk Space Usage");
  231. m->st_space = rrdset_create_localhost(
  232. "disk_space"
  233. , disk
  234. , NULL
  235. , family
  236. , "disk.space"
  237. , title
  238. , "GiB"
  239. , PLUGIN_DISKSPACE_NAME
  240. , NULL
  241. , NETDATA_CHART_PRIO_DISKSPACE_SPACE
  242. , update_every
  243. , RRDSET_TYPE_STACKED
  244. );
  245. }
  246. m->rd_space_avail = rrddim_add(m->st_space, "avail", NULL, (collected_number)bsize, 1024 * 1024 * 1024, RRD_ALGORITHM_ABSOLUTE);
  247. m->rd_space_used = rrddim_add(m->st_space, "used", NULL, (collected_number)bsize, 1024 * 1024 * 1024, RRD_ALGORITHM_ABSOLUTE);
  248. m->rd_space_reserved = rrddim_add(m->st_space, "reserved_for_root", "reserved for root", (collected_number)bsize, 1024 * 1024 * 1024, RRD_ALGORITHM_ABSOLUTE);
  249. }
  250. else
  251. rrdset_next(m->st_space);
  252. rrddim_set_by_pointer(m->st_space, m->rd_space_avail, (collected_number)bavail);
  253. rrddim_set_by_pointer(m->st_space, m->rd_space_used, (collected_number)bused);
  254. rrddim_set_by_pointer(m->st_space, m->rd_space_reserved, (collected_number)breserved_root);
  255. rrdset_done(m->st_space);
  256. rendered++;
  257. }
  258. // --------------------------------------------------------------------------
  259. if(m->do_inodes == CONFIG_BOOLEAN_YES || (m->do_inodes == CONFIG_BOOLEAN_AUTO &&
  260. (favail || freserved_root || fused ||
  261. netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES))) {
  262. if(unlikely(!m->st_inodes)) {
  263. m->do_inodes = CONFIG_BOOLEAN_YES;
  264. m->st_inodes = rrdset_find_active_bytype_localhost("disk_inodes", disk);
  265. if(unlikely(!m->st_inodes)) {
  266. char title[4096 + 1];
  267. snprintfz(title, 4096, "Disk Files (inodes) Usage");
  268. m->st_inodes = rrdset_create_localhost(
  269. "disk_inodes"
  270. , disk
  271. , NULL
  272. , family
  273. , "disk.inodes"
  274. , title
  275. , "inodes"
  276. , PLUGIN_DISKSPACE_NAME
  277. , NULL
  278. , NETDATA_CHART_PRIO_DISKSPACE_INODES
  279. , update_every
  280. , RRDSET_TYPE_STACKED
  281. );
  282. }
  283. m->rd_inodes_avail = rrddim_add(m->st_inodes, "avail", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
  284. m->rd_inodes_used = rrddim_add(m->st_inodes, "used", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
  285. m->rd_inodes_reserved = rrddim_add(m->st_inodes, "reserved_for_root", "reserved for root", 1, 1, RRD_ALGORITHM_ABSOLUTE);
  286. }
  287. else
  288. rrdset_next(m->st_inodes);
  289. rrddim_set_by_pointer(m->st_inodes, m->rd_inodes_avail, (collected_number)favail);
  290. rrddim_set_by_pointer(m->st_inodes, m->rd_inodes_used, (collected_number)fused);
  291. rrddim_set_by_pointer(m->st_inodes, m->rd_inodes_reserved, (collected_number)freserved_root);
  292. rrdset_done(m->st_inodes);
  293. rendered++;
  294. }
  295. // --------------------------------------------------------------------------
  296. if(likely(rendered))
  297. m->collected++;
  298. }
  299. static void diskspace_main_cleanup(void *ptr) {
  300. struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
  301. static_thread->enabled = NETDATA_MAIN_THREAD_EXITING;
  302. info("cleaning up...");
  303. static_thread->enabled = NETDATA_MAIN_THREAD_EXITED;
  304. }
  305. void *diskspace_main(void *ptr) {
  306. netdata_thread_cleanup_push(diskspace_main_cleanup, ptr);
  307. int vdo_cpu_netdata = config_get_boolean("plugin:proc", "netdata server resources", 1);
  308. cleanup_mount_points = config_get_boolean(CONFIG_SECTION_DISKSPACE, "remove charts of unmounted disks" , cleanup_mount_points);
  309. int update_every = (int)config_get_number(CONFIG_SECTION_DISKSPACE, "update every", localhost->rrd_update_every);
  310. if(update_every < localhost->rrd_update_every)
  311. update_every = localhost->rrd_update_every;
  312. check_for_new_mountpoints_every = (int)config_get_number(CONFIG_SECTION_DISKSPACE, "check for new mount points every", check_for_new_mountpoints_every);
  313. if(check_for_new_mountpoints_every < update_every)
  314. check_for_new_mountpoints_every = update_every;
  315. struct rusage thread;
  316. usec_t duration = 0;
  317. usec_t step = update_every * USEC_PER_SEC;
  318. heartbeat_t hb;
  319. heartbeat_init(&hb);
  320. while(!netdata_exit) {
  321. duration = heartbeat_monotonic_dt_to_now_usec(&hb);
  322. /* usec_t hb_dt = */ heartbeat_next(&hb, step);
  323. if(unlikely(netdata_exit)) break;
  324. // --------------------------------------------------------------------------
  325. // this is smart enough not to reload it every time
  326. mountinfo_reload(0);
  327. // --------------------------------------------------------------------------
  328. // disk space metrics
  329. struct mountinfo *mi;
  330. for(mi = disk_mountinfo_root; mi; mi = mi->next) {
  331. if(unlikely(mi->flags & (MOUNTINFO_IS_DUMMY | MOUNTINFO_IS_BIND)))
  332. continue;
  333. // exclude mounts made by ProtectHome and ProtectSystem systemd hardening options
  334. if(mi->flags & MOUNTINFO_READONLY && !strcmp(mi->root, mi->mount_point))
  335. continue;
  336. do_disk_space_stats(mi, update_every);
  337. if(unlikely(netdata_exit)) break;
  338. }
  339. if(unlikely(netdata_exit)) break;
  340. if(dict_mountpoints)
  341. dictionary_get_all(dict_mountpoints, mount_point_cleanup, NULL);
  342. if(vdo_cpu_netdata) {
  343. static RRDSET *stcpu_thread = NULL, *st_duration = NULL;
  344. static RRDDIM *rd_user = NULL, *rd_system = NULL, *rd_duration = NULL;
  345. // ----------------------------------------------------------------
  346. getrusage(RUSAGE_THREAD, &thread);
  347. if(unlikely(!stcpu_thread)) {
  348. stcpu_thread = rrdset_create_localhost(
  349. "netdata"
  350. , "plugin_diskspace"
  351. , NULL
  352. , "diskspace"
  353. , NULL
  354. , "Netdata Disk Space Plugin CPU usage"
  355. , "milliseconds/s"
  356. , PLUGIN_DISKSPACE_NAME
  357. , NULL
  358. , NETDATA_CHART_PRIO_NETDATA_DISKSPACE
  359. , update_every
  360. , RRDSET_TYPE_STACKED
  361. );
  362. rd_user = rrddim_add(stcpu_thread, "user", NULL, 1, 1000, RRD_ALGORITHM_INCREMENTAL);
  363. rd_system = rrddim_add(stcpu_thread, "system", NULL, 1, 1000, RRD_ALGORITHM_INCREMENTAL);
  364. }
  365. else
  366. rrdset_next(stcpu_thread);
  367. rrddim_set_by_pointer(stcpu_thread, rd_user, thread.ru_utime.tv_sec * 1000000ULL + thread.ru_utime.tv_usec);
  368. rrddim_set_by_pointer(stcpu_thread, rd_system, thread.ru_stime.tv_sec * 1000000ULL + thread.ru_stime.tv_usec);
  369. rrdset_done(stcpu_thread);
  370. // ----------------------------------------------------------------
  371. if(unlikely(!st_duration)) {
  372. st_duration = rrdset_create_localhost(
  373. "netdata"
  374. , "plugin_diskspace_dt"
  375. , NULL
  376. , "diskspace"
  377. , NULL
  378. , "Netdata Disk Space Plugin Duration"
  379. , "milliseconds/run"
  380. , PLUGIN_DISKSPACE_NAME
  381. , NULL
  382. , 132021
  383. , update_every
  384. , RRDSET_TYPE_AREA
  385. );
  386. rd_duration = rrddim_add(st_duration, "duration", NULL, 1, 1000, RRD_ALGORITHM_ABSOLUTE);
  387. }
  388. else
  389. rrdset_next(st_duration);
  390. rrddim_set_by_pointer(st_duration, rd_duration, duration);
  391. rrdset_done(st_duration);
  392. // ----------------------------------------------------------------
  393. if(unlikely(netdata_exit)) break;
  394. }
  395. }
  396. netdata_thread_cleanup_pop(1);
  397. return NULL;
  398. }