| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #include "plugin_proc.h" |
| 4 | |
| 5 | #define RRD_TYPE_NET_IPVS "ipvs" |
| 6 | #define PLUGIN_PROC_MODULE_NET_IPVS_NAME "/proc/net/ip_vs_stats" |
| 7 | #define CONFIG_SECTION_PLUGIN_PROC_NET_IPVS "plugin:" PLUGIN_PROC_CONFIG_NAME ":" PLUGIN_PROC_MODULE_NET_IPVS_NAME |
| 8 | |
| 9 | int do_proc_net_ip_vs_stats(int update_every, usec_t dt) { |
| 10 | (void)dt; |
| 11 | static int do_bandwidth = -1, do_sockets = -1, do_packets = -1; |
| 12 | static procfile *ff = NULL; |
| 13 | |
| 14 | if(do_bandwidth == -1) do_bandwidth = inicfg_get_boolean(&netdata_config, CONFIG_SECTION_PLUGIN_PROC_NET_IPVS, "IPVS bandwidth", 1); |
| 15 | if(do_sockets == -1) do_sockets = inicfg_get_boolean(&netdata_config, CONFIG_SECTION_PLUGIN_PROC_NET_IPVS, "IPVS connections", 1); |
| 16 | if(do_packets == -1) do_packets = inicfg_get_boolean(&netdata_config, CONFIG_SECTION_PLUGIN_PROC_NET_IPVS, "IPVS packets", 1); |
| 17 | |
| 18 | if(!ff) { |
| 19 | char filename[FILENAME_MAX + 1]; |
| 20 | snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/proc/net/ip_vs_stats"); |
| 21 | ff = procfile_open(inicfg_get(&netdata_config, CONFIG_SECTION_PLUGIN_PROC_NET_IPVS, "filename to monitor", filename), " \t,:|", PROCFILE_FLAG_DEFAULT); |
| 22 | } |
| 23 | if(!ff) return 1; |
| 24 | |
| 25 | ff = procfile_readall(ff); |
| 26 | if(!ff) return 0; // we return 0, so that we will retry to open it next time |
| 27 | |
| 28 | // make sure we have 3 lines |
| 29 | if(procfile_lines(ff) < 3) return 1; |
| 30 | |
| 31 | // make sure we have 5 words on the 3rd line |
| 32 | if(procfile_linewords(ff, 2) < 5) return 1; |
| 33 | |
| 34 | unsigned long long entries, InPackets, OutPackets, InBytes, OutBytes; |
| 35 | |
| 36 | entries = strtoull(procfile_lineword(ff, 2, 0), NULL, 16); |
| 37 | InPackets = strtoull(procfile_lineword(ff, 2, 1), NULL, 16); |
| 38 | OutPackets = strtoull(procfile_lineword(ff, 2, 2), NULL, 16); |
| 39 | InBytes = strtoull(procfile_lineword(ff, 2, 3), NULL, 16); |
| 40 | OutBytes = strtoull(procfile_lineword(ff, 2, 4), NULL, 16); |
| 41 | |
| 42 | if(do_sockets) { |
| 43 | static RRDSET *st = NULL; |
| 44 | |
| 45 | if(unlikely(!st)) { |
| 46 | st = rrdset_create_localhost( |
| 47 | RRD_TYPE_NET_IPVS |
| 48 | , "sockets" |
| 49 | , NULL |
| 50 | , RRD_TYPE_NET_IPVS |
| 51 | , NULL |
| 52 | , "IPVS New Connections" |
| 53 | , "connections/s" |
| 54 | , PLUGIN_PROC_NAME |
| 55 | , PLUGIN_PROC_MODULE_NET_IPVS_NAME |
| 56 | , NETDATA_CHART_PRIO_IPVS_SOCKETS |
| 57 | , update_every |
| 58 | , RRDSET_TYPE_LINE |
| 59 | ); |
| 60 | |
| 61 | rrddim_add(st, "connections", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL); |
| 62 | } |
| 63 | |
| 64 | rrddim_set(st, "connections", entries); |
| 65 | rrdset_done(st); |
| 66 | } |
| 67 | |
| 68 | if(do_packets) { |
| 69 | static RRDSET *st = NULL; |
| 70 | if(unlikely(!st)) { |
| 71 | st = rrdset_create_localhost( |
| 72 | RRD_TYPE_NET_IPVS |
| 73 | , "packets" |
| 74 | , NULL |
| 75 | , RRD_TYPE_NET_IPVS |
| 76 | , NULL |
| 77 | , "IPVS Packets" |
| 78 | , "packets/s" |
| 79 | , PLUGIN_PROC_NAME |
| 80 | , PLUGIN_PROC_MODULE_NET_IPVS_NAME |
| 81 | , NETDATA_CHART_PRIO_IPVS_PACKETS |
| 82 | , update_every |
| 83 | , RRDSET_TYPE_LINE |
| 84 | ); |
| 85 | |
| 86 | rrddim_add(st, "received", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL); |
| 87 | rrddim_add(st, "sent", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL); |
| 88 | } |
| 89 | |
| 90 | rrddim_set(st, "received", InPackets); |
| 91 | rrddim_set(st, "sent", OutPackets); |
| 92 | rrdset_done(st); |
| 93 | } |
| 94 | |
| 95 | if(do_bandwidth) { |
| 96 | static RRDSET *st = NULL; |
| 97 | if(unlikely(!st)) { |
| 98 | st = rrdset_create_localhost( |
| 99 | RRD_TYPE_NET_IPVS |
| 100 | , "net" |
| 101 | , NULL |
| 102 | , RRD_TYPE_NET_IPVS |
| 103 | , NULL |
| 104 | , "IPVS Bandwidth" |
| 105 | , "kilobits/s" |
| 106 | , PLUGIN_PROC_NAME |
| 107 | , PLUGIN_PROC_MODULE_NET_IPVS_NAME |
| 108 | , NETDATA_CHART_PRIO_IPVS_NET |
| 109 | , update_every |
| 110 | , RRDSET_TYPE_AREA |
| 111 | ); |
| 112 | |
| 113 | rrddim_add(st, "received", NULL, 8, BITS_IN_A_KILOBIT, RRD_ALGORITHM_INCREMENTAL); |
| 114 | rrddim_add(st, "sent", NULL, -8, BITS_IN_A_KILOBIT, RRD_ALGORITHM_INCREMENTAL); |
| 115 | } |
| 116 | |
| 117 | rrddim_set(st, "received", InBytes); |
| 118 | rrddim_set(st, "sent", OutBytes); |
| 119 | rrdset_done(st); |
| 120 | } |
| 121 | |
| 122 | return 0; |
| 123 | } |