Add IPv4 bandwidth chart to macOS plugin
Vladimir Kobal committed
Dec 28, 2016 at 08:40 UTC
7ffa985b3a1181a92108eaccb999407eaf73fd5c
1 file changed
+63
-1
src/macos_sysctl.c
+63
-1
@@ -1,5 +1,7 @@
1
#include "common.h"
2
#include <sys/sysctl.h>
3
+// NEEDED BY: do_bandwidth
4
+#include <net/route.h>
5
6
#define GETSYSCTL(name, var) getsysctl(name, &(var), sizeof(var))
7
@@ -11,11 +13,12 @@ int getsysctl(const char *name, void *ptr, size_t len);
13
int do_macos_sysctl(int update_every, usec_t dt) {
14
(void)dt;
15
14
- static int do_loadavg = -1, do_swap = -1;
16
+ static int do_loadavg = -1, do_swap = -1, do_bandwidth = -1;
17
18
if (unlikely(do_loadavg == -1)) {
19
do_loadavg = config_get_boolean("plugin:macos:sysctl", "enable load average", 1);
20
do_swap = config_get_boolean("plugin:macos:sysctl", "system swap", 1);
21
+ do_bandwidth = config_get_boolean("plugin:macos:sysctl", "bandwidth", 1);
22
}
23
24
RRDSET *st;
@@ -32,6 +35,16 @@ int do_macos_sysctl(int update_every, usec_t dt) {
35
// NEEDED BY: do_swap
36
struct xsw_usage swap_usage;
37
38
+ // NEEDED BY: do_bandwidth
39
+ int mib[6];
40
+ static char *ifstatdata = NULL;
41
+ char *lim, *next;
42
+ struct if_msghdr *ifm;
43
+ struct iftot {
44
+ u_long ift_ibytes;
45
+ u_long ift_obytes;
46
+ } iftot = {0, 0};
47
+
48
if (last_loadavg_usec <= dt) {
49
if (likely(do_loadavg)) {
50
if (unlikely(GETSYSCTL("vm.loadavg", sysload))) {
@@ -80,6 +93,55 @@ int do_macos_sysctl(int update_every, usec_t dt) {
93
}
94
}
95
96
+ // --------------------------------------------------------------------
97
+
98
+ if (likely(do_bandwidth)) {
99
+ mib[0] = CTL_NET;
100
+ mib[1] = PF_ROUTE;
101
+ mib[2] = 0;
102
+ mib[3] = AF_INET;
103
+ mib[4] = NET_RT_IFLIST2;
104
+ mib[5] = 0;
105
+ if (unlikely(sysctl(mib, 6, NULL, &size, NULL, 0))) {
106
+ error("MACOS: sysctl(%s...) failed: %s", "net interfaces", strerror(errno));
107
+ do_bandwidth = 0;
108
+ error("DISABLED: system.ipv4");
109
+ } else {
110
+ ifstatdata = reallocz(ifstatdata, size);
111
+ if (unlikely(sysctl(mib, 6, ifstatdata, &size, NULL, 0) < 0)) {
112
+ error("MACOS: sysctl(%s...) failed: %s", "net interfaces", strerror(errno));
113
+ do_bandwidth = 0;
114
+ error("DISABLED: system.ipv4");
115
+ } else {
116
+ lim = ifstatdata + size;
117
+ iftot.ift_ibytes = iftot.ift_obytes = 0;
118
+ for (next = ifstatdata; next < lim; ) {
119
+ ifm = (struct if_msghdr *)next;
120
+ next += ifm->ifm_msglen;
121
+
122
+ if (ifm->ifm_type == RTM_IFINFO2) {
123
+ struct if_msghdr2 *if2m = (struct if_msghdr2 *)ifm;
124
+
125
+ iftot.ift_ibytes += if2m->ifm_data.ifi_ibytes;
126
+ iftot.ift_obytes += if2m->ifm_data.ifi_obytes;
127
+ }
128
+ }
129
+ st = rrdset_find("system.ipv4");
130
+ if (unlikely(!st)) {
131
+ st = rrdset_create("system", "ipv4", NULL, "network", NULL, "IPv4 Bandwidth", "kilobits/s", 500, update_every, RRDSET_TYPE_AREA);
132
+
133
+ rrddim_add(st, "InOctets", "received", 8, 1024, RRDDIM_INCREMENTAL);
134
+ rrddim_add(st, "OutOctets", "sent", -8, 1024, RRDDIM_INCREMENTAL);
135
+ }
136
+ else rrdset_next(st);
137
+
138
+ rrddim_set(st, "InOctets", iftot.ift_ibytes);
139
+ rrddim_set(st, "OutOctets", iftot.ift_obytes);
140
+ rrdset_done(st);
141
+ }
142
+ }
143
+ }
144
+
145
return 0;
146
}
147