corosync-quorumtool.c 15 KB

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