summaryrefslogtreecommitdiffstats
path: root/src/plugin_nfacct.c
blob: 7843161d32bf86e3bce565cf742e2dd3bfbfce97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include "common.h"

#ifdef INTERNAL_PLUGIN_NFACCT
#include <libmnl/libmnl.h>
#include <libnetfilter_acct/libnetfilter_acct.h>

struct mynfacct {
    const char *name;
    uint64_t pkts;
    uint64_t bytes;
    struct nfacct *nfacct;
};

struct nfacct_list {
    int size;
    int len;
    struct mynfacct data[];
} *nfacct_list = NULL;

static int nfacct_callback(const struct nlmsghdr *nlh, void *data) {
    if(data) {};

    if(!nfacct_list || nfacct_list->len == nfacct_list->size) {
        int size = (nfacct_list) ? nfacct_list->size : 0;
        int len = (nfacct_list) ? nfacct_list->len : 0;
        size++;

        info("nfacct.plugin: increasing nfacct_list to size %d", size);

        nfacct_list = reallocz(nfacct_list, sizeof(struct nfacct_list) + (sizeof(struct mynfacct) * size));

        nfacct_list->data[len].nfacct = nfacct_alloc();
        if(!nfacct_list->data[size - 1].nfacct) {
            error("nfacct.plugin: nfacct_alloc() failed.");
            free(nfacct_list);
            nfacct_list = NULL;
            return MNL_CB_OK;
        }

        nfacct_list->size = size;
        nfacct_list->len = len;
    }

    if(nfacct_nlmsg_parse_payload(nlh, nfacct_list->data[nfacct_list->len].nfacct) < 0) {
        error("nfacct.plugin: nfacct_nlmsg_parse_payload() failed.");
        return MNL_CB_OK;
    }

    nfacct_list->data[nfacct_list->len].name  = nfacct_attr_get_str(nfacct_list->data[nfacct_list->len].nfacct, NFACCT_ATTR_NAME);
    nfacct_list->data[nfacct_list->len].pkts  = nfacct_attr_get_u64(nfacct_list->data[nfacct_list->len].nfacct, NFACCT_ATTR_PKTS);
    nfacct_list->data[nfacct_list->len].bytes = nfacct_attr_get_u64(nfacct_list->data[nfacct_list->len].nfacct, NFACCT_ATTR_BYTES);

    nfacct_list->len++;
    return MNL_CB_OK;
}

void *nfacct_main(void *ptr) {
    if(ptr) { ; }

    info("NFACCT thread created with task id %d", gettid());

    if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
        error("nfacct.plugin: Cannot set pthread cancel type to DEFERRED.");

    if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
        error("nfacct.plugin: Cannot set pthread cancel state to ENABLE.");

    char buf[MNL_SOCKET_BUFFER_SIZE];
    struct mnl_socket *nl = NULL;
    struct nlmsghdr *nlh = NULL;
    unsigned int seq = 0, portid = 0;

    seq = time(NULL) - 1;

    nl  = mnl_socket_open(NETLINK_NETFILTER);
    if(!nl) {
        error("nfacct.plugin: mnl_socket_open() failed");
        pthread_exit(NULL);
        return NULL;
    }

    if(mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
        mnl_socket_close(nl);
        error("nfacct.plugin: mnl_socket_bind() failed");
        pthread_exit(NULL);
        return NULL;
    }
    portid = mnl_socket_get_portid(nl);

    // ------------------------------------------------------------------------

    struct timeval last, now;
    unsigned long long usec = 0, susec = 0;
    RRDSET *st = NULL;

    gettimeofday(&last, NULL);

    // ------------------------------------------------------------------------

    while(1) {
        if(unlikely(netdata_exit)) break;

        seq++;

        nlh = nfacct_nlmsg_build_hdr(buf, NFNL_MSG_ACCT_GET, NLM_F_DUMP, seq);
        if(!nlh) {
            mnl_socket_close(nl);
            error("nfacct.plugin: nfacct_nlmsg_build_hdr() failed");
            pthread_exit(NULL);
            return NULL;
        }

        if(mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
            error("nfacct.plugin: mnl_socket_send");
            pthread_exit(NULL);
            return NULL;
        }

        if(nfacct_list) nfacct_list->len = 0;

        int ret;
        while((ret = mnl_socket_recvfrom(nl, buf, sizeof(buf))) > 0) {
            if((ret = mnl_cb_run(buf, ret, seq, portid, nfacct_callback, NULL)) <= 0) break;
        }

        if (ret == -1) {
            error("nfacct.plugin: error communicating with kernel.");
            pthread_exit(NULL);
            return NULL;
        }

        // --------------------------------------------------------------------

        gettimeofday(&now, NULL);
        usec = usec_dt(&now, &last) - susec;
        debug(D_NFACCT_LOOP, "nfacct.plugin: last loop took %llu usec (worked for %llu, sleeped for %llu).", usec + susec, usec, susec);

        if(usec < (rrd_update_every * 1000000ULL / 2ULL)) susec = (rrd_update_every * 1000000ULL) - usec;
        else susec = rrd_update_every * 1000000ULL / 2ULL;


        // --------------------------------------------------------------------

        if(nfacct_list && nfacct_list->len) {
            int i;

            st = rrdset_find_bytype("netfilter", "nfacct_packets");
            if(!st) {
                st = rrdset_create("netfilter", "nfacct_packets", NULL, "nfacct", NULL, "Netfilter Accounting Packets", "packets/s", 3206, rrd_update_every, RRDSET_TYPE_STACKED);

                for(i = 0; i < nfacct_list->len ; i++)
                    rrddim_add(st, nfacct_list->data[i].name, NULL, 1, rrd_update_every, RRDDIM_INCREMENTAL);
            }
            else rrdset_next(st);

            for(i = 0; i < nfacct_list->len ; i++) {
                RRDDIM *rd = rrddim_find(st, nfacct_list->data[i].name);

                if(!rd) rd = rrddim_add(st, nfacct_list->data[i].name, NULL, 1, rrd_update_every, RRDDIM_INCREMENTAL);
                if(rd) rrddim_set_by_pointer(st, rd, nfacct_list->data[i].pkts);
            }

            rrdset_done(st);

            // ----------------------------------------------------------------

            st = rrdset_find_bytype("netfilter", "nfacct_bytes");
            if(!st) {
                st = rrdset_create("netfilter", "nfacct_bytes", NULL, "nfacct", NULL, "Netfilter Accounting Bandwidth", "kilobytes/s", 3207, rrd_update_every, RRDSET_TYPE_STACKED);

                for(i = 0; i < nfacct_list->len ; i++)
                    rrddim_add(st, nfacct_list->data[i].name, NULL, 1, 1000 * rrd_update_every, RRDDIM_INCREMENTAL);
            }
            else rrdset_next(st);

            for(i = 0; i < nfacct_list->len ; i++) {
                RRDDIM *rd = rrddim_find(st, nfacct_list->data[i].name);

                if(!rd) rd = rrddim_add(st, nfacct_list->data[i].name, NULL, 1, 1000 * rrd_update_every, RRDDIM_INCREMENTAL);
                if(rd) rrddim_set_by_pointer(st, rd, nfacct_list->data[i].bytes);
            }

            rrdset_done(st);
        }

        // --------------------------------------------------------------------

        usleep(susec);

        // copy current to last
        memmove(&last, &now, sizeof(struct timeval));
    }

    mnl_socket_close(nl);
    pthread_exit(NULL);
    return NULL;
}
#endif