| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #include "commands.h" |
| 4 | #include "plugins.d/pluginsd_internals.h" |
| 5 | |
| 6 | PARSER_RC stream_receiver_pluginsd_claimed_id(char **words, size_t num_words, PARSER *parser) { |
| 7 | const char *machine_guid_str = get_word(words, num_words, 1); |
| 8 | const char *claim_id_str = get_word(words, num_words, 2); |
| 9 | |
| 10 | if (!machine_guid_str || !claim_id_str) { |
| 11 | netdata_log_error("PLUGINSD: command CLAIMED_ID came malformed, machine_guid '%s', claim_id '%s'", |
| 12 | machine_guid_str ? machine_guid_str : "[unset]", |
| 13 | claim_id_str ? claim_id_str : "[unset]"); |
| 14 | return PARSER_RC_ERROR; |
| 15 | } |
| 16 | |
| 17 | RRDHOST *host = parser->user.host; |
| 18 | |
| 19 | nd_uuid_t machine_uuid; |
| 20 | if(uuid_parse(machine_guid_str, machine_uuid)) { |
| 21 | netdata_log_error("PLUGINSD: parameter machine guid to CLAIMED_ID command is not valid UUID. " |
| 22 | "Received: '%s'.", machine_guid_str); |
| 23 | return PARSER_RC_ERROR; |
| 24 | } |
| 25 | |
| 26 | nd_uuid_t claim_uuid; |
| 27 | if(strcmp(claim_id_str, "NULL") == 0) |
| 28 | uuid_clear(claim_uuid); |
| 29 | |
| 30 | else if(uuid_parse(claim_id_str, claim_uuid) != 0) { |
| 31 | netdata_log_error("PLUGINSD: parameter claim id to CLAIMED_ID command is not valid UUID. " |
| 32 | "Received: '%s'.", claim_id_str); |
| 33 | return PARSER_RC_ERROR; |
| 34 | } |
| 35 | |
| 36 | if(strcmp(machine_guid_str, host->machine_guid) != 0) { |
| 37 | netdata_log_error("PLUGINSD: received claim id for host '%s' but it came over the connection of '%s'", |
| 38 | machine_guid_str, host->machine_guid); |
| 39 | return PARSER_RC_OK; //the message is OK problem must be somewhere else |
| 40 | } |
| 41 | |
| 42 | if(host == localhost) { |
| 43 | netdata_log_error("PLUGINSD: CLAIMED_ID command cannot be used to set the claimed id of localhost. " |
| 44 | "Received: '%s'.", claim_id_str); |
| 45 | return PARSER_RC_OK; |
| 46 | } |
| 47 | |
| 48 | if(!uuid_is_null(claim_uuid)) { |
| 49 | uuid_copy(host->aclk.claim_id_of_origin.uuid, claim_uuid); |
| 50 | stream_sender_send_claimed_id(host); |
| 51 | } |
| 52 | |
| 53 | return PARSER_RC_OK; |
| 54 | } |
| 55 | |
| 56 | void stream_sender_send_claimed_id(RRDHOST *host) { |
| 57 | if(!stream_sender_has_capabilities(host, STREAM_CAP_CLAIM)) |
| 58 | return; |
| 59 | |
| 60 | if(unlikely(!rrdhost_can_stream_metadata_to_parent(host))) |
| 61 | return; |
| 62 | |
| 63 | CLEAN_BUFFER *wb = buffer_create(0, NULL); |
| 64 | |
| 65 | char str[UUID_STR_LEN] = ""; |
| 66 | ND_UUID uuid = host->aclk.claim_id_of_origin; |
| 67 | if(!UUIDiszero(uuid)) |
| 68 | uuid_unparse_lower(uuid.uuid, str); |
| 69 | else |
| 70 | strncpyz(str, "NULL", sizeof(str) - 1); |
| 71 | |
| 72 | buffer_sprintf(wb, PLUGINSD_KEYWORD_CLAIMED_ID " '%s' '%s'\n", |
| 73 | host->machine_guid, str); |
| 74 | |
| 75 | sender_commit_clean_buffer(host->sender, wb, STREAM_TRAFFIC_TYPE_METADATA); |
| 76 | } |