plugin_freebsd.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
  57. static_thread->enabled = NETDATA_MAIN_THREAD_EXITING;
  58. info("cleaning up...");
  59. static_thread->enabled = NETDATA_MAIN_THREAD_EXITED;
  60. }
  61. void *freebsd_main(void *ptr) {
  62. netdata_thread_cleanup_push(freebsd_main_cleanup, ptr);
  63. int vdo_cpu_netdata = config_get_boolean("plugin:freebsd", "netdata server resources", 1);
  64. // initialize FreeBSD plugin
  65. if (freebsd_plugin_init())
  66. netdata_cleanup_and_exit(1);
  67. // check the enabled status for each module
  68. int i;
  69. for(i = 0 ; freebsd_modules[i].name ;i++) {
  70. struct freebsd_module *pm = &freebsd_modules[i];
  71. pm->enabled = config_get_boolean("plugin:freebsd", pm->name, pm->enabled);
  72. pm->duration = 0ULL;
  73. pm->rd = NULL;
  74. }
  75. usec_t step = localhost->rrd_update_every * USEC_PER_SEC;
  76. heartbeat_t hb;
  77. heartbeat_init(&hb);
  78. while(!netdata_exit) {
  79. usec_t hb_dt = heartbeat_next(&hb, step);
  80. usec_t duration = 0ULL;
  81. if(unlikely(netdata_exit)) break;
  82. // BEGIN -- the job to be done
  83. for(i = 0 ; freebsd_modules[i].name ;i++) {
  84. struct freebsd_module *pm = &freebsd_modules[i];
  85. if(unlikely(!pm->enabled)) continue;
  86. debug(D_PROCNETDEV_LOOP, "FREEBSD calling %s.", pm->name);
  87. pm->enabled = !pm->func(localhost->rrd_update_every, hb_dt);
  88. pm->duration = heartbeat_monotonic_dt_to_now_usec(&hb) - duration;
  89. duration += pm->duration;
  90. if(unlikely(netdata_exit)) break;
  91. }
  92. // END -- the job is done
  93. // --------------------------------------------------------------------
  94. if(vdo_cpu_netdata) {
  95. static RRDSET *st = NULL;
  96. if(unlikely(!st)) {
  97. st = rrdset_find_bytype_localhost("netdata", "plugin_freebsd_modules");
  98. if(!st) {
  99. st = rrdset_create_localhost(
  100. "netdata"
  101. , "plugin_freebsd_modules"
  102. , NULL
  103. , "freebsd"
  104. , NULL
  105. , "NetData FreeBSD Plugin Modules Durations"
  106. , "milliseconds/run"
  107. , "netdata"
  108. , "stats"
  109. , 132001
  110. , localhost->rrd_update_every
  111. , RRDSET_TYPE_STACKED
  112. );
  113. for(i = 0 ; freebsd_modules[i].name ;i++) {
  114. struct freebsd_module *pm = &freebsd_modules[i];
  115. if(unlikely(!pm->enabled)) continue;
  116. pm->rd = rrddim_add(st, pm->dim, NULL, 1, 1000, RRD_ALGORITHM_ABSOLUTE);
  117. }
  118. }
  119. }
  120. else rrdset_next(st);
  121. for(i = 0 ; freebsd_modules[i].name ;i++) {
  122. struct freebsd_module *pm = &freebsd_modules[i];
  123. if(unlikely(!pm->enabled)) continue;
  124. rrddim_set_by_pointer(st, pm->rd, pm->duration);
  125. }
  126. rrdset_done(st);
  127. global_statistics_charts();
  128. registry_statistics();
  129. }
  130. }
  131. netdata_thread_cleanup_pop(1);
  132. return NULL;
  133. }