1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
// SPDX-License-Identifier: GPL-3.0-or-later
#include "claim.h"
// --------------------------------------------------------------------------------------------------------------------
// keep track of the last claiming failure reason
static char cloud_claim_failure_reason[4096] = "";
void claim_agent_failure_reason_set(const char *format, ...) {
if(!format || !*format) {
cloud_claim_failure_reason[0] = '\0';
return;
}
va_list args;
va_start(args, format);
vsnprintf(cloud_claim_failure_reason, sizeof(cloud_claim_failure_reason), format, args);
va_end(args);
nd_log(NDLS_DAEMON, NDLP_ERR,
"CLAIM: %s", cloud_claim_failure_reason);
}
const char *claim_agent_failure_reason_get(void) {
if(!cloud_claim_failure_reason[0])
return "Agent is not claimed yet";
else
return cloud_claim_failure_reason;
}
// --------------------------------------------------------------------------------------------------------------------
// claimed_id load/save
bool claimed_id_save_to_file(const char *claimed_id_str) {
bool ret;
const char *filename = filename_from_path_entry_strdupz(netdata_configured_cloud_dir, "claimed_id");
FILE *fp = fopen(filename, "w");
if(fp) {
fprintf(fp, "%s", claimed_id_str);
fclose(fp);
ret = true;
}
else {
nd_log(NDLS_DAEMON, NDLP_ERR,
"CLAIM: cannot open file '%s' for writing.", filename);
ret = false;
}
freez((void *)filename);
return ret;
}
static ND_UUID claimed_id_parse(const char *claimed_id, const char *source) {
ND_UUID uuid;
if(uuid_parse_flexi(claimed_id, uuid.uuid) != 0) {
uuid = UUID_ZERO;
nd_log(NDLS_DAEMON, NDLP_ERR,
"CLAIM: claimed_id '%s' (loaded from '%s'), is not a valid UUID.",
claimed_id, source);
}
return uuid;
}
static ND_UUID claimed_id_load_from_file(void) {
ND_UUID uuid;
long bytes_read;
const char *filename = filename_from_path_entry_strdupz(netdata_configured_cloud_dir, "claimed_id");
char *claimed_id = read_by_filename(filename, &bytes_read);
if(!claimed_id)
uuid = UUID_ZERO;
else
uuid = claimed_id_parse(claimed_id, filename);
freez(claimed_id);
freez((void *)filename);
return uuid;
}
static ND_UUID claimed_id_get_from_cloud_conf(void) {
if(appconfig_exists(&cloud_config, CONFIG_SECTION_GLOBAL, "claimed_id")) {
const char *claimed_id = appconfig_get(&cloud_config, CONFIG_SECTION_GLOBAL, "claimed_id", "");
if(claimed_id && *claimed_id)
return claimed_id_parse(claimed_id, "cloud.conf");
}
return UUID_ZERO;
}
static ND_UUID claimed_id_load(void) {
ND_UUID uuid = claimed_id_get_from_cloud_conf();
if(UUIDiszero(uuid))
uuid = claimed_id_load_from_file();
return uuid;
}
bool is_agent_claimed(void) {
ND_UUID uuid = claim_id_get_uuid();
return !UUIDiszero(uuid);
}
// --------------------------------------------------------------------------------------------------------------------
bool claim_id_matches(const char *claim_id) {
ND_UUID this_one = UUID_ZERO;
if(uuid_parse_flexi(claim_id, this_one.uuid) != 0 || UUIDiszero(this_one))
return false;
ND_UUID having = claim_id_get_uuid();
if(!UUIDiszero(having) && UUIDeq(having, this_one))
return true;
return false;
}
bool claim_id_matches_any(const char *claim_id) {
ND_UUID this_one = UUID_ZERO;
if(uuid_parse_flexi(claim_id, this_one.uuid) != 0 || UUIDiszero(this_one))
return false;
ND_UUID having = claim_id_get_uuid();
if(!UUIDiszero(having) && UUIDeq(having, this_one))
return true;
having = localhost->aclk.claim_id_of_parent;
if(!UUIDiszero(having) && UUIDeq(having, this_one))
return true;
having = localhost->aclk.claim_id_of_origin;
if(!UUIDiszero(having) && UUIDeq(having, this_one))
return true;
return false;
}
/* Change the claimed state of the agent.
*
* This only happens when the user has explicitly requested it:
* - via the cli tool by reloading the claiming state
* - after spawning the claim because of a command-line argument
* If this happens with the ACLK active under an old claim then we MUST KILL THE LINK
*/
bool load_claiming_state(void) {
if (aclk_online()) {
nd_log(NDLS_DAEMON, NDLP_ERR,
"CLAIM: agent was already connected to NC - forcing reconnection under new credentials");
disconnect_req = ACLK_RELOAD_CONF;
}
aclk_disable_runtime = 0;
ND_UUID uuid = claimed_id_load();
if(UUIDiszero(uuid)) {
// not found
if(claim_agent_automatically())
uuid = claimed_id_load();
}
bool have_claimed_id = false;
if(!UUIDiszero(uuid)) {
// we go it somehow
claim_id_set(uuid);
have_claimed_id = true;
}
invalidate_node_instances(&localhost->host_id.uuid, have_claimed_id ? &uuid.uuid : NULL);
metaqueue_store_claim_id(&localhost->host_id.uuid, have_claimed_id ? &uuid.uuid : NULL);
errno_clear();
if (!have_claimed_id)
nd_log(NDLS_DAEMON, NDLP_ERR,
"CLAIM: Unable to find our claimed_id, setting state to AGENT_UNCLAIMED");
else
nd_log(NDLS_DAEMON, NDLP_INFO,
"CLAIM: Found a valid claimed_id, setting state to AGENT_CLAIMED");
return have_claimed_id;
}
CLOUD_STATUS claim_reload_and_wait_online(void) {
nd_log(NDLS_DAEMON, NDLP_INFO,
"CLAIM: Reloading Agent Claiming configuration.");
nd_log_limits_unlimited();
cloud_conf_load(0);
bool claimed = load_claiming_state();
registry_update_cloud_base_url();
rrdpush_sender_send_claimed_id(localhost);
nd_log_limits_reset();
CLOUD_STATUS status = cloud_status();
if(claimed) {
int ms = 0;
do {
status = cloud_status();
if ((status == CLOUD_STATUS_ONLINE) && !UUIDiszero(localhost->node_id))
break;
sleep_usec(50 * USEC_PER_MS);
ms += 50;
} while (ms < 10000);
}
return status;
}
|