diff options
Diffstat (limited to 'claim/claim.c')
-rw-r--r-- | claim/claim.c | 50 |
1 files changed, 32 insertions, 18 deletions
diff --git a/claim/claim.c b/claim/claim.c index a30fd0dab..d81440d2a 100644 --- a/claim/claim.c +++ b/claim/claim.c @@ -57,7 +57,8 @@ CLAIM_AGENT_RESPONSE claim_agent(const char *claiming_arguments, bool force, con #ifndef DISABLE_CLOUD int exit_code; pid_t command_pid; - char command_buffer[CLAIMING_COMMAND_LENGTH + 1]; + char command_exec_buffer[CLAIMING_COMMAND_LENGTH + 1]; + char command_line_buffer[CLAIMING_COMMAND_LENGTH + 1]; FILE *fp_child_output, *fp_child_input; // This is guaranteed to be set early in main via post_conf_load() @@ -76,42 +77,55 @@ CLAIM_AGENT_RESPONSE claim_agent(const char *claiming_arguments, bool force, con if (proxy_type == PROXY_TYPE_SOCKS5 || proxy_type == PROXY_TYPE_HTTP) snprintf(proxy_flag, CLAIMING_PROXY_LENGTH, "-proxy=\"%s\"", proxy_str); - snprintfz(command_buffer, + snprintfz(command_exec_buffer, CLAIMING_COMMAND_LENGTH, + "exec \"%s%snetdata-claim.sh\"", + netdata_exe_path[0] ? netdata_exe_path : "", + netdata_exe_path[0] ? "/" : "" + ); + + snprintfz(command_line_buffer, CLAIMING_COMMAND_LENGTH, - "exec netdata-claim.sh %s -hostname=%s -id=%s -url=%s -noreload %s", + "%s %s -hostname=%s -id=%s -url=%s -noreload %s", + command_exec_buffer, proxy_flag, netdata_configured_hostname, localhost->machine_guid, cloud_base_url, claiming_arguments); - netdata_log_info("Executing agent claiming command 'netdata-claim.sh'"); - fp_child_output = netdata_popen(command_buffer, &command_pid, &fp_child_input); + netdata_log_info("Executing agent claiming command: %s", command_exec_buffer); + fp_child_output = netdata_popen(command_line_buffer, &command_pid, &fp_child_input); if(!fp_child_output) { - netdata_log_error("Cannot popen(\"%s\").", command_buffer); + netdata_log_error("Cannot popen(\"%s\").", command_exec_buffer); return CLAIM_AGENT_CANNOT_EXECUTE_CLAIM_SCRIPT; } - netdata_log_info("Waiting for claiming command to finish."); - while (fgets(command_buffer, CLAIMING_COMMAND_LENGTH, fp_child_output) != NULL) {;} + + netdata_log_info("Waiting for claiming command '%s' to finish.", command_exec_buffer); + char read_buffer[100 + 1]; + while (fgets(read_buffer, 100, fp_child_output) != NULL) {;} + exit_code = netdata_pclose(fp_child_input, fp_child_output, command_pid); - netdata_log_info("Agent claiming command returned with code %d", exit_code); + + netdata_log_info("Agent claiming command '%s' returned with code %d", command_exec_buffer, exit_code); if (0 == exit_code) { load_claiming_state(); return CLAIM_AGENT_OK; } if (exit_code < 0) { - netdata_log_error("Agent claiming command failed to complete its run."); + netdata_log_error("Agent claiming command '%s' failed to complete its run", command_exec_buffer); return CLAIM_AGENT_CLAIM_SCRIPT_FAILED; } errno = 0; unsigned maximum_known_exit_code = sizeof(claiming_errors) / sizeof(claiming_errors[0]) - 1; if ((unsigned)exit_code > maximum_known_exit_code) { - netdata_log_error("Agent failed to be claimed with an unknown error."); + netdata_log_error("Agent failed to be claimed with an unknown error. Cmd: '%s'", command_exec_buffer); return CLAIM_AGENT_CLAIM_SCRIPT_RETURNED_INVALID_CODE; } - netdata_log_error("Agent failed to be claimed with the following error message:"); + netdata_log_error("Agent failed to be claimed using the command '%s' with the following error message:", + command_exec_buffer); + netdata_log_error("\"%s\"", claiming_errors[exit_code]); if(msg) *msg = claiming_errors[exit_code]; @@ -342,7 +356,7 @@ int api_v2_claim(struct web_client *w, char *url) { BUFFER *wb = w->response.data; buffer_flush(wb); - buffer_json_initialize(wb, "\"", "\"", 0, true, false); + buffer_json_initialize(wb, "\"", "\"", 0, true, BUFFER_JSON_OPTIONS_DEFAULT); time_t now_s = now_realtime_sec(); CLOUD_STATUS status = buffer_json_cloud_status(wb, now_s); @@ -409,12 +423,12 @@ int api_v2_claim(struct web_client *w, char *url) { int ms = 0; do { status = cloud_status(); - if (status == CLOUD_STATUS_ONLINE) + if (status == CLOUD_STATUS_ONLINE && __atomic_load_n(&localhost->node_id, __ATOMIC_RELAXED)) break; - sleep_usec(100 * USEC_PER_MS); - ms += 100; - } while (ms < 5000); + sleep_usec(50 * USEC_PER_MS); + ms += 50; + } while (ms < 10000); } break; @@ -448,7 +462,7 @@ int api_v2_claim(struct web_client *w, char *url) { // our status may have changed // refresh the status in our output buffer_flush(wb); - buffer_json_initialize(wb, "\"", "\"", 0, true, false); + buffer_json_initialize(wb, "\"", "\"", 0, true, BUFFER_JSON_OPTIONS_DEFAULT); now_s = now_realtime_sec(); buffer_json_cloud_status(wb, now_s); |