diff --git a/src/libpgagroal/management.c b/src/libpgagroal/management.c index 62615bd9..30af7d54 100644 --- a/src/libpgagroal/management.c +++ b/src/libpgagroal/management.c @@ -573,8 +573,8 @@ pgagroal_management_read_status(SSL* ssl, int socket, char output_format) { cJSON* json = pgagroal_management_json_read_status_details(ssl, socket, false); - // check we have an answer and it is not an error - if (!json || pgagroal_json_is_command_object_faulty(json)) + // check we have an answer, note that a faulty answer is still valid to be printed! + if (!json) { goto error; } @@ -810,7 +810,7 @@ pgagroal_management_json_read_status_details(SSL* ssl, int socket, bool include_ cJSON* current_connection_json = cJSON_CreateObject(); - cJSON_AddNumberToObject(current_connection_json, "number", i); + cJSON_AddNumberToObject(current_connection_json, "number", i + 1); cJSON_AddStringToObject(current_connection_json, "state", pgagroal_server_state_as_string(state)); cJSON_AddStringToObject(current_connection_json, "time", time > 0 ? ts : ""); cJSON_AddStringToObject(current_connection_json, "pid", pid > 0 ? p : ""); @@ -925,8 +925,8 @@ pgagroal_management_read_details(SSL* ssl, int socket, char output_format) { cJSON* json = pgagroal_management_json_read_status_details(ssl, socket, true); - // check we have an answer and it is not an error - if (!json || pgagroal_json_is_command_object_faulty(json)) + // check we have an answer, note that a faulty answer is still worth to be printed + if (!json) { goto error; } @@ -1802,7 +1802,7 @@ pgagroal_management_read_config_get(int socket, char* config_key, char* expected cJSON* json = pgagroal_managment_json_read_config_get(socket, config_key, expected_value); int status = EXIT_STATUS_OK; - if (!json || pgagroal_json_is_command_object_faulty(json)) + if (!json) { goto error; } @@ -1988,7 +1988,7 @@ pgagroal_management_read_conf_ls(SSL* ssl, int socket, char output_format) cJSON* json = pgagroal_management_json_read_conf_ls(ssl, socket); // check we have an answer and it is not an error - if (!json || pgagroal_json_is_command_object_faulty(json)) + if (!json) { goto error; } @@ -2184,9 +2184,10 @@ pgagroal_management_json_print_status_details(cJSON* json) { bool is_command_details = false; /* is this command 'status details' ? */ int status = EXIT_STATUS_OK; + bool previous_section_printed = false; /* in 'status details', print an header bar between sections */ // sanity check - if (!json || pgagroal_json_is_command_object_faulty(json)) + if (!json) { goto error; } @@ -2259,7 +2260,11 @@ pgagroal_management_json_print_status_details(cJSON* json) cJSON* servers_list = cJSON_GetObjectItemCaseSensitive(servers, JSON_TAG_ARRAY_NAME); cJSON_ArrayForEach(current, servers_list) { - printf("---------------------\n"); + if (!previous_section_printed) + { + printf("---------------------\n"); + previous_section_printed = true; + } printf("Server: %s\n", cJSON_GetObjectItemCaseSensitive(current, "server")->valuestring); printf("Host: %s\n", cJSON_GetObjectItemCaseSensitive(current, "host")->valuestring); printf("Port: %d\n", cJSON_GetObjectItemCaseSensitive(current, "port")->valueint); @@ -2273,7 +2278,11 @@ pgagroal_management_json_print_status_details(cJSON* json) cJSON* limits_list = cJSON_GetObjectItemCaseSensitive(limits, JSON_TAG_ARRAY_NAME); cJSON_ArrayForEach(current, limits_list) { - printf("---------------------\n"); + if (!previous_section_printed) + { + printf("---------------------\n"); + previous_section_printed = true; + } printf("Database: %s\n", cJSON_GetObjectItemCaseSensitive(current, "database")->valuestring); printf("Username: %s\n", cJSON_GetObjectItemCaseSensitive(current, "username")->valuestring); cJSON* current_connections = cJSON_GetObjectItemCaseSensitive(current, "connections"); @@ -2285,11 +2294,16 @@ pgagroal_management_json_print_status_details(cJSON* json) } // print the connection information - int i = 0; cJSON_ArrayForEach(current, cJSON_GetObjectItemCaseSensitive(connections, JSON_TAG_ARRAY_NAME)) { + if (!previous_section_printed) + { + printf("---------------------\n"); + previous_section_printed = false; + } + printf("Connection %4d: %-15s %-19s %-6s %-6s %s %s %s\n", - i++, + cJSON_GetObjectItemCaseSensitive(current, "number")->valueint, cJSON_GetObjectItemCaseSensitive(current, "state")->valuestring, cJSON_GetObjectItemCaseSensitive(current, "time")->valuestring, cJSON_GetObjectItemCaseSensitive(current, "pid")->valuestring, @@ -2300,6 +2314,9 @@ pgagroal_management_json_print_status_details(cJSON* json) } + // all done + goto end; + error: status = 1; end: @@ -2444,7 +2461,7 @@ pgagroal_management_json_print_conf_ls(cJSON* json) int status = EXIT_STATUS_OK; // sanity check - if (!json || pgagroal_json_is_command_object_faulty(json)) + if (!json) { goto error; }