0
0
Fork 0
mirror of https://github.com/netdata/netdata.git synced 2025-04-14 17:48:37 +00:00
netdata_netdata/claim/claim.c
Andrew Moss aa3ec552c8
Enable support for Netdata Cloud.
This PR merges the feature-branch to make the cloud live. It contains the following work:
Co-authored-by: Andrew Moss <1043609+amoss@users.noreply.github.com(opens in new tab)>
Co-authored-by: Jacek Kolasa <jacek.kolasa@gmail.com(opens in new tab)>
Co-authored-by: Austin S. Hemmelgarn <austin@netdata.cloud(opens in new tab)>
Co-authored-by: James Mills <prologic@shortcircuit.net.au(opens in new tab)>
Co-authored-by: Markos Fountoulakis <44345837+mfundul@users.noreply.github.com(opens in new tab)>
Co-authored-by: Timotej S <6674623+underhood@users.noreply.github.com(opens in new tab)>
Co-authored-by: Stelios Fragkakis <52996999+stelfrag@users.noreply.github.com(opens in new tab)>
* dashboard with new navbars, v1.0-alpha.9: PR 
* dashboard v1.0.11: 
Co-authored-by: Jacek Kolasa <jacek.kolasa@gmail.com(opens in new tab)>
* Added installer code to bundle JSON-c if it's not present. PR 
Co-authored-by: James Mills <prologic@shortcircuit.net.au(opens in new tab)>
* Fix claiming config PR 
* Adds JSON-c as hard dep. for ACLK PR 
* Fix SSL renegotiation errors in old versions of openssl. PR . Also - we have a transient problem with opensuse CI so this PR disables them with a commit from @prologic.
Co-authored-by: James Mills <prologic@shortcircuit.net.au(opens in new tab)>
* Fix claiming error handling PR 
* Added CI to verify JSON-C bundling code in installer PR 
* Make cloud-enabled flag in web/api/v1/info be independent of ACLK build success PR 
* Reduce ACLK_STABLE_TIMEOUT from 10 to 3 seconds PR 
* remove old-cloud related UI from old dashboard (accessible now via /old suffix) PR 
* dashboard v1.0.13 PR 
* dashboard v1.0.14 PR 
* Provide feedback on proxy setting changes PR 
* Change the name of the connect message to update during an ongoing session PR 
* Fetch active alarms from alarm_log PR 
2020-05-11 16:37:27 +10:00

163 lines
5.6 KiB
C

// SPDX-License-Identifier: GPL-3.0-or-later
#include "claim.h"
#include "../registry/registry_internals.h"
#include "../aclk/aclk_common.h"
char *claiming_pending_arguments = NULL;
static char *claiming_errors[] = {
"Agent claimed successfully", // 0
"Unknown argument", // 1
"Problems with claiming working directory", // 2
"Missing dependencies", // 3
"Failure to connect to endpoint", // 4
"The CLI didn't work", // 5
"Wrong user", // 6
"Unknown HTTP error message", // 7
"invalid node id", // 8
"invalid node name", // 9
"invalid room id", // 10
"invalid public key", // 11
"token expired/token not found/invalid token", // 12
"already claimed", // 13
"processing claiming", // 14
"Internal Server Error", // 15
"Gateway Timeout", // 16
"Service Unavailable" // 17
};
static char *claimed_id = NULL;
char *is_agent_claimed(void)
{
return claimed_id;
}
#define CLAIMING_COMMAND_LENGTH 16384
#define CLAIMING_PROXY_LENGTH CLAIMING_COMMAND_LENGTH/4
extern struct registry registry;
/* rrd_init() and post_conf_load() must have been called before this function */
void claim_agent(char *claiming_arguments)
{
if (!netdata_cloud_setting) {
error("Refusing to claim agent -> cloud functionality has been disabled");
return;
}
#ifndef DISABLE_CLOUD
int exit_code;
pid_t command_pid;
char command_buffer[CLAIMING_COMMAND_LENGTH + 1];
FILE *fp;
// This is guaranteed to be set early in main via post_conf_load()
char *cloud_base_url = appconfig_get(&cloud_config, CONFIG_SECTION_GLOBAL, "cloud base url", NULL);
if (cloud_base_url == NULL)
fatal("Do not move the cloud base url out of post_conf_load!!");
const char *proxy_str;
ACLK_PROXY_TYPE proxy_type;
char proxy_flag[CLAIMING_PROXY_LENGTH] = "-noproxy";
proxy_str = aclk_get_proxy(&proxy_type);
if (proxy_type == PROXY_TYPE_SOCKS5 || proxy_type == PROXY_TYPE_HTTP)
snprintf(proxy_flag, CLAIMING_PROXY_LENGTH, "-proxy=\"%s\"", proxy_str);
snprintfz(command_buffer,
CLAIMING_COMMAND_LENGTH,
"exec netdata-claim.sh %s -hostname=%s -id=%s -url=%s -noreload %s",
proxy_flag,
netdata_configured_hostname,
localhost->machine_guid,
cloud_base_url,
claiming_arguments);
info("Executing agent claiming command 'netdata-claim.sh'");
fp = mypopen(command_buffer, &command_pid);
if(!fp) {
error("Cannot popen(\"%s\").", command_buffer);
return;
}
info("Waiting for claiming command to finish.");
while (fgets(command_buffer, CLAIMING_COMMAND_LENGTH, fp) != NULL) {;}
exit_code = mypclose(fp, command_pid);
info("Agent claiming command returned with code %d", exit_code);
if (0 == exit_code) {
load_claiming_state();
return;
}
if (exit_code < 0) {
error("Agent claiming command failed to complete its run.");
return;
}
errno = 0;
unsigned maximum_known_exit_code = sizeof(claiming_errors) / sizeof(claiming_errors[0]) - 1;
if ((unsigned)exit_code > maximum_known_exit_code) {
error("Agent failed to be claimed with an unknown error.");
return;
}
error("Agent failed to be claimed with the following error message:");
error("\"%s\"", claiming_errors[exit_code]);
#else
UNUSED(claiming_arguments);
UNUSED(claiming_errors);
#endif
}
void load_claiming_state(void)
{
if (claimed_id != NULL) {
freez(claimed_id);
claimed_id = NULL;
}
// Propagate into aclk and registry. Be kind of atomic...
appconfig_get(&cloud_config, CONFIG_SECTION_GLOBAL, "cloud base url", DEFAULT_CLOUD_BASE_URL);
char filename[FILENAME_MAX + 1];
snprintfz(filename, FILENAME_MAX, "%s/cloud.d/claimed_id", netdata_configured_varlib_dir);
long bytes_read;
claimed_id = read_by_filename(filename, &bytes_read);
if (!claimed_id) {
info("Unable to load '%s', setting state to AGENT_UNCLAIMED", filename);
return;
}
info("File '%s' was found. Setting state to AGENT_CLAIMED.", filename);
// --------------------------------------------------------------------
// Check if the cloud is enabled
#if defined( DISABLE_CLOUD ) || !defined( ENABLE_ACLK )
netdata_cloud_setting = 0;
#else
netdata_cloud_setting = appconfig_get_boolean(&cloud_config, CONFIG_SECTION_GLOBAL, "enabled", 1);
#endif
}
struct config cloud_config = { .first_section = NULL,
.last_section = NULL,
.mutex = NETDATA_MUTEX_INITIALIZER,
.index = { .avl_tree = { .root = NULL, .compar = appconfig_section_compare },
.rwlock = AVL_LOCK_INITIALIZER } };
void load_cloud_conf(int silent)
{
char *filename;
errno = 0;
int ret = 0;
filename = strdupz_path_subpath(netdata_configured_varlib_dir, "cloud.d/cloud.conf");
ret = appconfig_load(&cloud_config, filename, 1, NULL);
if(!ret && !silent) {
info("CONFIG: cannot load cloud config '%s'. Running with internal defaults.", filename);
}
freez(filename);
}