| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630 |
- /*
- * Copyright (c) 2009-2012 Red Hat, Inc.
- *
- * All rights reserved.
- *
- * Authors: Christine Caulfield <ccaulfie@redhat.com>
- * Fabio M. Di Nitto (fdinitto@redhat.com)
- *
- * This software licensed under BSD license, the text of which follows:
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * - Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- * - Neither the name of the Red Hat Inc. nor the names of its
- * contributors may be used to endorse or promote products derived from this
- * software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
- * THE POSSIBILITY OF SUCH DAMAGE.
- */
- #include <config.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <unistd.h>
- #include <string.h>
- #include <pthread.h>
- #include <inttypes.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <sys/select.h>
- #include <sys/un.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <netdb.h>
- #include <corosync/corotypes.h>
- #include <corosync/totem/totem.h>
- #include <corosync/cfg.h>
- #include <corosync/cmap.h>
- #include <corosync/quorum.h>
- #include <corosync/votequorum.h>
- typedef enum {
- NODEID_FORMAT_DECIMAL,
- NODEID_FORMAT_HEX
- } nodeid_format_t;
- typedef enum {
- ADDRESS_FORMAT_NAME,
- ADDRESS_FORMAT_IP
- } name_format_t;
- typedef enum {
- CMD_UNKNOWN,
- CMD_SHOWNODES,
- CMD_SHOWSTATUS,
- CMD_SETVOTES,
- CMD_SETEXPECTED,
- CMD_MONITOR
- } command_t;
- /*
- * global vars
- */
- /*
- * cmap bits
- */
- static cmap_handle_t cmap_handle;
- /*
- * quorum bits
- */
- static void quorum_notification_fn(
- quorum_handle_t handle,
- uint32_t quorate,
- uint64_t ring_id,
- uint32_t view_list_entries,
- uint32_t *view_list);
- static quorum_handle_t q_handle;
- static uint32_t q_type;
- static quorum_callbacks_t q_callbacks = {
- .quorum_notify_fn = quorum_notification_fn
- };
- /*
- * quorum call back vars
- */
- static uint32_t g_quorate;
- static uint64_t g_ring_id;
- static uint32_t g_view_list_entries;
- static uint32_t *g_view_list = NULL;
- static uint32_t g_called;
- /*
- * votequorum bits
- */
- static votequorum_handle_t v_handle;
- static votequorum_callbacks_t v_callbacks = {
- .votequorum_notify_fn = NULL,
- .votequorum_expectedvotes_notify_fn = NULL
- };
- /*
- * cfg bits
- */
- static corosync_cfg_handle_t c_handle;
- static corosync_cfg_callbacks_t c_callbacks = {
- .corosync_cfg_state_track_callback = NULL,
- .corosync_cfg_shutdown_callback = NULL
- };
- static void show_usage(const char *name)
- {
- printf("usage: \n");
- printf("%s <options>\n", name);
- printf("\n");
- printf(" options:\n");
- printf("\n");
- printf(" -s show quorum status\n");
- printf(" -m monitor quorum status\n");
- printf(" -l list nodes\n");
- printf(" -v <votes> change the number of votes for a node *\n");
- printf(" -n <nodeid> optional nodeid of node for -v\n");
- printf(" -e <expected> change expected votes for the cluster *\n");
- printf(" -H show nodeids in hexadecimal rather than decimal\n");
- printf(" -i show node IP addresses instead of the resolved name\n");
- printf(" -h show this help text\n");
- printf("\n");
- printf(" * Starred items only work if votequorum is the quorum provider for corosync\n");
- printf("\n");
- }
- static int get_quorum_type(char *quorum_type, size_t quorum_type_len)
- {
- int err;
- char *str;
- if ((!quorum_type) || (quorum_type_len <= 0)) {
- errno = EINVAL;
- return -1;
- }
- if (q_type == QUORUM_FREE) {
- return -1;
- }
- if ((err = cmap_get_string(cmap_handle, "quorum.provider", &str)) != CS_OK) {
- goto out;
- }
- strncpy(quorum_type, str, quorum_type_len - 1);
- free(str);
- return 0;
- out:
- return err;
- }
- /*
- * Returns 1 if 'votequorum' is active. The called then knows that
- * votequorum calls should work and can provide extra information
- */
- static int using_votequorum(void)
- {
- char quorumtype[256];
- int using_voteq;
- if (get_quorum_type(quorumtype, sizeof(quorumtype))) {
- return -1;
- }
- if (strcmp(quorumtype, "corosync_votequorum") == 0) {
- using_voteq = 1;
- } else {
- using_voteq = 0;
- }
- return using_voteq;
- }
- static int set_votes(uint32_t nodeid, int votes)
- {
- int err;
- if ((err=votequorum_setvotes(v_handle, nodeid, votes)) != CS_OK) {
- fprintf(stderr, "set votes FAILED: %d\n", err);
- }
- return err==CS_OK?0:err;
- }
- static int set_expected(int expected_votes)
- {
- int err;
- if ((err=votequorum_setexpected(v_handle, expected_votes)) != CS_OK) {
- fprintf(stderr, "set expected votes FAILED: %d\n", err);
- }
- return err==CS_OK?0:err;
- }
- static int get_votes(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;
- }
- /*
- * This resolves the first address assigned to a node
- * and returns the name or IP address. Use cfgtool if you need more information.
- */
- static const char *node_name(uint32_t nodeid, name_format_t name_format)
- {
- int err;
- int numaddrs;
- corosync_cfg_node_address_t addrs[INTERFACE_MAX];
- static char buf[INET6_ADDRSTRLEN];
- socklen_t addrlen;
- struct sockaddr_storage *ss;
- err = corosync_cfg_get_node_addrs(c_handle, nodeid, INTERFACE_MAX, &numaddrs, addrs);
- if (err != CS_OK) {
- fprintf(stderr, "Unable to get node address for nodeid %u: %d\n", nodeid, err);
- return "";
- }
- ss = (struct sockaddr_storage *)addrs[0].address;
- if (ss->ss_family == AF_INET6) {
- addrlen = sizeof(struct sockaddr_in6);
- } else {
- addrlen = sizeof(struct sockaddr_in);
- }
- if (!getnameinfo(
- (struct sockaddr *)addrs[0].address, addrlen,
- buf, sizeof(buf),
- NULL, 0,
- (name_format == ADDRESS_FORMAT_IP)?NI_NUMERICHOST:0)) {
- return buf;
- }
- return "";
- }
- static void quorum_notification_fn(
- quorum_handle_t handle,
- uint32_t quorate,
- uint64_t ring_id,
- uint32_t view_list_entries,
- uint32_t *view_list)
- {
- g_called = 1;
- g_quorate = quorate;
- g_ring_id = ring_id;
- g_view_list_entries = view_list_entries;
- if (g_view_list) {
- free(g_view_list);
- }
- g_view_list = malloc(sizeof(uint32_t) * view_list_entries);
- if (g_view_list) {
- memcpy(g_view_list, view_list,sizeof(uint32_t) * view_list_entries);
- }
- }
- static int display_quorum_data(int is_quorate, int loop)
- {
- struct votequorum_info info;
- int err;
- char quorum_type[256];
- if (!loop) {
- printf("Version: %s\n", VERSION);
- if (get_quorum_type(quorum_type, sizeof(quorum_type))) {
- strncpy(quorum_type, "Not configured", sizeof(quorum_type) - 1);
- }
- printf("Quorum type: %s\n", quorum_type);
- }
- printf("Nodes: %d\n", g_view_list_entries);
- printf("Ring ID: %" PRIu64 "\n", g_ring_id);
- printf("Quorate: %s\n", is_quorate?"Yes":"No");
- if (!v_handle) {
- return CS_OK;
- }
- if ((err=votequorum_getinfo(v_handle, 0, &info)) == CS_OK) {
- 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_TWONODE) printf("2Node ");
- if (info.flags & VOTEQUORUM_INFO_FLAG_QUORATE) printf("Quorate ");
- printf("\n");
- } else {
- fprintf(stderr, "votequorum_getinfo FAILED: %d\n", err);
- }
- return err;
- }
- /*
- * return 1 if quorate
- * 0 if not quorate
- * -1 on error
- */
- static int show_status(void)
- {
- int is_quorate;
- int err;
- err=quorum_getquorate(q_handle, &is_quorate);
- if (err != CS_OK) {
- fprintf(stderr, "quorum_getquorate FAILED: %d\n", err);
- goto quorum_err;
- }
- err=quorum_trackstart(q_handle, CS_TRACK_CURRENT);
- if (err != CS_OK) {
- fprintf(stderr, "quorum_trackstart FAILED: %d\n", err);
- goto quorum_err;
- }
- g_called = 0;
- while (g_called == 0 && err == CS_OK) {
- err = quorum_dispatch(q_handle, CS_DISPATCH_ONE);
- if (err != CS_OK) {
- fprintf(stderr, "quorum_dispatch FAILED: %d\n", err);
- }
- }
- if (quorum_trackstop(q_handle) != CS_OK) {
- fprintf(stderr, "quorum_trackstop FAILED: %d\n", err);
- }
- quorum_err:
- if (err < 0) {
- return err;
- }
- err = display_quorum_data(is_quorate, 0);
- if (err != CS_OK) {
- return err;
- }
- return is_quorate;
- }
- static int monitor_status(nodeid_format_t nodeid_format, name_format_t name_format) {
- int err;
- int loop = 0;
- if (q_type == QUORUM_FREE) {
- show_status();
- printf("\nQuorum is not configured - cannot monitor\n");
- return 0;
- }
- err=quorum_trackstart(q_handle, CS_TRACK_CHANGES);
- if (err != CS_OK) {
- fprintf(stderr, "quorum_trackstart FAILED: %d\n", err);
- goto quorum_err;
- }
- while (1) {
- time_t t;
- err = quorum_dispatch(q_handle, CS_DISPATCH_ONE);
- if (err != CS_OK) {
- fprintf(stderr, "quorum_dispatch FAILED: %d\n", err);
- goto quorum_err;
- }
- time(&t);
- printf("date: %s", ctime((const time_t *)&t));
- err = display_quorum_data(g_quorate, loop);
- printf("\n");
- loop = 1;
- if (err != CS_OK) {
- fprintf(stderr, "display_quorum_data FAILED: %d\n", err);
- goto quorum_err;
- }
- }
- quorum_err:
- return err;
- }
- static int show_nodes(nodeid_format_t nodeid_format, name_format_t name_format)
- {
- int i;
- int err;
- int result = EXIT_FAILURE;
- err = quorum_trackstart(q_handle, CS_TRACK_CURRENT);
- if (err != CS_OK) {
- fprintf(stderr, "quorum_trackstart FAILED: %d\n", err);
- goto err_exit;
- }
- g_called = 0;
- while (g_called == 0) {
- quorum_dispatch(q_handle, CS_DISPATCH_ONE);
- }
- quorum_finalize(q_handle);
- q_handle = 0;
- err = corosync_cfg_initialize(&c_handle, &c_callbacks);
- if (err != CS_OK) {
- fprintf(stderr, "Cannot initialise CFG service\n");
- c_handle = 0;
- goto err_exit;
- }
- if (v_handle) {
- printf("Nodeid Votes Name\n");
- } else {
- printf("Nodeid Name\n");
- }
- for (i=0; i < g_view_list_entries; i++) {
- if (nodeid_format == NODEID_FORMAT_DECIMAL) {
- printf("%4u ", g_view_list[i]);
- } else {
- printf("0x%04x ", g_view_list[i]);
- }
- if (v_handle) {
- printf("%3d %s\n", get_votes(g_view_list[i]), node_name(g_view_list[i], name_format));
- } else {
- printf("%s\n", node_name(g_view_list[i], name_format));
- }
- }
- result = EXIT_SUCCESS;
- err_exit:
- return result;
- }
- /*
- * return -1 on error
- * 0 if OK
- */
- static int init_all(void) {
- cmap_handle = 0;
- q_handle = 0;
- v_handle = 0;
- c_handle = 0;
- if (cmap_initialize(&cmap_handle) != CS_OK) {
- fprintf(stderr, "Cannot initialize CMAP service\n");
- cmap_handle = 0;
- goto out;
- }
- if (quorum_initialize(&q_handle, &q_callbacks, &q_type) != CS_OK) {
- fprintf(stderr, "Cannot initialize QUORUM service\n");
- q_handle = 0;
- goto out;
- }
- if (corosync_cfg_initialize(&c_handle, &c_callbacks) != CS_OK) {
- fprintf(stderr, "Cannot initialise CFG service\n");
- c_handle = 0;
- goto out;
- }
- if (using_votequorum() <= 0) {
- return 0;
- }
- if (votequorum_initialize(&v_handle, &v_callbacks) != CS_OK) {
- fprintf(stderr, "Cannot initialise VOTEQUORUM service\n");
- v_handle = 0;
- goto out;
- }
- return 0;
- out:
- return -1;
- }
- static void close_all(void) {
- if (cmap_handle) {
- cmap_finalize(cmap_handle);
- }
- if (q_handle) {
- quorum_finalize(q_handle);
- }
- if (c_handle) {
- corosync_cfg_finalize(c_handle);
- }
- if (v_handle) {
- votequorum_finalize(v_handle);
- }
- }
- int main (int argc, char *argv[]) {
- const char *options = "VHslme:v:hin:d:";
- char *endptr;
- int opt;
- int votes = 0;
- int ret = 0;
- uint32_t nodeid = VOTEQUORUM_NODEID_US;
- nodeid_format_t nodeid_format = NODEID_FORMAT_DECIMAL;
- name_format_t address_format = ADDRESS_FORMAT_NAME;
- command_t command_opt = CMD_UNKNOWN;
- if (argc == 1) {
- show_usage (argv[0]);
- exit(0);
- }
- if (init_all()) {
- close_all();
- exit(1);
- }
- while ( (opt = getopt(argc, argv, options)) != -1 ) {
- switch (opt) {
- case 's':
- command_opt = CMD_SHOWSTATUS;
- break;
- case 'm':
- command_opt = CMD_MONITOR;
- break;
- case 'i':
- address_format = ADDRESS_FORMAT_IP;
- break;
- case 'H':
- nodeid_format = NODEID_FORMAT_HEX;
- break;
- case 'l':
- command_opt = CMD_SHOWNODES;
- break;
- case 'e':
- if (using_votequorum() > 0) {
- votes = strtol(optarg, &endptr, 0);
- if ((votes == 0 && endptr == optarg) || votes <= 0) {
- fprintf(stderr, "New expected votes value was not valid, try a positive number\n");
- } else {
- command_opt = CMD_SETEXPECTED;
- }
- } else {
- fprintf(stderr, "You cannot change expected votes, corosync is not using votequorum\n");
- exit(2);
- }
- break;
- case 'n':
- nodeid = strtol(optarg, &endptr, 0);
- if ((nodeid == 0 && endptr == optarg) || nodeid <= 0) {
- fprintf(stderr, "The nodeid was not valid, try a positive number\n");
- }
- break;
- case 'v':
- if (using_votequorum() > 0) {
- votes = strtol(optarg, &endptr, 0);
- if ((votes == 0 && endptr == optarg) || votes < 0) {
- fprintf(stderr, "New votes value was not valid, try a positive number or zero\n");
- } else {
- command_opt = CMD_SETVOTES;
- }
- }
- else {
- fprintf(stderr, "You cannot change node votes, corosync is not using votequorum\n");
- exit(2);
- }
- break;
- case 'h':
- case '?':
- default:
- break;
- }
- }
- switch (command_opt) {
- case CMD_UNKNOWN:
- show_usage(argv[0]);
- ret = -1;
- break;
- case CMD_SHOWNODES:
- ret = show_nodes(nodeid_format, address_format);
- break;
- case CMD_SHOWSTATUS:
- ret = show_status();
- break;
- case CMD_SETVOTES:
- ret = set_votes(nodeid, votes);
- break;
- case CMD_SETEXPECTED:
- ret = set_expected(votes);
- break;
- case CMD_MONITOR:
- ret = monitor_status(nodeid_format, address_format);
- break;
- }
- close_all();
- return (ret);
- }
|