]> arthur.barton.de Git - netdata.git/blob - src/plugin_nfacct.c
nfacct plugin with static dimension pointers
[netdata.git] / src / plugin_nfacct.c
1 #include "common.h"
2
3 #ifdef INTERNAL_PLUGIN_NFACCT
4 #include <libmnl/libmnl.h>
5 #include <libnetfilter_acct/libnetfilter_acct.h>
6
7 struct mynfacct {
8     const char *name;
9     uint32_t hash;
10
11     uint64_t pkts;
12     uint64_t bytes;
13
14     RRDDIM *rd_bytes;
15     RRDDIM *rd_packets;
16
17     int updated;
18 };
19
20 struct nfacct_list {
21     int size;
22     int len;
23     struct mynfacct data[];
24 } *nfacct_list = NULL;
25
26 static inline void nfacct_list_grow() {
27     if(!nfacct_list || nfacct_list->len == nfacct_list->size) {
28         int size = (nfacct_list) ? nfacct_list->size : 0;
29         int len = (nfacct_list) ? nfacct_list->len : 0;
30         size++;
31
32         info("nfacct.plugin: increasing nfacct_list to size %d", size);
33
34         nfacct_list = reallocz(nfacct_list, sizeof(struct nfacct_list) + (sizeof(struct mynfacct) * size));
35
36         nfacct_list->data[len].rd_bytes = NULL;
37         nfacct_list->data[len].rd_packets = NULL;
38         nfacct_list->data[len].updated = 0;
39
40         nfacct_list->size = size;
41         nfacct_list->len = len;
42     }
43 }
44
45 static int nfacct_callback(const struct nlmsghdr *nlh, void *data) {
46     (void)data;
47
48     static struct nfacct *nfacct = NULL;
49
50     if(unlikely(!nfacct)) {
51         nfacct = nfacct_alloc();
52         if(!nfacct) {
53             error("nfacct.plugin: nfacct_alloc() failed.");
54             return MNL_CB_OK;
55         }
56     }
57
58     if(nfacct_nlmsg_parse_payload(nlh, nfacct) < 0) {
59         error("nfacct.plugin: nfacct_nlmsg_parse_payload() failed.");
60         return MNL_CB_OK;
61     }
62
63     const char *name = nfacct_attr_get_str(nfacct, NFACCT_ATTR_NAME);
64     uint32_t hash = simple_hash(name);
65
66     int i;
67     struct mynfacct *mynfacct = NULL;
68     for(i = 0; i < nfacct_list->len; i++) {
69         if(nfacct_list->data[i].hash == hash && !strcmp(nfacct_list->data[i].name, name)) {
70             mynfacct = &nfacct_list->data[i];
71             break;
72         }
73     }
74
75     if(!mynfacct) {
76         nfacct_list_grow();
77         mynfacct = &nfacct_list->data[nfacct_list->len++];
78         mynfacct->name = name;
79         mynfacct->hash = hash;
80     }
81
82     mynfacct->pkts  = nfacct_attr_get_u64(nfacct, NFACCT_ATTR_PKTS);
83     mynfacct->bytes = nfacct_attr_get_u64(nfacct, NFACCT_ATTR_BYTES);
84     mynfacct->updated = 1;
85
86     nfacct_list->len++;
87     return MNL_CB_OK;
88 }
89
90 void *nfacct_main(void *ptr) {
91     struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
92
93     info("NFACCT thread created with task id %d", gettid());
94
95     if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
96         error("nfacct.plugin: Cannot set pthread cancel type to DEFERRED.");
97
98     if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
99         error("nfacct.plugin: Cannot set pthread cancel state to ENABLE.");
100
101     char buf[MNL_SOCKET_BUFFER_SIZE];
102     struct mnl_socket *nl = NULL;
103     struct nlmsghdr *nlh = NULL;
104     unsigned int seq = 0, portid = 0;
105
106     seq = (unsigned int)now_realtime_sec() - 1;
107
108     nl  = mnl_socket_open(NETLINK_NETFILTER);
109     if(!nl) {
110         error("nfacct.plugin: mnl_socket_open() failed");
111         goto cleanup;
112     }
113
114     if(mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
115         error("nfacct.plugin: mnl_socket_bind() failed");
116         goto cleanup;
117     }
118     portid = mnl_socket_get_portid(nl);
119
120     // ------------------------------------------------------------------------
121
122     RRDSET *st_bytes = NULL, *st_packets = NULL;
123
124     // ------------------------------------------------------------------------
125
126     int update_every = (int)config_get_number("plugin:nfacct", "update every", localhost->rrd_update_every);
127     if(update_every < localhost->rrd_update_every)
128         update_every = localhost->rrd_update_every;
129
130     usec_t step = update_every * USEC_PER_SEC;
131     heartbeat_t hb;
132     heartbeat_init(&hb);
133     for(;;) {
134         heartbeat_dt_usec(&hb);
135         heartbeat_next(&hb, step);
136
137         if(unlikely(netdata_exit)) break;
138
139         seq++;
140
141         nlh = nfacct_nlmsg_build_hdr(buf, NFNL_MSG_ACCT_GET, NLM_F_DUMP, (uint32_t)seq);
142         if(!nlh) {
143             error("nfacct.plugin: nfacct_nlmsg_build_hdr() failed");
144             goto cleanup;
145         }
146
147         if(mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
148             error("nfacct.plugin: mnl_socket_send");
149             goto cleanup;
150         }
151
152         ssize_t ret;
153         while((ret = mnl_socket_recvfrom(nl, buf, sizeof(buf))) > 0) {
154             if((ret = mnl_cb_run(buf, (size_t)ret, seq, portid, nfacct_callback, NULL)) <= 0) break;
155         }
156
157         if (ret == -1) {
158             error("nfacct.plugin: error communicating with kernel. NFACCT plugin can only work when netdata runs as root.");
159             goto cleanup;
160         }
161
162         // --------------------------------------------------------------------
163
164         if(nfacct_list && nfacct_list->len) {
165             int i;
166
167             if(!st_packets) {
168                 st_packets = rrdset_create_localhost(
169                         "netfilter"
170                         , "nfacct_packets"
171                         , NULL
172                         , "nfacct"
173                         , NULL
174                         , "Netfilter Accounting Packets"
175                         , "packets/s"
176                         , 3206
177                         , update_every
178                         , RRDSET_TYPE_STACKED
179                 );
180             }
181             else rrdset_next(st_packets);
182
183             for(i = 0; i < nfacct_list->len ; i++) {
184                 if(nfacct_list->data[i].updated) {
185                     if(unlikely(!nfacct_list->data[i].rd_packets))
186                         nfacct_list->data[i].rd_packets = rrddim_add(
187                                 st_packets
188                                 , nfacct_list->data[i].name
189                                 , NULL
190                                 , 1
191                                 , update_every
192                                 , RRD_ALGORITHM_INCREMENTAL
193                         );
194
195                     rrddim_set_by_pointer(
196                             st_packets
197                             , nfacct_list->data[i].rd_packets
198                             , (collected_number)nfacct_list->data[i].pkts
199                     );
200                 }
201             }
202
203             rrdset_done(st_packets);
204
205             // ----------------------------------------------------------------
206
207             st_bytes = rrdset_find_bytype_localhost("netfilter", "nfacct_bytes");
208             if(!st_bytes) {
209                 st_bytes = rrdset_create_localhost(
210                         "netfilter"
211                         , "nfacct_bytes"
212                         , NULL
213                         , "nfacct"
214                         , NULL
215                         , "Netfilter Accounting Bandwidth"
216                         , "kilobytes/s"
217                         , 3207
218                         , update_every
219                         , RRDSET_TYPE_STACKED
220                 );
221             }
222             else rrdset_next(st_bytes);
223
224             for(i = 0; i < nfacct_list->len ; i++) {
225                 if(nfacct_list->data[i].updated) {
226                     if(unlikely(!nfacct_list->data[i].rd_bytes))
227                         nfacct_list->data[i].rd_bytes = rrddim_add(
228                                 st_bytes
229                                 , nfacct_list->data[i].name
230                                 , NULL
231                                 , 1
232                                 , 1000 * update_every
233                                 , RRD_ALGORITHM_INCREMENTAL
234                         );
235
236                     rrddim_set_by_pointer(
237                             st_bytes
238                             , nfacct_list->data[i].rd_bytes
239                             , (collected_number)nfacct_list->data[i].bytes
240                     );
241                 }
242             }
243
244             rrdset_done(st_bytes);
245
246
247             // ----------------------------------------------------------------
248             // prepare for the next loop
249
250             for(i = 0; i < nfacct_list->len ; i++)
251                 nfacct_list->data[i].updated = 0;
252         }
253     }
254
255 cleanup:
256     info("NFACCT thread exiting");
257
258     if(nl) mnl_socket_close(nl);
259
260     static_thread->enabled = 0;
261     pthread_exit(NULL);
262     return NULL;
263 }
264 #endif