Initialize cli earlier during agent startup (#21188)
Initialize the cli command in two phases. Support the ping command while the agent is initializing Enable the full cli once the agent has finished initialization
Stelios Fragkakis committed
Oct 22, 2025 at 21:24 UTC
8ca14bcc361cb49d77f7d9087ee25fcab9e4c3a9
3 files changed
+48
-24
src/daemon/commands.c
+36
-23
@@ -14,7 +14,7 @@ char cmd_prefix_by_status[] = {
14
CMD_PREFIX_ERROR
15
};
16
17
-static int command_server_initialized = 0;
17
+static cmd_init_status_t command_server_initialized = CMD_INIT_STATUS_OFF;
18
static int command_thread_error;
19
static int command_thread_shutdown;
20
static unsigned clients = 0;
@@ -52,24 +52,24 @@ static cmd_status_t cmd_mark_stale_nodes_ephemeral(char *args, char **message);
52
static cmd_status_t cmd_update_node_info(char *args, char **message);
53
54
static command_info_t command_info_array[] = {
55
- {"help", "", "Show this help menu.", cmd_help_execute, CMD_TYPE_HIGH_PRIORITY}, // show help menu
56
- {"reload-health", "", "Reload health configuration.", cmd_reload_health_execute, CMD_TYPE_ORTHOGONAL}, // reload health configuration
57
- {"reopen-logs", "", "Close and reopen log files.", cmd_reopen_logs_execute, CMD_TYPE_ORTHOGONAL}, // Close and reopen log files
58
- {"shutdown-agent", "", "Cleanup and exit the netdata agent.", cmd_exit_execute, CMD_TYPE_EXCLUSIVE}, // exit cleanly
59
- {"fatal-agent", "", "Log the state and halt the netdata agent.", cmd_fatal_execute, CMD_TYPE_HIGH_PRIORITY}, // exit with fatal error
60
- {"reload-claiming-state", "", "Reload agent claiming state from disk.", cmd_reload_claiming_state_execute, CMD_TYPE_ORTHOGONAL}, // reload claiming state
61
- {"reload-labels", "", "Reload all localhost labels.", cmd_reload_labels_execute, CMD_TYPE_ORTHOGONAL}, // reload the labels
62
- {"read-config", "", "", cmd_read_config_execute, CMD_TYPE_CONCURRENT},
63
- {"write-config", "", "", cmd_write_config_execute, CMD_TYPE_ORTHOGONAL},
64
- {"ping", "", "Return with 'pong' if agent is alive.", cmd_ping_execute, CMD_TYPE_ORTHOGONAL},
65
- {"aclk-state", "[json]", "Returns current state of ACLK and Netdata Cloud connection. (optionally in json).", cmd_aclk_state, CMD_TYPE_ORTHOGONAL},
66
- {"version", "", "Returns the netdata version.", cmd_version, CMD_TYPE_ORTHOGONAL},
67
- {"dumpconfig", "", "Returns the current netdata.conf on stdout.", cmd_dumpconfig, CMD_TYPE_ORTHOGONAL},
55
+ {"help", "", "Show this help menu.", cmd_help_execute, CMD_TYPE_HIGH_PRIORITY, CMD_INIT_STATUS_INIT}, // show help menu
56
+ {"reload-health", "", "Reload health configuration.", cmd_reload_health_execute, CMD_TYPE_ORTHOGONAL, CMD_INIT_STATUS_FULL}, // reload health configuration
57
+ {"reopen-logs", "", "Close and reopen log files.", cmd_reopen_logs_execute, CMD_TYPE_ORTHOGONAL, CMD_INIT_STATUS_FULL}, // Close and reopen log files
58
+ {"shutdown-agent", "", "Cleanup and exit the netdata agent.", cmd_exit_execute, CMD_TYPE_EXCLUSIVE, CMD_INIT_STATUS_FULL}, // exit cleanly
59
+ {"fatal-agent", "", "Log the state and halt the netdata agent.", cmd_fatal_execute, CMD_TYPE_HIGH_PRIORITY, CMD_INIT_STATUS_FULL}, // exit with fatal error
60
+ {"reload-claiming-state", "", "Reload agent claiming state from disk.", cmd_reload_claiming_state_execute, CMD_TYPE_ORTHOGONAL, CMD_INIT_STATUS_FULL}, // reload claiming state
61
+ {"reload-labels", "", "Reload all localhost labels.", cmd_reload_labels_execute, CMD_TYPE_ORTHOGONAL, CMD_INIT_STATUS_FULL}, // reload the labels
62
+ {"read-config", "", "", cmd_read_config_execute, CMD_TYPE_CONCURRENT, CMD_INIT_STATUS_FULL},
63
+ {"write-config", "", "", cmd_write_config_execute, CMD_TYPE_ORTHOGONAL, CMD_INIT_STATUS_FULL},
64
+ {"ping", "", "Return with 'pong' if agent is alive.", cmd_ping_execute, CMD_TYPE_ORTHOGONAL, CMD_INIT_STATUS_INIT}, // ping command
65
+ {"aclk-state", "[json]", "Returns current state of ACLK and Netdata Cloud connection. (optionally in json).", cmd_aclk_state, CMD_TYPE_ORTHOGONAL, CMD_INIT_STATUS_FULL},
66
+ {"version", "", "Returns the netdata version.", cmd_version, CMD_TYPE_ORTHOGONAL, CMD_INIT_STATUS_INIT},
67
+ {"dumpconfig", "", "Returns the current netdata.conf on stdout.", cmd_dumpconfig, CMD_TYPE_ORTHOGONAL, CMD_INIT_STATUS_FULL},
68
{"mark-stale-nodes-ephemeral", "<node_id | machine_guid | hostname | ALL_NODES>",
69
- "Marks one or all disconnected nodes as ephemeral, while keeping their retention\n available for queries on both this Netdata Agent dashboard and Netdata Cloud", cmd_mark_stale_nodes_ephemeral, CMD_TYPE_ORTHOGONAL},
69
+ "Marks one or all disconnected nodes as ephemeral, while keeping their retention\n available for queries on both this Netdata Agent dashboard and Netdata Cloud", cmd_mark_stale_nodes_ephemeral, CMD_TYPE_ORTHOGONAL, CMD_INIT_STATUS_FULL},
70
{"remove-stale-node", "<node_id | machine_guid | hostname | ALL_NODES>",
71
- "Marks one or all disconnected nodes as ephemeral, and removes them\n so that they are no longer available for queries, from both this\n Netdata Agent dashboard and Netdata Cloud.", cmd_remove_stale_node, CMD_TYPE_ORTHOGONAL},
72
- {"update-node-info", "", "Schedules an node update message for localhost to Netdata Cloud.", cmd_update_node_info, CMD_TYPE_ORTHOGONAL},
71
+ "Marks one or all disconnected nodes as ephemeral, and removes them\n so that they are no longer available for queries, from both this\n Netdata Agent dashboard and Netdata Cloud.", cmd_remove_stale_node, CMD_TYPE_ORTHOGONAL, CMD_INIT_STATUS_FULL},
72
+ {"update-node-info", "", "Schedules an node update message for localhost to Netdata Cloud.", cmd_update_node_info, CMD_TYPE_ORTHOGONAL, CMD_INIT_STATUS_FULL},
73
};
74
75
/* Mutexes for commands of type CMD_TYPE_ORTHOGONAL */
@@ -599,7 +599,12 @@ cmd_status_t execute_command(cmd_t idx, char *args, char **message)
599
cmd_type_t type = command_info_array[idx].type;
600
601
cmd_lock_by_type[type](idx);
602
- status = command_info_array[idx].func(args, message);
602
+ if (command_server_initialized >= command_info_array[idx].init_status)
603
+ status = command_info_array[idx].func(args, message);
604
+ else {
605
+ *message = strdupz("Agent is initializing");
606
+ status = CMD_STATUS_SUCCESS;
607
+ }
608
cmd_unlock_by_type[type](idx);
609
610
return status;
@@ -851,10 +856,19 @@ void commands_init(void)
856
int error;
857
858
sanity_check();
854
- if (command_server_initialized)
859
+ if (command_server_initialized == CMD_INIT_STATUS_FULL)
860
return;
861
857
- netdata_log_info("Initializing command server.");
862
+ if (command_server_initialized == CMD_INIT_STATUS_OFF) {
863
+ netdata_log_info("Initializing command server for liveness CHECK");
864
+ command_server_initialized = CMD_INIT_STATUS_INIT;
865
+ }
866
+ else {
867
+ netdata_log_info("Initializing full command server.");
868
+ command_server_initialized = CMD_INIT_STATUS_FULL;
869
+ return;
870
+ }
871
+
872
for (i = 0 ; i < CMD_TOTAL_COMMANDS ; ++i) {
873
fatal_assert(0 == netdata_mutex_init(&command_lock_array[i]));
874
}
@@ -878,7 +892,6 @@ void commands_init(void)
892
goto after_error;
893
}
894
881
- command_server_initialized = 1;
895
return;
896
897
after_error:
@@ -889,7 +902,7 @@ void commands_exit(void)
902
{
903
cmd_t i;
904
892
- if (!command_server_initialized)
905
+ if (command_server_initialized == CMD_INIT_STATUS_OFF)
906
return;
907
908
command_thread_shutdown = 1;
@@ -903,5 +916,5 @@ void commands_exit(void)
916
}
917
netdata_rwlock_destroy(&exclusive_rwlock);
918
netdata_log_info("Command server has stopped.");
906
- command_server_initialized = 0;
919
+ command_server_initialized = CMD_INIT_STATUS_OFF;
920
}
src/daemon/commands.h
+7
@@ -65,12 +65,19 @@ typedef enum cmd_type {
65
*/
66
typedef cmd_status_t (command_action_t) (char *args, char **message);
67
68
+typedef enum cmd_init_status {
69
+ CMD_INIT_STATUS_OFF,
70
+ CMD_INIT_STATUS_INIT,
71
+ CMD_INIT_STATUS_FULL,
72
+} cmd_init_status_t;
73
+
74
typedef struct command_info {
75
char *cmd_str; // the command string
76
char *params;
77
char *help;
78
command_action_t *func; // the function that executes the command
79
cmd_type_t type; // Concurrency control information for the command
80
+ cmd_init_status_t init_status; // command availability during start
81
} command_info_t;
82
83
typedef void (command_lock_t) (unsigned index);
src/daemon/main.c
+5
-1
@@ -1055,6 +1055,10 @@ int netdata_main(int argc, char **argv) {
1055
// ----------------------------------------------------------------------------------------------------------------
1056
delta_startup_time("RRD structures");
1057
1058
+ delta_startup_time("commands liveness support");
1059
+
1060
+ commands_init();
1061
+
1062
abort_on_fatal_disable();
1063
if (rrd_init(netdata_configured_hostname, system_info, false))
1064
fatal("Cannot initialize localhost instance with name '%s'.", netdata_configured_hostname);
@@ -1098,7 +1102,7 @@ int netdata_main(int argc, char **argv) {
1102
ml_start_threads();
1103
1104
// ----------------------------------------------------------------------------------------------------------------
1101
- delta_startup_time("commands API");
1105
+ delta_startup_time("commands full API");
1106
1107
commands_init();
1108