@cryptotaxi247 / netdata-1 / commits / e14676302

configuration option to prevent multiple streaming connections for the a host; #3505

Costa Tsaousis (ktsaou) committed Mar 7, 2018 at 03:16 UTC e146763024ae7700b5dad731a81c351b5f63c9d3
1 file changed +52 -2
src/rrdpush.c
+52 -2
@@ -25,6 +25,11 @@
25
26 #define START_STREAMING_PROMPT "Hit me baby, push them over..."
27
28 +typedef enum {
29 + RRDPUSH_MULTIPLE_CONNECTIONS_ALLOW,
30 + RRDPUSH_MULTIPLE_CONNECTIONS_DENY_NEW
31 +} RRDPUSH_MULTIPLE_CONNECTIONS_STRATEGY;
32 +
33 int default_rrdpush_enabled = 0;
34 char *default_rrdpush_destination = NULL;
35 char *default_rrdpush_api_key = NULL;
@@ -687,6 +692,35 @@ static void log_stream_connection(const char *client_ip, const char *client_port
692 log_access("STREAM: %d '[%s]:%s' '%s' host '%s' api key '%s' machine guid '%s'", gettid(), client_ip, client_port, msg, host, api_key, machine_guid);
693 }
694
695 +static RRDPUSH_MULTIPLE_CONNECTIONS_STRATEGY get_multiple_connections_strategy(struct config *c, const char *section, const char *name, RRDPUSH_MULTIPLE_CONNECTIONS_STRATEGY def) {
696 + char *value;
697 + switch(def) {
698 + default:
699 + case RRDPUSH_MULTIPLE_CONNECTIONS_ALLOW:
700 + value = "allow";
701 + break;
702 +
703 + case RRDPUSH_MULTIPLE_CONNECTIONS_DENY_NEW:
704 + value = "deny";
705 + break;
706 + }
707 +
708 + value = appconfig_get(c, section, name, value);
709 +
710 + RRDPUSH_MULTIPLE_CONNECTIONS_STRATEGY ret = def;
711 +
712 + if(strcasecmp(value, "allow") == 0 || strcasecmp(value, "permit") == 0 || strcasecmp(value, "accept") == 0)
713 + ret = RRDPUSH_MULTIPLE_CONNECTIONS_ALLOW;
714 +
715 + else if(strcasecmp(value, "deny") == 0 || strcasecmp(value, "reject") == 0 || strcasecmp(value, "block") == 0)
716 + ret = RRDPUSH_MULTIPLE_CONNECTIONS_DENY_NEW;
717 +
718 + else
719 + error("Invalid stream config value at section [%s], setting '%s', value '%s'", section, name, value);
720 +
721 + return ret;
722 +}
723 +
724 static int rrdpush_receive(int fd, const char *key, const char *hostname, const char *registry_hostname, const char *machine_guid, const char *os, const char *timezone, const char *tags, int update_every, char *client_ip, char *client_port) {
725 RRDHOST *host;
726 int history = default_rrd_history_entries;
@@ -696,6 +730,7 @@ static int rrdpush_receive(int fd, const char *key, const char *hostname, const
730 char *rrdpush_destination = default_rrdpush_destination;
731 char *rrdpush_api_key = default_rrdpush_api_key;
732 time_t alarms_delay = 60;
733 + RRDPUSH_MULTIPLE_CONNECTIONS_STRATEGY rrdpush_multiple_connections_strategy = RRDPUSH_MULTIPLE_CONNECTIONS_ALLOW;
734
735 update_every = (int)appconfig_get_number(&stream_config, machine_guid, "update every", update_every);
736 if(update_every < 0) update_every = 1;
@@ -722,6 +757,9 @@ static int rrdpush_receive(int fd, const char *key, const char *hostname, const
757 rrdpush_api_key = appconfig_get(&stream_config, key, "default proxy api key", rrdpush_api_key);
758 rrdpush_api_key = appconfig_get(&stream_config, machine_guid, "proxy api key", rrdpush_api_key);
759
760 + rrdpush_multiple_connections_strategy = get_multiple_connections_strategy(&stream_config, key, "multiple connections", rrdpush_multiple_connections_strategy);
761 + rrdpush_multiple_connections_strategy = get_multiple_connections_strategy(&stream_config, machine_guid, "multiple connections", rrdpush_multiple_connections_strategy);
762 +
763 tags = appconfig_set_default(&stream_config, machine_guid, "host tags", (tags)?tags:"");
764 if(tags && !*tags) tags = NULL;
765
@@ -805,8 +843,20 @@ static int rrdpush_receive(int fd, const char *key, const char *hostname, const
843 }
844
845 rrdhost_wrlock(host);
808 - if(host->connected_senders > 0)
809 - info("STREAM %s [receive from [%s]:%s]: multiple streaming connections for the same host detected. If multiple netdata are pushing metrics for the same charts, at the same time, the result is unexpected.", host->hostname, client_ip, client_port);
846 + if(host->connected_senders > 0) {
847 + switch(rrdpush_multiple_connections_strategy) {
848 + case RRDPUSH_MULTIPLE_CONNECTIONS_ALLOW:
849 + info("STREAM %s [receive from [%s]:%s]: multiple streaming connections for the same host detected. If multiple netdata are pushing metrics for the same charts, at the same time, the result is unexpected.", host->hostname, client_ip, client_port);
850 + break;
851 +
852 + case RRDPUSH_MULTIPLE_CONNECTIONS_DENY_NEW:
853 + rrdhost_unlock(host);
854 + log_stream_connection(client_ip, client_port, key, host->machine_guid, host->hostname, "REJECTED - ALREADY CONNECTED");
855 + info("STREAM %s [receive from [%s]:%s]: multiple streaming connections for the same host detected. Rejecting new connection.", host->hostname, client_ip, client_port);
856 + fclose(fp);
857 + return 0;
858 + }
859 + }
860
861 rrdhost_flag_clear(host, RRDHOST_FLAG_ORPHAN);
862 host->connected_senders++;