| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #include "claim.h" |
| 4 | |
| 5 | ENUM_STR_MAP_DEFINE(CLOUD_STATUS) = { |
| 6 | { CLOUD_STATUS_ONLINE, "online"}, |
| 7 | { CLOUD_STATUS_INDIRECT, "indirect"}, |
| 8 | { CLOUD_STATUS_AVAILABLE, "available"}, |
| 9 | { CLOUD_STATUS_BANNED, "banned"}, |
| 10 | { CLOUD_STATUS_OFFLINE, "offline"}, |
| 11 | |
| 12 | // terminator |
| 13 | { 0, NULL }, |
| 14 | }; |
| 15 | ENUM_STR_DEFINE_FUNCTIONS(CLOUD_STATUS, CLOUD_STATUS_AVAILABLE, "available"); |
| 16 | |
| 17 | CLOUD_STATUS cloud_status(void) { |
| 18 | if(unlikely(aclk_disable_runtime)) |
| 19 | return CLOUD_STATUS_BANNED; |
| 20 | |
| 21 | if(likely(aclk_online())) |
| 22 | return CLOUD_STATUS_ONLINE; |
| 23 | |
| 24 | if(localhost && |
| 25 | localhost->sender && |
| 26 | rrdhost_flag_check(localhost, RRDHOST_FLAG_STREAM_SENDER_READY_4_METRICS) && |
| 27 | stream_sender_has_capabilities(localhost, STREAM_CAP_NODE_ID) && |
| 28 | !UUIDiszero(localhost->node_id) && |
| 29 | !UUIDiszero(localhost->aclk.claim_id_of_parent)) |
| 30 | return CLOUD_STATUS_INDIRECT; |
| 31 | |
| 32 | if(is_agent_claimed()) |
| 33 | return CLOUD_STATUS_OFFLINE; |
| 34 | |
| 35 | return CLOUD_STATUS_AVAILABLE; |
| 36 | } |
| 37 | |
| 38 | time_t cloud_last_change(void) { |
| 39 | time_t ret = MAX(last_conn_time_mqtt, last_disconnect_time); |
| 40 | if(!ret) ret = netdata_start_time; |
| 41 | return ret; |
| 42 | } |
| 43 | |
| 44 | time_t cloud_next_connection_attempt(void) { |
| 45 | return next_connection_attempt; |
| 46 | } |
| 47 | |
| 48 | size_t cloud_connection_id(void) { |
| 49 | return aclk_connection_counter; |
| 50 | } |
| 51 | |
| 52 | const char *cloud_status_aclk_offline_reason() { |
| 53 | if(aclk_disable_runtime) |
| 54 | return "banned"; |
| 55 | |
| 56 | return aclk_status_to_string(); |
| 57 | } |
| 58 | |
| 59 | const char *cloud_status_aclk_base_url() { |
| 60 | return aclk_cloud_base_url; |
| 61 | } |
| 62 | |
| 63 | CLOUD_STATUS buffer_json_cloud_status(BUFFER *wb, time_t now_s) { |
| 64 | CLOUD_STATUS status = cloud_status(); |
| 65 | |
| 66 | buffer_json_member_add_object(wb, "cloud"); |
| 67 | { |
| 68 | size_t id = cloud_connection_id(); |
| 69 | time_t last_change = cloud_last_change(); |
| 70 | time_t next_connect = cloud_next_connection_attempt(); |
| 71 | buffer_json_member_add_uint64(wb, "id", id); |
| 72 | buffer_json_member_add_string(wb, "status", CLOUD_STATUS_2str(status)); |
| 73 | buffer_json_member_add_time_t(wb, "since", last_change); |
| 74 | buffer_json_member_add_time_t(wb, "age", now_s - last_change); |
| 75 | |
| 76 | switch(status) { |
| 77 | default: |
| 78 | case CLOUD_STATUS_AVAILABLE: |
| 79 | // the agent is not claimed |
| 80 | buffer_json_member_add_string(wb, "url", cloud_config_url_get()); |
| 81 | buffer_json_member_add_string(wb, "reason", claim_agent_failure_reason_get()); |
| 82 | break; |
| 83 | |
| 84 | case CLOUD_STATUS_BANNED: { |
| 85 | // the agent is claimed, but has been banned from NC |
| 86 | CLAIM_ID claim_id = claim_id_get(); |
| 87 | buffer_json_member_add_string(wb, "claim_id", claim_id.str); |
| 88 | buffer_json_member_add_string(wb, "url", cloud_status_aclk_base_url()); |
| 89 | buffer_json_member_add_string(wb, "reason", "Agent is banned from Netdata Cloud"); |
| 90 | buffer_json_member_add_string(wb, "url", cloud_config_url_get()); |
| 91 | break; |
| 92 | } |
| 93 | |
| 94 | case CLOUD_STATUS_OFFLINE: { |
| 95 | // the agent is claimed, but cannot get online |
| 96 | CLAIM_ID claim_id = rrdhost_claim_id_get(localhost); |
| 97 | buffer_json_member_add_string(wb, "claim_id", claim_id.str); |
| 98 | buffer_json_member_add_string(wb, "url", cloud_status_aclk_base_url()); |
| 99 | buffer_json_member_add_string(wb, "reason", cloud_status_aclk_offline_reason()); |
| 100 | if (next_connect > now_s) { |
| 101 | buffer_json_member_add_time_t(wb, "next_check", next_connect); |
| 102 | buffer_json_member_add_time_t(wb, "next_in", next_connect - now_s); |
| 103 | } |
| 104 | break; |
| 105 | } |
| 106 | |
| 107 | case CLOUD_STATUS_ONLINE: { |
| 108 | // the agent is claimed and online |
| 109 | CLAIM_ID claim_id = claim_id_get(); |
| 110 | buffer_json_member_add_string(wb, "claim_id", claim_id.str); |
| 111 | buffer_json_member_add_string(wb, "url", cloud_status_aclk_base_url()); |
| 112 | buffer_json_member_add_string(wb, "reason", ""); |
| 113 | break; |
| 114 | } |
| 115 | |
| 116 | case CLOUD_STATUS_INDIRECT: { |
| 117 | CLAIM_ID claim_id = rrdhost_claim_id_get(localhost); |
| 118 | buffer_json_member_add_string(wb, "claim_id", claim_id.str); |
| 119 | buffer_json_member_add_string(wb, "url", cloud_config_url_get()); |
| 120 | buffer_json_member_add_string(wb, "reason", cloud_status_aclk_offline_reason()); |
| 121 | break; |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | buffer_json_object_close(wb); // cloud |
| 126 | |
| 127 | return status; |
| 128 | } |