Modify CLI command remove-stale-node to accept hostname (#18386)
* Remove stale node by name. If ALL is specified then ALL are checked and removed if they are not live * Change option ALL to ALL_NODES * When ALL_NODES is used, do not report the attempt to clean live, locahost or already unregistered nodes * Update src/daemon/commands.c Co-authored-by: Ilya Mashchenko <ilya@netdata.cloud> --------- Co-authored-by: Ilya Mashchenko <ilya@netdata.cloud>
Stelios Fragkakis committed
Aug 21, 2024 at 21:24 UTC
17f51628f92594eca926c4c042e38c0c011124c4
1 file changed
+67
-24
src/daemon/commands.c
+67
-24
@@ -136,7 +136,7 @@ static cmd_status_t cmd_help_execute(char *args, char **message)
136
"dumpconfig\n"
137
" Returns the current netdata.conf on stdout.\n"
138
#ifdef ENABLE_ACLK
139
- "remove-stale-node node_id|machine_guid\n"
139
+ "remove-stale-node node_id|machine_guid|hostname|ALL_NODES\n"
140
" Unregisters and removes a node from the cloud.\n"
141
#endif
142
"version\n"
@@ -345,13 +345,48 @@ static cmd_status_t cmd_dumpconfig(char *args, char **message)
345
}
346
347
#ifdef ENABLE_ACLK
348
+
349
+static int remove_ephemeral_host(BUFFER *wb, RRDHOST *host, bool report_error)
350
+{
351
+ if (host == localhost) {
352
+ if (report_error)
353
+ buffer_sprintf(wb, "You cannot unregister the parent node (%s)", rrdhost_hostname(host));
354
+ return 0;
355
+ }
356
+
357
+ if (rrdhost_is_online(host)) {
358
+ if (report_error)
359
+ buffer_sprintf(wb, "Cannot unregister a live node (%s)", rrdhost_hostname(host));
360
+ return 0;
361
+ }
362
+
363
+ if (!rrdhost_option_check(host, RRDHOST_OPTION_EPHEMERAL_HOST)) {
364
+ rrdhost_option_set(host, RRDHOST_OPTION_EPHEMERAL_HOST);
365
+ sql_set_host_label(&host->host_uuid, "_is_ephemeral", "true");
366
+ aclk_host_state_update(host, 0, 0);
367
+ unregister_node(host->machine_guid);
368
+ freez(host->node_id);
369
+ host->node_id = NULL;
370
+ buffer_sprintf(wb, "Unregistering node with machine guid %s, hostname = %s", host->machine_guid, rrdhost_hostname(host));
371
+ rrd_wrlock();
372
+ rrdhost_free___while_having_rrd_wrlock(host, true);
373
+ rrd_wrunlock();
374
+ return 1;
375
+ }
376
+ if (report_error)
377
+ buffer_sprintf(wb, "Node with machine guid %s, hostname = %s is already unregistered", host->machine_guid, rrdhost_hostname(host));
378
+ return 0;
379
+}
380
+
381
+#define SQL_HOSTNAME_TO_REMOVE "SELECT host_id FROM host WHERE (hostname = @hostname OR @hostname = 'ALL_NODES')"
382
+
383
static cmd_status_t cmd_remove_node(char *args, char **message)
384
{
385
(void)args;
386
387
BUFFER *wb = buffer_create(1024, NULL);
388
if (strlen(args) == 0) {
354
- buffer_sprintf(wb, "Please specify a machine or node UUID");
389
+ buffer_sprintf(wb, "Please specify a machine or node UUID or hostname");
390
goto done;
391
}
392
@@ -360,35 +395,43 @@ static cmd_status_t cmd_remove_node(char *args, char **message)
395
if (!host)
396
host = find_host_by_node_id(args);
397
363
- if (!host)
364
- buffer_sprintf(wb, "Node with machine or node UUID \"%s\" not found", args);
365
- else {
398
+ if (!host) {
399
+ sqlite3_stmt *res = NULL;
400
367
- if (host == localhost) {
368
- buffer_sprintf(wb, "You cannot unregister the parent node");
369
- goto done;
370
- }
401
+ bool report_error = strcmp(args, "ALL_NODES");
402
372
- if (rrdhost_is_online(host)) {
373
- buffer_sprintf(wb, "Cannot unregister a live node");
403
+ if (!PREPARE_STATEMENT(db_meta, SQL_HOSTNAME_TO_REMOVE, &res)) {
404
+ buffer_sprintf(wb, "Failed to prepare database statement to check for stale nodes");
405
goto done;
406
}
407
377
- if (!rrdhost_option_check(host, RRDHOST_OPTION_EPHEMERAL_HOST)) {
378
- rrdhost_option_set(host, RRDHOST_OPTION_EPHEMERAL_HOST);
379
- sql_set_host_label(&host->host_uuid, "_is_ephemeral", "true");
380
- aclk_host_state_update(host, 0, 0);
381
- unregister_node(host->machine_guid);
382
- freez(host->node_id);
383
- host->node_id = NULL;
384
- buffer_sprintf(wb, "Unregistering node with machine guid %s, hostname = %s", host->machine_guid, rrdhost_hostname(host));
385
- rrd_wrlock();
386
- rrdhost_free___while_having_rrd_wrlock(host, true);
387
- rrd_wrunlock();
408
+ int param = 0;
409
+ SQLITE_BIND_FAIL(done0, sqlite3_bind_text(res, ++param, args, -1, SQLITE_STATIC));
410
+
411
+ param = 0;
412
+ int cnt = 0;
413
+ while (sqlite3_step_monitored(res) == SQLITE_ROW) {
414
+ char guid[UUID_STR_LEN];
415
+ uuid_unparse_lower(*(nd_uuid_t *)sqlite3_column_blob(res, 0), guid);
416
+ host = rrdhost_find_by_guid(guid);
417
+ if (host) {
418
+ if (cnt)
419
+ buffer_fast_strcat(wb, "\n", 1);
420
+ cnt += remove_ephemeral_host(wb, host, report_error);
421
+ }
422
}
389
- else
390
- buffer_sprintf(wb, "Node with machine guid %s, hostname = %s is already unregistered", host->machine_guid, rrdhost_hostname(host));
423
+ if (!cnt && buffer_strlen(wb) == 0) {
424
+ if (report_error)
425
+ buffer_sprintf(wb, "No match for \"%s\"", args);
426
+ else
427
+ buffer_sprintf(wb, "No stale nodes found");
428
+ }
429
+ done0:
430
+ REPORT_BIND_FAIL(res, param);
431
+ SQLITE_FINALIZE(res);
432
}
433
+ else
434
+ (void) remove_ephemeral_host(wb, host, true);
435
436
done:
437
*message = strdupz(buffer_tostring(wb));