]> arthur.barton.de Git - netdata.git/blob - src/freebsd_sysctl.c
Separate net.inet6.ip6.stats module
[netdata.git] / src / freebsd_sysctl.c
1 #include "common.h"
2
3 #include <sys/vmmeter.h>
4 #include <sys/devicestat.h>
5 #include <sys/mount.h>
6 #include <vm/vm_param.h>
7
8 #define _KERNEL
9 #include <sys/sem.h>
10 #include <sys/shm.h>
11 #include <sys/msg.h>
12 #undef _KERNEL
13
14 #include <net/netisr.h>
15 #include <net/if.h>
16 #include <ifaddrs.h>
17
18 #include <netinet/ip.h>
19 #include <netinet/ip_var.h>
20 #include <netinet/ip_icmp.h>
21 #include <netinet/icmp_var.h>
22 #include <netinet6/ip6_var.h>
23 #include <netinet/icmp6.h>
24 #include <netinet/tcp_var.h>
25 #include <netinet/tcp_fsm.h>
26 #include <netinet/udp.h>
27 #include <netinet/udp_var.h>
28
29 // --------------------------------------------------------------------------------------------------------------------
30 // common definitions and variables
31
32 #define KILO_FACTOR 1024
33 #define MEGA_FACTOR 1048576     // 1024 * 1024
34 #define GIGA_FACTOR 1073741824  // 1024 * 1024 * 1024
35
36 #define MAX_INT_DIGITS 10 // maximum number of digits for int
37
38 int system_pagesize = PAGE_SIZE;
39 int number_of_cpus = 1;
40
41 // --------------------------------------------------------------------------------------------------------------------
42 // FreeBSD plugin initialization
43
44 int freebsd_plugin_init()
45 {
46     system_pagesize = getpagesize();
47     if (system_pagesize <= 0) {
48         error("FREEBSD: can't get system page size");
49         return 1;
50     }
51
52     if (unlikely(GETSYSCTL_BY_NAME("kern.smp.cpus", number_of_cpus))) {
53         error("FREEBSD: can't get number of cpus");
54         return 1;
55     }
56
57     if (unlikely(!number_of_cpus)) {
58         error("FREEBSD: wrong number of cpus");
59         return 1;
60     }
61
62     return 0;
63 }
64
65 // --------------------------------------------------------------------------------------------------------------------
66 // vm.loadavg
67
68 // FreeBSD calculates load averages once every 5 seconds
69 #define MIN_LOADAVG_UPDATE_EVERY 5
70
71 int do_vm_loadavg(int update_every, usec_t dt){
72     static usec_t next_loadavg_dt = 0;
73
74     if (next_loadavg_dt <= dt) {
75         static int mib[2] = {0, 0};
76         struct loadavg sysload;
77
78         if (unlikely(GETSYSCTL_SIMPLE("vm.loadavg", mib, sysload))) {
79             error("DISABLED: system.load chart");
80             error("DISABLED: vm.loadavg module");
81             return 1;
82         } else {
83
84             // --------------------------------------------------------------------
85
86             static RRDSET *st = NULL;
87             static RRDDIM *rd_load1 = NULL, *rd_load2 = NULL, *rd_load3 = NULL;
88
89             if (unlikely(!st)) {
90                 st = rrdset_create_localhost("system",
91                                              "load",
92                                              NULL,
93                                              "load",
94                                              NULL,
95                                              "System Load Average",
96                                              "load",
97                                              100,
98                                              (update_every < MIN_LOADAVG_UPDATE_EVERY) ?
99                                              MIN_LOADAVG_UPDATE_EVERY : update_every, RRDSET_TYPE_LINE
100                 );
101                 rd_load1 = rrddim_add(st, "load1", NULL, 1, 1000, RRD_ALGORITHM_ABSOLUTE);
102                 rd_load2 = rrddim_add(st, "load5", NULL, 1, 1000, RRD_ALGORITHM_ABSOLUTE);
103                 rd_load3 = rrddim_add(st, "load15", NULL, 1, 1000, RRD_ALGORITHM_ABSOLUTE);
104             } else
105                 rrdset_next(st);
106
107             rrddim_set_by_pointer(st, rd_load1, (collected_number) ((double) sysload.ldavg[0] / sysload.fscale * 1000));
108             rrddim_set_by_pointer(st, rd_load2, (collected_number) ((double) sysload.ldavg[1] / sysload.fscale * 1000));
109             rrddim_set_by_pointer(st, rd_load3, (collected_number) ((double) sysload.ldavg[2] / sysload.fscale * 1000));
110             rrdset_done(st);
111
112             next_loadavg_dt = st->update_every * USEC_PER_SEC;
113         }
114     }
115     else
116         next_loadavg_dt -= dt;
117
118     return 0;
119 }
120
121 // --------------------------------------------------------------------------------------------------------------------
122 // vm.vmtotal
123
124 int do_vm_vmtotal(int update_every, usec_t dt) {
125     static int do_all_processes = -1, do_processes = -1, do_committed = -1;
126
127     if (unlikely(do_all_processes == -1)) {
128         do_all_processes    = config_get_boolean("plugin:freebsd:vm.vmtotal", "enable total processes", 1);
129         do_processes        = config_get_boolean("plugin:freebsd:vm.vmtotal", "processes running", 1);
130         do_committed        = config_get_boolean("plugin:freebsd:vm.vmtotal", "committed memory", 1);
131     }
132
133     if (likely(do_all_processes | do_processes | do_committed)) {
134         static int mib[2] = {0, 0};
135         struct vmtotal vmtotal_data;
136
137         if (unlikely(GETSYSCTL_SIMPLE("vm.vmtotal", mib, vmtotal_data))) {
138             do_all_processes = 0;
139             error("DISABLED: system.active_processes chart");
140             do_processes = 0;
141             error("DISABLED: system.processes chart");
142             do_committed = 0;
143             error("DISABLED: mem.committed chart");
144             error("DISABLED: vm.vmtotal module");
145             return 1;
146         } else {
147
148             // --------------------------------------------------------------------
149
150             if (likely(do_all_processes)) {
151                 static RRDSET *st = NULL;
152                 static RRDDIM *rd = NULL;
153
154                 if (unlikely(!st)) {
155                     st = rrdset_create_localhost("system",
156                                                  "active_processes",
157                                                  NULL,
158                                                  "processes",
159                                                  NULL,
160                                                  "System Active Processes",
161                                                  "processes",
162                                                  750,
163                                                  update_every,
164                                                  RRDSET_TYPE_LINE
165                     );
166                     rd = rrddim_add(st, "active", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
167                 }
168                 else rrdset_next(st);
169
170                 rrddim_set_by_pointer(st, rd, (vmtotal_data.t_rq + vmtotal_data.t_dw + vmtotal_data.t_pw + vmtotal_data.t_sl + vmtotal_data.t_sw));
171                 rrdset_done(st);
172             }
173
174             // --------------------------------------------------------------------
175
176             if (likely(do_processes)) {
177                 static RRDSET *st = NULL;
178                 static RRDDIM *rd_running = NULL, *rd_blocked = NULL;
179
180                 st = rrdset_find_bytype_localhost("system", "processes");
181                 if (unlikely(!st)) {
182                     st = rrdset_create_localhost("system",
183                                                  "processes",
184                                                  NULL,
185                                                  "processes",
186                                                  NULL,
187                                                  "System Processes",
188                                                  "processes",
189                                                  600,
190                                                  update_every,
191                                                  RRDSET_TYPE_LINE
192                     );
193
194                     rd_running = rrddim_add(st, "running", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
195                     rd_blocked = rrddim_add(st, "blocked", NULL, -1, 1, RRD_ALGORITHM_ABSOLUTE);
196                 }
197                 else rrdset_next(st);
198
199                 rrddim_set_by_pointer(st, rd_running, vmtotal_data.t_rq);
200                 rrddim_set_by_pointer(st, rd_blocked, (vmtotal_data.t_dw + vmtotal_data.t_pw));
201                 rrdset_done(st);
202             }
203
204             // --------------------------------------------------------------------
205
206             if (likely(do_committed)) {
207                 static RRDSET *st = NULL;
208                 static RRDDIM *rd = NULL;
209
210                 if (unlikely(!st)) {
211                     st = rrdset_create_localhost("mem",
212                                                  "committed",
213                                                  NULL,
214                                                  "system",
215                                                  NULL,
216                                                  "Committed (Allocated) Memory",
217                                                  "MB",
218                                                  5000,
219                                                  update_every,
220                                                  RRDSET_TYPE_AREA
221                     );
222                     rrdset_flag_set(st, RRDSET_FLAG_DETAIL);
223
224                     rd = rrddim_add(st, "Committed_AS", NULL, system_pagesize, MEGA_FACTOR, RRD_ALGORITHM_ABSOLUTE);
225                 }
226                 else rrdset_next(st);
227
228                 rrddim_set_by_pointer(st, rd, vmtotal_data.t_rm);
229                 rrdset_done(st);
230             }
231         }
232     } else {
233         error("DISABLED: vm.vmtotal module");
234         return 1;
235     }
236
237     return 0;
238 }
239
240 // --------------------------------------------------------------------------------------------------------------------
241 // kern.cp_time
242
243 int do_kern_cp_time(int update_every, usec_t dt) {
244     if (unlikely(CPUSTATES != 5)) {
245         error("FREEBSD: There are %d CPU states (5 was expected)", CPUSTATES);
246         error("DISABLED: system.cpu chart");
247         error("DISABLED: kern.cp_time module");
248         return 1;
249     } else {
250         static int mib[2] = {0, 0};
251         long cp_time[CPUSTATES];
252
253         if (unlikely(GETSYSCTL_SIMPLE("kern.cp_time", mib, cp_time))) {
254             error("DISABLED: system.cpu chart");
255             error("DISABLED: kern.cp_time module");
256             return 1;
257         } else {
258
259             // --------------------------------------------------------------------
260
261             static RRDSET *st = NULL;
262             static RRDDIM *rd_nice = NULL, *rd_system = NULL, *rd_user = NULL, *rd_interrupt = NULL, *rd_idle = NULL;
263
264             if (unlikely(!st)) {
265                 st = rrdset_create_localhost("system",
266                                              "cpu",
267                                              NULL,
268                                              "cpu",
269                                              "system.cpu",
270                                              "Total CPU utilization",
271                                              "percentage",
272                                              100, update_every,
273                                              RRDSET_TYPE_STACKED
274                 );
275
276                 rd_nice         = rrddim_add(st, "nice", NULL, 1, 1, RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
277                 rd_system       = rrddim_add(st, "system", NULL, 1, 1, RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
278                 rd_user         = rrddim_add(st, "user", NULL, 1, 1, RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
279                 rd_interrupt    = rrddim_add(st, "interrupt", NULL, 1, 1, RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
280                 rd_idle         = rrddim_add(st, "idle", NULL, 1, 1, RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
281                 rrddim_hide(st, "idle");
282             }
283             else rrdset_next(st);
284
285             rrddim_set_by_pointer(st, rd_nice, cp_time[1]);
286             rrddim_set_by_pointer(st, rd_system, cp_time[2]);
287             rrddim_set_by_pointer(st, rd_user, cp_time[0]);
288             rrddim_set_by_pointer(st, rd_interrupt, cp_time[3]);
289             rrddim_set_by_pointer(st, rd_idle, cp_time[4]);
290             rrdset_done(st);
291         }
292     }
293
294     return 0;
295 }
296
297 // --------------------------------------------------------------------------------------------------------------------
298 // kern.cp_times
299
300 int do_kern_cp_times(int update_every, usec_t dt) {
301     if (unlikely(CPUSTATES != 5)) {
302         error("FREEBSD: There are %d CPU states (5 was expected)", CPUSTATES);
303         error("DISABLED: cpu.cpuXX charts");
304         error("DISABLED: kern.cp_times module");
305         return 1;
306     } else {
307         static int mib[2] = {0, 0};
308         long cp_time[CPUSTATES];
309         static long *pcpu_cp_time = NULL;
310
311         pcpu_cp_time = reallocz(pcpu_cp_time, sizeof(cp_time) * number_of_cpus);
312         if (unlikely(GETSYSCTL_WSIZE("kern.cp_times", mib, pcpu_cp_time, sizeof(cp_time) * number_of_cpus))) {
313             error("DISABLED: cpu.cpuXX charts");
314             error("DISABLED: kern.cp_times module");
315             return 1;
316         } else {
317
318             // --------------------------------------------------------------------
319
320             int i;
321             static struct cpu_chart {
322                 char cpuid[MAX_INT_DIGITS + 4];
323                 RRDSET *st;
324                 RRDDIM *rd_user;
325                 RRDDIM *rd_nice;
326                 RRDDIM *rd_system;
327                 RRDDIM *rd_interrupt;
328                 RRDDIM *rd_idle;
329             } *all_cpu_charts = NULL;
330
331             all_cpu_charts = reallocz(all_cpu_charts, sizeof(struct cpu_chart) * number_of_cpus);
332
333             for (i = 0; i < number_of_cpus; i++) {
334                 if (unlikely(!all_cpu_charts[i].st)) {
335                     snprintfz(all_cpu_charts[i].cpuid, MAX_INT_DIGITS, "cpu%d", i);
336                     all_cpu_charts[i].st = rrdset_create_localhost("cpu",
337                                                                all_cpu_charts[i].cpuid,
338                                                               NULL,
339                                                               "utilization",
340                                                               "cpu.cpu",
341                                                               "Core utilization",
342                                                               "percentage",
343                                                               1000,
344                                                               update_every,
345                                                               RRDSET_TYPE_STACKED
346                     );
347
348                     all_cpu_charts[i].rd_nice       = rrddim_add(all_cpu_charts[i].st, "nice", NULL, 1, 1,
349                                                                  RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
350                     all_cpu_charts[i].rd_system     = rrddim_add(all_cpu_charts[i].st, "system", NULL, 1, 1,
351                                                                  RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
352                     all_cpu_charts[i].rd_user       = rrddim_add(all_cpu_charts[i].st, "user", NULL, 1, 1,
353                                                                  RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
354                     all_cpu_charts[i].rd_interrupt  = rrddim_add(all_cpu_charts[i].st, "interrupt", NULL, 1, 1,
355                                                                  RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
356                     all_cpu_charts[i].rd_idle       = rrddim_add(all_cpu_charts[i].st, "idle", NULL, 1, 1,
357                                                                  RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
358                     rrddim_hide(all_cpu_charts[i].st, "idle");
359                 } else rrdset_next(all_cpu_charts[i].st);
360
361                 rrddim_set_by_pointer(all_cpu_charts[i].st, all_cpu_charts[i].rd_nice, pcpu_cp_time[i * 5 + 1]);
362                 rrddim_set_by_pointer(all_cpu_charts[i].st, all_cpu_charts[i].rd_system, pcpu_cp_time[i * 5 + 2]);
363                 rrddim_set_by_pointer(all_cpu_charts[i].st, all_cpu_charts[i].rd_user, pcpu_cp_time[i * 5 + 0]);
364                 rrddim_set_by_pointer(all_cpu_charts[i].st, all_cpu_charts[i].rd_interrupt, pcpu_cp_time[i * 5 + 3]);
365                 rrddim_set_by_pointer(all_cpu_charts[i].st, all_cpu_charts[i].rd_idle, pcpu_cp_time[i * 5 + 4]);
366                 rrdset_done(all_cpu_charts[i].st);
367             }
368         }
369     }
370
371     return 0;
372 }
373
374 // --------------------------------------------------------------------------------------------------------------------
375 // hw.intrcnt
376
377 int do_hw_intcnt(int update_every, usec_t dt) {
378     static int mib_hw_intrcnt[2] = {0, 0};
379     size_t intrcnt_size;
380     int i;
381
382     if (unlikely(GETSYSCTL_SIZE("hw.intrcnt", mib_hw_intrcnt, intrcnt_size))) {
383         error("DISABLED: system.intr chart");
384         error("DISABLED: system.interrupts chart");
385         error("DISABLED: hw.intrcnt module");
386         return 1;
387     } else {
388         unsigned long nintr = 0;
389         static unsigned long *intrcnt = NULL;
390         unsigned long long totalintr = 0;
391
392         nintr = intrcnt_size / sizeof(u_long);
393         intrcnt = reallocz(intrcnt, nintr * sizeof(u_long));
394         if (unlikely(GETSYSCTL_WSIZE("hw.intrcnt", mib_hw_intrcnt, intrcnt, nintr * sizeof(u_long)))) {
395             error("DISABLED: system.intr chart");
396             error("DISABLED: system.interrupts chart");
397             error("DISABLED: hw.intrcnt module");
398             return 1;
399         } else {
400             for (i = 0; i < nintr; i++)
401                 totalintr += intrcnt[i];
402
403             // --------------------------------------------------------------------
404
405             static RRDSET *st_intr = NULL;
406             static RRDDIM *rd_intr = NULL;
407
408             if (unlikely(!st_intr)) {
409                 st_intr = rrdset_create_localhost("system",
410                                              "intr",
411                                              NULL,
412                                              "interrupts",
413                                              NULL,
414                                              "Total Hardware Interrupts",
415                                              "interrupts/s",
416                                              900,
417                                              update_every,
418                                              RRDSET_TYPE_LINE
419                 );
420                 rrdset_flag_set(st_intr, RRDSET_FLAG_DETAIL);
421
422                 rd_intr = rrddim_add(st_intr, "interrupts", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
423             } else
424                 rrdset_next(st_intr);
425
426             rrddim_set_by_pointer(st_intr, rd_intr, totalintr);
427             rrdset_done(st_intr);
428
429             // --------------------------------------------------------------------
430
431             size_t size;
432             static int mib_hw_intrnames[2] = {0, 0};
433             static char *intrnames = NULL;
434
435             size = nintr * (MAXCOMLEN + 1);
436             intrnames = reallocz(intrnames, size);
437             if (unlikely(GETSYSCTL_WSIZE("hw.intrnames", mib_hw_intrnames, intrnames, size))) {
438                 error("DISABLED: system.intr chart");
439                 error("DISABLED: system.interrupts chart");
440                 error("DISABLED: hw.intrcnt module");
441                 return 1;
442             } else {
443
444                 // --------------------------------------------------------------------
445
446                 static RRDSET *st_interrupts = NULL;
447                 RRDDIM *rd_interrupts = NULL;
448                 void *p;
449
450                 if (unlikely(!st_interrupts))
451                     st_interrupts = rrdset_create_localhost("system",
452                                                  "interrupts",
453                                                  NULL,
454                                                  "interrupts",
455                                                  NULL,
456                                                  "System interrupts",
457                                                  "interrupts/s",
458                                                  1000,
459                                                  update_every,
460                                                  RRDSET_TYPE_STACKED
461                     );
462                 else
463                     rrdset_next(st_interrupts);
464
465                 for (i = 0; i < nintr; i++) {
466                     p = intrnames + i * (MAXCOMLEN + 1);
467                     if (unlikely((intrcnt[i] != 0) && (*(char *) p != 0))) {
468                         rd_interrupts = rrddim_find(st_interrupts, p);
469                         if (unlikely(!rd_interrupts))
470                             rd_interrupts = rrddim_add(st_interrupts, p, NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
471                         rrddim_set_by_pointer(st_interrupts, rd_interrupts, intrcnt[i]);
472                     }
473                 }
474                 rrdset_done(st_interrupts);
475             }
476         }
477     }
478
479     return 0;
480 }
481
482 // --------------------------------------------------------------------------------------------------------------------
483 // vm.stats.sys.v_intr
484
485 int do_vm_stats_sys_v_intr(int update_every, usec_t dt) {
486     static int mib[4] = {0, 0, 0, 0};
487     u_int int_number;
488
489     if (unlikely(GETSYSCTL_SIMPLE("vm.stats.sys.v_intr", mib, int_number))) {
490         error("DISABLED: system.dev_intr chart");
491         error("DISABLED: vm.stats.sys.v_intr module");
492         return 1;
493     } else {
494
495         // --------------------------------------------------------------------
496
497         static RRDSET *st = NULL;
498         static RRDDIM *rd = NULL;
499
500         st = rrdset_find_bytype_localhost("system", "dev_intr");
501         if (unlikely(!st)) {
502             st = rrdset_create_localhost("system",
503                                          "dev_intr",
504                                          NULL,
505                                          "interrupts",
506                                          NULL,
507                                          "Device Interrupts",
508                                          "interrupts/s",
509                                          1000,
510                                          update_every,
511                                          RRDSET_TYPE_LINE
512             );
513
514             rd = rrddim_add(st, "interrupts", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
515         }
516         else rrdset_next(st);
517
518         rrddim_set_by_pointer(st, rd, int_number);
519         rrdset_done(st);
520     }
521
522     return 0;
523 }
524
525 // --------------------------------------------------------------------------------------------------------------------
526 // vm.stats.sys.v_soft
527
528 int do_vm_stats_sys_v_soft(int update_every, usec_t dt) {
529     static int mib[4] = {0, 0, 0, 0};
530     u_int soft_intr_number;
531
532     if (unlikely(GETSYSCTL_SIMPLE("vm.stats.sys.v_soft", mib, soft_intr_number))) {
533         error("DISABLED: system.dev_intr chart");
534         error("DISABLED: vm.stats.sys.v_soft module");
535         return 1;
536     } else {
537
538         // --------------------------------------------------------------------
539
540         static RRDSET *st = NULL;
541         static RRDDIM *rd = NULL;
542
543         if (unlikely(!st)) {
544             st = rrdset_create_localhost("system",
545                                          "soft_intr",
546                                          NULL,
547                                          "interrupts",
548                                          NULL,
549                                          "Software Interrupts",
550                                          "interrupts/s",
551                                          1100,
552                                          update_every,
553                                          RRDSET_TYPE_LINE
554             );
555
556             rd = rrddim_add(st, "interrupts", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
557         }
558         else rrdset_next(st);
559
560         rrddim_set_by_pointer(st, rd, soft_intr_number);
561         rrdset_done(st);
562     }
563
564     return 0;
565 }
566
567 // --------------------------------------------------------------------------------------------------------------------
568 // vm.stats.sys.v_swtch
569
570 int do_vm_stats_sys_v_swtch(int update_every, usec_t dt) {
571     static int mib[4] = {0, 0, 0, 0};
572     u_int ctxt_number;
573
574     if (unlikely(GETSYSCTL_SIMPLE("vm.stats.sys.v_swtch", mib, ctxt_number))) {
575         error("DISABLED: system.ctxt chart");
576         error("DISABLED: vm.stats.sys.v_swtch module");
577         return 1;
578     } else {
579
580         // --------------------------------------------------------------------
581
582         static RRDSET *st = NULL;
583         static RRDDIM *rd = NULL;
584
585         if (unlikely(!st)) {
586             st = rrdset_create_localhost("system",
587                                          "ctxt",
588                                          NULL,
589                                          "processes",
590                                          NULL,
591                                          "CPU Context Switches",
592                                          "context switches/s",
593                                          800,
594                                          update_every,
595                                          RRDSET_TYPE_LINE
596             );
597
598             rd = rrddim_add(st, "switches", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
599         }
600         else rrdset_next(st);
601
602         rrddim_set_by_pointer(st, rd, ctxt_number);
603         rrdset_done(st);
604     }
605
606     return 0;
607 }
608
609 // --------------------------------------------------------------------------------------------------------------------
610 // vm.stats.vm.v_forks
611
612 int do_vm_stats_sys_v_forks(int update_every, usec_t dt) {
613     static int mib[4] = {0, 0, 0, 0};
614     u_int forks_number;
615
616     if (unlikely(GETSYSCTL_SIMPLE("vm.stats.vm.v_forks", mib, forks_number))) {
617         error("DISABLED: system.forks chart");
618         error("DISABLED: vm.stats.sys.v_swtch module");
619         return 1;
620     } else {
621
622         // --------------------------------------------------------------------
623
624         static RRDSET *st = NULL;
625         static RRDDIM *rd = NULL;
626
627         if (unlikely(!st)) {
628             st = rrdset_create_localhost("system",
629                                          "forks",
630                                          NULL,
631                                          "processes",
632                                          NULL,
633                                          "Started Processes",
634                                          "processes/s",
635                                          700,
636                                          update_every,
637                                          RRDSET_TYPE_LINE
638             );
639
640             rrdset_flag_set(st, RRDSET_FLAG_DETAIL);
641
642             rd = rrddim_add(st, "started", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
643         }
644         else rrdset_next(st);
645
646         rrddim_set_by_pointer(st, rd, forks_number);
647         rrdset_done(st);
648     }
649
650     return 0;
651 }
652
653 // --------------------------------------------------------------------------------------------------------------------
654 // vm.swap_info
655
656 int do_vm_swap_info(int update_every, usec_t dt) {
657     static int mib[3] = {0, 0, 0};
658
659     if (unlikely(getsysctl_mib("vm.swap_info", mib, 2))) {
660         error("DISABLED: system.swap chart");
661         error("DISABLED: vm.swap_info module");
662         return 1;
663     } else {
664         int i;
665         struct xswdev xsw;
666         struct total_xsw {
667             collected_number bytes_used;
668             collected_number bytes_total;
669         } total_xsw = {0, 0};
670
671         for (i = 0; ; i++) {
672             size_t size;
673
674             mib[2] = i;
675             size = sizeof(xsw);
676             if (unlikely(sysctl(mib, 3, &xsw, &size, NULL, 0) == -1 )) {
677                 if (unlikely(errno != ENOENT)) {
678                     error("FREEBSD: sysctl(%s...) failed: %s", "vm.swap_info", strerror(errno));
679                     error("DISABLED: system.swap chart");
680                     error("DISABLED: vm.swap_info module");
681                     return 1;
682                 } else {
683                     if (unlikely(size != sizeof(xsw))) {
684                         error("FREEBSD: sysctl(%s...) expected %lu, got %lu", "vm.swap_info", (unsigned long)sizeof(xsw), (unsigned long)size);
685                         error("DISABLED: system.swap chart");
686                         error("DISABLED: vm.swap_info module");
687                         return 1;
688                     } else break;
689                 }
690             }
691             total_xsw.bytes_used += xsw.xsw_used;
692             total_xsw.bytes_total += xsw.xsw_nblks;
693         }
694
695         // --------------------------------------------------------------------
696
697         static RRDSET *st = NULL;
698         static RRDDIM *rd_free = NULL, *rd_used = NULL;
699
700         if (unlikely(!st)) {
701             st = rrdset_create_localhost("system",
702                                          "swap",
703                                          NULL,
704                                          "swap",
705                                          NULL,
706                                          "System Swap",
707                                          "MB",
708                                          201,
709                                          update_every,
710                                          RRDSET_TYPE_STACKED
711             );
712
713             rrdset_flag_set(st, RRDSET_FLAG_DETAIL);
714
715             rd_free = rrddim_add(st, "free",    NULL, system_pagesize, MEGA_FACTOR, RRD_ALGORITHM_ABSOLUTE);
716             rd_used = rrddim_add(st, "used",    NULL, system_pagesize, MEGA_FACTOR, RRD_ALGORITHM_ABSOLUTE);
717         }
718         else rrdset_next(st);
719
720         rrddim_set_by_pointer(st, rd_free, total_xsw.bytes_total - total_xsw.bytes_used);
721         rrddim_set_by_pointer(st, rd_used, total_xsw.bytes_used);
722         rrdset_done(st);
723     }
724
725     return 0;
726 }
727
728 // --------------------------------------------------------------------------------------------------------------------
729 // system.ram
730
731 int do_system_ram(int update_every, usec_t dt) {
732     static int mib_active_count[4] = {0, 0, 0, 0}, mib_inactive_count[4] = {0, 0, 0, 0}, mib_wire_count[4] = {0, 0, 0, 0},
733                mib_cache_count[4] = {0, 0, 0, 0}, mib_vfs_bufspace[2] = {0, 0}, mib_free_count[4] = {0, 0, 0, 0};
734     struct vmmeter vmmeter_data;
735     int vfs_bufspace_count;
736
737     if (unlikely(GETSYSCTL_SIMPLE("vm.stats.vm.v_active_count",   mib_active_count,   vmmeter_data.v_active_count) ||
738                  GETSYSCTL_SIMPLE("vm.stats.vm.v_inactive_count", mib_inactive_count, vmmeter_data.v_inactive_count) ||
739                  GETSYSCTL_SIMPLE("vm.stats.vm.v_wire_count",     mib_wire_count,     vmmeter_data.v_wire_count) ||
740 #if __FreeBSD_version < 1200016
741                  GETSYSCTL_SIMPLE("vm.stats.vm.v_cache_count",    mib_cache_count,    vmmeter_data.v_cache_count) ||
742 #endif
743                  GETSYSCTL_SIMPLE("vfs.bufspace",                 mib_vfs_bufspace,     vfs_bufspace_count) ||
744                  GETSYSCTL_SIMPLE("vm.stats.vm.v_free_count",     mib_free_count,     vmmeter_data.v_free_count))) {
745         error("DISABLED: system.ram chart");
746         error("DISABLED: System.ram module");
747         return 1;
748     } else {
749
750         // --------------------------------------------------------------------
751
752         static RRDSET *st = NULL;
753         static RRDDIM *rd_free = NULL, *rd_active = NULL, *rd_inactive = NULL,
754                       *rd_wired = NULL, *rd_cache = NULL, *rd_buffers = NULL;
755
756         st = rrdset_find_localhost("system.ram");
757         if (unlikely(!st)) {
758             st = rrdset_create_localhost("system",
759                                          "ram",
760                                          NULL,
761                                          "ram",
762                                          NULL,
763                                          "System RAM",
764                                          "MB",
765                                          200,
766                                          update_every,
767                                          RRDSET_TYPE_STACKED
768             );
769
770             rd_free     = rrddim_add(st, "free",     NULL, system_pagesize, MEGA_FACTOR, RRD_ALGORITHM_ABSOLUTE);
771             rd_active   = rrddim_add(st, "active",   NULL, system_pagesize, MEGA_FACTOR, RRD_ALGORITHM_ABSOLUTE);
772             rd_inactive = rrddim_add(st, "inactive", NULL, system_pagesize, MEGA_FACTOR, RRD_ALGORITHM_ABSOLUTE);
773             rd_wired    = rrddim_add(st, "wired",    NULL, system_pagesize, MEGA_FACTOR, RRD_ALGORITHM_ABSOLUTE);
774 #if __FreeBSD_version < 1200016
775             rd_cache    = rrddim_add(st, "cache",    NULL, system_pagesize, MEGA_FACTOR, RRD_ALGORITHM_ABSOLUTE);
776 #endif
777             rd_buffers  = rrddim_add(st, "buffers",  NULL, 1, MEGA_FACTOR, RRD_ALGORITHM_ABSOLUTE);
778         }
779         else rrdset_next(st);
780
781         rrddim_set_by_pointer(st, rd_free,     vmmeter_data.v_free_count);
782         rrddim_set_by_pointer(st, rd_active,   vmmeter_data.v_active_count);
783         rrddim_set_by_pointer(st, rd_inactive, vmmeter_data.v_inactive_count);
784         rrddim_set_by_pointer(st, rd_wired,    vmmeter_data.v_wire_count);
785 #if __FreeBSD_version < 1200016
786         rrddim_set_by_pointer(st, rd_cache,    vmmeter_data.v_cache_count);
787 #endif
788         rrddim_set_by_pointer(st, rd_buffers,  vfs_bufspace_count);
789         rrdset_done(st);
790     }
791
792     return 0;
793 }
794
795 // --------------------------------------------------------------------------------------------------------------------
796 // vm.stats.vm.v_swappgs
797
798 int do_vm_stats_sys_v_swappgs(int update_every, usec_t dt) {
799     static int mib_swappgsin[4] = {0, 0, 0, 0}, mib_swappgsout[4] = {0, 0, 0, 0};
800     struct vmmeter vmmeter_data;
801
802     if (unlikely(GETSYSCTL_SIMPLE("vm.stats.vm.v_swappgsin", mib_swappgsin, vmmeter_data.v_swappgsin) ||
803                  GETSYSCTL_SIMPLE("vm.stats.vm.v_swappgsout", mib_swappgsout, vmmeter_data.v_swappgsout))) {
804         error("DISABLED: system.swapio chart");
805         error("DISABLED: vm.stats.vm.v_swappgs module");
806         return 1;
807     } else {
808
809         // --------------------------------------------------------------------
810
811         static RRDSET *st = NULL;
812         static RRDDIM *rd_in = NULL, *rd_out = NULL;
813
814         if (unlikely(!st)) {
815             st = rrdset_create_localhost("system",
816                                          "swapio",
817                                          NULL,
818                                          "swap",
819                                          NULL,
820                                          "Swap I/O",
821                                          "kilobytes/s",
822                                          250,
823                                          update_every,
824                                          RRDSET_TYPE_AREA
825             );
826
827             rd_in = rrddim_add(st, "in",  NULL, system_pagesize, KILO_FACTOR, RRD_ALGORITHM_INCREMENTAL);
828             rd_out = rrddim_add(st, "out", NULL, -system_pagesize, KILO_FACTOR, RRD_ALGORITHM_INCREMENTAL);
829         }
830         else rrdset_next(st);
831
832         rrddim_set_by_pointer(st, rd_in, vmmeter_data.v_swappgsin);
833         rrddim_set_by_pointer(st, rd_out, vmmeter_data.v_swappgsout);
834         rrdset_done(st);
835     }
836
837     return 0;
838 }
839
840 // --------------------------------------------------------------------------------------------------------------------
841 // vm.stats.vm.v_pgfaults
842
843 int do_vm_stats_sys_v_pgfaults(int update_every, usec_t dt) {
844     static int mib_vm_faults[4] = {0, 0, 0, 0}, mib_io_faults[4] = {0, 0, 0, 0}, mib_cow_faults[4] = {0, 0, 0, 0},
845                mib_cow_optim[4] = {0, 0, 0, 0}, mib_intrans[4] = {0, 0, 0, 0};
846     struct vmmeter vmmeter_data;
847
848     if (unlikely(GETSYSCTL_SIMPLE("vm.stats.vm.v_vm_faults",  mib_vm_faults,  vmmeter_data.v_vm_faults) ||
849                  GETSYSCTL_SIMPLE("vm.stats.vm.v_io_faults",  mib_io_faults,  vmmeter_data.v_io_faults) ||
850                  GETSYSCTL_SIMPLE("vm.stats.vm.v_cow_faults", mib_cow_faults, vmmeter_data.v_cow_faults) ||
851                  GETSYSCTL_SIMPLE("vm.stats.vm.v_cow_optim",  mib_cow_optim,  vmmeter_data.v_cow_optim) ||
852                  GETSYSCTL_SIMPLE("vm.stats.vm.v_intrans",    mib_intrans,    vmmeter_data.v_intrans))) {
853         error("DISABLED: mem.pgfaults chart");
854         error("DISABLED: vm.stats.vm.v_pgfaults module");
855         return 1;
856     } else {
857
858         // --------------------------------------------------------------------
859
860         static RRDSET *st = NULL;
861         static RRDDIM *rd_memory = NULL, *rd_io_requiring = NULL, *rd_cow = NULL,
862                       *rd_cow_optimized = NULL, *rd_in_transit = NULL;
863
864         if (unlikely(!st)) {
865             st = rrdset_create_localhost("mem",
866                                          "pgfaults",
867                                          NULL,
868                                          "system",
869                                          NULL,
870                                          "Memory Page Faults",
871                                          "page faults/s",
872                                          500,
873                                          update_every,
874                                          RRDSET_TYPE_LINE
875             );
876
877             rrdset_flag_set(st, RRDSET_FLAG_DETAIL);
878
879             rd_memory        = rrddim_add(st, "memory",        NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
880             rd_io_requiring  = rrddim_add(st, "io_requiring",  NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
881             rd_cow           = rrddim_add(st, "cow",           NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
882             rd_cow_optimized = rrddim_add(st, "cow_optimized", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
883             rd_in_transit    = rrddim_add(st, "in_transit",    NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
884         }
885         else rrdset_next(st);
886
887         rrddim_set_by_pointer(st, rd_memory,        vmmeter_data.v_vm_faults);
888         rrddim_set_by_pointer(st, rd_io_requiring,  vmmeter_data.v_io_faults);
889         rrddim_set_by_pointer(st, rd_cow,           vmmeter_data.v_cow_faults);
890         rrddim_set_by_pointer(st, rd_cow_optimized, vmmeter_data.v_cow_optim);
891         rrddim_set_by_pointer(st, rd_in_transit,    vmmeter_data.v_intrans);
892         rrdset_done(st);
893     }
894
895     return 0;
896 }
897
898 // --------------------------------------------------------------------------------------------------------------------
899 // kern.ipc.sem
900
901 int do_kern_ipc_sem(int update_every, usec_t dt) {
902     static int mib_semmni[3] = {0, 0, 0}, mib_sema[3] = {0, 0, 0};
903     struct ipc_sem {
904         int semmni;
905         collected_number sets;
906         collected_number semaphores;
907     } ipc_sem = {0, 0, 0};
908
909     if (unlikely(GETSYSCTL_SIMPLE("kern.ipc.semmni", mib_semmni, ipc_sem.semmni))) {
910         error("DISABLED: system.ipc_semaphores chart");
911         error("DISABLED: system.ipc_semaphore_arrays chart");
912         error("DISABLED: kern.ipc.sem module");
913         return 1;
914     } else {
915         static struct semid_kernel *ipc_sem_data = NULL;
916
917         ipc_sem_data = reallocz(ipc_sem_data, sizeof(struct semid_kernel) * ipc_sem.semmni);
918         if (unlikely(GETSYSCTL_WSIZE("kern.ipc.sema", mib_sema, ipc_sem_data, sizeof(struct semid_kernel) * ipc_sem.semmni))) {
919             error("DISABLED: system.ipc_semaphores chart");
920             error("DISABLED: system.ipc_semaphore_arrays chart");
921             error("DISABLED: kern.ipc.sem module");
922             return 1;
923         } else {
924             int i;
925
926             for (i = 0; i < ipc_sem.semmni; i++) {
927                 if (unlikely(ipc_sem_data[i].u.sem_perm.mode & SEM_ALLOC)) {
928                     ipc_sem.sets += 1;
929                     ipc_sem.semaphores += ipc_sem_data[i].u.sem_nsems;
930                 }
931             }
932
933             // --------------------------------------------------------------------
934
935             static RRDSET *st_semaphores = NULL, *st_semaphore_arrays = NULL;
936             static RRDDIM *rd_semaphores = NULL, *rd_semaphore_arrays = NULL;
937
938             if (unlikely(!st_semaphores)) {
939                 st_semaphores = rrdset_create_localhost("system",
940                                                         "ipc_semaphores",
941                                                         NULL,
942                                                         "ipc semaphores",
943                                                         NULL,
944                                                         "IPC Semaphores",
945                                                         "semaphores",
946                                                         1000,
947                                                         localhost->rrd_update_every,
948                                                         RRDSET_TYPE_AREA
949                 );
950
951                 rd_semaphores = rrddim_add(st_semaphores, "semaphores", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
952             }
953             else rrdset_next(st_semaphores);
954
955             rrddim_set_by_pointer(st_semaphores, rd_semaphores, ipc_sem.semaphores);
956             rrdset_done(st_semaphores);
957
958             // --------------------------------------------------------------------
959
960             if (unlikely(!st_semaphore_arrays)) {
961                 st_semaphore_arrays = rrdset_create_localhost("system",
962                                                               "ipc_semaphore_arrays",
963                                                               NULL,
964                                                               "ipc semaphores",
965                                                               NULL,
966                                                               "IPC Semaphore Arrays",
967                                                               "arrays",
968                                                               1000,
969                                                               localhost->rrd_update_every,
970                                                               RRDSET_TYPE_AREA
971                 );
972
973                 rd_semaphore_arrays = rrddim_add(st_semaphore_arrays, "arrays", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
974             }
975             else rrdset_next(st_semaphore_arrays);
976
977             rrddim_set_by_pointer(st_semaphore_arrays, rd_semaphore_arrays, ipc_sem.sets);
978             rrdset_done(st_semaphore_arrays);
979         }
980     }
981
982     return 0;
983 }
984
985 // --------------------------------------------------------------------------------------------------------------------
986 // kern.ipc.shm
987
988 int do_kern_ipc_shm(int update_every, usec_t dt) {
989     static int mib_shmmni[3] = {0, 0, 0}, mib_shmsegs[3] = {0, 0, 0};
990     struct ipc_shm {
991         u_long shmmni;
992         collected_number segs;
993         collected_number segsize;
994     } ipc_shm = {0, 0, 0};
995
996     if (unlikely(GETSYSCTL_SIMPLE("kern.ipc.shmmni", mib_shmmni, ipc_shm.shmmni))) {
997         error("DISABLED: system.ipc_shared_mem_segs chart");
998         error("DISABLED: system.ipc_shared_mem_size chart");
999         error("DISABLED: kern.ipc.shmmodule");
1000         return 1;
1001     } else {
1002         static struct shmid_kernel *ipc_shm_data = NULL;
1003
1004         ipc_shm_data = reallocz(ipc_shm_data, sizeof(struct shmid_kernel) * ipc_shm.shmmni);
1005         if (unlikely(
1006                 GETSYSCTL_WSIZE("kern.ipc.shmsegs", mib_shmsegs, ipc_shm_data, sizeof(struct shmid_kernel) * ipc_shm.shmmni))) {
1007             error("DISABLED: system.ipc_shared_mem_segs chart");
1008             error("DISABLED: system.ipc_shared_mem_size chart");
1009             error("DISABLED: kern.ipc.shmmodule");
1010             return 1;
1011         } else {
1012             int i;
1013
1014             for (i = 0; i < ipc_shm.shmmni; i++) {
1015                 if (unlikely(ipc_shm_data[i].u.shm_perm.mode & 0x0800)) {
1016                     ipc_shm.segs += 1;
1017                     ipc_shm.segsize += ipc_shm_data[i].u.shm_segsz;
1018                 }
1019             }
1020
1021             // --------------------------------------------------------------------
1022
1023             static RRDSET *st_segs = NULL, *st_size = NULL;
1024             static RRDDIM *rd_segments = NULL, *rd_allocated = NULL;
1025
1026             if (unlikely(!st_segs)) {
1027                 st_segs = rrdset_create_localhost("system",
1028                                              "ipc_shared_mem_segs",
1029                                              NULL,
1030                                              "ipc shared memory",
1031                                              NULL,
1032                                              "IPC Shared Memory Segments",
1033                                              "segments",
1034                                              1000,
1035                                              localhost->rrd_update_every,
1036                                              RRDSET_TYPE_AREA
1037                 );
1038
1039                 rd_segments = rrddim_add(st_segs, "segments", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
1040             }
1041             else rrdset_next(st_segs);
1042
1043             rrddim_set_by_pointer(st_segs, rd_segments, ipc_shm.segs);
1044             rrdset_done(st_segs);
1045
1046             // --------------------------------------------------------------------
1047
1048             if (unlikely(!st_size)) {
1049                 st_size = rrdset_create_localhost("system",
1050                                              "ipc_shared_mem_size",
1051                                              NULL,
1052                                              "ipc shared memory",
1053                                              NULL,
1054                                              "IPC Shared Memory Segments Size",
1055                                              "kilobytes",
1056                                              1000,
1057                                              localhost->rrd_update_every,
1058                                              RRDSET_TYPE_AREA
1059                 );
1060
1061                 rd_allocated = rrddim_add(st_size, "allocated", NULL, 1, KILO_FACTOR, RRD_ALGORITHM_ABSOLUTE);
1062             }
1063             else rrdset_next(st_size);
1064
1065             rrddim_set_by_pointer(st_size, rd_allocated, ipc_shm.segsize);
1066             rrdset_done(st_size);
1067         }
1068     }
1069
1070     return 0;
1071 }
1072
1073 // --------------------------------------------------------------------------------------------------------------------
1074 // kern.ipc.msq
1075
1076 int do_kern_ipc_msq(int update_every, usec_t dt) {
1077     static int mib_msgmni[3] = {0, 0, 0}, mib_msqids[3] = {0, 0, 0};
1078     struct ipc_msq {
1079         int msgmni;
1080         collected_number queues;
1081         collected_number messages;
1082         collected_number usedsize;
1083         collected_number allocsize;
1084     } ipc_msq = {0, 0, 0, 0, 0};
1085
1086     if (unlikely(GETSYSCTL_SIMPLE("kern.ipc.msgmni", mib_msgmni, ipc_msq.msgmni))) {
1087         error("DISABLED: system.ipc_msq_queues chart");
1088         error("DISABLED: system.ipc_msq_messages chart");
1089         error("DISABLED: system.ipc_msq_size chart");
1090         error("DISABLED: kern.ipc.msg module");
1091         return 1;
1092     } else {
1093         static struct msqid_kernel *ipc_msq_data = NULL;
1094
1095         ipc_msq_data = reallocz(ipc_msq_data, sizeof(struct msqid_kernel) * ipc_msq.msgmni);
1096         if (unlikely(
1097                 GETSYSCTL_WSIZE("kern.ipc.msqids", mib_msqids, ipc_msq_data, sizeof(struct msqid_kernel) * ipc_msq.msgmni))) {
1098             error("DISABLED: system.ipc_msq_queues chart");
1099             error("DISABLED: system.ipc_msq_messages chart");
1100             error("DISABLED: system.ipc_msq_size chart");
1101             error("DISABLED: kern.ipc.msg module");
1102             return 1;
1103         } else {
1104             int i;
1105
1106             for (i = 0; i < ipc_msq.msgmni; i++) {
1107                 if (unlikely(ipc_msq_data[i].u.msg_qbytes != 0)) {
1108                     ipc_msq.queues += 1;
1109                     ipc_msq.messages += ipc_msq_data[i].u.msg_qnum;
1110                     ipc_msq.usedsize += ipc_msq_data[i].u.msg_cbytes;
1111                     ipc_msq.allocsize += ipc_msq_data[i].u.msg_qbytes;
1112                 }
1113             }
1114
1115             // --------------------------------------------------------------------
1116
1117             static RRDSET *st_queues = NULL, *st_messages = NULL, *st_size = NULL;
1118             static RRDDIM *rd_queues = NULL, *rd_messages = NULL, *rd_allocated = NULL, *rd_used = NULL;
1119
1120             if (unlikely(!st_queues)) {
1121                 st_queues = rrdset_create_localhost("system",
1122                                                     "ipc_msq_queues",
1123                                                     NULL,
1124                                                     "ipc message queues",
1125                                                     NULL,
1126                                                     "Number of IPC Message Queues",
1127                                                     "queues",
1128                                                     990,
1129                                                     localhost->rrd_update_every,
1130                                                     RRDSET_TYPE_AREA
1131                 );
1132
1133                 rd_queues = rrddim_add(st_queues, "queues", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
1134             }
1135             else rrdset_next(st_queues);
1136
1137             rrddim_set_by_pointer(st_queues, rd_queues, ipc_msq.queues);
1138             rrdset_done(st_queues);
1139
1140             // --------------------------------------------------------------------
1141
1142             if (unlikely(!st_messages)) {
1143                 st_messages = rrdset_create_localhost("system",
1144                                                       "ipc_msq_messages",
1145                                                       NULL,
1146                                                       "ipc message queues",
1147                                                       NULL,
1148                                                       "Number of Messages in IPC Message Queues",
1149                                                       "messages",
1150                                                       1000,
1151                                                       localhost->rrd_update_every,
1152                                                       RRDSET_TYPE_AREA
1153                 );
1154
1155                 rd_messages = rrddim_add(st_messages, "messages", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
1156             }
1157             else rrdset_next(st_messages);
1158
1159             rrddim_set_by_pointer(st_messages, rd_messages, ipc_msq.messages);
1160             rrdset_done(st_messages);
1161
1162             // --------------------------------------------------------------------
1163
1164             if (unlikely(!st_size)) {
1165                 st_size = rrdset_create_localhost("system",
1166                                              "ipc_msq_size",
1167                                              NULL,
1168                                              "ipc message queues",
1169                                              NULL,
1170                                              "Size of IPC Message Queues",
1171                                              "bytes",
1172                                              1100,
1173                                              localhost->rrd_update_every,
1174                                              RRDSET_TYPE_LINE
1175                 );
1176
1177                 rd_allocated = rrddim_add(st_size, "allocated", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
1178                 rd_used = rrddim_add(st_size, "used", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
1179             }
1180             else rrdset_next(st_size);
1181
1182             rrddim_set_by_pointer(st_size, rd_allocated, ipc_msq.allocsize);
1183             rrddim_set_by_pointer(st_size, rd_used, ipc_msq.usedsize);
1184             rrdset_done(st_size);
1185         }
1186     }
1187
1188     return 0;
1189 }
1190
1191 // --------------------------------------------------------------------------------------------------------------------
1192 // uptime
1193
1194 int do_uptime(int update_every, usec_t dt) {
1195     struct timespec up_time;
1196
1197     clock_gettime(CLOCK_UPTIME, &up_time);
1198
1199     // --------------------------------------------------------------------
1200
1201     static RRDSET *st = NULL;
1202     static RRDDIM *rd = NULL;
1203
1204     if(unlikely(!st)) {
1205         st = rrdset_create_localhost("system",
1206                                      "uptime",
1207                                      NULL,
1208                                      "uptime",
1209                                      NULL,
1210                                      "System Uptime",
1211                                      "seconds",
1212                                      1000,
1213                                      update_every,
1214                                      RRDSET_TYPE_LINE
1215         );
1216
1217         rd = rrddim_add(st, "uptime", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
1218     }
1219     else rrdset_next(st);
1220
1221     rrddim_set_by_pointer(st, rd, up_time.tv_sec);
1222     rrdset_done(st);
1223
1224     return 0;
1225 }
1226
1227 // --------------------------------------------------------------------------------------------------------------------
1228 // net.isr
1229
1230 int do_net_isr(int update_every, usec_t dt) {
1231     static int do_netisr = -1, do_netisr_per_core = -1;
1232
1233     if (unlikely(do_netisr == -1)) {
1234         do_netisr =          config_get_boolean("plugin:freebsd:sysctl", "netisr",          1);
1235         do_netisr_per_core = config_get_boolean("plugin:freebsd:sysctl", "netisr per core", 1);
1236     }
1237
1238     static int mib_workstream[3] = {0, 0, 0}, mib_work[3] = {0, 0, 0};
1239     int common_error = 0;
1240     int i, n;
1241     size_t netisr_workstream_size, netisr_work_size;
1242     unsigned long num_netisr_workstreams = 0, num_netisr_works = 0;
1243     static struct sysctl_netisr_workstream *netisr_workstream = NULL;
1244     static struct sysctl_netisr_work *netisr_work = NULL;
1245     static struct netisr_stats {
1246         collected_number dispatched;
1247         collected_number hybrid_dispatched;
1248         collected_number qdrops;
1249         collected_number queued;
1250     } *netisr_stats = NULL;
1251
1252     if (likely(do_netisr || do_netisr_per_core)) {
1253         if (unlikely(GETSYSCTL_SIZE("net.isr.workstream", mib_workstream, netisr_workstream_size))) {
1254             common_error = 1;
1255         } else if (unlikely(GETSYSCTL_SIZE("net.isr.work", mib_work, netisr_work_size))) {
1256             common_error = 1;
1257         } else {
1258             num_netisr_workstreams = netisr_workstream_size / sizeof(struct sysctl_netisr_workstream);
1259             netisr_workstream = reallocz(netisr_workstream, num_netisr_workstreams * sizeof(struct sysctl_netisr_workstream));
1260             if (unlikely(GETSYSCTL_WSIZE("net.isr.workstream", mib_workstream, netisr_workstream,
1261                                            num_netisr_workstreams * sizeof(struct sysctl_netisr_workstream)))){
1262                 common_error = 1;
1263             } else {
1264                 num_netisr_works = netisr_work_size / sizeof(struct sysctl_netisr_work);
1265                 netisr_work = reallocz(netisr_work, num_netisr_works * sizeof(struct sysctl_netisr_work));
1266                 if (unlikely(GETSYSCTL_WSIZE("net.isr.work", mib_work, netisr_work,
1267                                                num_netisr_works * sizeof(struct sysctl_netisr_work)))){
1268                     common_error = 1;
1269                 }
1270             }
1271         }
1272         if (unlikely(common_error)) {
1273             do_netisr = 0;
1274             error("DISABLED: system.softnet_stat chart");
1275             do_netisr_per_core = 0;
1276             error("DISABLED: system.cpuX_softnet_stat chart");
1277             common_error = 0;
1278             error("DISABLED: net.isr module");
1279             return 1;
1280         } else {
1281             netisr_stats = reallocz(netisr_stats, (number_of_cpus + 1) * sizeof(struct netisr_stats));
1282             bzero(netisr_stats, (number_of_cpus + 1) * sizeof(struct netisr_stats));
1283             for (i = 0; i < num_netisr_workstreams; i++) {
1284                 for (n = 0; n < num_netisr_works; n++) {
1285                     if (netisr_workstream[i].snws_wsid == netisr_work[n].snw_wsid) {
1286                         netisr_stats[netisr_workstream[i].snws_cpu].dispatched += netisr_work[n].snw_dispatched;
1287                         netisr_stats[netisr_workstream[i].snws_cpu].hybrid_dispatched += netisr_work[n].snw_hybrid_dispatched;
1288                         netisr_stats[netisr_workstream[i].snws_cpu].qdrops += netisr_work[n].snw_qdrops;
1289                         netisr_stats[netisr_workstream[i].snws_cpu].queued += netisr_work[n].snw_queued;
1290                     }
1291                 }
1292             }
1293             for (i = 0; i < number_of_cpus; i++) {
1294                 netisr_stats[number_of_cpus].dispatched += netisr_stats[i].dispatched;
1295                 netisr_stats[number_of_cpus].hybrid_dispatched += netisr_stats[i].hybrid_dispatched;
1296                 netisr_stats[number_of_cpus].qdrops += netisr_stats[i].qdrops;
1297                 netisr_stats[number_of_cpus].queued += netisr_stats[i].queued;
1298             }
1299         }
1300     } else {
1301         error("DISABLED: net.isr module");
1302         return 1;
1303     }
1304
1305     // --------------------------------------------------------------------
1306
1307     if (likely(do_netisr)) {
1308         static RRDSET *st = NULL;
1309         static RRDDIM *rd_dispatched = NULL, *rd_hybrid_dispatched = NULL, *rd_qdrops = NULL, *rd_queued = NULL;
1310
1311         if (unlikely(!st)) {
1312             st = rrdset_create_localhost("system",
1313                                          "softnet_stat",
1314                                          NULL,
1315                                          "softnet_stat",
1316                                          NULL,
1317                                          "System softnet_stat",
1318                                          "events/s",
1319                                          955,
1320                                          update_every,
1321                                          RRDSET_TYPE_LINE
1322             );
1323
1324             rd_dispatched        = rrddim_add(st, "dispatched",        NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
1325             rd_hybrid_dispatched = rrddim_add(st, "hybrid_dispatched", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
1326             rd_qdrops            = rrddim_add(st, "qdrops",            NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
1327             rd_queued            = rrddim_add(st, "queued",            NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
1328         }
1329         else rrdset_next(st);
1330
1331         rrddim_set_by_pointer(st, rd_dispatched,        netisr_stats[number_of_cpus].dispatched);
1332         rrddim_set_by_pointer(st, rd_hybrid_dispatched, netisr_stats[number_of_cpus].hybrid_dispatched);
1333         rrddim_set_by_pointer(st, rd_qdrops,            netisr_stats[number_of_cpus].qdrops);
1334         rrddim_set_by_pointer(st, rd_queued,            netisr_stats[number_of_cpus].queued);
1335         rrdset_done(st);
1336     }
1337
1338     // --------------------------------------------------------------------
1339
1340     if (likely(do_netisr_per_core)) {
1341         static struct softnet_chart {
1342             char netisr_cpuid[MAX_INT_DIGITS + 17];
1343             RRDSET *st;
1344             RRDDIM *rd_dispatched;
1345             RRDDIM *rd_hybrid_dispatched;
1346             RRDDIM *rd_qdrops;
1347             RRDDIM *rd_queued;
1348         } *all_softnet_charts = NULL;
1349
1350         all_softnet_charts = reallocz(all_softnet_charts, sizeof(struct softnet_chart) * number_of_cpus);
1351
1352         for (i = 0; i < number_of_cpus ;i++) {
1353             snprintfz(all_softnet_charts[i].netisr_cpuid, MAX_INT_DIGITS + 17, "cpu%d_softnet_stat", i);
1354
1355             if (unlikely(!all_softnet_charts[i].st)) {
1356                 all_softnet_charts[i].st = rrdset_create_localhost("cpu",
1357                                                                    all_softnet_charts[i].netisr_cpuid,
1358                                                                    NULL,
1359                                                                    "softnet_stat",
1360                                                                    NULL,
1361                                                                    "Per CPU netisr statistics",
1362                                                                    "events/s",
1363                                                                    1101 + i,
1364                                                                    update_every,
1365                                                                    RRDSET_TYPE_LINE
1366                 );
1367
1368                 all_softnet_charts[i].rd_dispatched        = rrddim_add(all_softnet_charts[i].st, "dispatched",
1369                                                                 NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
1370                 all_softnet_charts[i].rd_hybrid_dispatched = rrddim_add(all_softnet_charts[i].st, "hybrid_dispatched",
1371                                                                 NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
1372                 all_softnet_charts[i].rd_qdrops            = rrddim_add(all_softnet_charts[i].st, "qdrops",
1373                                                                 NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
1374                 all_softnet_charts[i].rd_queued            = rrddim_add(all_softnet_charts[i].st, "queued",
1375                                                                 NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
1376             }
1377             else rrdset_next(all_softnet_charts[i].st);
1378
1379             rrddim_set_by_pointer(all_softnet_charts[i].st, all_softnet_charts[i].rd_dispatched,
1380                                   netisr_stats[i].dispatched);
1381             rrddim_set_by_pointer(all_softnet_charts[i].st, all_softnet_charts[i].rd_hybrid_dispatched,
1382                                   netisr_stats[i].hybrid_dispatched);
1383             rrddim_set_by_pointer(all_softnet_charts[i].st, all_softnet_charts[i].rd_qdrops,
1384                                   netisr_stats[i].qdrops);
1385             rrddim_set_by_pointer(all_softnet_charts[i].st, all_softnet_charts[i].rd_queued,
1386                                   netisr_stats[i].queued);
1387             rrdset_done(all_softnet_charts[i].st);
1388         }
1389     }
1390
1391     return 0;
1392 }
1393
1394 // --------------------------------------------------------------------------------------------------------------------
1395 // net.inet.tcp.states
1396
1397 int do_net_inet_tcp_states(int update_every, usec_t dt) {
1398     static int mib[4] = {0, 0, 0, 0};
1399     uint64_t tcps_states[TCP_NSTATES];
1400
1401     // see http://net-snmp.sourceforge.net/docs/mibs/tcp.html
1402     if (unlikely(GETSYSCTL_SIMPLE("net.inet.tcp.states", mib, tcps_states))) {
1403         error("DISABLED: ipv4.tcpsock chart");
1404         error("DISABLED: net.inet.tcp.states module");
1405         return 1;
1406     } else {
1407
1408         // --------------------------------------------------------------------
1409
1410         static RRDSET *st = NULL;
1411         static RRDDIM *rd = NULL;
1412
1413         if (unlikely(!st)) {
1414             st = rrdset_create_localhost("ipv4",
1415                                          "tcpsock",
1416                                          NULL,
1417                                          "tcp",
1418                                          NULL,
1419                                          "IPv4 TCP Connections",
1420                                          "active connections",
1421                                          2500,
1422                                          update_every,
1423                                          RRDSET_TYPE_LINE
1424             );
1425
1426             rd = rrddim_add(st, "CurrEstab", "connections", 1, 1, RRD_ALGORITHM_ABSOLUTE);
1427         } else
1428             rrdset_next(st);
1429
1430         rrddim_set_by_pointer(st, rd, tcps_states[TCPS_ESTABLISHED]);
1431         rrdset_done(st);
1432     }
1433
1434     return 0;
1435 }
1436
1437 // --------------------------------------------------------------------------------------------------------------------
1438 // net.inet.tcp.stats
1439
1440 int do_net_inet_tcp_stats(int update_every, usec_t dt) {
1441     static int do_tcp_packets = -1, do_tcp_errors = -1, do_tcp_handshake = -1, do_tcpext_connaborts = -1, do_tcpext_ofo = -1, do_tcpext_syncookies = -1, do_ecn = -1;
1442
1443     if (unlikely(do_tcp_packets == -1)) {
1444         do_tcp_packets       = config_get_boolean("plugin:freebsd:sysctl", "ipv4 TCP packets",          1);
1445         do_tcp_errors        = config_get_boolean("plugin:freebsd:sysctl", "ipv4 TCP errors",           1);
1446         do_tcp_handshake     = config_get_boolean("plugin:freebsd:sysctl", "ipv4 TCP handshake issues", 1);
1447         do_tcpext_connaborts = config_get_boolean_ondemand("plugin:freebsd:sysctl", "TCP connection aborts",
1448                                                            CONFIG_BOOLEAN_AUTO);
1449         do_tcpext_ofo        = config_get_boolean_ondemand("plugin:freebsd:sysctl", "TCP out-of-order queue",
1450                                                            CONFIG_BOOLEAN_AUTO);
1451         do_tcpext_syncookies = config_get_boolean_ondemand("plugin:freebsd:sysctl", "TCP SYN cookies",
1452                                                            CONFIG_BOOLEAN_AUTO);
1453         do_ecn               = config_get_boolean_ondemand("plugin:freebsd:sysctl", "ECN packets",
1454                                                            CONFIG_BOOLEAN_AUTO);
1455     }
1456
1457     // see http://net-snmp.sourceforge.net/docs/mibs/tcp.html
1458     if (likely(do_tcp_packets || do_tcp_errors || do_tcp_handshake || do_tcpext_connaborts || do_tcpext_ofo || do_tcpext_syncookies || do_ecn)) {
1459         static int mib[4] = {0, 0, 0, 0};
1460         struct tcpstat tcpstat;
1461
1462         if (unlikely(GETSYSCTL_SIMPLE("net.inet.tcp.stats", mib, tcpstat))) {
1463             do_tcp_packets = 0;
1464             error("DISABLED: ipv4.tcppackets chart");
1465             do_tcp_errors = 0;
1466             error("DISABLED: ipv4.tcperrors  chart");
1467             do_tcp_handshake = 0;
1468             error("DISABLED: ipv4.tcphandshake  chart");
1469             do_tcpext_connaborts = 0;
1470             error("DISABLED: ipv4.tcpconnaborts  chart");
1471             do_tcpext_ofo = 0;
1472             error("DISABLED: ipv4.tcpofo chart");
1473             do_tcpext_syncookies = 0;
1474             error("DISABLED: ipv4.tcpsyncookies chart");
1475             do_ecn = 0;
1476             error("DISABLED: ipv4.ecnpkts chart");
1477             error("DISABLED: net.inet.tcp.stats module");
1478             return 1;
1479         } else {
1480
1481             // --------------------------------------------------------------------
1482
1483             if (likely(do_tcp_packets)) {
1484                 static RRDSET *st = NULL;
1485                 static RRDDIM *rd_in_segs = NULL, *rd_out_segs = NULL;
1486
1487                 if (unlikely(!st)) {
1488                     st = rrdset_create_localhost("ipv4",
1489                                                          "tcppackets",
1490                                                          NULL,
1491                                                          "tcp",
1492                                                          NULL,
1493                                                          "IPv4 TCP Packets",
1494                                                          "packets/s",
1495                                                          2600,
1496                                                          update_every,
1497                                                          RRDSET_TYPE_LINE
1498                     );
1499
1500                     rd_in_segs  = rrddim_add(st, "InSegs", "received", 1, 1, RRD_ALGORITHM_INCREMENTAL);
1501                     rd_out_segs = rrddim_add(st, "OutSegs", "sent", -1, 1, RRD_ALGORITHM_INCREMENTAL);
1502                 } else
1503                     rrdset_next(st);
1504
1505                 rrddim_set_by_pointer(st, rd_in_segs, tcpstat.tcps_rcvtotal);
1506                 rrddim_set_by_pointer(st, rd_out_segs, tcpstat.tcps_sndtotal);
1507                 rrdset_done(st);
1508             }
1509
1510             // --------------------------------------------------------------------
1511
1512             if (likely(do_tcp_errors)) {
1513                 static RRDSET *st = NULL;
1514                 static RRDDIM *rd_in_errs = NULL, *rd_in_csum_errs = NULL, *rd_retrans_segs = NULL;
1515
1516                 if (unlikely(!st)) {
1517                     st = rrdset_create_localhost("ipv4",
1518                                                         "tcperrors",
1519                                                         NULL,
1520                                                         "tcp",
1521                                                         NULL,
1522                                                         "IPv4 TCP Errors",
1523                                                         "packets/s",
1524                                                         2700,
1525                                                         update_every,
1526                                                         RRDSET_TYPE_LINE
1527                     );
1528
1529                     rrdset_flag_set(st, RRDSET_FLAG_DETAIL);
1530
1531                     rd_in_errs      = rrddim_add(st, "InErrs",       NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
1532                     rd_in_csum_errs = rrddim_add(st, "InCsumErrors", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
1533                     rd_retrans_segs = rrddim_add(st, "RetransSegs",  NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
1534                 } else
1535                     rrdset_next(st);
1536
1537 #if __FreeBSD__ >= 11
1538                 rrddim_set_by_pointer(st, rd_in_errs,      tcpstat.tcps_rcvbadoff + tcpstat.tcps_rcvreassfull +
1539                                                                   tcpstat.tcps_rcvshort);
1540 #else
1541                 rrddim_set_by_pointer(st, rd_in_errs,      tcpstat.tcps_rcvbadoff + tcpstat.tcps_rcvshort);
1542 #endif
1543                 rrddim_set_by_pointer(st, rd_in_csum_errs, tcpstat.tcps_rcvbadsum);
1544                 rrddim_set_by_pointer(st, rd_retrans_segs, tcpstat.tcps_sndrexmitpack);
1545                 rrdset_done(st);
1546             }
1547
1548             // --------------------------------------------------------------------
1549
1550             if (likely(do_tcp_handshake)) {
1551                 static RRDSET *st = NULL;
1552                 static RRDDIM *rd_estab_resets = NULL, *rd_active_opens = NULL, *rd_passive_opens = NULL,
1553                               *rd_attempt_fails = NULL;
1554
1555                 if (unlikely(!st)) {
1556                     st = rrdset_create_localhost("ipv4",
1557                                                            "tcphandshake",
1558                                                            NULL,
1559                                                            "tcp",
1560                                                            NULL,
1561                                                            "IPv4 TCP Handshake Issues",
1562                                                            "events/s",
1563                                                            2900,
1564                                                            update_every,
1565                                                            RRDSET_TYPE_LINE
1566                     );
1567
1568                     rrdset_flag_set(st, RRDSET_FLAG_DETAIL);
1569
1570                     rd_estab_resets  = rrddim_add(st, "EstabResets",  NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
1571                     rd_active_opens  = rrddim_add(st, "ActiveOpens",  NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
1572                     rd_passive_opens = rrddim_add(st, "PassiveOpens", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
1573                     rd_attempt_fails = rrddim_add(st, "AttemptFails", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
1574                 } else
1575                     rrdset_next(st);
1576
1577                 rrddim_set_by_pointer(st, rd_estab_resets,  tcpstat.tcps_drops);
1578                 rrddim_set_by_pointer(st, rd_active_opens,  tcpstat.tcps_connattempt);
1579                 rrddim_set_by_pointer(st, rd_passive_opens, tcpstat.tcps_accepts);
1580                 rrddim_set_by_pointer(st, rd_attempt_fails, tcpstat.tcps_conndrops);
1581                 rrdset_done(st);
1582             }
1583
1584             // --------------------------------------------------------------------
1585
1586             if (do_tcpext_connaborts == CONFIG_BOOLEAN_YES || (do_tcpext_connaborts == CONFIG_BOOLEAN_AUTO && (tcpstat.tcps_rcvpackafterwin || tcpstat.tcps_rcvafterclose || tcpstat.tcps_rcvmemdrop || tcpstat.tcps_persistdrop || tcpstat.tcps_finwait2_drops))) {
1587                 do_tcpext_connaborts = CONFIG_BOOLEAN_YES;
1588
1589                 static RRDSET *st = NULL;
1590                 static RRDDIM *rd_on_data = NULL, *rd_on_close = NULL, *rd_on_memory = NULL,
1591                               *rd_on_timeout = NULL, *rd_on_linger = NULL;
1592
1593                 if (unlikely(!st)) {
1594                     st = rrdset_create_localhost("ipv4",
1595                                                             "tcpconnaborts",
1596                                                             NULL,
1597                                                             "tcp",
1598                                                             NULL,
1599                                                             "TCP Connection Aborts",
1600                                                             "connections/s",
1601                                                             3010,
1602                                                             update_every,
1603                                                             RRDSET_TYPE_LINE
1604                     );
1605
1606                     rd_on_data    = rrddim_add(st, "TCPAbortOnData",    "baddata",     1, 1, RRD_ALGORITHM_INCREMENTAL);
1607                     rd_on_close   = rrddim_add(st, "TCPAbortOnClose",   "userclosed",  1, 1, RRD_ALGORITHM_INCREMENTAL);
1608                     rd_on_memory  = rrddim_add(st, "TCPAbortOnMemory",  "nomemory",    1, 1, RRD_ALGORITHM_INCREMENTAL);
1609                     rd_on_timeout = rrddim_add(st, "TCPAbortOnTimeout", "timeout",     1, 1, RRD_ALGORITHM_INCREMENTAL);
1610                     rd_on_linger  = rrddim_add(st, "TCPAbortOnLinger",  "linger",      1, 1, RRD_ALGORITHM_INCREMENTAL);
1611                 }
1612                 else rrdset_next(st);
1613
1614                 rrddim_set_by_pointer(st, rd_on_data,    tcpstat.tcps_rcvpackafterwin);
1615                 rrddim_set_by_pointer(st, rd_on_close,   tcpstat.tcps_rcvafterclose);
1616                 rrddim_set_by_pointer(st, rd_on_memory,  tcpstat.tcps_rcvmemdrop);
1617                 rrddim_set_by_pointer(st, rd_on_timeout, tcpstat.tcps_persistdrop);
1618                 rrddim_set_by_pointer(st, rd_on_linger,  tcpstat.tcps_finwait2_drops);
1619                 rrdset_done(st);
1620             }
1621
1622             // --------------------------------------------------------------------
1623
1624             if (do_tcpext_ofo == CONFIG_BOOLEAN_YES || (do_tcpext_ofo == CONFIG_BOOLEAN_AUTO && tcpstat.tcps_rcvoopack)) {
1625                 do_tcpext_ofo = CONFIG_BOOLEAN_YES;
1626
1627                 static RRDSET *st = NULL;
1628                 static RRDDIM *rd_ofo_queue = NULL;
1629
1630                 if (unlikely(!st)) {
1631                     st = rrdset_create_localhost("ipv4",
1632                                                            "tcpofo",
1633                                                            NULL,
1634                                                            "tcp",
1635                                                            NULL,
1636                                                            "TCP Out-Of-Order Queue",
1637                                                            "packets/s",
1638                                                            3050,
1639                                                            update_every,
1640                                                            RRDSET_TYPE_LINE
1641                     );
1642
1643                     rd_ofo_queue = rrddim_add(st, "TCPOFOQueue", "inqueue",  1, 1, RRD_ALGORITHM_INCREMENTAL);
1644                 }
1645                 else rrdset_next(st);
1646
1647                 rrddim_set_by_pointer(st, rd_ofo_queue,   tcpstat.tcps_rcvoopack);
1648                 rrdset_done(st);
1649             }
1650
1651             // --------------------------------------------------------------------
1652
1653             if (do_tcpext_syncookies == CONFIG_BOOLEAN_YES || (do_tcpext_syncookies == CONFIG_BOOLEAN_AUTO && (tcpstat.tcps_sc_sendcookie || tcpstat.tcps_sc_recvcookie || tcpstat.tcps_sc_zonefail))) {
1654                 do_tcpext_syncookies = CONFIG_BOOLEAN_YES;
1655
1656                 static RRDSET *st = NULL;
1657                 static RRDDIM *rd_recv = NULL, *rd_send = NULL, *rd_failed = NULL;
1658
1659                 if (unlikely(!st)) {
1660                     st = rrdset_create_localhost("ipv4",
1661                                                             "tcpsyncookies",
1662                                                             NULL,
1663                                                             "tcp",
1664                                                             NULL,
1665                                                             "TCP SYN Cookies",
1666                                                             "packets/s",
1667                                                             3100,
1668                                                             update_every,
1669                                                             RRDSET_TYPE_LINE
1670                     );
1671
1672                     rd_recv   = rrddim_add(st, "SyncookiesRecv",   "received",  1, 1, RRD_ALGORITHM_INCREMENTAL);
1673                     rd_send   = rrddim_add(st, "SyncookiesSent",   "sent",     -1, 1, RRD_ALGORITHM_INCREMENTAL);
1674                     rd_failed = rrddim_add(st, "SyncookiesFailed", "failed",   -1, 1, RRD_ALGORITHM_INCREMENTAL);
1675                 }
1676                 else rrdset_next(st);
1677
1678                 rrddim_set_by_pointer(st, rd_recv,   tcpstat.tcps_sc_recvcookie);
1679                 rrddim_set_by_pointer(st, rd_send,   tcpstat.tcps_sc_sendcookie);
1680                 rrddim_set_by_pointer(st, rd_failed, tcpstat.tcps_sc_zonefail);
1681                 rrdset_done(st);
1682             }
1683
1684             // --------------------------------------------------------------------
1685
1686             if (do_ecn == CONFIG_BOOLEAN_YES || (do_ecn == CONFIG_BOOLEAN_AUTO && (tcpstat.tcps_ecn_ce || tcpstat.tcps_ecn_ect0 || tcpstat.tcps_ecn_ect1))) {
1687                 do_ecn = CONFIG_BOOLEAN_YES;
1688
1689                 static RRDSET *st = NULL;
1690                 static RRDDIM *rd_ce = NULL, *rd_no_ect = NULL, *rd_ect0 = NULL, *rd_ect1 = NULL;
1691
1692                 if (unlikely(!st)) {
1693                     st = rrdset_create_localhost("ipv4",
1694                                                      "ecnpkts",
1695                                                      NULL,
1696                                                      "ecn",
1697                                                      NULL,
1698                                                      "IPv4 ECN Statistics",
1699                                                      "packets/s",
1700                                                      8700,
1701                                                      update_every,
1702                                                      RRDSET_TYPE_LINE
1703                     );
1704
1705                     rrdset_flag_set(st, RRDSET_FLAG_DETAIL);
1706
1707                     rd_ce     = rrddim_add(st, "InCEPkts", "CEP", 1, 1, RRD_ALGORITHM_INCREMENTAL);
1708                     rd_no_ect = rrddim_add(st, "InNoECTPkts", "NoECTP", -1, 1, RRD_ALGORITHM_INCREMENTAL);
1709                     rd_ect0   = rrddim_add(st, "InECT0Pkts", "ECTP0", 1, 1, RRD_ALGORITHM_INCREMENTAL);
1710                     rd_ect1   = rrddim_add(st, "InECT1Pkts", "ECTP1", 1, 1, RRD_ALGORITHM_INCREMENTAL);
1711                 }
1712                 else rrdset_next(st);
1713
1714                 rrddim_set_by_pointer(st, rd_ce,     tcpstat.tcps_ecn_ce);
1715                 rrddim_set_by_pointer(st, rd_no_ect, tcpstat.tcps_ecn_ce - (tcpstat.tcps_ecn_ect0 +
1716                                                                                 tcpstat.tcps_ecn_ect1));
1717                 rrddim_set_by_pointer(st, rd_ect0,   tcpstat.tcps_ecn_ect0);
1718                 rrddim_set_by_pointer(st, rd_ect1,   tcpstat.tcps_ecn_ect1);
1719                 rrdset_done(st);
1720             }
1721
1722         }
1723     } else {
1724         error("DISABLED: net.inet.tcp.stats module");
1725         return 1;
1726     }
1727
1728     return 0;
1729 }
1730
1731 // --------------------------------------------------------------------------------------------------------------------
1732 // net.inet.udp.stats
1733
1734 int do_net_inet_udp_stats(int update_every, usec_t dt) {
1735     static int do_udp_packets = -1, do_udp_errors = -1;
1736
1737     if (unlikely(do_udp_packets == -1)) {
1738         do_udp_packets = config_get_boolean("plugin:freebsd:sysctl", "ipv4 UDP packets", 1);
1739         do_udp_errors  = config_get_boolean("plugin:freebsd:sysctl", "ipv4 UDP errors", 1);
1740     }
1741
1742     // see http://net-snmp.sourceforge.net/docs/mibs/udp.html
1743     if (likely(do_udp_packets || do_udp_errors)) {
1744         static int mib[4] = {0, 0, 0, 0};
1745         struct udpstat udpstat;
1746
1747         if (unlikely(GETSYSCTL_SIMPLE("net.inet.udp.stats", mib, udpstat))) {
1748             do_udp_packets = 0;
1749             error("DISABLED: ipv4.udppackets chart");
1750             do_udp_errors = 0;
1751             error("DISABLED: ipv4.udperrors chart");
1752             error("DISABLED: net.inet.udp.stats module");
1753             return 1;
1754         } else {
1755
1756             // --------------------------------------------------------------------
1757
1758             if (likely(do_udp_packets)) {
1759                 static RRDSET *st = NULL;
1760                 static RRDDIM *rd_in = NULL, *rd_out = NULL;
1761
1762                 if (unlikely(!st)) {
1763                     st = rrdset_create_localhost("ipv4",
1764                                                  "udppackets",
1765                                                  NULL,
1766                                                  "udp",
1767                                                  NULL,
1768                                                  "IPv4 UDP Packets",
1769                                                  "packets/s",
1770                                                  2601,
1771                                                  update_every,
1772                                                  RRDSET_TYPE_LINE
1773                     );
1774
1775                     rd_in  = rrddim_add(st, "InDatagrams",  "received", 1, 1, RRD_ALGORITHM_INCREMENTAL);
1776                     rd_out = rrddim_add(st, "OutDatagrams", "sent",    -1, 1, RRD_ALGORITHM_INCREMENTAL);
1777                 } else
1778                     rrdset_next(st);
1779
1780                 rrddim_set_by_pointer(st, rd_in,  udpstat.udps_ipackets);
1781                 rrddim_set_by_pointer(st, rd_out, udpstat.udps_opackets);
1782                 rrdset_done(st);
1783             }
1784
1785             // --------------------------------------------------------------------
1786
1787             if (likely(do_udp_errors)) {
1788                 static RRDSET *st = NULL;
1789                 static RRDDIM *rd_in_errors = NULL, *rd_no_ports = NULL, *rd_recv_buf_errors = NULL,
1790                               *rd_in_csum_errors = NULL, *rd_ignored_multi = NULL;
1791
1792                 if (unlikely(!st)) {
1793                     st = rrdset_create_localhost("ipv4",
1794                                                  "udperrors",
1795                                                  NULL,
1796                                                  "udp",
1797                                                  NULL,
1798                                                  "IPv4 UDP Errors",
1799                                                  "events/s",
1800                                                  2701,
1801                                                  update_every,
1802                                                  RRDSET_TYPE_LINE
1803                     );
1804
1805                     rrdset_flag_set(st, RRDSET_FLAG_DETAIL);
1806
1807                     rd_in_errors       = rrddim_add(st, "InErrors",     NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
1808                     rd_no_ports        = rrddim_add(st, "NoPorts",      NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
1809                     rd_recv_buf_errors = rrddim_add(st, "RcvbufErrors", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
1810                     rd_in_csum_errors  = rrddim_add(st, "InCsumErrors", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
1811                     rd_ignored_multi   = rrddim_add(st, "IgnoredMulti", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
1812                 } else
1813                     rrdset_next(st);
1814
1815                 rrddim_set_by_pointer(st, rd_in_errors, udpstat.udps_hdrops + udpstat.udps_badlen);
1816                 rrddim_set_by_pointer(st, rd_no_ports, udpstat.udps_noport);
1817                 rrddim_set_by_pointer(st, rd_recv_buf_errors, udpstat.udps_fullsock);
1818                 rrddim_set_by_pointer(st, rd_in_csum_errors, udpstat.udps_badsum + udpstat.udps_nosum);
1819                 rrddim_set_by_pointer(st, rd_ignored_multi, udpstat.udps_filtermcast);
1820                 rrdset_done(st);
1821             }
1822         }
1823     } else {
1824         error("DISABLED: net.inet.udp.stats module");
1825         return 1;
1826     }
1827
1828     return 0;
1829 }
1830
1831 // --------------------------------------------------------------------------------------------------------------------
1832 // net.inet.icmp.stats
1833
1834 int do_net_inet_icmp_stats(int update_every, usec_t dt) {
1835     static int do_icmp_packets = -1, do_icmp_errors = -1, do_icmpmsg = -1;
1836
1837     if (unlikely(do_icmp_packets == -1)) {
1838         do_icmp_packets = config_get_boolean("plugin:freebsd:sysctl", "ipv4 ICMP packets",  1);
1839         do_icmp_errors  = config_get_boolean("plugin:freebsd:sysctl", "ipv4 ICMP errors",   1);
1840         do_icmpmsg      = config_get_boolean("plugin:freebsd:sysctl", "ipv4 ICMP messages", 1);
1841     }
1842
1843     if (likely(do_icmp_packets || do_icmp_errors || do_icmpmsg)) {
1844         static int mib[4] = {0, 0, 0, 0};
1845         struct icmpstat icmpstat;
1846         int i;
1847         struct icmp_total {
1848             u_long  msgs_in;
1849             u_long  msgs_out;
1850         } icmp_total = {0, 0};
1851
1852         if (unlikely(GETSYSCTL_SIMPLE("net.inet.icmp.stats", mib, icmpstat))) {
1853             do_icmp_packets = 0;
1854             error("DISABLED: ipv4.icmp chart");
1855             do_icmp_errors = 0;
1856             error("DISABLED: ipv4.icmp_errors chart");
1857             do_icmpmsg = 0;
1858             error("DISABLED: ipv4.icmpmsg chart");
1859             error("DISABLED: net.inet.icmp.stats module");
1860             return 1;
1861         } else {
1862             for (i = 0; i <= ICMP_MAXTYPE; i++) {
1863                 icmp_total.msgs_in += icmpstat.icps_inhist[i];
1864                 icmp_total.msgs_out += icmpstat.icps_outhist[i];
1865             }
1866             icmp_total.msgs_in += icmpstat.icps_badcode + icmpstat.icps_badlen + icmpstat.icps_checksum + icmpstat.icps_tooshort;
1867
1868             // --------------------------------------------------------------------
1869
1870             if (likely(do_icmp_packets)) {
1871                 static RRDSET *st = NULL;
1872                 static RRDDIM *rd_in = NULL, *rd_out = NULL;
1873
1874                 if (unlikely(!st)) {
1875                     st = rrdset_create_localhost("ipv4",
1876                                                  "icmp",
1877                                                  NULL,
1878                                                  "icmp",
1879                                                  NULL,
1880                                                  "IPv4 ICMP Packets",
1881                                                  "packets/s",
1882                                                  2602,
1883                                                  update_every,
1884                                                  RRDSET_TYPE_LINE
1885                     );
1886
1887                     rd_in  = rrddim_add(st, "InMsgs",  "received", 1, 1, RRD_ALGORITHM_INCREMENTAL);
1888                     rd_out = rrddim_add(st, "OutMsgs", "sent",    -1, 1, RRD_ALGORITHM_INCREMENTAL);
1889                 } else
1890                     rrdset_next(st);
1891
1892                 rrddim_set_by_pointer(st, rd_in,  icmp_total.msgs_in);
1893                 rrddim_set_by_pointer(st, rd_out, icmp_total.msgs_out);
1894
1895                 rrdset_done(st);
1896             }
1897
1898             // --------------------------------------------------------------------
1899
1900             if (likely(do_icmp_errors)) {
1901                 static RRDSET *st = NULL;
1902                 static RRDDIM *rd_in = NULL, *rd_out = NULL, *rd_in_csum = NULL;
1903
1904                 if (unlikely(!st)) {
1905                     st = rrdset_create_localhost("ipv4", "icmp_errors", NULL, "icmp", NULL, "IPv4 ICMP Errors",
1906                                                  "packets/s",
1907                                                  2603, update_every, RRDSET_TYPE_LINE);
1908
1909                     rd_in      = rrddim_add(st, "InErrors", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
1910                     rd_out     = rrddim_add(st, "OutErrors", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
1911                     rd_in_csum = rrddim_add(st, "InCsumErrors", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
1912                 } else
1913                     rrdset_next(st);
1914
1915                 rrddim_set_by_pointer(st, rd_in,      icmpstat.icps_badcode + icmpstat.icps_badlen +
1916                                                       icmpstat.icps_checksum + icmpstat.icps_tooshort);
1917                 rrddim_set_by_pointer(st, rd_out,     icmpstat.icps_error);
1918                 rrddim_set_by_pointer(st, rd_in_csum, icmpstat.icps_checksum);
1919
1920                 rrdset_done(st);
1921             }
1922
1923             // --------------------------------------------------------------------
1924
1925             if (likely(do_icmpmsg)) {
1926                 static RRDSET *st = NULL;
1927                 static RRDDIM *rd_in_reps = NULL, *rd_out_reps = NULL, *rd_in = NULL, *rd_out = NULL;
1928
1929                 if (unlikely(!st)) {
1930                     st = rrdset_create_localhost("ipv4", "icmpmsg", NULL, "icmp", NULL, "IPv4 ICMP Messsages",
1931                                                  "packets/s", 2604, update_every, RRDSET_TYPE_LINE);
1932
1933                     rd_in_reps  = rrddim_add(st, "InEchoReps",  NULL,  1, 1, RRD_ALGORITHM_INCREMENTAL);
1934                     rd_out_reps = rrddim_add(st, "OutEchoReps", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
1935                     rd_in       = rrddim_add(st, "InEchos",     NULL,  1, 1, RRD_ALGORITHM_INCREMENTAL);
1936                     rd_out      = rrddim_add(st, "OutEchos",    NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
1937                 } else
1938                     rrdset_next(st);
1939
1940                 rrddim_set_by_pointer(st, rd_in_reps, icmpstat.icps_inhist[ICMP_ECHOREPLY]);
1941                 rrddim_set_by_pointer(st, rd_out_reps, icmpstat.icps_outhist[ICMP_ECHOREPLY]);
1942                 rrddim_set_by_pointer(st, rd_in, icmpstat.icps_inhist[ICMP_ECHO]);
1943                 rrddim_set_by_pointer(st, rd_out, icmpstat.icps_outhist[ICMP_ECHO]);
1944
1945                 rrdset_done(st);
1946             }
1947         }
1948     } else {
1949         error("DISABLED: net.inet.icmp.stats module");
1950         return 1;
1951     }
1952
1953     return 0;
1954 }
1955
1956 // --------------------------------------------------------------------------------------------------------------------
1957 // net.inet.ip.stats
1958
1959 int do_net_inet_ip_stats(int update_every, usec_t dt) {
1960     static int do_ip_packets = -1, do_ip_fragsout = -1, do_ip_fragsin = -1, do_ip_errors = -1;
1961
1962     if (unlikely(do_ip_packets == -1)) {
1963         do_ip_packets  = config_get_boolean("plugin:freebsd:sysctl", "ipv4 packets", 1);
1964         do_ip_fragsout = config_get_boolean("plugin:freebsd:sysctl", "ipv4 fragments sent", 1);
1965         do_ip_fragsin  = config_get_boolean("plugin:freebsd:sysctl", "ipv4 fragments assembly", 1);
1966         do_ip_errors   = config_get_boolean("plugin:freebsd:sysctl", "ipv4 errors", 1);
1967     }
1968
1969     // see also http://net-snmp.sourceforge.net/docs/mibs/ip.html
1970     if (likely(do_ip_packets || do_ip_fragsout || do_ip_fragsin || do_ip_errors)) {
1971         static int mib[4] = {0, 0, 0, 0};
1972         struct ipstat ipstat;
1973
1974         if (unlikely(GETSYSCTL_SIMPLE("net.inet.ip.stats", mib, ipstat))) {
1975             do_ip_packets = 0;
1976             error("DISABLED: ipv4.packets chart");
1977             do_ip_fragsout = 0;
1978             error("DISABLED: ipv4.fragsout chart");
1979             do_ip_fragsin = 0;
1980             error("DISABLED: ipv4.fragsin chart");
1981             do_ip_errors = 0;
1982             error("DISABLED: ipv4.errors chart");
1983             error("DISABLED: net.inet.ip.stats module");
1984             return 1;
1985         } else {
1986
1987             // --------------------------------------------------------------------
1988
1989             if (likely(do_ip_packets)) {
1990                 static RRDSET *st = NULL;
1991                 static RRDDIM *rd_in_receives = NULL, *rd_out_requests = NULL, *rd_forward_datagrams = NULL,
1992                               *rd_in_delivers = NULL;
1993
1994                 if (unlikely(!st)) {
1995                     st = rrdset_create_localhost("ipv4",
1996                                                  "packets",
1997                                                  NULL,
1998                                                  "packets",
1999                                                  NULL,
2000                                                  "IPv4 Packets",
2001                                                  "packets/s",
2002                                                  3000,
2003                                                  update_every,
2004                                                  RRDSET_TYPE_LINE
2005                     );
2006
2007                     rd_in_receives       = rrddim_add(st, "InReceives",    "received",  1, 1, RRD_ALGORITHM_INCREMENTAL);
2008                     rd_out_requests      = rrddim_add(st, "OutRequests",   "sent",     -1, 1, RRD_ALGORITHM_INCREMENTAL);
2009                     rd_forward_datagrams = rrddim_add(st, "ForwDatagrams", "forwarded", 1, 1, RRD_ALGORITHM_INCREMENTAL);
2010                     rd_in_delivers       = rrddim_add(st, "InDelivers",    "delivered", 1, 1, RRD_ALGORITHM_INCREMENTAL);
2011                 } else
2012                     rrdset_next(st);
2013
2014                 rrddim_set_by_pointer(st, rd_in_receives,       ipstat.ips_total);
2015                 rrddim_set_by_pointer(st, rd_out_requests,      ipstat.ips_localout);
2016                 rrddim_set_by_pointer(st, rd_forward_datagrams, ipstat.ips_forward);
2017                 rrddim_set_by_pointer(st, rd_in_delivers,       ipstat.ips_delivered);
2018                 rrdset_done(st);
2019             }
2020
2021             // --------------------------------------------------------------------
2022
2023             if (likely(do_ip_fragsout)) {
2024                 static RRDSET *st = NULL;
2025                 static RRDDIM *rd_ok = NULL, *rd_fails = NULL, *rd_created = NULL;
2026
2027                 if (unlikely(!st)) {
2028                     st = rrdset_create_localhost("ipv4",
2029                                                  "fragsout",
2030                                                  NULL,
2031                                                  "fragments",
2032                                                  NULL,
2033                                                  "IPv4 Fragments Sent",
2034                                                  "packets/s",
2035                                                  3010,
2036                                                  update_every,
2037                                                  RRDSET_TYPE_LINE
2038                     );
2039
2040                     rrdset_flag_set(st, RRDSET_FLAG_DETAIL);
2041
2042                     rd_ok      = rrddim_add(st, "FragOKs",     "ok",      1, 1, RRD_ALGORITHM_INCREMENTAL);
2043                     rd_fails   = rrddim_add(st, "FragFails",   "failed", -1, 1, RRD_ALGORITHM_INCREMENTAL);
2044                     rd_created = rrddim_add(st, "FragCreates", "created", 1, 1, RRD_ALGORITHM_INCREMENTAL);
2045                 } else
2046                     rrdset_next(st);
2047
2048                 rrddim_set_by_pointer(st, rd_ok,      ipstat.ips_fragmented);
2049                 rrddim_set_by_pointer(st, rd_fails,   ipstat.ips_cantfrag);
2050                 rrddim_set_by_pointer(st, rd_created, ipstat.ips_ofragments);
2051                 rrdset_done(st);
2052             }
2053
2054             // --------------------------------------------------------------------
2055
2056             if (likely(do_ip_fragsin)) {
2057                 static RRDSET *st = NULL;
2058                 static RRDDIM *rd_ok = NULL, *rd_failed = NULL, *rd_all = NULL;
2059
2060                 if (unlikely(!st)) {
2061                     st = rrdset_create_localhost("ipv4",
2062                                                  "fragsin",
2063                                                  NULL,
2064                                                  "fragments",
2065                                                  NULL,
2066                                                  "IPv4 Fragments Reassembly",
2067                                                  "packets/s",
2068                                                  3011,
2069                                                  update_every,
2070                                                  RRDSET_TYPE_LINE
2071                     );
2072
2073                     rrdset_flag_set(st, RRDSET_FLAG_DETAIL);
2074
2075                     rd_ok     = rrddim_add(st, "ReasmOKs",   "ok",      1, 1, RRD_ALGORITHM_INCREMENTAL);
2076                     rd_failed = rrddim_add(st, "ReasmFails", "failed", -1, 1, RRD_ALGORITHM_INCREMENTAL);
2077                     rd_all    = rrddim_add(st, "ReasmReqds", "all",     1, 1, RRD_ALGORITHM_INCREMENTAL);
2078                 } else
2079                     rrdset_next(st);
2080
2081                 rrddim_set_by_pointer(st, rd_ok,     ipstat.ips_fragments);
2082                 rrddim_set_by_pointer(st, rd_failed, ipstat.ips_fragdropped);
2083                 rrddim_set_by_pointer(st, rd_all,    ipstat.ips_reassembled);
2084                 rrdset_done(st);
2085             }
2086
2087             // --------------------------------------------------------------------
2088
2089             if (likely(do_ip_errors)) {
2090                 static RRDSET *st = NULL;
2091                 static RRDDIM *rd_in_discards = NULL, *rd_out_discards = NULL,
2092                               *rd_in_hdr_errors = NULL, *rd_out_no_routes = NULL,
2093                               *rd_in_addr_errors = NULL, *rd_in_unknown_protos = NULL;
2094
2095                 if (unlikely(!st)) {
2096                     st = rrdset_create_localhost("ipv4",
2097                                                  "errors",
2098                                                  NULL,
2099                                                  "errors",
2100                                                  NULL,
2101                                                  "IPv4 Errors",
2102                                                  "packets/s",
2103                                                  3002,
2104                                                  update_every,
2105                                                  RRDSET_TYPE_LINE
2106                     );
2107
2108                     rrdset_flag_set(st, RRDSET_FLAG_DETAIL);
2109
2110                     rd_in_discards       = rrddim_add(st, "InDiscards",      NULL,  1, 1, RRD_ALGORITHM_INCREMENTAL);
2111                     rd_out_discards      = rrddim_add(st, "OutDiscards",     NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
2112                     rd_in_hdr_errors     = rrddim_add(st, "InHdrErrors",     NULL,  1, 1, RRD_ALGORITHM_INCREMENTAL);
2113                     rd_out_no_routes     = rrddim_add(st, "OutNoRoutes",     NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
2114                     rd_in_addr_errors    = rrddim_add(st, "InAddrErrors",    NULL,  1, 1, RRD_ALGORITHM_INCREMENTAL);
2115                     rd_in_unknown_protos = rrddim_add(st, "InUnknownProtos", NULL,  1, 1, RRD_ALGORITHM_INCREMENTAL);
2116                 } else
2117                     rrdset_next(st);
2118
2119                 rrddim_set_by_pointer(st, rd_in_discards,       ipstat.ips_badsum + ipstat.ips_tooshort +
2120                                                                 ipstat.ips_toosmall + ipstat.ips_toolong);
2121                 rrddim_set_by_pointer(st, rd_out_discards,      ipstat.ips_odropped);
2122                 rrddim_set_by_pointer(st, rd_in_hdr_errors,     ipstat.ips_badhlen + ipstat.ips_badlen +
2123                                                                 ipstat.ips_badoptions + ipstat.ips_badvers);
2124                 rrddim_set_by_pointer(st, rd_out_no_routes,     ipstat.ips_noroute);
2125                 rrddim_set_by_pointer(st, rd_in_addr_errors,    ipstat.ips_badaddr);
2126                 rrddim_set_by_pointer(st, rd_in_unknown_protos, ipstat.ips_noproto);
2127                 rrdset_done(st);
2128             }
2129         }
2130     } else {
2131         error("DISABLED: net.inet.ip.stats module");
2132         return 1;
2133     }
2134
2135     return 0;
2136 }
2137
2138 // --------------------------------------------------------------------------------------------------------------------
2139 // net.inet6.ip6.stats
2140
2141 int do_net_inet6_ip6_stats(int update_every, usec_t dt) {
2142     static int do_ip6_packets = -1, do_ip6_fragsout = -1, do_ip6_fragsin = -1, do_ip6_errors = -1;
2143
2144     if (unlikely(do_ip6_packets == -1)) {
2145         do_ip6_packets  = config_get_boolean_ondemand("plugin:freebsd:sysctl", "ipv6 packets",        CONFIG_BOOLEAN_AUTO);
2146         do_ip6_fragsout = config_get_boolean_ondemand("plugin:freebsd:sysctl", "ipv6 fragments sent", CONFIG_BOOLEAN_AUTO);
2147         do_ip6_fragsin  = config_get_boolean_ondemand("plugin:freebsd:sysctl", "ipv6 fragments assembly",
2148                                                       CONFIG_BOOLEAN_AUTO);
2149         do_ip6_errors   = config_get_boolean_ondemand("plugin:freebsd:sysctl", "ipv6 errors",         CONFIG_BOOLEAN_AUTO);
2150     }
2151
2152     if (likely(do_ip6_packets || do_ip6_fragsout || do_ip6_fragsin || do_ip6_errors)) {
2153         static int mib[4] = {0, 0, 0, 0};
2154         struct ip6stat ip6stat;
2155
2156         if (unlikely(GETSYSCTL_SIMPLE("net.inet6.ip6.stats", mib, ip6stat))) {
2157             do_ip6_packets = 0;
2158             error("DISABLED: ipv6.packets chart");
2159             do_ip6_fragsout = 0;
2160             error("DISABLED: ipv6.fragsout chart");
2161             do_ip6_fragsin = 0;
2162             error("DISABLED: ipv6.fragsin chart");
2163             do_ip6_errors = 0;
2164             error("DISABLED: ipv6.errors chart");
2165             error("DISABLED: net.inet6.ip6.stats module");
2166             return 1;
2167         } else {
2168
2169             // --------------------------------------------------------------------
2170
2171             if (do_ip6_packets == CONFIG_BOOLEAN_YES || (do_ip6_packets == CONFIG_BOOLEAN_AUTO &&
2172                                                          (ip6stat.ip6s_localout || ip6stat.ip6s_total ||
2173                                                           ip6stat.ip6s_forward || ip6stat.ip6s_delivered))) {
2174                 do_ip6_packets = CONFIG_BOOLEAN_YES;
2175
2176                 static RRDSET *st = NULL;
2177                 static RRDDIM *rd_received = NULL, *rd_sent = NULL, *rd_forwarded = NULL, *rd_delivers = NULL;
2178
2179                 if (unlikely(!st)) {
2180                     st = rrdset_create_localhost("ipv6",
2181                                                  "packets",
2182                                                  NULL,
2183                                                  "packets",
2184                                                  NULL,
2185                                                  "IPv6 Packets",
2186                                                  "packets/s",
2187                                                  3000,
2188                                                  update_every,
2189                                                  RRDSET_TYPE_LINE
2190                     );
2191
2192                     rd_received  = rrddim_add(st, "received",  NULL,  1, 1, RRD_ALGORITHM_INCREMENTAL);
2193                     rd_sent      = rrddim_add(st, "sent",      NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
2194                     rd_forwarded = rrddim_add(st, "forwarded", NULL,  1, 1, RRD_ALGORITHM_INCREMENTAL);
2195                     rd_delivers  = rrddim_add(st, "delivers",  NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
2196                 } else
2197                     rrdset_next(st);
2198
2199                 rrddim_set_by_pointer(st, rd_sent,      ip6stat.ip6s_localout);
2200                 rrddim_set_by_pointer(st, rd_received,  ip6stat.ip6s_total);
2201                 rrddim_set_by_pointer(st, rd_forwarded, ip6stat.ip6s_forward);
2202                 rrddim_set_by_pointer(st, rd_delivers,  ip6stat.ip6s_delivered);
2203                 rrdset_done(st);
2204             }
2205
2206             // --------------------------------------------------------------------
2207
2208             if (do_ip6_fragsout == CONFIG_BOOLEAN_YES || (do_ip6_fragsout == CONFIG_BOOLEAN_AUTO &&
2209                                                           (ip6stat.ip6s_fragmented || ip6stat.ip6s_cantfrag ||
2210                                                            ip6stat.ip6s_ofragments))) {
2211                 do_ip6_fragsout = CONFIG_BOOLEAN_YES;
2212
2213                 static RRDSET *st = NULL;
2214                 static RRDDIM *rd_ok = NULL, *rd_failed = NULL, *rd_all = NULL;
2215
2216                 if (unlikely(!st)) {
2217                     st = rrdset_create_localhost("ipv6",
2218                                                  "fragsout",
2219                                                  NULL,
2220                                                  "fragments",
2221                                                  NULL,
2222                                                  "IPv6 Fragments Sent",
2223                                                  "packets/s",
2224                                                  3010,
2225                                                  update_every,
2226                                                  RRDSET_TYPE_LINE
2227                     );
2228
2229                     rrdset_flag_set(st, RRDSET_FLAG_DETAIL);
2230
2231                     rd_ok     = rrddim_add(st, "ok",     NULL,  1, 1, RRD_ALGORITHM_INCREMENTAL);
2232                     rd_failed = rrddim_add(st, "failed", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
2233                     rd_all    = rrddim_add(st, "all",    NULL,  1, 1, RRD_ALGORITHM_INCREMENTAL);
2234                 } else
2235                     rrdset_next(st);
2236
2237                 rrddim_set_by_pointer(st, rd_ok, ip6stat.ip6s_fragmented);
2238                 rrddim_set_by_pointer(st, rd_failed, ip6stat.ip6s_cantfrag);
2239                 rrddim_set_by_pointer(st, rd_all, ip6stat.ip6s_ofragments);
2240                 rrdset_done(st);
2241             }
2242
2243             // --------------------------------------------------------------------
2244
2245             if (do_ip6_fragsin == CONFIG_BOOLEAN_YES || (do_ip6_fragsin == CONFIG_BOOLEAN_AUTO &&
2246                                                          (ip6stat.ip6s_reassembled || ip6stat.ip6s_fragdropped ||
2247                                                           ip6stat.ip6s_fragtimeout || ip6stat.ip6s_fragments))) {
2248                 do_ip6_fragsin = CONFIG_BOOLEAN_YES;
2249
2250                 static RRDSET *st = NULL;
2251                 static RRDDIM *rd_ok = NULL, *rd_failed = NULL, *rd_timeout = NULL, *rd_all = NULL;
2252
2253                 if (unlikely(!st)) {
2254                     st = rrdset_create_localhost("ipv6",
2255                                                  "fragsin",
2256                                                  NULL,
2257                                                  "fragments",
2258                                                  NULL,
2259                                                  "IPv6 Fragments Reassembly",
2260                                                  "packets/s",
2261                                                  3011,
2262                                                  update_every,
2263                                                  RRDSET_TYPE_LINE
2264                     );
2265
2266                     rrdset_flag_set(st, RRDSET_FLAG_DETAIL);
2267
2268                     rd_ok      = rrddim_add(st, "ok",      NULL,  1, 1, RRD_ALGORITHM_INCREMENTAL);
2269                     rd_failed  = rrddim_add(st, "failed",  NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
2270                     rd_timeout = rrddim_add(st, "timeout", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
2271                     rd_all     = rrddim_add(st, "all",     NULL,  1, 1, RRD_ALGORITHM_INCREMENTAL);
2272                 } else
2273                     rrdset_next(st);
2274
2275                 rrddim_set_by_pointer(st, rd_ok,      ip6stat.ip6s_reassembled);
2276                 rrddim_set_by_pointer(st, rd_failed,  ip6stat.ip6s_fragdropped);
2277                 rrddim_set_by_pointer(st, rd_timeout, ip6stat.ip6s_fragtimeout);
2278                 rrddim_set_by_pointer(st, rd_all,     ip6stat.ip6s_fragments);
2279                 rrdset_done(st);
2280             }
2281
2282             // --------------------------------------------------------------------
2283
2284             if (do_ip6_errors == CONFIG_BOOLEAN_YES || (do_ip6_errors == CONFIG_BOOLEAN_AUTO && (
2285                     ip6stat.ip6s_toosmall ||
2286                     ip6stat.ip6s_odropped ||
2287                     ip6stat.ip6s_badoptions ||
2288                     ip6stat.ip6s_badvers ||
2289                     ip6stat.ip6s_exthdrtoolong ||
2290                     ip6stat.ip6s_sources_none ||
2291                     ip6stat.ip6s_tooshort ||
2292                     ip6stat.ip6s_cantforward ||
2293                     ip6stat.ip6s_noroute))) {
2294                 do_ip6_errors = CONFIG_BOOLEAN_YES;
2295
2296                 static RRDSET *st = NULL;
2297                 static RRDDIM *rd_in_discards = NULL, *rd_out_discards = NULL,
2298                               *rd_in_hdr_errors = NULL, *rd_in_addr_errors = NULL, *rd_in_truncated_pkts = NULL,
2299                               *rd_in_no_routes = NULL, *rd_out_no_routes = NULL;
2300
2301                 if (unlikely(!st)) {
2302                     st = rrdset_create_localhost("ipv6",
2303                                                  "errors",
2304                                                  NULL,
2305                                                  "errors",
2306                                                  NULL,
2307                                                  "IPv6 Errors",
2308                                                  "packets/s",
2309                                                  3002,
2310                                                  update_every,
2311                                                  RRDSET_TYPE_LINE
2312                     );
2313
2314                     rrdset_flag_set(st, RRDSET_FLAG_DETAIL);
2315
2316                     rd_in_discards       = rrddim_add(st, "InDiscards",      NULL,  1, 1, RRD_ALGORITHM_INCREMENTAL);
2317                     rd_out_discards      = rrddim_add(st, "OutDiscards",     NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
2318                     rd_in_hdr_errors     = rrddim_add(st, "InHdrErrors",     NULL,  1, 1, RRD_ALGORITHM_INCREMENTAL);
2319                     rd_in_addr_errors    = rrddim_add(st, "InAddrErrors",    NULL,  1, 1, RRD_ALGORITHM_INCREMENTAL);
2320                     rd_in_truncated_pkts = rrddim_add(st, "InTruncatedPkts", NULL,  1, 1, RRD_ALGORITHM_INCREMENTAL);
2321                     rd_in_no_routes      = rrddim_add(st, "InNoRoutes",      NULL,  1, 1, RRD_ALGORITHM_INCREMENTAL);
2322                     rd_out_no_routes     = rrddim_add(st, "OutNoRoutes",     NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
2323                 } else
2324                     rrdset_next(st);
2325
2326                 rrddim_set_by_pointer(st, rd_in_discards,       ip6stat.ip6s_toosmall);
2327                 rrddim_set_by_pointer(st, rd_out_discards,      ip6stat.ip6s_odropped);
2328                 rrddim_set_by_pointer(st, rd_in_hdr_errors,     ip6stat.ip6s_badoptions + ip6stat.ip6s_badvers +
2329                                                                 ip6stat.ip6s_exthdrtoolong);
2330                 rrddim_set_by_pointer(st, rd_in_addr_errors,    ip6stat.ip6s_sources_none);
2331                 rrddim_set_by_pointer(st, rd_in_truncated_pkts, ip6stat.ip6s_tooshort);
2332                 rrddim_set_by_pointer(st, rd_in_no_routes,      ip6stat.ip6s_cantforward);
2333                 rrddim_set_by_pointer(st, rd_out_no_routes,     ip6stat.ip6s_noroute);
2334                 rrdset_done(st);
2335             }
2336         }
2337     } else {
2338         error("DISABLED: net.inet6.ip6.stats module");
2339         return 1;
2340     }
2341
2342     return 0;
2343 }
2344
2345 // --------------------------------------------------------------------------------------------------------------------
2346 // old sources
2347
2348 int do_freebsd_sysctl_old(int update_every, usec_t dt) {
2349     static int do_disk_io = -1,
2350         do_bandwidth = -1,
2351         do_icmp6 = -1, do_icmp6_redir = -1, do_icmp6_errors = -1, do_icmp6_echos = -1, do_icmp6_router = -1,
2352         do_icmp6_neighbor = -1, do_icmp6_types = -1, do_space = -1, do_inodes = -1;
2353
2354     if (unlikely(do_disk_io == -1)) {
2355         do_disk_io              = config_get_boolean("plugin:freebsd:sysctl", "stats for all disks", 1);
2356         do_bandwidth            = config_get_boolean("plugin:freebsd:sysctl", "bandwidth", 1);
2357         do_icmp6                = config_get_boolean_ondemand("plugin:freebsd:sysctl", "icmp", CONFIG_BOOLEAN_AUTO);
2358         do_icmp6_redir          = config_get_boolean_ondemand("plugin:freebsd:sysctl", "icmp redirects", CONFIG_BOOLEAN_AUTO);
2359         do_icmp6_errors         = config_get_boolean_ondemand("plugin:freebsd:sysctl", "icmp errors", CONFIG_BOOLEAN_AUTO);
2360         do_icmp6_echos          = config_get_boolean_ondemand("plugin:freebsd:sysctl", "icmp echos", CONFIG_BOOLEAN_AUTO);
2361         do_icmp6_router         = config_get_boolean_ondemand("plugin:freebsd:sysctl", "icmp router", CONFIG_BOOLEAN_AUTO);
2362         do_icmp6_neighbor       = config_get_boolean_ondemand("plugin:freebsd:sysctl", "icmp neighbor", CONFIG_BOOLEAN_AUTO);
2363         do_icmp6_types          = config_get_boolean_ondemand("plugin:freebsd:sysctl", "icmp types", CONFIG_BOOLEAN_AUTO);
2364         do_space                = config_get_boolean("plugin:freebsd:sysctl", "space usage for all disks", 1);
2365         do_inodes               = config_get_boolean("plugin:freebsd:sysctl", "inodes usage for all disks", 1);
2366     }
2367
2368     RRDSET *st;
2369
2370     int i;
2371     char title[4096 + 1];
2372
2373     // NEEDED BY: do_disk_io
2374     #define RRD_TYPE_DISK "disk"
2375     #define BINTIME_SCALE 5.42101086242752217003726400434970855712890625e-17 // this is 1000/2^64
2376     int numdevs;
2377     static void *devstat_data = NULL;
2378     struct devstat *dstat;
2379     char disk[DEVSTAT_NAME_LEN + MAX_INT_DIGITS + 1];
2380     struct cur_dstat {
2381         collected_number duration_read_ms;
2382         collected_number duration_write_ms;
2383         collected_number busy_time_ms;
2384     } cur_dstat;
2385     struct prev_dstat {
2386         collected_number bytes_read;
2387         collected_number bytes_write;
2388         collected_number operations_read;
2389         collected_number operations_write;
2390         collected_number duration_read_ms;
2391         collected_number duration_write_ms;
2392         collected_number busy_time_ms;
2393     } prev_dstat;
2394
2395     // NEEDED BY: do_bandwidth
2396     #define IFA_DATA(s) (((struct if_data *)ifa->ifa_data)->ifi_ ## s)
2397     struct ifaddrs *ifa, *ifap;
2398     struct iftot {
2399         u_long  ift_ibytes;
2400         u_long  ift_obytes;
2401     } iftot = {0, 0};
2402
2403     // NEEDED BY: do_icmp6...
2404     struct icmp6stat icmp6stat;
2405     struct icmp6_total {
2406         u_long  msgs_in;
2407         u_long  msgs_out;
2408     } icmp6_total = {0, 0};
2409
2410     // NEEDED BY: do_space, do_inodes
2411     struct statfs *mntbuf;
2412     int mntsize;
2413     char mntonname[MNAMELEN + 1];
2414
2415     // --------------------------------------------------------------------
2416
2417     if (likely(do_disk_io)) {
2418         if (unlikely(GETSYSCTL_BY_NAME("kern.devstat.numdevs", numdevs))) {
2419             do_disk_io = 0;
2420             error("DISABLED: disk.io");
2421         } else {
2422             devstat_data = reallocz(devstat_data, sizeof(long) + sizeof(struct devstat) * numdevs); // there is generation number before devstat structures
2423             if (unlikely(
2424                     getsysctl_by_name("kern.devstat.all", devstat_data, sizeof(long) + sizeof(struct devstat) * numdevs))) {
2425                 do_disk_io = 0;
2426                 error("DISABLED: disk.io");
2427             } else {
2428                 dstat = devstat_data + sizeof(long); // skip generation number
2429                 collected_number total_disk_kbytes_read = 0;
2430                 collected_number total_disk_kbytes_write = 0;
2431
2432                 for (i = 0; i < numdevs; i++) {
2433                     if (((dstat[i].device_type & DEVSTAT_TYPE_MASK) == DEVSTAT_TYPE_DIRECT) || ((dstat[i].device_type & DEVSTAT_TYPE_MASK) == DEVSTAT_TYPE_STORARRAY)) {
2434                         sprintf(disk, "%s%d", dstat[i].device_name, dstat[i].unit_number);
2435
2436                         // --------------------------------------------------------------------
2437
2438                         st = rrdset_find_bytype_localhost(RRD_TYPE_DISK, disk);
2439                         if (unlikely(!st)) {
2440                             st = rrdset_create_localhost(RRD_TYPE_DISK, disk, NULL, disk, "disk.io", "Disk I/O Bandwidth", "kilobytes/s", 2000, update_every, RRDSET_TYPE_AREA);
2441
2442                             rrddim_add(st, "reads", NULL, 1, 1024, RRD_ALGORITHM_INCREMENTAL);
2443                             rrddim_add(st, "writes", NULL, -1, 1024, RRD_ALGORITHM_INCREMENTAL);
2444                         }
2445                         else rrdset_next(st);
2446
2447                         total_disk_kbytes_read += dstat[i].bytes[DEVSTAT_READ]/KILO_FACTOR;
2448                         total_disk_kbytes_write += dstat[i].bytes[DEVSTAT_WRITE]/KILO_FACTOR;
2449                         prev_dstat.bytes_read = rrddim_set(st, "reads", dstat[i].bytes[DEVSTAT_READ]);
2450                         prev_dstat.bytes_write = rrddim_set(st, "writes", dstat[i].bytes[DEVSTAT_WRITE]);
2451                         rrdset_done(st);
2452
2453                         // --------------------------------------------------------------------
2454
2455                         st = rrdset_find_bytype_localhost("disk_ops", disk);
2456                         if (unlikely(!st)) {
2457                             st = rrdset_create_localhost("disk_ops", disk, NULL, disk, "disk.ops", "Disk Completed I/O Operations", "operations/s", 2001, update_every, RRDSET_TYPE_LINE);
2458                             rrdset_flag_set(st, RRDSET_FLAG_DETAIL);
2459
2460                             rrddim_add(st, "reads", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
2461                             rrddim_add(st, "writes", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
2462                         }
2463                         else rrdset_next(st);
2464
2465                         prev_dstat.operations_read = rrddim_set(st, "reads", dstat[i].operations[DEVSTAT_READ]);
2466                         prev_dstat.operations_write = rrddim_set(st, "writes", dstat[i].operations[DEVSTAT_WRITE]);
2467                         rrdset_done(st);
2468
2469                         // --------------------------------------------------------------------
2470
2471                         st = rrdset_find_bytype_localhost("disk_qops", disk);
2472                         if (unlikely(!st)) {
2473                             st = rrdset_create_localhost("disk_qops", disk, NULL, disk, "disk.qops", "Disk Current I/O Operations", "operations", 2002, update_every, RRDSET_TYPE_LINE);
2474                             rrdset_flag_set(st, RRDSET_FLAG_DETAIL);
2475
2476                             rrddim_add(st, "operations", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
2477                         }
2478                         else rrdset_next(st);
2479
2480                         rrddim_set(st, "operations", dstat[i].start_count - dstat[i].end_count);
2481                         rrdset_done(st);
2482
2483                         // --------------------------------------------------------------------
2484
2485                         st = rrdset_find_bytype_localhost("disk_util", disk);
2486                         if (unlikely(!st)) {
2487                             st = rrdset_create_localhost("disk_util", disk, NULL, disk, "disk.util", "Disk Utilization Time", "% of time working", 2004, update_every, RRDSET_TYPE_AREA);
2488                             rrdset_flag_set(st, RRDSET_FLAG_DETAIL);
2489
2490                             rrddim_add(st, "utilization", NULL, 1, 10, RRD_ALGORITHM_INCREMENTAL);
2491                         }
2492                         else rrdset_next(st);
2493
2494                         cur_dstat.busy_time_ms = dstat[i].busy_time.sec * 1000 + dstat[i].busy_time.frac * BINTIME_SCALE;
2495                         prev_dstat.busy_time_ms = rrddim_set(st, "utilization", cur_dstat.busy_time_ms);
2496                         rrdset_done(st);
2497
2498                         // --------------------------------------------------------------------
2499
2500                         st = rrdset_find_bytype_localhost("disk_iotime", disk);
2501                         if (unlikely(!st)) {
2502                             st = rrdset_create_localhost("disk_iotime", disk, NULL, disk, "disk.iotime", "Disk Total I/O Time", "milliseconds/s", 2022, update_every, RRDSET_TYPE_LINE);
2503                             rrdset_flag_set(st, RRDSET_FLAG_DETAIL);
2504
2505                             rrddim_add(st, "reads", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
2506                             rrddim_add(st, "writes", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
2507                         }
2508                         else rrdset_next(st);
2509
2510                         cur_dstat.duration_read_ms = dstat[i].duration[DEVSTAT_READ].sec * 1000 + dstat[i].duration[DEVSTAT_READ].frac * BINTIME_SCALE;
2511                         cur_dstat.duration_write_ms = dstat[i].duration[DEVSTAT_WRITE].sec * 1000 + dstat[i].duration[DEVSTAT_READ].frac * BINTIME_SCALE;
2512                         prev_dstat.duration_read_ms = rrddim_set(st, "reads", cur_dstat.duration_read_ms);
2513                         prev_dstat.duration_write_ms = rrddim_set(st, "writes", cur_dstat.duration_write_ms);
2514                         rrdset_done(st);
2515
2516                         // --------------------------------------------------------------------
2517                         // calculate differential charts
2518                         // only if this is not the first time we run
2519
2520                         if (likely(dt)) {
2521
2522                             // --------------------------------------------------------------------
2523
2524                             st = rrdset_find_bytype_localhost("disk_await", disk);
2525                             if (unlikely(!st)) {
2526                                 st = rrdset_create_localhost("disk_await", disk, NULL, disk, "disk.await", "Average Completed I/O Operation Time", "ms per operation", 2005, update_every, RRDSET_TYPE_LINE);
2527                                 rrdset_flag_set(st, RRDSET_FLAG_DETAIL);
2528
2529                                 rrddim_add(st, "reads", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
2530                                 rrddim_add(st, "writes", NULL, -1, 1, RRD_ALGORITHM_ABSOLUTE);
2531                             }
2532                             else rrdset_next(st);
2533
2534                             rrddim_set(st, "reads", (dstat[i].operations[DEVSTAT_READ] - prev_dstat.operations_read) ?
2535                                 (cur_dstat.duration_read_ms - prev_dstat.duration_read_ms) / (dstat[i].operations[DEVSTAT_READ] - prev_dstat.operations_read) : 0);
2536                             rrddim_set(st, "writes", (dstat[i].operations[DEVSTAT_WRITE] - prev_dstat.operations_write) ?
2537                                 (cur_dstat.duration_write_ms - prev_dstat.duration_write_ms) / (dstat[i].operations[DEVSTAT_WRITE] - prev_dstat.operations_write) : 0);
2538                             rrdset_done(st);
2539
2540                             // --------------------------------------------------------------------
2541
2542                             st = rrdset_find_bytype_localhost("disk_avgsz", disk);
2543                             if (unlikely(!st)) {
2544                                 st = rrdset_create_localhost("disk_avgsz", disk, NULL, disk, "disk.avgsz", "Average Completed I/O Operation Bandwidth", "kilobytes per operation", 2006, update_every, RRDSET_TYPE_AREA);
2545                                 rrdset_flag_set(st, RRDSET_FLAG_DETAIL);
2546
2547                                 rrddim_add(st, "reads", NULL, 1, 1024, RRD_ALGORITHM_ABSOLUTE);
2548                                 rrddim_add(st, "writes", NULL, -1, 1024, RRD_ALGORITHM_ABSOLUTE);
2549                             }
2550                             else rrdset_next(st);
2551
2552                             rrddim_set(st, "reads", (dstat[i].operations[DEVSTAT_READ] - prev_dstat.operations_read) ?
2553                                 (dstat[i].bytes[DEVSTAT_READ] - prev_dstat.bytes_read) / (dstat[i].operations[DEVSTAT_READ] - prev_dstat.operations_read) : 0);
2554                             rrddim_set(st, "writes", (dstat[i].operations[DEVSTAT_WRITE] - prev_dstat.operations_write) ?
2555                                 (dstat[i].bytes[DEVSTAT_WRITE] - prev_dstat.bytes_write) / (dstat[i].operations[DEVSTAT_WRITE] - prev_dstat.operations_write) : 0);
2556                             rrdset_done(st);
2557
2558                             // --------------------------------------------------------------------
2559
2560                             st = rrdset_find_bytype_localhost("disk_svctm", disk);
2561                             if (unlikely(!st)) {
2562                                 st = rrdset_create_localhost("disk_svctm", disk, NULL, disk, "disk.svctm", "Average Service Time", "ms per operation", 2007, update_every, RRDSET_TYPE_LINE);
2563                                 rrdset_flag_set(st, RRDSET_FLAG_DETAIL);
2564
2565                                 rrddim_add(st, "svctm", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
2566                             }
2567                             else rrdset_next(st);
2568
2569                             rrddim_set(st, "svctm", ((dstat[i].operations[DEVSTAT_READ] - prev_dstat.operations_read) + (dstat[i].operations[DEVSTAT_WRITE] - prev_dstat.operations_write)) ?
2570                                 (cur_dstat.busy_time_ms - prev_dstat.busy_time_ms) / ((dstat[i].operations[DEVSTAT_READ] - prev_dstat.operations_read) + (dstat[i].operations[DEVSTAT_WRITE] - prev_dstat.operations_write)) : 0);
2571                             rrdset_done(st);
2572                         }
2573                     }
2574                 }
2575
2576                 // --------------------------------------------------------------------
2577
2578                 st = rrdset_find_bytype_localhost("system", "io");
2579                 if (unlikely(!st)) {
2580                     st = rrdset_create_localhost("system", "io", NULL, "disk", NULL, "Disk I/O", "kilobytes/s", 150, update_every, RRDSET_TYPE_AREA);
2581                     rrddim_add(st, "in",  NULL,  1, 1, RRD_ALGORITHM_INCREMENTAL);
2582                     rrddim_add(st, "out", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
2583                 }
2584                 else rrdset_next(st);
2585
2586                 rrddim_set(st, "in", total_disk_kbytes_read);
2587                 rrddim_set(st, "out", total_disk_kbytes_write);
2588                 rrdset_done(st);
2589             }
2590         }
2591     }
2592
2593     // --------------------------------------------------------------------
2594
2595     if (likely(do_bandwidth)) {
2596         if (unlikely(getifaddrs(&ifap))) {
2597             error("FREEBSD: getifaddrs()");
2598             do_bandwidth = 0;
2599             error("DISABLED: system.ipv4");
2600         } else {
2601             iftot.ift_ibytes = iftot.ift_obytes = 0;
2602             for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
2603                 if (ifa->ifa_addr->sa_family != AF_INET)
2604                         continue;
2605                 iftot.ift_ibytes += IFA_DATA(ibytes);
2606                 iftot.ift_obytes += IFA_DATA(obytes);
2607             }
2608
2609             st = rrdset_find_localhost("system.ipv4");
2610             if (unlikely(!st)) {
2611                 st = rrdset_create_localhost("system", "ipv4", NULL, "network", NULL, "IPv4 Bandwidth", "kilobits/s", 500, update_every, RRDSET_TYPE_AREA);
2612
2613                 rrddim_add(st, "InOctets", "received", 8, 1024, RRD_ALGORITHM_INCREMENTAL);
2614                 rrddim_add(st, "OutOctets", "sent", -8, 1024, RRD_ALGORITHM_INCREMENTAL);
2615             }
2616             else rrdset_next(st);
2617
2618             rrddim_set(st, "InOctets", iftot.ift_ibytes);
2619             rrddim_set(st, "OutOctets", iftot.ift_obytes);
2620             rrdset_done(st);
2621
2622             // --------------------------------------------------------------------
2623
2624             iftot.ift_ibytes = iftot.ift_obytes = 0;
2625             for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
2626                 if (ifa->ifa_addr->sa_family != AF_INET6)
2627                         continue;
2628                 iftot.ift_ibytes += IFA_DATA(ibytes);
2629                 iftot.ift_obytes += IFA_DATA(obytes);
2630             }
2631
2632             st = rrdset_find_localhost("system.ipv6");
2633             if (unlikely(!st)) {
2634                 st = rrdset_create_localhost("system", "ipv6", NULL, "network", NULL, "IPv6 Bandwidth", "kilobits/s", 500, update_every, RRDSET_TYPE_AREA);
2635
2636                 rrddim_add(st, "received", NULL, 8, 1024, RRD_ALGORITHM_INCREMENTAL);
2637                 rrddim_add(st, "sent", NULL, -8, 1024, RRD_ALGORITHM_INCREMENTAL);
2638             }
2639             else rrdset_next(st);
2640
2641             rrddim_set(st, "sent", iftot.ift_obytes);
2642             rrddim_set(st, "received", iftot.ift_ibytes);
2643             rrdset_done(st);
2644
2645             for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
2646                 if (ifa->ifa_addr->sa_family != AF_LINK)
2647                         continue;
2648
2649                 // --------------------------------------------------------------------
2650
2651                 st = rrdset_find_bytype_localhost("net", ifa->ifa_name);
2652                 if (unlikely(!st)) {
2653                     st = rrdset_create_localhost("net", ifa->ifa_name, NULL, ifa->ifa_name, "net.net", "Bandwidth", "kilobits/s", 7000, update_every, RRDSET_TYPE_AREA);
2654
2655                     rrddim_add(st, "received", NULL, 8, 1024, RRD_ALGORITHM_INCREMENTAL);
2656                     rrddim_add(st, "sent", NULL, -8, 1024, RRD_ALGORITHM_INCREMENTAL);
2657                 }
2658                 else rrdset_next(st);
2659
2660                 rrddim_set(st, "received", IFA_DATA(ibytes));
2661                 rrddim_set(st, "sent", IFA_DATA(obytes));
2662                 rrdset_done(st);
2663
2664                 // --------------------------------------------------------------------
2665
2666                 st = rrdset_find_bytype_localhost("net_packets", ifa->ifa_name);
2667                 if (unlikely(!st)) {
2668                     st = rrdset_create_localhost("net_packets", ifa->ifa_name, NULL, ifa->ifa_name, "net.packets", "Packets", "packets/s", 7001, update_every, RRDSET_TYPE_LINE);
2669                     rrdset_flag_set(st, RRDSET_FLAG_DETAIL);
2670
2671                     rrddim_add(st, "received", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
2672                     rrddim_add(st, "sent", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
2673                     rrddim_add(st, "multicast_received", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
2674                     rrddim_add(st, "multicast_sent", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
2675                 }
2676                 else rrdset_next(st);
2677
2678                 rrddim_set(st, "received", IFA_DATA(ipackets));
2679                 rrddim_set(st, "sent", IFA_DATA(opackets));
2680                 rrddim_set(st, "multicast_received", IFA_DATA(imcasts));
2681                 rrddim_set(st, "multicast_sent", IFA_DATA(omcasts));
2682                 rrdset_done(st);
2683
2684                 // --------------------------------------------------------------------
2685
2686                 st = rrdset_find_bytype_localhost("net_errors", ifa->ifa_name);
2687                 if (unlikely(!st)) {
2688                     st = rrdset_create_localhost("net_errors", ifa->ifa_name, NULL, ifa->ifa_name, "net.errors", "Interface Errors", "errors/s", 7002, update_every, RRDSET_TYPE_LINE);
2689                     rrdset_flag_set(st, RRDSET_FLAG_DETAIL);
2690
2691                     rrddim_add(st, "inbound", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
2692                     rrddim_add(st, "outbound", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
2693                 }
2694                 else rrdset_next(st);
2695
2696                 rrddim_set(st, "inbound", IFA_DATA(ierrors));
2697                 rrddim_set(st, "outbound", IFA_DATA(oerrors));
2698                 rrdset_done(st);
2699
2700                 // --------------------------------------------------------------------
2701
2702                 st = rrdset_find_bytype_localhost("net_drops", ifa->ifa_name);
2703                 if (unlikely(!st)) {
2704                     st = rrdset_create_localhost("net_drops", ifa->ifa_name, NULL, ifa->ifa_name, "net.drops", "Interface Drops", "drops/s", 7003, update_every, RRDSET_TYPE_LINE);
2705                     rrdset_flag_set(st, RRDSET_FLAG_DETAIL);
2706
2707                     rrddim_add(st, "inbound", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
2708 #if __FreeBSD__ >= 11
2709                     rrddim_add(st, "outbound", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
2710 #endif
2711                 }
2712                 else rrdset_next(st);
2713
2714                 rrddim_set(st, "inbound", IFA_DATA(iqdrops));
2715 #if __FreeBSD__ >= 11
2716                 rrddim_set(st, "outbound", IFA_DATA(oqdrops));
2717 #endif
2718                 rrdset_done(st);
2719
2720                 // --------------------------------------------------------------------
2721
2722                 st = rrdset_find_bytype_localhost("net_events", ifa->ifa_name);
2723                 if (unlikely(!st)) {
2724                     st = rrdset_create_localhost("net_events", ifa->ifa_name, NULL, ifa->ifa_name, "net.events", "Network Interface Events", "events/s", 7006, update_every, RRDSET_TYPE_LINE);
2725                     rrdset_flag_set(st, RRDSET_FLAG_DETAIL);
2726
2727                     rrddim_add(st, "frames", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
2728                     rrddim_add(st, "collisions", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
2729                     rrddim_add(st, "carrier", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
2730                 }
2731                 else rrdset_next(st);
2732
2733                 rrddim_set(st, "collisions", IFA_DATA(collisions));
2734                 rrdset_done(st);
2735             }
2736
2737             freeifaddrs(ifap);
2738         }
2739     }
2740
2741     // --------------------------------------------------------------------
2742
2743     if (likely(do_icmp6 || do_icmp6_redir || do_icmp6_errors || do_icmp6_echos || do_icmp6_router || do_icmp6_neighbor || do_icmp6_types)) {
2744         if (unlikely(GETSYSCTL_BY_NAME("net.inet6.icmp6.stats", icmp6stat))) {
2745             do_icmp6 = 0;
2746             error("DISABLED: ipv6.icmp");
2747         } else {
2748             for (i = 0; i <= ICMP6_MAXTYPE; i++) {
2749                 icmp6_total.msgs_in += icmp6stat.icp6s_inhist[i];
2750                 icmp6_total.msgs_out += icmp6stat.icp6s_outhist[i];
2751             }
2752             icmp6_total.msgs_in += icmp6stat.icp6s_badcode + icmp6stat.icp6s_badlen + icmp6stat.icp6s_checksum + icmp6stat.icp6s_tooshort;
2753             if (do_icmp6 == CONFIG_BOOLEAN_YES || (do_icmp6 == CONFIG_BOOLEAN_AUTO && (icmp6_total.msgs_in || icmp6_total.msgs_out))) {
2754                 do_icmp6 = CONFIG_BOOLEAN_YES;
2755                 st = rrdset_find_localhost("ipv6.icmp");
2756                 if (unlikely(!st)) {
2757                     st = rrdset_create_localhost("ipv6", "icmp", NULL, "icmp", NULL, "IPv6 ICMP Messages",
2758                                        "messages/s", 10000, update_every, RRDSET_TYPE_LINE);
2759
2760                     rrddim_add(st, "received", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
2761                     rrddim_add(st, "sent", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
2762                 } else
2763                     rrdset_next(st);
2764
2765                 rrddim_set(st, "sent", icmp6_total.msgs_in);
2766                 rrddim_set(st, "received", icmp6_total.msgs_out);
2767                 rrdset_done(st);
2768             }
2769
2770             // --------------------------------------------------------------------
2771
2772             if (do_icmp6_redir == CONFIG_BOOLEAN_YES || (do_icmp6_redir == CONFIG_BOOLEAN_AUTO && (icmp6stat.icp6s_inhist[ND_REDIRECT] || icmp6stat.icp6s_outhist[ND_REDIRECT]))) {
2773                 do_icmp6_redir = CONFIG_BOOLEAN_YES;
2774                 st = rrdset_find_localhost("ipv6.icmpredir");
2775                 if (unlikely(!st)) {
2776                     st = rrdset_create_localhost("ipv6", "icmpredir", NULL, "icmp", NULL, "IPv6 ICMP Redirects",
2777                                        "redirects/s", 10050, update_every, RRDSET_TYPE_LINE);
2778
2779                     rrddim_add(st, "received", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
2780                     rrddim_add(st, "sent", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
2781                 } else
2782                     rrdset_next(st);
2783
2784                 rrddim_set(st, "sent", icmp6stat.icp6s_inhist[ND_REDIRECT]);
2785                 rrddim_set(st, "received", icmp6stat.icp6s_outhist[ND_REDIRECT]);
2786                 rrdset_done(st);
2787             }
2788
2789             // --------------------------------------------------------------------
2790
2791             if (do_icmp6_errors == CONFIG_BOOLEAN_YES || (do_icmp6_errors == CONFIG_BOOLEAN_AUTO && (
2792                                                                             icmp6stat.icp6s_badcode ||
2793                                                                             icmp6stat.icp6s_badlen ||
2794                                                                             icmp6stat.icp6s_checksum ||
2795                                                                             icmp6stat.icp6s_tooshort ||
2796                                                                             icmp6stat.icp6s_error ||
2797                                                                             icmp6stat.icp6s_inhist[ICMP6_DST_UNREACH] ||
2798                                                                             icmp6stat.icp6s_inhist[ICMP6_TIME_EXCEEDED] ||
2799                                                                             icmp6stat.icp6s_inhist[ICMP6_PARAM_PROB] ||
2800                                                                             icmp6stat.icp6s_outhist[ICMP6_DST_UNREACH] ||
2801                                                                             icmp6stat.icp6s_outhist[ICMP6_TIME_EXCEEDED] ||
2802                                                                             icmp6stat.icp6s_outhist[ICMP6_PARAM_PROB]))) {
2803                 do_icmp6_errors = CONFIG_BOOLEAN_YES;
2804                 st = rrdset_find_localhost("ipv6.icmperrors");
2805                 if (unlikely(!st)) {
2806                     st = rrdset_create_localhost("ipv6", "icmperrors", NULL, "icmp", NULL, "IPv6 ICMP Errors", "errors/s", 10100, update_every, RRDSET_TYPE_LINE);
2807
2808                     rrddim_add(st, "InErrors", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
2809                     rrddim_add(st, "OutErrors", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
2810
2811                     rrddim_add(st, "InCsumErrors", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
2812                     rrddim_add(st, "InDestUnreachs", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
2813                     rrddim_add(st, "InPktTooBigs", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
2814                     rrddim_add(st, "InTimeExcds", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
2815                     rrddim_add(st, "InParmProblems", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
2816                     rrddim_add(st, "OutDestUnreachs", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
2817                     rrddim_add(st, "OutTimeExcds", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
2818                     rrddim_add(st, "OutParmProblems", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
2819                 } else
2820                     rrdset_next(st);
2821
2822                 rrddim_set(st, "InErrors", icmp6stat.icp6s_badcode + icmp6stat.icp6s_badlen + icmp6stat.icp6s_checksum + icmp6stat.icp6s_tooshort);
2823                 rrddim_set(st, "OutErrors", icmp6stat.icp6s_error);
2824                 rrddim_set(st, "InCsumErrors", icmp6stat.icp6s_checksum);
2825                 rrddim_set(st, "InDestUnreachs", icmp6stat.icp6s_inhist[ICMP6_DST_UNREACH]);
2826                 rrddim_set(st, "InPktTooBigs", icmp6stat.icp6s_badlen);
2827                 rrddim_set(st, "InTimeExcds", icmp6stat.icp6s_inhist[ICMP6_TIME_EXCEEDED]);
2828                 rrddim_set(st, "InParmProblems", icmp6stat.icp6s_inhist[ICMP6_PARAM_PROB]);
2829                 rrddim_set(st, "OutDestUnreachs", icmp6stat.icp6s_outhist[ICMP6_DST_UNREACH]);
2830                 rrddim_set(st, "OutTimeExcds", icmp6stat.icp6s_outhist[ICMP6_TIME_EXCEEDED]);
2831                 rrddim_set(st, "OutParmProblems", icmp6stat.icp6s_outhist[ICMP6_PARAM_PROB]);
2832                 rrdset_done(st);
2833             }
2834
2835             // --------------------------------------------------------------------
2836
2837             if (do_icmp6_echos == CONFIG_BOOLEAN_YES || (do_icmp6_echos == CONFIG_BOOLEAN_AUTO && (
2838                                                                  icmp6stat.icp6s_inhist[ICMP6_ECHO_REQUEST] ||
2839                                                                  icmp6stat.icp6s_outhist[ICMP6_ECHO_REQUEST] ||
2840                                                                  icmp6stat.icp6s_inhist[ICMP6_ECHO_REPLY] ||
2841                                                                  icmp6stat.icp6s_outhist[ICMP6_ECHO_REPLY]))) {
2842                 do_icmp6_echos = CONFIG_BOOLEAN_YES;
2843                 st = rrdset_find_localhost("ipv6.icmpechos");
2844                 if (unlikely(!st)) {
2845                     st = rrdset_create_localhost("ipv6", "icmpechos", NULL, "icmp", NULL, "IPv6 ICMP Echo", "messages/s", 10200, update_every, RRDSET_TYPE_LINE);
2846
2847                     rrddim_add(st, "InEchos", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
2848                     rrddim_add(st, "OutEchos", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
2849                     rrddim_add(st, "InEchoReplies", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
2850                     rrddim_add(st, "OutEchoReplies", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
2851                 } else
2852                     rrdset_next(st);
2853
2854                 rrddim_set(st, "InEchos", icmp6stat.icp6s_inhist[ICMP6_ECHO_REQUEST]);
2855                 rrddim_set(st, "OutEchos", icmp6stat.icp6s_outhist[ICMP6_ECHO_REQUEST]);
2856                 rrddim_set(st, "InEchoReplies", icmp6stat.icp6s_inhist[ICMP6_ECHO_REPLY]);
2857                 rrddim_set(st, "OutEchoReplies", icmp6stat.icp6s_outhist[ICMP6_ECHO_REPLY]);
2858                 rrdset_done(st);
2859             }
2860
2861             // --------------------------------------------------------------------
2862
2863             if (do_icmp6_router == CONFIG_BOOLEAN_YES || (do_icmp6_router == CONFIG_BOOLEAN_AUTO && (
2864                                                                     icmp6stat.icp6s_inhist[ND_ROUTER_SOLICIT] ||
2865                                                                     icmp6stat.icp6s_outhist[ND_ROUTER_SOLICIT] ||
2866                                                                     icmp6stat.icp6s_inhist[ND_ROUTER_ADVERT] ||
2867                                                                     icmp6stat.icp6s_outhist[ND_ROUTER_ADVERT]))) {
2868                 do_icmp6_router = CONFIG_BOOLEAN_YES;
2869                 st = rrdset_find_localhost("ipv6.icmprouter");
2870                 if (unlikely(!st)) {
2871                     st = rrdset_create_localhost("ipv6", "icmprouter", NULL, "icmp", NULL, "IPv6 Router Messages", "messages/s", 10400, update_every, RRDSET_TYPE_LINE);
2872
2873                     rrddim_add(st, "InSolicits", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
2874                     rrddim_add(st, "OutSolicits", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
2875                     rrddim_add(st, "InAdvertisements", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
2876                     rrddim_add(st, "OutAdvertisements", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
2877                 } else
2878                     rrdset_next(st);
2879
2880                 rrddim_set(st, "InSolicits", icmp6stat.icp6s_inhist[ND_ROUTER_SOLICIT]);
2881                 rrddim_set(st, "OutSolicits", icmp6stat.icp6s_outhist[ND_ROUTER_SOLICIT]);
2882                 rrddim_set(st, "InAdvertisements", icmp6stat.icp6s_inhist[ND_ROUTER_ADVERT]);
2883                 rrddim_set(st, "OutAdvertisements", icmp6stat.icp6s_outhist[ND_ROUTER_ADVERT]);
2884                 rrdset_done(st);
2885             }
2886
2887             // --------------------------------------------------------------------
2888
2889             if (do_icmp6_neighbor == CONFIG_BOOLEAN_YES || (do_icmp6_neighbor == CONFIG_BOOLEAN_AUTO && (
2890                                                                     icmp6stat.icp6s_inhist[ND_NEIGHBOR_SOLICIT] ||
2891                                                                     icmp6stat.icp6s_outhist[ND_NEIGHBOR_SOLICIT] ||
2892                                                                     icmp6stat.icp6s_inhist[ND_NEIGHBOR_ADVERT] ||
2893                                                                     icmp6stat.icp6s_outhist[ND_NEIGHBOR_ADVERT]))) {
2894                 do_icmp6_neighbor = CONFIG_BOOLEAN_YES;
2895                 st = rrdset_find_localhost("ipv6.icmpneighbor");
2896                 if (unlikely(!st)) {
2897                     st = rrdset_create_localhost("ipv6", "icmpneighbor", NULL, "icmp", NULL, "IPv6 Neighbor Messages", "messages/s", 10500, update_every, RRDSET_TYPE_LINE);
2898
2899                     rrddim_add(st, "InSolicits", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
2900                     rrddim_add(st, "OutSolicits", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
2901                     rrddim_add(st, "InAdvertisements", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
2902                     rrddim_add(st, "OutAdvertisements", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
2903                 } else
2904                     rrdset_next(st);
2905
2906                 rrddim_set(st, "InSolicits", icmp6stat.icp6s_inhist[ND_NEIGHBOR_SOLICIT]);
2907                 rrddim_set(st, "OutSolicits", icmp6stat.icp6s_outhist[ND_NEIGHBOR_SOLICIT]);
2908                 rrddim_set(st, "InAdvertisements", icmp6stat.icp6s_inhist[ND_NEIGHBOR_ADVERT]);
2909                 rrddim_set(st, "OutAdvertisements", icmp6stat.icp6s_outhist[ND_NEIGHBOR_ADVERT]);
2910                 rrdset_done(st);
2911             }
2912
2913             // --------------------------------------------------------------------
2914
2915             if (do_icmp6_types == CONFIG_BOOLEAN_YES || (do_icmp6_types == CONFIG_BOOLEAN_AUTO && (
2916                                                                     icmp6stat.icp6s_inhist[1] ||
2917                                                                     icmp6stat.icp6s_inhist[128] ||
2918                                                                     icmp6stat.icp6s_inhist[129] ||
2919                                                                     icmp6stat.icp6s_inhist[136] ||
2920                                                                     icmp6stat.icp6s_outhist[1] ||
2921                                                                     icmp6stat.icp6s_outhist[128] ||
2922                                                                     icmp6stat.icp6s_outhist[129] ||
2923                                                                     icmp6stat.icp6s_outhist[133] ||
2924                                                                     icmp6stat.icp6s_outhist[135] ||
2925                                                                     icmp6stat.icp6s_outhist[136]))) {
2926                 do_icmp6_types = CONFIG_BOOLEAN_YES;
2927                 st = rrdset_find_localhost("ipv6.icmptypes");
2928                 if (unlikely(!st)) {
2929                     st = rrdset_create_localhost("ipv6", "icmptypes", NULL, "icmp", NULL, "IPv6 ICMP Types",
2930                                        "messages/s", 10700, update_every, RRDSET_TYPE_LINE);
2931
2932                     rrddim_add(st, "InType1", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
2933                     rrddim_add(st, "InType128", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
2934                     rrddim_add(st, "InType129", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
2935                     rrddim_add(st, "InType136", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
2936                     rrddim_add(st, "OutType1", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
2937                     rrddim_add(st, "OutType128", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
2938                     rrddim_add(st, "OutType129", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
2939                     rrddim_add(st, "OutType133", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
2940                     rrddim_add(st, "OutType135", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
2941                     rrddim_add(st, "OutType143", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
2942                 } else
2943                     rrdset_next(st);
2944
2945                 rrddim_set(st, "InType1", icmp6stat.icp6s_inhist[1]);
2946                 rrddim_set(st, "InType128", icmp6stat.icp6s_inhist[128]);
2947                 rrddim_set(st, "InType129", icmp6stat.icp6s_inhist[129]);
2948                 rrddim_set(st, "InType136", icmp6stat.icp6s_inhist[136]);
2949                 rrddim_set(st, "OutType1", icmp6stat.icp6s_outhist[1]);
2950                 rrddim_set(st, "OutType128", icmp6stat.icp6s_outhist[128]);
2951                 rrddim_set(st, "OutType129", icmp6stat.icp6s_outhist[129]);
2952                 rrddim_set(st, "OutType133", icmp6stat.icp6s_outhist[133]);
2953                 rrddim_set(st, "OutType135", icmp6stat.icp6s_outhist[135]);
2954                 rrddim_set(st, "OutType143", icmp6stat.icp6s_outhist[143]);
2955                 rrdset_done(st);
2956             }
2957         }
2958     }
2959
2960     // --------------------------------------------------------------------------
2961
2962     if (likely(do_space || do_inodes)) {
2963         // there is no mount info in sysctl MIBs
2964         if (unlikely(!(mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)))) {
2965             error("FREEBSD: getmntinfo() failed");
2966             do_space = 0;
2967             error("DISABLED: disk_space.X");
2968             do_inodes = 0;
2969             error("DISABLED: disk_inodes.X");
2970         } else {
2971             for (i = 0; i < mntsize; i++) {
2972                 if (mntbuf[i].f_flags == MNT_RDONLY ||
2973                         mntbuf[i].f_blocks == 0 ||
2974                         // taken from gnulib/mountlist.c and shortened to FreeBSD related fstypes
2975                         strcmp(mntbuf[i].f_fstypename, "autofs") == 0 ||
2976                         strcmp(mntbuf[i].f_fstypename, "procfs") == 0 ||
2977                         strcmp(mntbuf[i].f_fstypename, "subfs") == 0 ||
2978                         strcmp(mntbuf[i].f_fstypename, "devfs") == 0 ||
2979                         strcmp(mntbuf[i].f_fstypename, "none") == 0)
2980                     continue;
2981
2982                 // --------------------------------------------------------------------------
2983
2984                 if (likely(do_space)) {
2985                     st = rrdset_find_bytype_localhost("disk_space", mntbuf[i].f_mntonname);
2986                     if (unlikely(!st)) {
2987                         snprintfz(title, 4096, "Disk Space Usage for %s [%s]", mntbuf[i].f_mntonname, mntbuf[i].f_mntfromname);
2988                         st = rrdset_create_localhost("disk_space", mntbuf[i].f_mntonname, NULL, mntbuf[i].f_mntonname, "disk.space", title, "GB", 2023,
2989                                            update_every,
2990                                            RRDSET_TYPE_STACKED);
2991
2992                         rrddim_add(st, "avail", NULL, mntbuf[i].f_bsize, GIGA_FACTOR, RRD_ALGORITHM_ABSOLUTE);
2993                         rrddim_add(st, "used", NULL, mntbuf[i].f_bsize, GIGA_FACTOR, RRD_ALGORITHM_ABSOLUTE);
2994                         rrddim_add(st, "reserved_for_root", "reserved for root", mntbuf[i].f_bsize, GIGA_FACTOR,
2995                                 RRD_ALGORITHM_ABSOLUTE);
2996                     } else
2997                         rrdset_next(st);
2998
2999                     rrddim_set(st, "avail", (collected_number) mntbuf[i].f_bavail);
3000                     rrddim_set(st, "used", (collected_number) (mntbuf[i].f_blocks - mntbuf[i].f_bfree));
3001                     rrddim_set(st, "reserved_for_root", (collected_number) (mntbuf[i].f_bfree - mntbuf[i].f_bavail));
3002                     rrdset_done(st);
3003                 }
3004
3005                 // --------------------------------------------------------------------------
3006
3007                 if (likely(do_inodes)) {
3008                     st = rrdset_find_bytype_localhost("disk_inodes", mntbuf[i].f_mntonname);
3009                     if (unlikely(!st)) {
3010                         snprintfz(title, 4096, "Disk Files (inodes) Usage for %s [%s]", mntbuf[i].f_mntonname, mntbuf[i].f_mntfromname);
3011                         st = rrdset_create_localhost("disk_inodes", mntbuf[i].f_mntonname, NULL, mntbuf[i].f_mntonname, "disk.inodes", title, "Inodes", 2024,
3012                                            update_every, RRDSET_TYPE_STACKED);
3013
3014                         rrddim_add(st, "avail", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
3015                         rrddim_add(st, "used", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
3016                         rrddim_add(st, "reserved_for_root", "reserved for root", 1, 1, RRD_ALGORITHM_ABSOLUTE);
3017                     } else
3018                         rrdset_next(st);
3019
3020                     rrddim_set(st, "avail", (collected_number) mntbuf[i].f_ffree);
3021                     rrddim_set(st, "used", (collected_number) (mntbuf[i].f_files - mntbuf[i].f_ffree));
3022                     rrdset_done(st);
3023                 }
3024             }
3025         }
3026     }
3027
3028     return 0;
3029 }