proc_spl_kstat_zfs.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "plugin_proc.h"
  3. #include "zfs_common.h"
  4. #define ZFS_PROC_ARCSTATS "/proc/spl/kstat/zfs/arcstats"
  5. #define ZFS_PROC_POOLS "/proc/spl/kstat/zfs"
  6. #define STATE_SIZE 9
  7. #define MAX_CHART_ID 256
  8. extern struct arcstats arcstats;
  9. int do_proc_spl_kstat_zfs_arcstats(int update_every, usec_t dt) {
  10. (void)dt;
  11. static int show_zero_charts = 0, do_zfs_stats = 0;
  12. static procfile *ff = NULL;
  13. static char *dirname = NULL;
  14. static ARL_BASE *arl_base = NULL;
  15. arcstats.l2exist = -1;
  16. if(unlikely(!arl_base)) {
  17. arl_base = arl_create("arcstats", NULL, 60);
  18. arl_expect(arl_base, "hits", &arcstats.hits);
  19. arl_expect(arl_base, "misses", &arcstats.misses);
  20. arl_expect(arl_base, "demand_data_hits", &arcstats.demand_data_hits);
  21. arl_expect(arl_base, "demand_data_misses", &arcstats.demand_data_misses);
  22. arl_expect(arl_base, "demand_metadata_hits", &arcstats.demand_metadata_hits);
  23. arl_expect(arl_base, "demand_metadata_misses", &arcstats.demand_metadata_misses);
  24. arl_expect(arl_base, "prefetch_data_hits", &arcstats.prefetch_data_hits);
  25. arl_expect(arl_base, "prefetch_data_misses", &arcstats.prefetch_data_misses);
  26. arl_expect(arl_base, "prefetch_metadata_hits", &arcstats.prefetch_metadata_hits);
  27. arl_expect(arl_base, "prefetch_metadata_misses", &arcstats.prefetch_metadata_misses);
  28. arl_expect(arl_base, "mru_hits", &arcstats.mru_hits);
  29. arl_expect(arl_base, "mru_ghost_hits", &arcstats.mru_ghost_hits);
  30. arl_expect(arl_base, "mfu_hits", &arcstats.mfu_hits);
  31. arl_expect(arl_base, "mfu_ghost_hits", &arcstats.mfu_ghost_hits);
  32. arl_expect(arl_base, "deleted", &arcstats.deleted);
  33. arl_expect(arl_base, "mutex_miss", &arcstats.mutex_miss);
  34. arl_expect(arl_base, "evict_skip", &arcstats.evict_skip);
  35. arl_expect(arl_base, "evict_not_enough", &arcstats.evict_not_enough);
  36. arl_expect(arl_base, "evict_l2_cached", &arcstats.evict_l2_cached);
  37. arl_expect(arl_base, "evict_l2_eligible", &arcstats.evict_l2_eligible);
  38. arl_expect(arl_base, "evict_l2_ineligible", &arcstats.evict_l2_ineligible);
  39. arl_expect(arl_base, "evict_l2_skip", &arcstats.evict_l2_skip);
  40. arl_expect(arl_base, "hash_elements", &arcstats.hash_elements);
  41. arl_expect(arl_base, "hash_elements_max", &arcstats.hash_elements_max);
  42. arl_expect(arl_base, "hash_collisions", &arcstats.hash_collisions);
  43. arl_expect(arl_base, "hash_chains", &arcstats.hash_chains);
  44. arl_expect(arl_base, "hash_chain_max", &arcstats.hash_chain_max);
  45. arl_expect(arl_base, "p", &arcstats.p);
  46. arl_expect(arl_base, "c", &arcstats.c);
  47. arl_expect(arl_base, "c_min", &arcstats.c_min);
  48. arl_expect(arl_base, "c_max", &arcstats.c_max);
  49. arl_expect(arl_base, "size", &arcstats.size);
  50. arl_expect(arl_base, "hdr_size", &arcstats.hdr_size);
  51. arl_expect(arl_base, "data_size", &arcstats.data_size);
  52. arl_expect(arl_base, "metadata_size", &arcstats.metadata_size);
  53. arl_expect(arl_base, "other_size", &arcstats.other_size);
  54. arl_expect(arl_base, "anon_size", &arcstats.anon_size);
  55. arl_expect(arl_base, "anon_evictable_data", &arcstats.anon_evictable_data);
  56. arl_expect(arl_base, "anon_evictable_metadata", &arcstats.anon_evictable_metadata);
  57. arl_expect(arl_base, "mru_size", &arcstats.mru_size);
  58. arl_expect(arl_base, "mru_evictable_data", &arcstats.mru_evictable_data);
  59. arl_expect(arl_base, "mru_evictable_metadata", &arcstats.mru_evictable_metadata);
  60. arl_expect(arl_base, "mru_ghost_size", &arcstats.mru_ghost_size);
  61. arl_expect(arl_base, "mru_ghost_evictable_data", &arcstats.mru_ghost_evictable_data);
  62. arl_expect(arl_base, "mru_ghost_evictable_metadata", &arcstats.mru_ghost_evictable_metadata);
  63. arl_expect(arl_base, "mfu_size", &arcstats.mfu_size);
  64. arl_expect(arl_base, "mfu_evictable_data", &arcstats.mfu_evictable_data);
  65. arl_expect(arl_base, "mfu_evictable_metadata", &arcstats.mfu_evictable_metadata);
  66. arl_expect(arl_base, "mfu_ghost_size", &arcstats.mfu_ghost_size);
  67. arl_expect(arl_base, "mfu_ghost_evictable_data", &arcstats.mfu_ghost_evictable_data);
  68. arl_expect(arl_base, "mfu_ghost_evictable_metadata", &arcstats.mfu_ghost_evictable_metadata);
  69. arl_expect(arl_base, "l2_hits", &arcstats.l2_hits);
  70. arl_expect(arl_base, "l2_misses", &arcstats.l2_misses);
  71. arl_expect(arl_base, "l2_feeds", &arcstats.l2_feeds);
  72. arl_expect(arl_base, "l2_rw_clash", &arcstats.l2_rw_clash);
  73. arl_expect(arl_base, "l2_read_bytes", &arcstats.l2_read_bytes);
  74. arl_expect(arl_base, "l2_write_bytes", &arcstats.l2_write_bytes);
  75. arl_expect(arl_base, "l2_writes_sent", &arcstats.l2_writes_sent);
  76. arl_expect(arl_base, "l2_writes_done", &arcstats.l2_writes_done);
  77. arl_expect(arl_base, "l2_writes_error", &arcstats.l2_writes_error);
  78. arl_expect(arl_base, "l2_writes_lock_retry", &arcstats.l2_writes_lock_retry);
  79. arl_expect(arl_base, "l2_evict_lock_retry", &arcstats.l2_evict_lock_retry);
  80. arl_expect(arl_base, "l2_evict_reading", &arcstats.l2_evict_reading);
  81. arl_expect(arl_base, "l2_evict_l1cached", &arcstats.l2_evict_l1cached);
  82. arl_expect(arl_base, "l2_free_on_write", &arcstats.l2_free_on_write);
  83. arl_expect(arl_base, "l2_cdata_free_on_write", &arcstats.l2_cdata_free_on_write);
  84. arl_expect(arl_base, "l2_abort_lowmem", &arcstats.l2_abort_lowmem);
  85. arl_expect(arl_base, "l2_cksum_bad", &arcstats.l2_cksum_bad);
  86. arl_expect(arl_base, "l2_io_error", &arcstats.l2_io_error);
  87. arl_expect(arl_base, "l2_size", &arcstats.l2_size);
  88. arl_expect(arl_base, "l2_asize", &arcstats.l2_asize);
  89. arl_expect(arl_base, "l2_hdr_size", &arcstats.l2_hdr_size);
  90. arl_expect(arl_base, "l2_compress_successes", &arcstats.l2_compress_successes);
  91. arl_expect(arl_base, "l2_compress_zeros", &arcstats.l2_compress_zeros);
  92. arl_expect(arl_base, "l2_compress_failures", &arcstats.l2_compress_failures);
  93. arl_expect(arl_base, "memory_throttle_count", &arcstats.memory_throttle_count);
  94. arl_expect(arl_base, "duplicate_buffers", &arcstats.duplicate_buffers);
  95. arl_expect(arl_base, "duplicate_buffers_size", &arcstats.duplicate_buffers_size);
  96. arl_expect(arl_base, "duplicate_reads", &arcstats.duplicate_reads);
  97. arl_expect(arl_base, "memory_direct_count", &arcstats.memory_direct_count);
  98. arl_expect(arl_base, "memory_indirect_count", &arcstats.memory_indirect_count);
  99. arl_expect(arl_base, "arc_no_grow", &arcstats.arc_no_grow);
  100. arl_expect(arl_base, "arc_tempreserve", &arcstats.arc_tempreserve);
  101. arl_expect(arl_base, "arc_loaned_bytes", &arcstats.arc_loaned_bytes);
  102. arl_expect(arl_base, "arc_prune", &arcstats.arc_prune);
  103. arl_expect(arl_base, "arc_meta_used", &arcstats.arc_meta_used);
  104. arl_expect(arl_base, "arc_meta_limit", &arcstats.arc_meta_limit);
  105. arl_expect(arl_base, "arc_meta_max", &arcstats.arc_meta_max);
  106. arl_expect(arl_base, "arc_meta_min", &arcstats.arc_meta_min);
  107. arl_expect(arl_base, "arc_need_free", &arcstats.arc_need_free);
  108. arl_expect(arl_base, "arc_sys_free", &arcstats.arc_sys_free);
  109. }
  110. if(unlikely(!ff)) {
  111. char filename[FILENAME_MAX + 1];
  112. snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, ZFS_PROC_ARCSTATS);
  113. ff = procfile_open(config_get("plugin:proc:" ZFS_PROC_ARCSTATS, "filename to monitor", filename), " \t:", PROCFILE_FLAG_DEFAULT);
  114. if(unlikely(!ff))
  115. return 1;
  116. snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/proc/spl/kstat/zfs");
  117. dirname = config_get("plugin:proc:" ZFS_PROC_ARCSTATS, "directory to monitor", filename);
  118. show_zero_charts = config_get_boolean_ondemand("plugin:proc:" ZFS_PROC_ARCSTATS, "show zero charts", CONFIG_BOOLEAN_NO);
  119. if(show_zero_charts == CONFIG_BOOLEAN_AUTO && netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES)
  120. show_zero_charts = CONFIG_BOOLEAN_YES;
  121. if(unlikely(show_zero_charts == CONFIG_BOOLEAN_YES))
  122. do_zfs_stats = 1;
  123. }
  124. // check if any pools exist
  125. if(likely(!do_zfs_stats)) {
  126. DIR *dir = opendir(dirname);
  127. if(unlikely(!dir)) {
  128. error("Cannot read directory '%s'", dirname);
  129. return 1;
  130. }
  131. struct dirent *de = NULL;
  132. while(likely(de = readdir(dir))) {
  133. if(likely(de->d_type == DT_DIR
  134. && (
  135. (de->d_name[0] == '.' && de->d_name[1] == '\0')
  136. || (de->d_name[0] == '.' && de->d_name[1] == '.' && de->d_name[2] == '\0')
  137. )))
  138. continue;
  139. if(unlikely(de->d_type == DT_LNK || de->d_type == DT_DIR)) {
  140. do_zfs_stats = 1;
  141. break;
  142. }
  143. }
  144. closedir(dir);
  145. }
  146. // do not show ZFS filesystem metrics if there haven't been any pools in the system yet
  147. if(unlikely(!do_zfs_stats))
  148. return 0;
  149. ff = procfile_readall(ff);
  150. if(unlikely(!ff))
  151. return 0; // we return 0, so that we will retry to open it next time
  152. size_t lines = procfile_lines(ff), l;
  153. arl_begin(arl_base);
  154. for(l = 0; l < lines ;l++) {
  155. size_t words = procfile_linewords(ff, l);
  156. if(unlikely(words < 3)) {
  157. if(unlikely(words)) error("Cannot read " ZFS_PROC_ARCSTATS " line %zu. Expected 3 params, read %zu.", l, words);
  158. continue;
  159. }
  160. const char *key = procfile_lineword(ff, l, 0);
  161. const char *value = procfile_lineword(ff, l, 2);
  162. if(unlikely(arcstats.l2exist == -1)) {
  163. if(key[0] == 'l' && key[1] == '2' && key[2] == '_')
  164. arcstats.l2exist = 1;
  165. }
  166. if(unlikely(arl_check(arl_base, key, value))) break;
  167. }
  168. if(unlikely(arcstats.l2exist == -1))
  169. arcstats.l2exist = 0;
  170. generate_charts_arcstats(PLUGIN_PROC_NAME, ZFS_PROC_ARCSTATS, show_zero_charts, update_every);
  171. generate_charts_arc_summary(PLUGIN_PROC_NAME, ZFS_PROC_ARCSTATS, show_zero_charts, update_every);
  172. return 0;
  173. }
  174. struct zfs_pool {
  175. RRDSET *st;
  176. RRDDIM *rd_online;
  177. RRDDIM *rd_degraded;
  178. RRDDIM *rd_faulted;
  179. RRDDIM *rd_offline;
  180. RRDDIM *rd_removed;
  181. RRDDIM *rd_unavail;
  182. int updated;
  183. int disabled;
  184. int online;
  185. int degraded;
  186. int faulted;
  187. int offline;
  188. int removed;
  189. int unavail;
  190. };
  191. struct deleted_zfs_pool {
  192. char *name;
  193. struct deleted_zfs_pool *next;
  194. } *deleted_zfs_pools = NULL;
  195. DICTIONARY *zfs_pools = NULL;
  196. void disable_zfs_pool_state(struct zfs_pool *pool)
  197. {
  198. if (pool->st)
  199. rrdset_is_obsolete(pool->st);
  200. pool->st = NULL;
  201. pool->rd_online = NULL;
  202. pool->rd_degraded = NULL;
  203. pool->rd_faulted = NULL;
  204. pool->rd_offline = NULL;
  205. pool->rd_removed = NULL;
  206. pool->rd_unavail = NULL;
  207. pool->disabled = 1;
  208. }
  209. int update_zfs_pool_state_chart(char *name, void *pool_p, void *update_every_p)
  210. {
  211. struct zfs_pool *pool = (struct zfs_pool *)pool_p;
  212. int update_every = *(int *)update_every_p;
  213. if (pool->updated) {
  214. pool->updated = 0;
  215. if (!pool->disabled) {
  216. if (unlikely(!pool->st)) {
  217. char chart_id[MAX_CHART_ID + 1];
  218. snprintf(chart_id, MAX_CHART_ID, "state_%s", name);
  219. pool->st = rrdset_create_localhost(
  220. "zfspool",
  221. chart_id,
  222. NULL,
  223. name,
  224. "zfspool.state",
  225. "ZFS pool state",
  226. "boolean",
  227. PLUGIN_PROC_NAME,
  228. ZFS_PROC_POOLS,
  229. NETDATA_CHART_PRIO_ZFS_POOL_STATE,
  230. update_every,
  231. RRDSET_TYPE_LINE);
  232. pool->rd_online = rrddim_add(pool->st, "online", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
  233. pool->rd_degraded = rrddim_add(pool->st, "degraded", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
  234. pool->rd_faulted = rrddim_add(pool->st, "faulted", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
  235. pool->rd_offline = rrddim_add(pool->st, "offline", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
  236. pool->rd_removed = rrddim_add(pool->st, "removed", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
  237. pool->rd_unavail = rrddim_add(pool->st, "unavail", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
  238. } else
  239. rrdset_next(pool->st);
  240. rrddim_set_by_pointer(pool->st, pool->rd_online, pool->online);
  241. rrddim_set_by_pointer(pool->st, pool->rd_degraded, pool->degraded);
  242. rrddim_set_by_pointer(pool->st, pool->rd_faulted, pool->faulted);
  243. rrddim_set_by_pointer(pool->st, pool->rd_offline, pool->offline);
  244. rrddim_set_by_pointer(pool->st, pool->rd_removed, pool->removed);
  245. rrddim_set_by_pointer(pool->st, pool->rd_unavail, pool->unavail);
  246. rrdset_done(pool->st);
  247. }
  248. } else {
  249. disable_zfs_pool_state(pool);
  250. struct deleted_zfs_pool *new = calloc(1, sizeof(struct deleted_zfs_pool));
  251. new->name = strdupz(name);
  252. new->next = deleted_zfs_pools;
  253. deleted_zfs_pools = new;
  254. }
  255. return 0;
  256. }
  257. int do_proc_spl_kstat_zfs_pool_state(int update_every, usec_t dt)
  258. {
  259. (void)dt;
  260. static int do_zfs_pool_state = -1;
  261. static char *dirname = NULL;
  262. int pool_found = 0, state_file_found = 0;
  263. if (unlikely(do_zfs_pool_state == -1)) {
  264. char filename[FILENAME_MAX + 1];
  265. snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/proc/spl/kstat/zfs");
  266. dirname = config_get("plugin:proc:" ZFS_PROC_POOLS, "directory to monitor", filename);
  267. zfs_pools = dictionary_create(DICTIONARY_FLAG_SINGLE_THREADED);
  268. do_zfs_pool_state = 1;
  269. }
  270. if (likely(do_zfs_pool_state)) {
  271. DIR *dir = opendir(dirname);
  272. if (unlikely(!dir)) {
  273. error("Cannot read directory '%s'", dirname);
  274. return 1;
  275. }
  276. struct dirent *de = NULL;
  277. while (likely(de = readdir(dir))) {
  278. if (likely(
  279. de->d_type == DT_DIR && ((de->d_name[0] == '.' && de->d_name[1] == '\0') ||
  280. (de->d_name[0] == '.' && de->d_name[1] == '.' && de->d_name[2] == '\0'))))
  281. continue;
  282. if (unlikely(de->d_type == DT_LNK || de->d_type == DT_DIR)) {
  283. pool_found = 1;
  284. struct zfs_pool *pool = dictionary_get(zfs_pools, de->d_name);
  285. if (unlikely(!pool)) {
  286. struct zfs_pool new_zfs_pool = {};
  287. pool = dictionary_set(zfs_pools, de->d_name, &new_zfs_pool, sizeof(struct zfs_pool));
  288. };
  289. pool->updated = 1;
  290. if (pool->disabled) {
  291. state_file_found = 1;
  292. continue;
  293. }
  294. pool->online = 0;
  295. pool->degraded = 0;
  296. pool->faulted = 0;
  297. pool->offline = 0;
  298. pool->removed = 0;
  299. pool->unavail = 0;
  300. char filename[FILENAME_MAX + 1];
  301. snprintfz(
  302. filename, FILENAME_MAX, "%s%s/%s/state", netdata_configured_host_prefix, dirname, de->d_name);
  303. char state[STATE_SIZE + 1];
  304. int ret = read_file(filename, state, STATE_SIZE);
  305. if (!ret) {
  306. state_file_found = 1;
  307. // ZFS pool states are described at https://openzfs.github.io/openzfs-docs/man/8/zpoolconcepts.8.html?#Device_Failure_and_Recovery
  308. if (!strcmp(state, "ONLINE\n")) {
  309. pool->online = 1;
  310. } else if (!strcmp(state, "DEGRADED\n")) {
  311. pool->degraded = 1;
  312. } else if (!strcmp(state, "FAULTED\n")) {
  313. pool->faulted = 1;
  314. } else if (!strcmp(state, "OFFLINE\n")) {
  315. pool->offline = 1;
  316. } else if (!strcmp(state, "REMOVED\n")) {
  317. pool->removed = 1;
  318. } else if (!strcmp(state, "UNAVAIL\n")) {
  319. pool->unavail = 1;
  320. } else {
  321. disable_zfs_pool_state(pool);
  322. char *c = strchr(state, '\n');
  323. if (c)
  324. *c = '\0';
  325. error("ZFS POOLS: Undefined state %s for zpool %s, disabling the chart", state, de->d_name);
  326. }
  327. }
  328. }
  329. }
  330. closedir(dir);
  331. }
  332. if (do_zfs_pool_state && pool_found && !state_file_found) {
  333. info("ZFS POOLS: State files not found. Disabling the module.");
  334. do_zfs_pool_state = 0;
  335. }
  336. if (do_zfs_pool_state)
  337. dictionary_get_all_name_value(zfs_pools, update_zfs_pool_state_chart, &update_every);
  338. while (deleted_zfs_pools) {
  339. struct deleted_zfs_pool *current_pool = deleted_zfs_pools;
  340. dictionary_del(zfs_pools, current_pool->name);
  341. deleted_zfs_pools = deleted_zfs_pools->next;
  342. freez(current_pool->name);
  343. freez(current_pool);
  344. }
  345. return 0;
  346. }