]> arthur.barton.de Git - netdata.git/blob - src/plugin_freebsd.c
Separate getmntinfo module with simple patterns adopted
[netdata.git] / src / plugin_freebsd.c
1 #include "common.h"
2
3 static struct freebsd_module {
4     const char *name;
5     const char *dim;
6
7     int enabled;
8
9     int (*func)(int update_every, usec_t dt);
10     usec_t duration;
11
12     RRDDIM *rd;
13
14 } freebsd_modules[] = {
15
16         { .name = "freebsd_old", .dim = "freebsd_old", .enabled = 1, .func = do_freebsd_sysctl_old },
17
18         // system metrics
19         { .name = "vm.loadavg",   .dim = "loadavg", .enabled = 1, .func = do_vm_loadavg },
20         { .name = "vm.vmtotal",   .dim = "vmtotal", .enabled = 1, .func = do_vm_vmtotal },
21         { .name = "kern.cp_time", .dim = "cp_time", .enabled = 1, .func = do_kern_cp_time },
22         { .name = "hw.intrcnt",   .dim = "hw_intr", .enabled = 1, .func = do_hw_intcnt },
23         { .name = "vm.stats.sys.v_intr",   .dim = "dev_intr",      .enabled = 1, .func = do_vm_stats_sys_v_intr },
24         { .name = "vm.stats.sys.v_soft",   .dim = "soft_intr",     .enabled = 1, .func = do_vm_stats_sys_v_soft },
25         { .name = "vm.stats.sys.v_swtch",  .dim = "context_swtch", .enabled = 1, .func = do_vm_stats_sys_v_swtch },
26         { .name = "vm.stats.vm.v_forks",   .dim = "forks",         .enabled = 1, .func = do_vm_stats_sys_v_forks },
27         { .name = "vm.swap_info", .dim = "swap",       .enabled = 1, .func = do_vm_swap_info },
28         { .name = "system.ram",   .dim = "system_ram", .enabled = 1, .func = do_system_ram },
29         { .name = "vm.stats.vm.v_swappgs", .dim = "swap_io",       .enabled = 1, .func = do_vm_stats_sys_v_swappgs },
30         { .name = "kern.ipc.sem", .dim = "semaphores",     .enabled = 1, .func = do_kern_ipc_sem },
31         { .name = "kern.ipc.shm", .dim = "shared_memory",  .enabled = 1, .func = do_kern_ipc_shm },
32         { .name = "kern.ipc.msq", .dim = "message_queues", .enabled = 1, .func = do_kern_ipc_msq },
33         { .name = "uptime",       .dim = "uptime",         .enabled = 1, .func = do_uptime },
34         { .name = "net.isr",      .dim = "net_isr",        .enabled = 1, .func = do_net_isr },
35
36         // CPU metrics
37         { .name = "kern.cp_times", .dim = "cp_times", .enabled = 1, .func = do_kern_cp_times },
38
39         // memory metrics
40         { .name = "vm.stats.vm.v_pgfaults", .dim = "pgfaults", .enabled = 1, .func = do_vm_stats_sys_v_pgfaults },
41
42         // network metrics
43         { .name = "net.inet.tcp.states",   .dim = "tcp_states",  .enabled = 1, .func = do_net_inet_tcp_states },
44         { .name = "net.inet.tcp.stats",    .dim = "tcp_stats",   .enabled = 1, .func = do_net_inet_tcp_stats },
45         { .name = "net.inet.udp.stats",    .dim = "udp_stats",   .enabled = 1, .func = do_net_inet_udp_stats },
46         { .name = "net.inet.icmp.stats",   .dim = "icmp_stats",  .enabled = 1, .func = do_net_inet_icmp_stats },
47         { .name = "net.inet.ip.stats",     .dim = "ip_stats",    .enabled = 1, .func = do_net_inet_ip_stats },
48         { .name = "net.inet6.ip6.stats",   .dim = "ip6_stats",   .enabled = 1, .func = do_net_inet6_ip6_stats },
49         { .name = "net.inet6.icmp6.stats", .dim = "icmp6_stats", .enabled = 1, .func = do_net_inet6_icmp6_stats },
50
51         // disk metrics
52         { .name = "getmntinfo", .dim = "getmntinfo", .enabled = 1, .func = do_getmntinfo },
53
54         // the terminator of this array
55         { .name = NULL, .dim = NULL, .enabled = 0, .func = NULL }
56 };
57
58 void *freebsd_main(void *ptr) {
59     struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
60
61     info("FREEBSD Plugin thread created with task id %d", gettid());
62
63     if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
64         error("Cannot set pthread cancel type to DEFERRED.");
65
66     if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
67         error("Cannot set pthread cancel state to ENABLE.");
68
69     int vdo_cpu_netdata = config_get_boolean("plugin:freebsd", "netdata server resources", 1);
70
71     // initialize FreeBSD plugin
72     if (freebsd_plugin_init())
73         netdata_exit = 1;
74
75     // check the enabled status for each module
76     int i;
77     for(i = 0 ; freebsd_modules[i].name ;i++) {
78         struct freebsd_module *pm = &freebsd_modules[i];
79
80         pm->enabled = config_get_boolean("plugin:freebsd", pm->name, pm->enabled);
81         pm->duration = 0ULL;
82         pm->rd = NULL;
83     }
84
85     usec_t step = localhost->rrd_update_every * USEC_PER_SEC;
86     heartbeat_t hb;
87     heartbeat_init(&hb);
88
89     for(;;) {
90         usec_t hb_dt = heartbeat_next(&hb, step);
91         usec_t duration = 0ULL;
92
93         if(unlikely(netdata_exit)) break;
94
95         // BEGIN -- the job to be done
96
97         for(i = 0 ; freebsd_modules[i].name ;i++) {
98             struct freebsd_module *pm = &freebsd_modules[i];
99             if(unlikely(!pm->enabled)) continue;
100
101             debug(D_PROCNETDEV_LOOP, "FREEBSD calling %s.", pm->name);
102
103             pm->enabled = !pm->func(localhost->rrd_update_every, hb_dt);
104             pm->duration = heartbeat_dt_usec(&hb) - duration;
105             duration += pm->duration;
106
107             if(unlikely(netdata_exit)) break;
108         }
109
110         // END -- the job is done
111
112         // --------------------------------------------------------------------
113
114         if(vdo_cpu_netdata) {
115             static RRDSET *st = NULL;
116
117             if(unlikely(!st)) {
118                 st = rrdset_find_bytype_localhost("netdata", "plugin_freebsd_modules");
119
120                 if(!st) {
121                     st = rrdset_create_localhost("netdata", "plugin_freebsd_modules", NULL, "freebsd", NULL
122                     , "NetData FreeBSD Plugin Modules Durations", "milliseconds/run", 132001
123                     , localhost->rrd_update_every, RRDSET_TYPE_STACKED);
124
125                     for(i = 0 ; freebsd_modules[i].name ;i++) {
126                         struct freebsd_module *pm = &freebsd_modules[i];
127                         if(unlikely(!pm->enabled)) continue;
128
129                         pm->rd = rrddim_add(st, pm->dim, NULL, 1, 1000, RRD_ALGORITHM_ABSOLUTE);
130                     }
131                 }
132             }
133             else rrdset_next(st);
134
135             for(i = 0 ; freebsd_modules[i].name ;i++) {
136                 struct freebsd_module *pm = &freebsd_modules[i];
137                 if(unlikely(!pm->enabled)) continue;
138
139                 rrddim_set_by_pointer(st, pm->rd, pm->duration);
140             }
141             rrdset_done(st);
142
143             global_statistics_charts();
144             registry_statistics();
145         }
146     }
147
148     info("FREEBSD thread exiting");
149
150     static_thread->enabled = 0;
151     pthread_exit(NULL);
152     return NULL;
153 }