plugin_diskspace.c 18 KB

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