static int print_info(int ok_to_fail) { struct votequorum_info info; int err; if ( (err=votequorum_getinfo(handle, VOTEQUORUM_QDEVICE_NODEID, &info)) != CS_OK) { fprintf(stderr, "votequorum_getinfo error %d: %s\n", err, ok_to_fail?"OK":"FAILED"); return -1; } else { printf("name %s\n", info.qdevice_name); printf("qdevice votes %d\n", info.qdevice_votes); if (info.flags & VOTEQUORUM_INFO_QDEVICE_ALIVE) { printf("alive "); } if (info.flags & VOTEQUORUM_INFO_QDEVICE_CAST_VOTE) { printf("cast-vote "); } if (info.flags & VOTEQUORUM_INFO_QDEVICE_MASTER_WINS) { printf("master-wins"); } printf("\n\n"); } return 0; }
static void getinfo (int sock) { int ret; struct votequorum_info info; char response[100]; q_lib_init (); ret = votequorum_getinfo(vq_handle, 0, &info); if (ret != CS_OK) { snprintf (response, 100, "%s", FAIL_STR); qb_log (LOG_ERR, "votequorum_getinfo FAILED: %d", ret); goto send_response; } snprintf (response, 100, "%d:%d:%d:%d:%d", info.node_votes, info.node_expected_votes, info.highest_expected, info.total_votes, info.quorum); send_response: send (sock, response, strlen (response), 0); }
static int get_votes(votequorum_handle_t v_handle, uint32_t nodeid) { int votes = -1; struct votequorum_info info; if (votequorum_getinfo(v_handle, nodeid, &info) == CS_OK) votes = info.node_votes; return votes; }
static int unregister_qdevice(void) { int err; struct votequorum_info info; err = votequorum_getinfo(v_handle, our_nodeid, &info); if (err != CS_OK) { fprintf(stderr, "Unable to get quorum device info: %s\n", cs_strerror(err)); return -1; } if (!(info.flags & VOTEQUORUM_INFO_QDEVICE_REGISTERED)) { return 0; } err = votequorum_qdevice_unregister(v_handle, info.qdevice_name); if (err != CS_OK) { fprintf(stderr, "Unable to unregister quorum device: %s\n", cs_strerror(err)); return -1; } return 0; }
static int display_quorum_data(int is_quorate, nodeid_format_t nodeid_format, name_format_t name_format, sorttype_t sort_type, int loop) { struct votequorum_info info; int err; char quorumtype[256]; time_t t; memset(quorumtype, 0, sizeof(quorumtype)); printf("Quorum information\n"); printf("------------------\n"); time(&t); printf("Date: %s", ctime((const time_t *)&t)); if (get_quorum_type(quorumtype, sizeof(quorumtype))) { strncpy(quorumtype, "Not configured", sizeof(quorumtype) - 1); } printf("Quorum provider: %s\n", quorumtype); printf("Nodes: %d\n", g_view_list_entries); if (nodeid_format == NODEID_FORMAT_DECIMAL) { printf("Node ID: %u\n", our_nodeid); } else { printf("Node ID: 0x%08x\n", our_nodeid); } if (v_handle) { printf("Ring ID: %d/%" PRIu64 "\n", g_ring_id_rep_node, g_ring_id); } else { printf("Ring ID: %" PRIu64 "\n", g_ring_id); } printf("Quorate: %s\n", is_quorate?"Yes":"No"); if (!v_handle) { return CS_OK; } err=votequorum_getinfo(v_handle, our_nodeid, &info); if ((err == CS_OK) || (err == CS_ERR_NOT_EXIST)) { printf("\nVotequorum information\n"); printf("----------------------\n"); printf("Expected votes: %d\n", info.node_expected_votes); printf("Highest expected: %d\n", info.highest_expected); printf("Total votes: %d\n", info.total_votes); printf("Quorum: %d %s\n", info.quorum, info.flags & VOTEQUORUM_INFO_QUORATE?" ":"Activity blocked"); printf("Flags: "); if (info.flags & VOTEQUORUM_INFO_TWONODE) printf("2Node "); if (info.flags & VOTEQUORUM_INFO_QUORATE) printf("Quorate "); if (info.flags & VOTEQUORUM_INFO_WAIT_FOR_ALL) printf("WaitForAll "); if (info.flags & VOTEQUORUM_INFO_LAST_MAN_STANDING) printf("LastManStanding "); if (info.flags & VOTEQUORUM_INFO_AUTO_TIE_BREAKER) printf("AutoTieBreaker "); if (info.flags & VOTEQUORUM_INFO_ALLOW_DOWNSCALE) printf("AllowDownscale "); if (info.flags & VOTEQUORUM_INFO_QDEVICE_REGISTERED) printf("Qdevice "); printf("\n"); } else { fprintf(stderr, "Unable to get node info: %s\n", cs_strerror(err)); } display_nodes_data(nodeid_format, name_format, sort_type); return err; }
static void display_nodes_data(nodeid_format_t nodeid_format, name_format_t name_format, sorttype_t sort_type) { int i, display_qdevice = 0; unsigned int our_flags = 0; struct votequorum_info info[g_view_list_entries]; /* * cache node info because we need to parse them twice */ if (v_handle) { for (i=0; i < g_view_list_entries; i++) { if (votequorum_getinfo(v_handle, g_view_list[i].node_id, &info[i]) != CS_OK) { printf("Unable to get node %u info\n", g_view_list[i].node_id); } g_view_list[i].vq_info = &info[i]; if (info[i].flags & VOTEQUORUM_INFO_QDEVICE_REGISTERED) { display_qdevice = 1; } } } /* * Get node names */ for (i=0; i < g_view_list_entries; i++) { g_view_list[i].name = strdup(node_name(g_view_list[i].node_id, name_format)); } printf("\nMembership information\n"); printf("----------------------\n"); print_string_padded("Nodeid"); if (v_handle) { print_string_padded("Votes"); if ((display_qdevice) || (machine_parsable)) { print_string_padded("Qdevice"); } } printf("Name\n"); /* corosync sends them already sorted by address */ if (sort_type == SORT_NODEID) { qsort(g_view_list, g_view_list_entries, sizeof(view_list_entry_t), compare_nodeids); } if (sort_type == SORT_NODENAME) { qsort(g_view_list, g_view_list_entries, sizeof(view_list_entry_t), compare_nodenames); } for (i=0; i < g_view_list_entries; i++) { if (nodeid_format == NODEID_FORMAT_DECIMAL) { print_uint32_padded(g_view_list[i].node_id); } else { printf("0x%08x ", g_view_list[i].node_id); } if (v_handle) { int votes = -1; votes = info[i].node_votes; print_uint32_padded(votes); if ((display_qdevice) || (machine_parsable)) { if (info[i].flags & VOTEQUORUM_INFO_QDEVICE_REGISTERED) { char buf[10]; snprintf(buf, sizeof(buf), "%s,%s,%s", info[i].flags & VOTEQUORUM_INFO_QDEVICE_ALIVE?"A":"NA", info[i].flags & VOTEQUORUM_INFO_QDEVICE_CAST_VOTE?"V":"NV", info[i].flags & VOTEQUORUM_INFO_QDEVICE_MASTER_WINS?"MW":"NMW"); print_string_padded(buf); } else { print_string_padded("NR"); } } } printf("%s", g_view_list[i].name); if (g_view_list[i].node_id == our_nodeid) { printf(" (local)"); our_flags = info[i].flags; } printf("\n"); } if (g_view_list_entries) { for (i=0; i < g_view_list_entries; i++) { free(g_view_list[i].name); } free(g_view_list); g_view_list = NULL; } if (display_qdevice) { if (nodeid_format == NODEID_FORMAT_DECIMAL) { print_uint32_padded(VOTEQUORUM_QDEVICE_NODEID); } else { printf("0x%08x ", VOTEQUORUM_QDEVICE_NODEID); } /* If the quorum device is inactive on this node then show votes as 0 so that the display is not confusing */ if (our_flags & VOTEQUORUM_INFO_QDEVICE_CAST_VOTE) { print_uint32_padded(info[0].qdevice_votes); } else { print_uint32_padded(0); } printf(" %s", info[0].qdevice_name); if (our_flags & VOTEQUORUM_INFO_QDEVICE_CAST_VOTE) { printf("\n"); } else { printf(" (votes %d)\n", info[0].qdevice_votes); } } }
static void show_status(void) { quorum_handle_t q_handle; votequorum_handle_t v_handle; votequorum_callbacks_t v_callbacks; quorum_callbacks_t callbacks; struct votequorum_info info; int is_quorate; int err; callbacks.quorum_notify_fn = quorum_notification_fn; err=quorum_initialize(&q_handle, &callbacks); if (err != CS_OK) { fprintf(stderr, "Cannot connect to quorum service, is it loaded?\n"); return; } err=quorum_getquorate(q_handle, &is_quorate); if (err != CS_OK) { fprintf(stderr, "quorum_getquorate FAILED: %d\n", err); return; } err=quorum_trackstart(q_handle, CS_TRACK_CURRENT); if (err != CS_OK) { fprintf(stderr, "quorum_trackstart FAILED: %d\n", err); return; } g_called = 0; while (g_called == 0) quorum_dispatch(q_handle, CS_DISPATCH_ONE); quorum_finalize(q_handle); printf("Version: %s\n", VERSION); printf("Nodes: %d\n", g_view_list_entries); printf("Ring ID: %" PRIu64 "\n", g_ring_id); printf("Quorum type: %s\n", get_quorum_type()); printf("Quorate: %s\n", is_quorate?"Yes":"No"); if (using_votequorum()) { v_callbacks.votequorum_notify_fn = NULL; v_callbacks.votequorum_expectedvotes_notify_fn = NULL; if ( (err=votequorum_initialize(&v_handle, &v_callbacks)) != CS_OK) { fprintf(stderr, "votequorum_initialize FAILED: %d, this is probably a configuration error\n", err); goto err_exit; } if ( (err=votequorum_getinfo(v_handle, 0, &info)) != CS_OK) fprintf(stderr, "votequorum_getinfo FAILED: %d\n", err); else { printf("Node votes: %d\n", info.node_votes); printf("Expected votes: %d\n", info.node_expected_votes); printf("Highest expected: %d\n", info.highest_expected); printf("Total votes: %d\n", info.total_votes); printf("Quorum: %d %s\n", info.quorum, info.flags & VOTEQUORUM_INFO_FLAG_QUORATE?" ":"Activity blocked"); printf("Flags: "); if (info.flags & VOTEQUORUM_INFO_FLAG_HASSTATE) printf("HasState "); if (info.flags & VOTEQUORUM_INFO_FLAG_DISALLOWED) printf("DisallowedNodes "); if (info.flags & VOTEQUORUM_INFO_FLAG_TWONODE) printf("2Node "); if (info.flags & VOTEQUORUM_INFO_FLAG_QUORATE) printf("Quorate "); printf("\n"); } } err_exit: return; }
int main(int argc, char *argv[]) { struct votequorum_info info; votequorum_callbacks_t callbacks; int err; if (argc > 1 && strcmp(argv[1], "-h")==0) { fprintf(stderr, "usage: %s [new-expected] [new-votes]\n", argv[0]); return 0; } callbacks.votequorum_notify_fn = votequorum_notification_fn; callbacks.votequorum_expectedvotes_notify_fn = votequorum_expectedvotes_notification_fn; if ( (err=votequorum_initialize(&g_handle, &callbacks)) != CS_OK) fprintf(stderr, "votequorum_initialize FAILED: %d\n", err); if ( (err = votequorum_trackstart(g_handle, g_handle, CS_TRACK_CHANGES)) != CS_OK) fprintf(stderr, "votequorum_trackstart FAILED: %d\n", err); if ( (err=votequorum_getinfo(g_handle, 0, &info)) != CS_OK) fprintf(stderr, "votequorum_getinfo FAILED: %d\n", err); else { printf("node votes %d\n", info.node_votes); printf("expected votes %d\n", info.node_expected_votes); printf("highest expected %d\n", info.highest_expected); printf("total votes %d\n", info.total_votes); printf("quorum %d\n", info.quorum); printf("flags "); if (info.flags & VOTEQUORUM_INFO_FLAG_TWONODE) printf("2Node "); if (info.flags & VOTEQUORUM_INFO_FLAG_QUORATE) printf("Quorate "); if (info.flags & VOTEQUORUM_INFO_WAIT_FOR_ALL) printf("WaitForAll "); if (info.flags & VOTEQUORUM_INFO_LAST_MAN_STANDING) printf("LastManStanding "); if (info.flags & VOTEQUORUM_INFO_AUTO_TIE_BREAKER) printf("AutoTieBreaker "); if (info.flags & VOTEQUORUM_INFO_LEAVE_REMOVE) printf("LeaveRemove "); printf("\n"); } if (argc >= 2 && atoi(argv[1])) { if ( (err=votequorum_setexpected(g_handle, atoi(argv[1]))) != CS_OK) fprintf(stderr, "set expected votes FAILED: %d\n", err); } if (argc >= 3 && atoi(argv[2])) { if ( (err=votequorum_setvotes(g_handle, 0, atoi(argv[2]))) != CS_OK) fprintf(stderr, "set votes FAILED: %d\n", err); } if (argc >= 2) { if ( (err=votequorum_getinfo(g_handle, 0, &info)) != CS_OK) fprintf(stderr, "votequorum_getinfo2 FAILED: %d\n", err); else { printf("-------------------\n"); printf("node votes %d\n", info.node_votes); printf("expected votes %d\n", info.node_expected_votes); printf("highest expected %d\n", info.highest_expected); printf("total votes %d\n", info.total_votes); printf("votequorum %d\n", info.quorum); printf("flags "); if (info.flags & VOTEQUORUM_INFO_FLAG_TWONODE) printf("2Node "); if (info.flags & VOTEQUORUM_INFO_FLAG_QUORATE) printf("Quorate "); if (info.flags & VOTEQUORUM_INFO_WAIT_FOR_ALL) printf("WaitForAll "); if (info.flags & VOTEQUORUM_INFO_LAST_MAN_STANDING) printf("LastManStanding "); if (info.flags & VOTEQUORUM_INFO_AUTO_TIE_BREAKER) printf("AutoTieBreaker "); if (info.flags & VOTEQUORUM_INFO_LEAVE_REMOVE) printf("LeaveRemove "); printf("\n"); } } printf("Waiting for votequorum events, press ^C to finish\n"); printf("-------------------\n"); while (1) { if (votequorum_dispatch(g_handle, CS_DISPATCH_ALL) != CS_OK) { fprintf(stderr, "votequorum_dispatch error\n"); return -1; } } return 0; }