static int cmd_list_stores(void) { /* verbose listing moved to separate function for clarity */ if (verbosity >= V_VERBOSE) { return cmd_list_stores_verbose(); } fsa_node_addr *nodes = get_nodes_from_cmd_line(); if (nodes == NULL) { /* error messages already printed */ return 1; } GHashTable *kb_hash = g_hash_table_new(g_str_hash, g_str_equal); fsa_node_addr *node = nodes; fsa_node_addr *tmp_node = NULL; fsa_kb_info *kis; fsa_kb_info *ki; fsa_kb_info *next_ki; int node_num = 0; int name_len; int max_name_len = 10; /* track lengths of kb names */ /* connect to each node separately */ while (node != NULL) { /* only pass a single node to fetch_kb_info, so break linked list */ tmp_node = node->next; node->next = NULL; kis = fsaf_fetch_kb_info(NULL, node); /* restore next item in list */ node->next = tmp_node; if (kis != NULL) { /* insert each kb info record into hash table */ ki = kis; while (ki != NULL) { /* will be breaking the linked list, so track next */ next_ki = ki->next; /* will return NULL if key not found */ ki->next = g_hash_table_lookup(kb_hash, ki->name); g_hash_table_insert(kb_hash, ki->name, ki); /* used to align columns when printing */ name_len = strlen((char *)ki->name); if (name_len > max_name_len) { max_name_len = name_len; } ki = next_ki; } } node_num += 1; node = node->next; } /* sort hash keys, print info for each kb in turn */ GList *kb_name_list = g_hash_table_get_keys(kb_hash); kb_name_list = g_list_sort(kb_name_list, (GCompareFunc)strcmp); char *kb_name; int n_total, n_running, n_stopped, n_unknown; int comma = 0; int print_store_header = 1; if (kb_name_list == NULL) { printf("No stores found\n"); } while (kb_name_list != NULL) { if (print_store_header) { if (colour_flag) { printf(ANSI_COLOUR_BLUE); } printf("%-*s store_status backend_status\n", max_name_len, "store_name"); if (colour_flag) { printf(ANSI_COLOUR_RESET); } print_store_header = 0; } n_running = n_stopped = n_unknown = 0; kb_name = kb_name_list->data; kis = g_hash_table_lookup(kb_hash, kb_name); for (ki = kis; ki != NULL; ki = ki->next) { switch (ki->status) { case KB_STATUS_RUNNING: n_running += 1; break; case KB_STATUS_STOPPED: n_stopped += 1; break; case KB_STATUS_UNKNOWN: n_unknown += 1; break; default: break; } } n_total = n_running + n_stopped + n_unknown; /* output info for this kb */ printf("%-*s ", max_name_len, kb_name); if (n_running == n_total) { print_colour("available ", ANSI_COLOUR_GREEN); } else { print_colour("unavailable ", ANSI_COLOUR_RED); } comma = 0; if (n_running > 0) { printf("%d/%d ", n_running, n_total); print_colour("running", ANSI_COLOUR_GREEN); comma = 1; } if (n_stopped > 0) { if (comma) { printf(", "); } printf("%d/%d ", n_stopped, n_total); print_colour("stopped", ANSI_COLOUR_RED); comma = 1; } if (n_unknown > 0) { if (comma) { printf(", "); } printf("%d/%d ", n_unknown, n_total); print_colour("unknown", ANSI_COLOUR_YELLOW); } printf("\n"); /* done with data in hash entry now */ fsa_kb_info_free(kis); kb_name_list = g_list_next(kb_name_list); } kb_name_list = g_list_first(kb_name_list); g_list_free(kb_name_list); fsa_node_addr_free(nodes); g_hash_table_destroy(kb_hash); return 0; }
/* convenience function */ fsa_kb_info *fsaf_fetch_kb_info_all() { return fsaf_fetch_kb_info(NULL, NULL); }
static int cmd_list_stores_verbose(void) { fsa_node_addr *nodes = get_nodes_from_cmd_line(); if (nodes == NULL) { /* error messages already printed */ return 1; } fsa_node_addr *node = nodes; fsa_node_addr *tmp_node = NULL; fsa_kb_info *ki; fsa_kb_info *kis; int node_num = 0; int info_header_printed = 0; int print_node_header = 1; /* connect to each node separately */ while (node != NULL) { print_node_line(node_num, node->host, print_node_header); print_node_header = 0; /* only pass a single node to fetch_kb_info, so break linked list */ tmp_node = node->next; node->next = NULL; kis = fsaf_fetch_kb_info(NULL, node); /* restore next item in list */ node->next = tmp_node; /* TODO: better error handling, differentiate between err and no * stores */ if (kis != NULL) { /* get column widths */ int max_name = 10; int max_segs = 10; int curlen; for (ki = kis; ki != NULL; ki = ki->next) { curlen = strlen((char *)ki->name); if (curlen > max_name) { max_name = curlen; } curlen = ki->p_segments_len * 3; if (curlen > max_segs) { max_segs = curlen; } } /* print header */ if (!info_header_printed) { if (colour_flag) { printf(ANSI_COLOUR_BLUE); } printf(" %-*s status port number_of_segments\n", max_name, "store_name"); if (colour_flag) { printf(ANSI_COLOUR_RESET); } info_header_printed = 1; } /* print kb info */ for (ki = kis; ki != NULL; ki = ki->next) { printf(" %-*s ", max_name, ki->name); const char *kistat = fsa_kb_info_status_to_string(ki->status); if (ki->status == KB_STATUS_RUNNING) { print_colour(kistat, ANSI_COLOUR_GREEN); } else if (ki->status == KB_STATUS_STOPPED) { print_colour(kistat, ANSI_COLOUR_RED); } else { print_colour(kistat, ANSI_COLOUR_YELLOW); } printf(" "); if (ki->port > 0) { printf("%-5d ", ki->port); } else { printf(" "); } if (ki->p_segments_len > 0) { if (verbosity == V_VERBOSE) { for (int i = 0; i < ki->p_segments_len; i++) { printf("%d", ki->p_segments_data[i]); if (i == ki->p_segments_len-1) { printf(" of %d", ki->num_segments); } else { printf(","); } } } else { /* print summary of segments */ printf("%d of %d", ki->p_segments_len, ki->num_segments); } } printf("\n"); } } fsa_kb_info_free(kis); node_num += 1; node = node->next; } fsa_node_addr_free(nodes); return 0; }