corosync-quorumtool.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. /*
  2. * Copyright (c) 2009-2011 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Christine Caulfield <ccaulfie@redhat.com>
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the Red Hat Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <config.h>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <errno.h>
  38. #include <unistd.h>
  39. #include <string.h>
  40. #include <pthread.h>
  41. #include <inttypes.h>
  42. #include <sys/types.h>
  43. #include <sys/socket.h>
  44. #include <sys/select.h>
  45. #include <sys/un.h>
  46. #include <netinet/in.h>
  47. #include <arpa/inet.h>
  48. #include <netdb.h>
  49. #include <corosync/corotypes.h>
  50. #include <corosync/totem/totem.h>
  51. #include <corosync/cfg.h>
  52. #include <corosync/confdb.h>
  53. #include <corosync/quorum.h>
  54. #include <corosync/votequorum.h>
  55. typedef enum { NODEID_FORMAT_DECIMAL, NODEID_FORMAT_HEX } nodeid_format_t;
  56. typedef enum { ADDRESS_FORMAT_NAME, ADDRESS_FORMAT_IP } name_format_t;
  57. typedef enum { CMD_UNKNOWN, CMD_SHOWNODES, CMD_SHOWSTATUS, CMD_SETVOTES, CMD_SETEXPECTED } command_t;
  58. static void show_usage(const char *name)
  59. {
  60. printf("usage: \n");
  61. printf("%s <options>\n", name);
  62. printf("\n");
  63. printf(" options:\n");
  64. printf("\n");
  65. printf(" -s show quorum status\n");
  66. printf(" -l list nodes\n");
  67. printf(" -v <votes> change the number of votes for a node *\n");
  68. printf(" -n <nodeid> optional nodeid of node for -v\n");
  69. printf(" -e <expected> change expected votes for the cluster *\n");
  70. printf(" -H show nodeids in hexadecimal rather than decimal\n");
  71. printf(" -i show node IP addresses instead of the resolved name\n");
  72. printf(" -h show this help text\n");
  73. printf("\n");
  74. printf(" * Starred items only work if votequorum is the quorum provider for corosync\n");
  75. printf("\n");
  76. }
  77. /*
  78. * Caller should free the returned string
  79. */
  80. static const char *get_quorum_type(void)
  81. {
  82. const char *qtype = NULL;
  83. int result;
  84. char buf[255];
  85. size_t namelen = sizeof(buf);
  86. hdb_handle_t quorum_handle;
  87. confdb_handle_t confdb_handle;
  88. confdb_callbacks_t callbacks = {
  89. .confdb_key_change_notify_fn = NULL,
  90. .confdb_object_create_change_notify_fn = NULL,
  91. .confdb_object_delete_change_notify_fn = NULL
  92. };
  93. if (confdb_initialize(&confdb_handle, &callbacks) != CS_OK) {
  94. errno = EINVAL;
  95. return NULL;
  96. }
  97. result = confdb_object_find_start(confdb_handle, OBJECT_PARENT_HANDLE);
  98. if (result != CS_OK)
  99. goto out;
  100. result = confdb_object_find(confdb_handle, OBJECT_PARENT_HANDLE, (void *)"quorum", strlen("quorum"), &quorum_handle);
  101. if (result != CS_OK)
  102. goto out;
  103. result = confdb_key_get(confdb_handle, quorum_handle, (void *)"provider", strlen("provider"), buf, &namelen);
  104. if (result != CS_OK)
  105. goto out;
  106. if (namelen >= sizeof(buf)) {
  107. namelen = sizeof(buf) - 1;
  108. }
  109. buf[namelen] = '\0';
  110. /* If strdup returns NULL then we just assume no quorum provider ?? */
  111. qtype = strdup(buf);
  112. out:
  113. confdb_finalize(confdb_handle);
  114. return qtype;
  115. }
  116. /*
  117. * Returns 1 if 'votequorum' is active. The called then knows that
  118. * votequorum calls should work and can provide extra information
  119. */
  120. static int using_votequorum(void)
  121. {
  122. const char *quorumtype = get_quorum_type();
  123. int using_voteq;
  124. if (!quorumtype)
  125. return 0;
  126. if (strcmp(quorumtype, "corosync_votequorum") == 0)
  127. using_voteq = 1;
  128. else
  129. using_voteq = 0;
  130. free((void *)quorumtype);
  131. return using_voteq;
  132. }
  133. static int set_votes(uint32_t nodeid, int votes)
  134. {
  135. votequorum_handle_t v_handle;
  136. votequorum_callbacks_t v_callbacks;
  137. int err;
  138. v_callbacks.votequorum_notify_fn = NULL;
  139. v_callbacks.votequorum_expectedvotes_notify_fn = NULL;
  140. if ( (err=votequorum_initialize(&v_handle, &v_callbacks)) != CS_OK) {
  141. fprintf(stderr, "votequorum_initialize FAILED: %d, this is probably a configuration error\n", err);
  142. return err;
  143. }
  144. if ( (err=votequorum_setvotes(v_handle, nodeid, votes)) != CS_OK)
  145. fprintf(stderr, "set votes FAILED: %d\n", err);
  146. votequorum_finalize(v_handle);
  147. return err==CS_OK?0:err;
  148. }
  149. static int set_expected(int expected_votes)
  150. {
  151. votequorum_handle_t v_handle;
  152. votequorum_callbacks_t v_callbacks;
  153. int err;
  154. v_callbacks.votequorum_notify_fn = NULL;
  155. v_callbacks.votequorum_expectedvotes_notify_fn = NULL;
  156. if ( (err=votequorum_initialize(&v_handle, &v_callbacks)) != CS_OK) {
  157. fprintf(stderr, "votequorum_initialize FAILED: %d, this is probably a configuration error\n", err);
  158. return err;
  159. }
  160. if ( (err=votequorum_setexpected(v_handle, expected_votes)) != CS_OK)
  161. fprintf(stderr, "set expected votes FAILED: %d\n", err);
  162. votequorum_finalize(v_handle);
  163. return err==CS_OK?0:err;
  164. }
  165. static int get_votes(votequorum_handle_t v_handle, uint32_t nodeid)
  166. {
  167. int votes = -1;
  168. struct votequorum_info info;
  169. if (votequorum_getinfo(v_handle, nodeid, &info) == CS_OK)
  170. votes = info.node_votes;
  171. return votes;
  172. }
  173. /*
  174. * This resolves the first address assigned to a node
  175. * and returns the name or IP address. Use cfgtool if you need more information.
  176. */
  177. static const char *node_name(corosync_cfg_handle_t c_handle, uint32_t nodeid, name_format_t name_format)
  178. {
  179. int ret;
  180. int numaddrs;
  181. corosync_cfg_node_address_t addrs[INTERFACE_MAX];
  182. if (corosync_cfg_get_node_addrs(c_handle, nodeid, INTERFACE_MAX, &numaddrs, addrs) == CS_OK) {
  183. static char buf[INET6_ADDRSTRLEN];
  184. socklen_t addrlen;
  185. struct sockaddr_storage *ss = (struct sockaddr_storage *)addrs[0].address;
  186. if (ss->ss_family == AF_INET6)
  187. addrlen = sizeof(struct sockaddr_in6);
  188. else
  189. addrlen = sizeof(struct sockaddr_in);
  190. ret = getnameinfo((struct sockaddr *)addrs[0].address, addrlen,
  191. buf, sizeof(buf),
  192. NULL, 0,
  193. (name_format == ADDRESS_FORMAT_IP)?NI_NUMERICHOST:0);
  194. if (!ret)
  195. return buf;
  196. }
  197. return "";
  198. }
  199. /*
  200. * Static variables to pass back to calling routine
  201. */
  202. static uint32_t g_quorate;
  203. static uint64_t g_ring_id;
  204. static uint32_t g_view_list_entries;
  205. static uint32_t *g_view_list;
  206. static uint32_t g_called;
  207. static void quorum_notification_fn(
  208. quorum_handle_t handle,
  209. uint32_t quorate,
  210. uint64_t ring_id,
  211. uint32_t view_list_entries,
  212. uint32_t *view_list)
  213. {
  214. g_called = 1;
  215. g_quorate = quorate;
  216. g_ring_id = ring_id;
  217. g_view_list_entries = view_list_entries;
  218. g_view_list = malloc(sizeof(uint32_t) * view_list_entries);
  219. if (g_view_list) {
  220. memcpy(g_view_list, view_list,sizeof(uint32_t) * view_list_entries);
  221. }
  222. }
  223. /*
  224. * return 1 if quorate
  225. * 0 if not quorate
  226. * -1 on error
  227. */
  228. static int show_status(void)
  229. {
  230. quorum_handle_t q_handle;
  231. votequorum_handle_t v_handle;
  232. votequorum_callbacks_t v_callbacks;
  233. quorum_callbacks_t callbacks;
  234. struct votequorum_info info;
  235. int is_quorate;
  236. int err;
  237. callbacks.quorum_notify_fn = quorum_notification_fn;
  238. err=quorum_initialize(&q_handle, &callbacks);
  239. if (err != CS_OK) {
  240. fprintf(stderr, "Cannot connect to quorum service, is it loaded?\n");
  241. return -1;
  242. }
  243. err=quorum_getquorate(q_handle, &is_quorate);
  244. if (err != CS_OK) {
  245. fprintf(stderr, "quorum_getquorate FAILED: %d\n", err);
  246. goto quorum_err;
  247. }
  248. err=quorum_trackstart(q_handle, CS_TRACK_CURRENT);
  249. if (err != CS_OK) {
  250. fprintf(stderr, "quorum_trackstart FAILED: %d\n", err);
  251. goto quorum_err;
  252. }
  253. g_called = 0;
  254. while (g_called == 0 && err == CS_OK) {
  255. err = quorum_dispatch(q_handle, CS_DISPATCH_ONE);
  256. if (err != CS_OK) {
  257. fprintf(stderr, "quorum_dispatch FAILED: %d\n", err);
  258. }
  259. }
  260. if (quorum_trackstop(q_handle) != CS_OK) {
  261. fprintf(stderr, "quorum_trackstop FAILED: %d\n", err);
  262. }
  263. quorum_err:
  264. if (quorum_finalize(q_handle) != CS_OK) {
  265. fprintf(stderr, "quorum_finalize FAILED: %d\n", err);
  266. }
  267. if (err < 0)
  268. return err;
  269. printf("Version: %s\n", VERSION);
  270. printf("Nodes: %d\n", g_view_list_entries);
  271. printf("Ring ID: %" PRIu64 "\n", g_ring_id);
  272. printf("Quorum type: %s\n", get_quorum_type());
  273. printf("Quorate: %s\n", is_quorate?"Yes":"No");
  274. if (!using_votequorum()) {
  275. return is_quorate;
  276. }
  277. v_callbacks.votequorum_notify_fn = NULL;
  278. v_callbacks.votequorum_expectedvotes_notify_fn = NULL;
  279. if ((err=votequorum_initialize(&v_handle, &v_callbacks)) != CS_OK) {
  280. fprintf(stderr, "votequorum_initialize FAILED: %d, this is probably a configuration error\n", err);
  281. goto err_exit;
  282. }
  283. if ((err=votequorum_getinfo(v_handle, 0, &info)) == CS_OK) {
  284. printf("Node votes: %d\n", info.node_votes);
  285. printf("Expected votes: %d\n", info.node_expected_votes);
  286. printf("Highest expected: %d\n", info.highest_expected);
  287. printf("Total votes: %d\n", info.total_votes);
  288. printf("Quorum: %d %s\n", info.quorum, info.flags & VOTEQUORUM_INFO_FLAG_QUORATE?" ":"Activity blocked");
  289. printf("Flags: ");
  290. if (info.flags & VOTEQUORUM_INFO_FLAG_HASSTATE) printf("HasState ");
  291. if (info.flags & VOTEQUORUM_INFO_FLAG_DISALLOWED) printf("DisallowedNodes ");
  292. if (info.flags & VOTEQUORUM_INFO_FLAG_TWONODE) printf("2Node ");
  293. if (info.flags & VOTEQUORUM_INFO_FLAG_QUORATE) printf("Quorate ");
  294. printf("\n");
  295. } else {
  296. fprintf(stderr, "votequorum_getinfo FAILED: %d\n", err);
  297. }
  298. if (votequorum_finalize(v_handle) != CS_OK) {
  299. fprintf(stderr, "votequorum_finalize FAILED: %d\n", err);
  300. }
  301. err_exit:
  302. if (err != CS_OK)
  303. return err;
  304. return is_quorate;
  305. }
  306. static int show_nodes(nodeid_format_t nodeid_format, name_format_t name_format)
  307. {
  308. quorum_handle_t q_handle = 0;
  309. votequorum_handle_t v_handle = 0;
  310. corosync_cfg_handle_t c_handle = 0;
  311. corosync_cfg_callbacks_t c_callbacks;
  312. int i;
  313. int using_vq = 0;
  314. quorum_callbacks_t q_callbacks;
  315. votequorum_callbacks_t v_callbacks;
  316. int err;
  317. int result = EXIT_FAILURE;
  318. q_callbacks.quorum_notify_fn = quorum_notification_fn;
  319. err=quorum_initialize(&q_handle, &q_callbacks);
  320. if (err != CS_OK) {
  321. fprintf(stderr, "Cannot connect to quorum service, is it loaded?\n");
  322. return result;
  323. }
  324. v_callbacks.votequorum_notify_fn = NULL;
  325. v_callbacks.votequorum_expectedvotes_notify_fn = NULL;
  326. using_vq = using_votequorum();
  327. if (using_vq) {
  328. if ( (err=votequorum_initialize(&v_handle, &v_callbacks)) != CS_OK) {
  329. fprintf(stderr, "votequorum_initialize FAILED: %d, this is probably a configuration error\n", err);
  330. v_handle = 0;
  331. goto err_exit;
  332. }
  333. }
  334. err = quorum_trackstart(q_handle, CS_TRACK_CURRENT);
  335. if (err != CS_OK) {
  336. fprintf(stderr, "quorum_trackstart FAILED: %d\n", err);
  337. goto err_exit;
  338. }
  339. g_called = 0;
  340. while (g_called == 0)
  341. quorum_dispatch(q_handle, CS_DISPATCH_ONE);
  342. quorum_finalize(q_handle);
  343. q_handle = 0;
  344. err = corosync_cfg_initialize(&c_handle, &c_callbacks);
  345. if (err != CS_OK) {
  346. fprintf(stderr, "Cannot initialise CFG service\n");
  347. c_handle = 0;
  348. goto err_exit;
  349. }
  350. if (using_vq)
  351. printf("Nodeid Votes Name\n");
  352. else
  353. printf("Nodeid Name\n");
  354. for (i=0; i < g_view_list_entries; i++) {
  355. if (nodeid_format == NODEID_FORMAT_DECIMAL) {
  356. printf("%4u ", g_view_list[i]);
  357. }
  358. else {
  359. printf("0x%04x ", g_view_list[i]);
  360. }
  361. if (using_vq) {
  362. printf("%3d %s\n", get_votes(v_handle, g_view_list[i]), node_name(c_handle, g_view_list[i], name_format));
  363. }
  364. else {
  365. printf("%s\n", node_name(c_handle, g_view_list[i], name_format));
  366. }
  367. }
  368. result = EXIT_SUCCESS;
  369. err_exit:
  370. if (q_handle != 0) {
  371. quorum_finalize (q_handle);
  372. }
  373. if (using_vq && v_handle != 0) {
  374. votequorum_finalize (v_handle);
  375. }
  376. if (c_handle != 0) {
  377. corosync_cfg_finalize (c_handle);
  378. }
  379. return result;
  380. }
  381. int main (int argc, char *argv[]) {
  382. const char *options = "VHsle:v:hin:d:";
  383. char *endptr;
  384. int opt;
  385. int votes = 0;
  386. int ret = 0;
  387. uint32_t nodeid = VOTEQUORUM_NODEID_US;
  388. nodeid_format_t nodeid_format = NODEID_FORMAT_DECIMAL;
  389. name_format_t address_format = ADDRESS_FORMAT_NAME;
  390. command_t command_opt = CMD_UNKNOWN;
  391. if (argc == 1) {
  392. show_usage (argv[0]);
  393. exit(0);
  394. }
  395. while ( (opt = getopt(argc, argv, options)) != -1 ) {
  396. switch (opt) {
  397. case 's':
  398. command_opt = CMD_SHOWSTATUS;
  399. break;
  400. case 'i':
  401. address_format = ADDRESS_FORMAT_IP;
  402. break;
  403. case 'H':
  404. nodeid_format = NODEID_FORMAT_HEX;
  405. break;
  406. case 'l':
  407. command_opt = CMD_SHOWNODES;
  408. break;
  409. case 'e':
  410. if (using_votequorum()) {
  411. votes = strtol(optarg, &endptr, 0);
  412. if ((votes == 0 && endptr == optarg) || votes <= 0) {
  413. fprintf(stderr, "New expected votes value was not valid, try a positive number\n");
  414. }
  415. else {
  416. command_opt = CMD_SETEXPECTED;
  417. }
  418. }
  419. else {
  420. fprintf(stderr, "You cannot change expected votes, corosync is not using votequorum\n");
  421. exit(2);
  422. }
  423. break;
  424. case 'n':
  425. nodeid = strtol(optarg, &endptr, 0);
  426. if ((nodeid == 0 && endptr == optarg) || nodeid <= 0) {
  427. fprintf(stderr, "The nodeid was not valid, try a positive number\n");
  428. }
  429. break;
  430. case 'v':
  431. if (using_votequorum()) {
  432. votes = strtol(optarg, &endptr, 0);
  433. if ((votes == 0 && endptr == optarg) || votes < 0) {
  434. fprintf(stderr, "New votes value was not valid, try a positive number or zero\n");
  435. }
  436. else {
  437. command_opt = CMD_SETVOTES;
  438. }
  439. }
  440. else {
  441. fprintf(stderr, "You cannot change node votes, corosync is not using votequorum\n");
  442. exit(2);
  443. }
  444. break;
  445. case 'h':
  446. case '?':
  447. default:
  448. break;
  449. }
  450. }
  451. switch (command_opt) {
  452. case CMD_UNKNOWN:
  453. show_usage(argv[0]);
  454. ret = -1;
  455. break;
  456. case CMD_SHOWNODES:
  457. ret = show_nodes(nodeid_format, address_format);
  458. break;
  459. case CMD_SHOWSTATUS:
  460. ret = show_status();
  461. break;
  462. case CMD_SETVOTES:
  463. ret = set_votes(nodeid, votes);
  464. break;
  465. case CMD_SETEXPECTED:
  466. ret = set_expected(votes);
  467. break;
  468. }
  469. return (ret);
  470. }