]> arthur.barton.de Git - netdata.git/blob - src/plugin_nfacct.c
added pthread_exit in hope it will solve the crash at exit
[netdata.git] / src / plugin_nfacct.c
1 #ifdef HAVE_CONFIG_H
2 #include <config.h>
3 #endif
4 #ifdef INTERNAL_PLUGIN_NFACCT
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <sys/types.h>
9 #include <dirent.h>
10 #include <sys/stat.h>
11 #include <fcntl.h>
12 #include <unistd.h>
13 #include <time.h>
14 #include <errno.h>
15
16 #include <libmnl/libmnl.h>
17 #include <libnetfilter_acct/libnetfilter_acct.h>
18
19 #include "global_statistics.h"
20 #include "common.h"
21 #include "appconfig.h"
22 #include "log.h"
23 #include "rrd.h"
24 #include "plugin_proc.h"
25
26 struct mynfacct {
27         const char *name;
28         uint64_t pkts;
29         uint64_t bytes;
30         struct nfacct *nfacct;
31 };
32
33 struct nfacct_list {
34         int size;
35         int len;
36         struct mynfacct data[];
37 } *nfacct_list = NULL;
38
39 static int nfacct_callback(const struct nlmsghdr *nlh, void *data) {
40         if(data) {};
41
42         if(!nfacct_list || nfacct_list->len == nfacct_list->size) {
43                 int size = (nfacct_list) ? nfacct_list->size : 0;
44                 int len = (nfacct_list) ? nfacct_list->len : 0;
45                 size++;
46
47                 info("nfacct.plugin: increasing nfacct_list to size %d", size);
48
49                 nfacct_list = realloc(nfacct_list, sizeof(struct nfacct_list) + (sizeof(struct mynfacct) * size));
50                 if(!nfacct_list) {
51                         error("nfacct.plugin: cannot allocate nfacct_list.");
52                         return MNL_CB_OK;
53                 }
54
55                 nfacct_list->data[len].nfacct = nfacct_alloc();
56                 if(!nfacct_list->data[size - 1].nfacct) {
57                         error("nfacct.plugin: nfacct_alloc() failed.");
58                         free(nfacct_list);
59                         nfacct_list = NULL;
60                         return MNL_CB_OK;
61                 }
62
63                 nfacct_list->size = size;
64                 nfacct_list->len = len;
65         }
66
67         if(nfacct_nlmsg_parse_payload(nlh, nfacct_list->data[nfacct_list->len].nfacct) < 0) {
68                 error("nfacct.plugin: nfacct_nlmsg_parse_payload() failed.");
69                 return MNL_CB_OK;
70         }
71
72         nfacct_list->data[nfacct_list->len].name  = nfacct_attr_get_str(nfacct_list->data[nfacct_list->len].nfacct, NFACCT_ATTR_NAME);
73         nfacct_list->data[nfacct_list->len].pkts  = nfacct_attr_get_u64(nfacct_list->data[nfacct_list->len].nfacct, NFACCT_ATTR_PKTS);
74         nfacct_list->data[nfacct_list->len].bytes = nfacct_attr_get_u64(nfacct_list->data[nfacct_list->len].nfacct, NFACCT_ATTR_BYTES);
75
76         nfacct_list->len++;
77         return MNL_CB_OK;
78 }
79
80 void *nfacct_main(void *ptr) {
81         if(ptr) { ; }
82
83         info("NFACCT thread created with task id %d", gettid());
84
85         if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
86                 error("nfacct.plugin: Cannot set pthread cancel type to DEFERRED.");
87
88         if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
89                 error("nfacct.plugin: Cannot set pthread cancel state to ENABLE.");
90
91         char buf[MNL_SOCKET_BUFFER_SIZE];
92         struct mnl_socket *nl = NULL;
93         struct nlmsghdr *nlh = NULL;
94         unsigned int seq = 0, portid = 0;
95
96         seq = time(NULL) - 1;
97
98         nl  = mnl_socket_open(NETLINK_NETFILTER);
99         if(!nl) {
100                 error("nfacct.plugin: mnl_socket_open() failed");
101                 pthread_exit(NULL);
102                 return NULL;
103         }
104
105         if(mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
106                 mnl_socket_close(nl);
107                 error("nfacct.plugin: mnl_socket_bind() failed");
108                 pthread_exit(NULL);
109                 return NULL;
110         }
111         portid = mnl_socket_get_portid(nl);
112
113         // ------------------------------------------------------------------------
114
115         struct timeval last, now;
116         unsigned long long usec = 0, susec = 0;
117         RRDSET *st = NULL;
118
119         gettimeofday(&last, NULL);
120
121         // ------------------------------------------------------------------------
122
123         while(1) {
124                 if(unlikely(netdata_exit)) break;
125
126                 seq++;
127
128                 nlh = nfacct_nlmsg_build_hdr(buf, NFNL_MSG_ACCT_GET, NLM_F_DUMP, seq);
129                 if(!nlh) {
130                         mnl_socket_close(nl);
131                         error("nfacct.plugin: nfacct_nlmsg_build_hdr() failed");
132                         pthread_exit(NULL);
133                         return NULL;
134                 }
135
136                 if(mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
137                         error("nfacct.plugin: mnl_socket_send");
138                         pthread_exit(NULL);
139                         return NULL;
140                 }
141
142                 if(nfacct_list) nfacct_list->len = 0;
143
144                 int ret;
145                 while((ret = mnl_socket_recvfrom(nl, buf, sizeof(buf))) > 0) {
146                         if((ret = mnl_cb_run(buf, ret, seq, portid, nfacct_callback, NULL)) <= 0) break;
147                 }
148
149                 if (ret == -1) {
150                         error("nfacct.plugin: error communicating with kernel.");
151                         pthread_exit(NULL);
152                         return NULL;
153                 }
154
155                 // --------------------------------------------------------------------
156
157                 gettimeofday(&now, NULL);
158                 usec = usecdiff(&now, &last) - susec;
159                 debug(D_NFACCT_LOOP, "nfacct.plugin: last loop took %llu usec (worked for %llu, sleeped for %llu).", usec + susec, usec, susec);
160
161                 if(usec < (rrd_update_every * 1000000ULL / 2ULL)) susec = (rrd_update_every * 1000000ULL) - usec;
162                 else susec = rrd_update_every * 1000000ULL / 2ULL;
163
164
165                 // --------------------------------------------------------------------
166
167                 if(nfacct_list && nfacct_list->len) {
168                         int i;
169
170                         st = rrdset_find_bytype("nfacct", "packets");
171                         if(!st) {
172                                 st = rrdset_create("nfacct", "packets", NULL, "netfilter", "Netfilter Accounting Packets", "packets/s", 1006, rrd_update_every, RRDSET_TYPE_STACKED);
173
174                                 for(i = 0; i < nfacct_list->len ; i++)
175                                         rrddim_add(st, nfacct_list->data[i].name, NULL, 1, rrd_update_every, RRDDIM_INCREMENTAL);
176                         }
177                         else rrdset_next(st);
178
179                         for(i = 0; i < nfacct_list->len ; i++) {
180                                 RRDDIM *rd = rrddim_find(st, nfacct_list->data[i].name);
181
182                                 if(!rd) rd = rrddim_add(st, nfacct_list->data[i].name, NULL, 1, rrd_update_every, RRDDIM_INCREMENTAL);
183                                 if(rd) rrddim_set_by_pointer(st, rd, nfacct_list->data[i].pkts);
184                         }
185
186                         rrdset_done(st);
187
188                         // ----------------------------------------------------------------
189
190                         st = rrdset_find_bytype("nfacct", "bytes");
191                         if(!st) {
192                                 st = rrdset_create("nfacct", "bytes", NULL, "netfilter", "Netfilter Accounting Bandwidth", "kilobytes/s", 1007, rrd_update_every, RRDSET_TYPE_STACKED);
193
194                                 for(i = 0; i < nfacct_list->len ; i++)
195                                         rrddim_add(st, nfacct_list->data[i].name, NULL, 1, 1000 * rrd_update_every, RRDDIM_INCREMENTAL);
196                         }
197                         else rrdset_next(st);
198
199                         for(i = 0; i < nfacct_list->len ; i++) {
200                                 RRDDIM *rd = rrddim_find(st, nfacct_list->data[i].name);
201
202                                 if(!rd) rd = rrddim_add(st, nfacct_list->data[i].name, NULL, 1, 1000 * rrd_update_every, RRDDIM_INCREMENTAL);
203                                 if(rd) rrddim_set_by_pointer(st, rd, nfacct_list->data[i].bytes);
204                         }
205
206                         rrdset_done(st);
207                 }
208
209                 // --------------------------------------------------------------------
210
211                 usleep(susec);
212
213                 // copy current to last
214                 bcopy(&now, &last, sizeof(struct timeval));
215         }
216
217         mnl_socket_close(nl);
218         pthread_exit(NULL);
219         return NULL;
220 }
221 #endif