@cryptotaxi247 / netdata-1 / commits / ee1b7ef69

fixed max interface speed calculation (#4594)

Costa Tsaousis committed Nov 10, 2018 at 00:55 UTC ee1b7ef695f8c8d16cb77425841d477b564ee125
4 files changed +63 -18
collectors/proc.plugin/proc_net_dev.c
+49 -14
@@ -66,7 +66,7 @@ static struct netdev {
66 kernel_uint_t tcollisions;
67 kernel_uint_t tcarrier;
68 kernel_uint_t tcompressed;
69 - kernel_uint_t speed_max;
69 + kernel_uint_t speed;
70
71 // charts
72 RRDSET *st_bandwidth;
@@ -96,6 +96,10 @@ static struct netdev {
96 RRDDIM *rd_tcarrier;
97 RRDDIM *rd_tcompressed;
98
99 + usec_t speed_last_collected_usec;
100 + char *filename_speed;
101 + RRDSETVAR *chart_var_speed;
102 +
103 struct netdev *next;
104 } *netdev_root = NULL, *netdev_last_used = NULL;
105
@@ -139,7 +143,7 @@ static void netdev_charts_release(struct netdev *d) {
143 d->rd_tcompressed = NULL;
144 }
145
142 -static void netdev_free_strings(struct netdev *d) {
146 +static void netdev_free_chart_strings(struct netdev *d) {
147 freez((void *)d->chart_type_net_bytes);
148 freez((void *)d->chart_type_net_compressed);
149 freez((void *)d->chart_type_net_drops);
@@ -161,9 +165,10 @@ static void netdev_free_strings(struct netdev *d) {
165
166 static void netdev_free(struct netdev *d) {
167 netdev_charts_release(d);
164 - netdev_free_strings(d);
168 + netdev_free_chart_strings(d);
169
170 freez((void *)d->name);
171 + freez((void *)d->filename_speed);
172 freez((void *)d);
173 netdev_added--;
174 }
@@ -265,7 +270,7 @@ static inline void netdev_rename_cgroup(struct netdev *d, struct netdev_rename *
270 info("CGROUP: renaming network interface '%s' as '%s' under '%s'", r->host_device, r->container_device, r->container_name);
271
272 netdev_charts_release(d);
268 - netdev_free_strings(d);
273 + netdev_free_chart_strings(d);
274
275 char buffer[RRD_ID_LENGTH_MAX + 1];
276
@@ -435,8 +440,8 @@ int do_proc_net_dev(int update_every, usec_t dt) {
440 static procfile *ff = NULL;
441 static int enable_new_interfaces = -1;
442 static int do_bandwidth = -1, do_packets = -1, do_errors = -1, do_drops = -1, do_fifo = -1, do_compressed = -1, do_events = -1;
438 - static char *path_to_sys_devices_virtual_net = NULL;
439 - static char *path_to_sys_net_speed = NULL;
443 + static char *path_to_sys_devices_virtual_net = NULL, *path_to_sys_class_net_speed = NULL;
444 + static long long int dt_to_refresh_speed = 0;
445
446 if(unlikely(enable_new_interfaces == -1)) {
447 char filename[FILENAME_MAX + 1];
@@ -444,6 +449,9 @@ int do_proc_net_dev(int update_every, usec_t dt) {
449 snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/sys/devices/virtual/net/%s");
450 path_to_sys_devices_virtual_net = config_get(CONFIG_SECTION_PLUGIN_PROC_NETDEV, "path to get virtual interfaces", filename);
451
452 + snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/sys/class/net/%s/speed");
453 + path_to_sys_class_net_speed = config_get(CONFIG_SECTION_PLUGIN_PROC_NETDEV, "path to get net device speed", filename);
454 +
455 enable_new_interfaces = config_get_boolean_ondemand(CONFIG_SECTION_PLUGIN_PROC_NETDEV, "enable new interfaces detected at runtime", CONFIG_BOOLEAN_AUTO);
456
457 do_bandwidth = config_get_boolean_ondemand(CONFIG_SECTION_PLUGIN_PROC_NETDEV, "bandwidth for all interfaces", CONFIG_BOOLEAN_AUTO);
@@ -455,6 +463,9 @@ int do_proc_net_dev(int update_every, usec_t dt) {
463 do_events = config_get_boolean_ondemand(CONFIG_SECTION_PLUGIN_PROC_NETDEV, "frames, collisions, carrier counters for all interfaces", CONFIG_BOOLEAN_AUTO);
464
465 disabled_list = simple_pattern_create(config_get(CONFIG_SECTION_PLUGIN_PROC_NETDEV, "disable by default interfaces matching", "lo fireqos* *-ifb"), NULL, SIMPLE_PATTERN_EXACT);
466 +
467 + dt_to_refresh_speed = config_get_number(CONFIG_SECTION_PLUGIN_PROC_NETDEV, "refresh interface speed every seconds", 10) * USEC_PER_SEC;
468 + if(dt_to_refresh_speed < 0) dt_to_refresh_speed = 0;
469 }
470
471 if(unlikely(!ff)) {
@@ -505,12 +516,10 @@ int do_proc_net_dev(int update_every, usec_t dt) {
516 else
517 d->virtual = 0;
518
508 - // set nic speed if present
519 if(likely(!d->virtual)) {
510 - snprintfz(buffer, FILENAME_MAX, "%s/sys/class/net/%s/speed", netdata_configured_host_prefix, d->name);
511 - path_to_sys_net_speed = config_get(CONFIG_SECTION_PLUGIN_PROC_NETDEV, "path to get net device speed", buffer);
512 - int ret = read_single_number_file(path_to_sys_net_speed, (unsigned long long*)&d->speed_max);
513 - if(ret) error("Cannot read '%s'.", path_to_sys_net_speed);
520 + // set the filename to get the interface speed
521 + snprintfz(buffer, FILENAME_MAX, path_to_sys_class_net_speed, d->name);
522 + d->filename_speed = strdupz(buffer);
523 }
524
525 snprintfz(buffer, FILENAME_MAX, "plugin:proc:/proc/net/dev:%s", d->name);
@@ -597,9 +606,6 @@ int do_proc_net_dev(int update_every, usec_t dt) {
606 , RRDSET_TYPE_AREA
607 );
608
600 - RRDSETVAR *nic_speed_max = rrdsetvar_custom_chart_variable_create(d->st_bandwidth, "nic_speed_max");
601 - if(nic_speed_max) rrdsetvar_custom_chart_variable_set(nic_speed_max, (calculated_number)d->speed_max);
602 -
609 d->rd_rbytes = rrddim_add(d->st_bandwidth, "received", NULL, 8, BITS_IN_A_KILOBIT, RRD_ALGORITHM_INCREMENTAL);
610 d->rd_tbytes = rrddim_add(d->st_bandwidth, "sent", NULL, -8, BITS_IN_A_KILOBIT, RRD_ALGORITHM_INCREMENTAL);
611
@@ -616,6 +622,35 @@ int do_proc_net_dev(int update_every, usec_t dt) {
622 rrddim_set_by_pointer(d->st_bandwidth, d->rd_rbytes, (collected_number)d->rbytes);
623 rrddim_set_by_pointer(d->st_bandwidth, d->rd_tbytes, (collected_number)d->tbytes);
624 rrdset_done(d->st_bandwidth);
625 +
626 + // update the interface speed
627 + if(d->filename_speed) {
628 + d->speed_last_collected_usec += dt;
629 +
630 + if(unlikely(d->speed_last_collected_usec >= (usec_t)dt_to_refresh_speed)) {
631 +
632 + if(unlikely(!d->chart_var_speed)) {
633 + d->chart_var_speed = rrdsetvar_custom_chart_variable_create(d->st_bandwidth, "nic_speed_max");
634 + if(!d->chart_var_speed) {
635 + error("Cannot create interface %s chart variable 'nic_speed_max'. Will not update its speed anymore.", d->name);
636 + freez(d->filename_speed);
637 + d->filename_speed = NULL;
638 + }
639 + }
640 +
641 + if(d->filename_speed && d->chart_var_speed) {
642 + if(read_single_number_file(d->filename_speed, (unsigned long long *) &d->speed)) {
643 + error("Cannot refresh interface %s speed by reading '%s'. Will not update its speed anymore.", d->name, d->filename_speed);
644 + freez(d->filename_speed);
645 + d->filename_speed = NULL;
646 + }
647 + else {
648 + rrdsetvar_custom_chart_variable_set(d->chart_var_speed, (calculated_number) d->speed);
649 + d->speed_last_collected_usec = 0;
650 + }
651 + }
652 + }
653 + }
654 }
655
656 // --------------------------------------------------------------------
database/rrdcalctemplate.h
+1 -1
@@ -58,7 +58,7 @@ struct rrdcalctemplate {
58 struct rrdcalctemplate *next;
59 };
60
61 -#define RRDCALCTEMPLATE_HAS_CALCULATION(rt) ((rt)->after)
61 +#define RRDCALCTEMPLATE_HAS_DB_LOOKUP(rt) ((rt)->after)
62
63 extern void rrdcalctemplate_link_matching(RRDSET *st);
64
health/health.d/net.conf
+12 -2
@@ -4,13 +4,23 @@
4 # -----------------------------------------------------------------------------
5 # net traffic overflow
6
7 + template: interface_speed
8 + on: net.net
9 + os: *
10 + hosts: *
11 + families: *
12 + calc: ( $nic_speed_max > 0 ) ? ( $nic_speed_max) : ( nan )
13 + units: Mbit
14 + every: 10s
15 + info: The current speed of the physical network interface
16 +
17 template: 1m_received_traffic_overflow
18 on: net.net
19 os: linux
20 hosts: *
21 families: *
22 lookup: average -1m unaligned absolute of received
13 - calc: ($nic_speed_max > 0) ? ($this * 100 / ($nic_speed_max * 1000)) : ( nan )
23 + calc: ($interface_speed > 0) ? ($this * 100 / ($interface_speed * 1000)) : ( nan )
24 units: %
25 every: 10s
26 warn: $this > (($status >= $WARNING) ? (80) : (85))
@@ -25,7 +35,7 @@
35 hosts: *
36 families: *
37 lookup: average -1m unaligned absolute of sent
28 - calc: ($nic_speed_max > 0) ? ($this * 100 / ($nic_speed_max * 1000)) : ( nan )
38 + calc: ($interface_speed > 0) ? ($this * 100 / ($interface_speed * 1000)) : ( nan )
39 units: %
40 every: 10s
41 warn: $this > (($status >= $WARNING) ? (80) : (85))
health/health_config.c
+1 -1
@@ -84,7 +84,7 @@ static inline int rrdcalctemplate_add_template_from_config(RRDHOST *host, RRDCAL
84 return 0;
85 }
86
87 - if(unlikely(!RRDCALCTEMPLATE_HAS_CALCULATION(rt) && !rt->warning && !rt->critical)) {
87 + if(unlikely(!RRDCALCTEMPLATE_HAS_DB_LOOKUP(rt) && !rt->calculation && !rt->warning && !rt->critical)) {
88 error("Health configuration for template '%s' is useless (no calculation, no warning and no critical evaluation)", rt->name);
89 return 0;
90 }