plugin_freebsd.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "plugin_freebsd.h"
  3. static struct freebsd_module {
  4. const char *name;
  5. const char *dim;
  6. int enabled;
  7. int (*func)(int update_every, usec_t dt);
  8. usec_t duration;
  9. RRDDIM *rd;
  10. } freebsd_modules[] = {
  11. // system metrics
  12. {.name = "kern.cp_time", .dim = "cp_time", .enabled = 1, .func = do_kern_cp_time},
  13. {.name = "vm.loadavg", .dim = "loadavg", .enabled = 1, .func = do_vm_loadavg},
  14. {.name = "system.ram", .dim = "system_ram", .enabled = 1, .func = do_system_ram},
  15. {.name = "vm.swap_info", .dim = "swap", .enabled = 1, .func = do_vm_swap_info},
  16. {.name = "vm.stats.vm.v_swappgs", .dim = "swap_io", .enabled = 1, .func = do_vm_stats_sys_v_swappgs},
  17. {.name = "vm.vmtotal", .dim = "vmtotal", .enabled = 1, .func = do_vm_vmtotal},
  18. {.name = "vm.stats.vm.v_forks", .dim = "forks", .enabled = 1, .func = do_vm_stats_sys_v_forks},
  19. {.name = "vm.stats.sys.v_swtch", .dim = "context_swtch", .enabled = 1, .func = do_vm_stats_sys_v_swtch},
  20. {.name = "hw.intrcnt", .dim = "hw_intr", .enabled = 1, .func = do_hw_intcnt},
  21. {.name = "vm.stats.sys.v_intr", .dim = "dev_intr", .enabled = 1, .func = do_vm_stats_sys_v_intr},
  22. {.name = "vm.stats.sys.v_soft", .dim = "soft_intr", .enabled = 1, .func = do_vm_stats_sys_v_soft},
  23. {.name = "net.isr", .dim = "net_isr", .enabled = 1, .func = do_net_isr},
  24. {.name = "kern.ipc.sem", .dim = "semaphores", .enabled = 1, .func = do_kern_ipc_sem},
  25. {.name = "kern.ipc.shm", .dim = "shared_memory", .enabled = 1, .func = do_kern_ipc_shm},
  26. {.name = "kern.ipc.msq", .dim = "message_queues", .enabled = 1, .func = do_kern_ipc_msq},
  27. {.name = "uptime", .dim = "uptime", .enabled = 1, .func = do_uptime},
  28. // memory metrics
  29. {.name = "vm.stats.vm.v_pgfaults", .dim = "pgfaults", .enabled = 1, .func = do_vm_stats_sys_v_pgfaults},
  30. // CPU metrics
  31. {.name = "kern.cp_times", .dim = "cp_times", .enabled = 1, .func = do_kern_cp_times},
  32. {.name = "dev.cpu.temperature", .dim = "cpu_temperature", .enabled = 1, .func = do_dev_cpu_temperature},
  33. {.name = "dev.cpu.0.freq", .dim = "cpu_frequency", .enabled = 1, .func = do_dev_cpu_0_freq},
  34. // disk metrics
  35. {.name = "kern.devstat", .dim = "kern_devstat", .enabled = 1, .func = do_kern_devstat},
  36. {.name = "getmntinfo", .dim = "getmntinfo", .enabled = 1, .func = do_getmntinfo},
  37. // network metrics
  38. {.name = "net.inet.tcp.states", .dim = "tcp_states", .enabled = 1, .func = do_net_inet_tcp_states},
  39. {.name = "net.inet.tcp.stats", .dim = "tcp_stats", .enabled = 1, .func = do_net_inet_tcp_stats},
  40. {.name = "net.inet.udp.stats", .dim = "udp_stats", .enabled = 1, .func = do_net_inet_udp_stats},
  41. {.name = "net.inet.icmp.stats", .dim = "icmp_stats", .enabled = 1, .func = do_net_inet_icmp_stats},
  42. {.name = "net.inet.ip.stats", .dim = "ip_stats", .enabled = 1, .func = do_net_inet_ip_stats},
  43. {.name = "net.inet6.ip6.stats", .dim = "ip6_stats", .enabled = 1, .func = do_net_inet6_ip6_stats},
  44. {.name = "net.inet6.icmp6.stats", .dim = "icmp6_stats", .enabled = 1, .func = do_net_inet6_icmp6_stats},
  45. // network interfaces metrics
  46. {.name = "getifaddrs", .dim = "getifaddrs", .enabled = 1, .func = do_getifaddrs},
  47. // ZFS metrics
  48. {.name = "kstat.zfs.misc.arcstats", .dim = "arcstats", .enabled = 1, .func = do_kstat_zfs_misc_arcstats},
  49. {.name = "kstat.zfs.misc.zio_trim", .dim = "trim", .enabled = 1, .func = do_kstat_zfs_misc_zio_trim},
  50. // ipfw metrics
  51. {.name = "ipfw", .dim = "ipfw", .enabled = 1, .func = do_ipfw},
  52. // the terminator of this array
  53. {.name = NULL, .dim = NULL, .enabled = 0, .func = NULL}
  54. };
  55. static void freebsd_main_cleanup(void *ptr)
  56. {
  57. struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
  58. static_thread->enabled = NETDATA_MAIN_THREAD_EXITING;
  59. info("cleaning up...");
  60. static_thread->enabled = NETDATA_MAIN_THREAD_EXITED;
  61. }
  62. void *freebsd_main(void *ptr)
  63. {
  64. netdata_thread_cleanup_push(freebsd_main_cleanup, ptr);
  65. int vdo_cpu_netdata = config_get_boolean("plugin:freebsd", "netdata server resources", 1);
  66. // initialize FreeBSD plugin
  67. if (freebsd_plugin_init())
  68. netdata_cleanup_and_exit(1);
  69. // check the enabled status for each module
  70. int i;
  71. for (i = 0; freebsd_modules[i].name; i++) {
  72. struct freebsd_module *pm = &freebsd_modules[i];
  73. pm->enabled = config_get_boolean("plugin:freebsd", pm->name, pm->enabled);
  74. pm->duration = 0ULL;
  75. pm->rd = NULL;
  76. }
  77. usec_t step = localhost->rrd_update_every * USEC_PER_SEC;
  78. heartbeat_t hb;
  79. heartbeat_init(&hb);
  80. while (!netdata_exit) {
  81. usec_t hb_dt = heartbeat_next(&hb, step);
  82. usec_t duration = 0ULL;
  83. if (unlikely(netdata_exit))
  84. break;
  85. // BEGIN -- the job to be done
  86. for (i = 0; freebsd_modules[i].name; i++) {
  87. struct freebsd_module *pm = &freebsd_modules[i];
  88. if (unlikely(!pm->enabled))
  89. continue;
  90. debug(D_PROCNETDEV_LOOP, "FREEBSD calling %s.", pm->name);
  91. pm->enabled = !pm->func(localhost->rrd_update_every, hb_dt);
  92. pm->duration = heartbeat_monotonic_dt_to_now_usec(&hb) - duration;
  93. duration += pm->duration;
  94. if (unlikely(netdata_exit))
  95. break;
  96. }
  97. // END -- the job is done
  98. if (vdo_cpu_netdata) {
  99. static RRDSET *st_cpu_thread = NULL, *st_duration = NULL;
  100. static RRDDIM *rd_user = NULL, *rd_system = NULL;
  101. // ----------------------------------------------------------------
  102. struct rusage thread;
  103. getrusage(RUSAGE_THREAD, &thread);
  104. if (unlikely(!st_cpu_thread)) {
  105. st_cpu_thread = rrdset_create_localhost(
  106. "netdata",
  107. "plugin_freebsd_cpu",
  108. NULL,
  109. "freebsd",
  110. NULL,
  111. "Netdata FreeBSD plugin CPU usage",
  112. "milliseconds/s",
  113. "freebsd.plugin",
  114. "stats",
  115. 132000,
  116. localhost->rrd_update_every,
  117. RRDSET_TYPE_STACKED);
  118. rd_user = rrddim_add(st_cpu_thread, "user", NULL, 1, USEC_PER_MS, RRD_ALGORITHM_INCREMENTAL);
  119. rd_system = rrddim_add(st_cpu_thread, "system", NULL, 1, USEC_PER_MS, RRD_ALGORITHM_INCREMENTAL);
  120. } else {
  121. rrdset_next(st_cpu_thread);
  122. }
  123. rrddim_set_by_pointer(
  124. st_cpu_thread, rd_user, thread.ru_utime.tv_sec * USEC_PER_SEC + thread.ru_utime.tv_usec);
  125. rrddim_set_by_pointer(
  126. st_cpu_thread, rd_system, thread.ru_stime.tv_sec * USEC_PER_SEC + thread.ru_stime.tv_usec);
  127. rrdset_done(st_cpu_thread);
  128. // ----------------------------------------------------------------
  129. if (unlikely(!st_duration)) {
  130. st_duration = rrdset_find_active_bytype_localhost("netdata", "plugin_freebsd_modules");
  131. if (!st_duration) {
  132. st_duration = rrdset_create_localhost(
  133. "netdata",
  134. "plugin_freebsd_modules",
  135. NULL,
  136. "freebsd",
  137. NULL,
  138. "Netdata FreeBSD plugin modules durations",
  139. "milliseconds/run",
  140. "freebsd.plugin",
  141. "stats",
  142. 132001,
  143. localhost->rrd_update_every,
  144. RRDSET_TYPE_STACKED);
  145. for (i = 0; freebsd_modules[i].name; i++) {
  146. struct freebsd_module *pm = &freebsd_modules[i];
  147. if (unlikely(!pm->enabled))
  148. continue;
  149. pm->rd = rrddim_add(st_duration, pm->dim, NULL, 1, 1000, RRD_ALGORITHM_ABSOLUTE);
  150. }
  151. }
  152. } else
  153. rrdset_next(st_duration);
  154. for (i = 0; freebsd_modules[i].name; i++) {
  155. struct freebsd_module *pm = &freebsd_modules[i];
  156. if (unlikely(!pm->enabled))
  157. continue;
  158. rrddim_set_by_pointer(st_duration, pm->rd, pm->duration);
  159. }
  160. rrdset_done(st_duration);
  161. }
  162. }
  163. netdata_thread_cleanup_pop(1);
  164. return NULL;
  165. }