]> arthur.barton.de Git - netdata.git/blob - src/proc_net_ip_vs_stats.c
d6b44911229220209ddd1fd4b30045e20f9aaf71
[netdata.git] / src / proc_net_ip_vs_stats.c
1 #include <inttypes.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4
5 #include "config.h"
6 #include "procfile.h"
7 #include "rrd.h"
8 #include "plugin_proc.h"
9
10 #define RRD_TYPE_NET_IPVS                       "ipvs"
11 #define RRD_TYPE_NET_IPVS_LEN           strlen(RRD_TYPE_NET_IPVS)
12
13 int do_proc_net_ip_vs_stats(int update_every, unsigned long long dt) {
14         static int do_bandwidth = -1, do_sockets = -1, do_packets = -1;
15         static procfile *ff = NULL;
16
17         if(do_bandwidth == -1)  do_bandwidth    = config_get_boolean("plugin:proc:/proc/net/ip_vs_stats", "IPVS bandwidth", 1);
18         if(do_sockets == -1)    do_sockets              = config_get_boolean("plugin:proc:/proc/net/ip_vs_stats", "IPVS connections", 1);
19         if(do_packets == -1)    do_packets              = config_get_boolean("plugin:proc:/proc/net/ip_vs_stats", "IPVS packets", 1);
20
21         if(dt) {};
22
23         if(!ff) ff = procfile_open("/proc/net/ip_vs_stats", " \t,:|");
24         if(!ff) return 1;
25
26         ff = procfile_readall(ff);
27         if(!ff) return 0; // we return 0, so that we will retry to open it next time
28
29         // make sure we have 3 lines
30         if(procfile_lines(ff) < 3) return 1;
31
32         // make sure we have 5 words on the 3rd line
33         if(procfile_linewords(ff, 2) < 5) return 1;
34
35         unsigned long long entries, InPackets, OutPackets, InBytes, OutBytes;
36
37         entries         = strtoull(procfile_lineword(ff, 2, 0), NULL, 16);
38         InPackets       = strtoull(procfile_lineword(ff, 2, 1), NULL, 16);
39         OutPackets      = strtoull(procfile_lineword(ff, 2, 2), NULL, 16);
40         InBytes         = strtoull(procfile_lineword(ff, 2, 3), NULL, 16);
41         OutBytes        = strtoull(procfile_lineword(ff, 2, 4), NULL, 16);
42
43         RRD_STATS *st;
44
45         // --------------------------------------------------------------------
46
47         if(do_sockets) {
48                 st = rrd_stats_find(RRD_TYPE_NET_IPVS ".sockets");
49                 if(!st) {
50                         st = rrd_stats_create(RRD_TYPE_NET_IPVS, "sockets", NULL, RRD_TYPE_NET_IPVS, "IPVS New Connections", "connections/s", 1001, update_every, CHART_TYPE_LINE);
51
52                         rrd_stats_dimension_add(st, "connections", NULL, 1, 1 * update_every, RRD_DIMENSION_INCREMENTAL);
53                 }
54                 else rrd_stats_next(st);
55
56                 rrd_stats_dimension_set(st, "connections", entries);
57                 rrd_stats_done(st);
58         }
59
60         // --------------------------------------------------------------------
61         
62         if(do_packets) {
63                 st = rrd_stats_find(RRD_TYPE_NET_IPVS ".packets");
64                 if(!st) {
65                         st = rrd_stats_create(RRD_TYPE_NET_IPVS, "packets", NULL, RRD_TYPE_NET_IPVS, "IPVS Packets", "packets/s", 1002, update_every, CHART_TYPE_LINE);
66
67                         rrd_stats_dimension_add(st, "received", NULL, 1, 1 * update_every, RRD_DIMENSION_INCREMENTAL);
68                         rrd_stats_dimension_add(st, "sent", NULL, -1, 1 * update_every, RRD_DIMENSION_INCREMENTAL);
69                 }
70                 else rrd_stats_next(st);
71
72                 rrd_stats_dimension_set(st, "received", InPackets);
73                 rrd_stats_dimension_set(st, "sent", OutPackets);
74                 rrd_stats_done(st);
75         }
76
77         // --------------------------------------------------------------------
78         
79         if(do_bandwidth) {
80                 st = rrd_stats_find(RRD_TYPE_NET_IPVS ".net");
81                 if(!st) {
82                         st = rrd_stats_create(RRD_TYPE_NET_IPVS, "net", NULL, RRD_TYPE_NET_IPVS, "IPVS Bandwidth", "kilobits/s", 1000, update_every, CHART_TYPE_AREA);
83
84                         rrd_stats_dimension_add(st, "received", NULL, 8, 1024 * update_every, RRD_DIMENSION_INCREMENTAL);
85                         rrd_stats_dimension_add(st, "sent", NULL, -8, 1024 * update_every, RRD_DIMENSION_INCREMENTAL);
86                 }
87                 else rrd_stats_next(st);
88
89                 rrd_stats_dimension_set(st, "received", InBytes);
90                 rrd_stats_dimension_set(st, "sent", OutBytes);
91                 rrd_stats_done(st);
92         }
93
94         return 0;
95 }