corosync-quorumtool.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  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/cmap.h>
  53. #include <corosync/quorum.h>
  54. #include <corosync/votequorum.h>
  55. typedef enum {
  56. NODEID_FORMAT_DECIMAL,
  57. NODEID_FORMAT_HEX
  58. } nodeid_format_t;
  59. typedef enum {
  60. ADDRESS_FORMAT_NAME,
  61. ADDRESS_FORMAT_IP
  62. } name_format_t;
  63. typedef enum {
  64. CMD_UNKNOWN,
  65. CMD_SHOWNODES,
  66. CMD_SHOWSTATUS,
  67. CMD_SETVOTES,
  68. CMD_SETEXPECTED,
  69. CMD_MONITOR
  70. } command_t;
  71. /*
  72. * global vars
  73. */
  74. /*
  75. * cmap bits
  76. */
  77. static cmap_handle_t cmap_handle;
  78. /*
  79. * quorum bits
  80. */
  81. static void quorum_notification_fn(
  82. quorum_handle_t handle,
  83. uint32_t quorate,
  84. uint64_t ring_id,
  85. uint32_t view_list_entries,
  86. uint32_t *view_list);
  87. static quorum_handle_t q_handle;
  88. static quorum_callbacks_t q_callbacks = {
  89. .quorum_notify_fn = quorum_notification_fn
  90. };
  91. /*
  92. * quorum call back vars
  93. */
  94. static uint32_t g_quorate;
  95. static uint64_t g_ring_id;
  96. static uint32_t g_view_list_entries;
  97. static uint32_t *g_view_list = NULL;
  98. static uint32_t g_called;
  99. /*
  100. * votequorum bits
  101. */
  102. static votequorum_handle_t v_handle;
  103. static votequorum_callbacks_t v_callbacks = {
  104. .votequorum_notify_fn = NULL,
  105. .votequorum_expectedvotes_notify_fn = NULL
  106. };
  107. /*
  108. * cfg bits
  109. */
  110. static corosync_cfg_handle_t c_handle;
  111. static corosync_cfg_callbacks_t c_callbacks = {
  112. .corosync_cfg_state_track_callback = NULL,
  113. .corosync_cfg_shutdown_callback = NULL
  114. };
  115. static void show_usage(const char *name)
  116. {
  117. printf("usage: \n");
  118. printf("%s <options>\n", name);
  119. printf("\n");
  120. printf(" options:\n");
  121. printf("\n");
  122. printf(" -s show quorum status\n");
  123. printf(" -m monitor quorum status\n");
  124. printf(" -l list nodes\n");
  125. printf(" -v <votes> change the number of votes for a node *\n");
  126. printf(" -n <nodeid> optional nodeid of node for -v\n");
  127. printf(" -e <expected> change expected votes for the cluster *\n");
  128. printf(" -H show nodeids in hexadecimal rather than decimal\n");
  129. printf(" -i show node IP addresses instead of the resolved name\n");
  130. printf(" -h show this help text\n");
  131. printf("\n");
  132. printf(" * Starred items only work if votequorum is the quorum provider for corosync\n");
  133. printf("\n");
  134. }
  135. static int get_quorum_type(char *quorum_type, size_t quorum_type_len)
  136. {
  137. int err;
  138. char *str;
  139. if ((!quorum_type) || (quorum_type_len <= 0)) {
  140. errno = EINVAL;
  141. return -1;
  142. }
  143. if ((err = cmap_get_string(cmap_handle, "quorum.provider", &str)) != CS_OK) {
  144. goto out;
  145. }
  146. strncpy(quorum_type, str, quorum_type_len - 1);
  147. free(str);
  148. return 0;
  149. out:
  150. return err;
  151. }
  152. /*
  153. * Returns 1 if 'votequorum' is active. The called then knows that
  154. * votequorum calls should work and can provide extra information
  155. */
  156. static int using_votequorum(void)
  157. {
  158. char quorumtype[256];
  159. int using_voteq;
  160. if (get_quorum_type(quorumtype, sizeof(quorumtype))) {
  161. return -1;
  162. }
  163. if (strcmp(quorumtype, "corosync_votequorum") == 0) {
  164. using_voteq = 1;
  165. } else {
  166. using_voteq = 0;
  167. }
  168. return using_voteq;
  169. }
  170. static int set_votes(uint32_t nodeid, int votes)
  171. {
  172. int err;
  173. if ((err=votequorum_initialize(&v_handle, &v_callbacks)) != CS_OK) {
  174. fprintf(stderr, "votequorum_initialize FAILED: %d, this is probably a configuration error\n", err);
  175. return err;
  176. }
  177. if ((err=votequorum_setvotes(v_handle, nodeid, votes)) != CS_OK) {
  178. fprintf(stderr, "set votes FAILED: %d\n", err);
  179. }
  180. return err==CS_OK?0:err;
  181. }
  182. static int set_expected(int expected_votes)
  183. {
  184. int err;
  185. if ((err=votequorum_initialize(&v_handle, &v_callbacks)) != CS_OK) {
  186. fprintf(stderr, "votequorum_initialize FAILED: %d, this is probably a configuration error\n", err);
  187. return err;
  188. }
  189. if ((err=votequorum_setexpected(v_handle, expected_votes)) != CS_OK) {
  190. fprintf(stderr, "set expected votes FAILED: %d\n", err);
  191. }
  192. return err==CS_OK?0:err;
  193. }
  194. static int get_votes(uint32_t nodeid)
  195. {
  196. int votes = -1;
  197. struct votequorum_info info;
  198. if (votequorum_getinfo(v_handle, nodeid, &info) == CS_OK) {
  199. votes = info.node_votes;
  200. }
  201. return votes;
  202. }
  203. /*
  204. * This resolves the first address assigned to a node
  205. * and returns the name or IP address. Use cfgtool if you need more information.
  206. */
  207. static const char *node_name(uint32_t nodeid, name_format_t name_format)
  208. {
  209. int err;
  210. int numaddrs;
  211. corosync_cfg_node_address_t addrs[INTERFACE_MAX];
  212. static char buf[INET6_ADDRSTRLEN];
  213. socklen_t addrlen;
  214. struct sockaddr_storage *ss;
  215. err = corosync_cfg_get_node_addrs(c_handle, nodeid, INTERFACE_MAX, &numaddrs, addrs);
  216. if (err != CS_OK) {
  217. fprintf(stderr, "Unable to get node address for nodeid %u: %d\n", nodeid, err);
  218. return "";
  219. }
  220. ss = (struct sockaddr_storage *)addrs[0].address;
  221. if (ss->ss_family == AF_INET6) {
  222. addrlen = sizeof(struct sockaddr_in6);
  223. } else {
  224. addrlen = sizeof(struct sockaddr_in);
  225. }
  226. if (!getnameinfo(
  227. (struct sockaddr *)addrs[0].address, addrlen,
  228. buf, sizeof(buf),
  229. NULL, 0,
  230. (name_format == ADDRESS_FORMAT_IP)?NI_NUMERICHOST:0)) {
  231. return buf;
  232. }
  233. return "";
  234. }
  235. static void quorum_notification_fn(
  236. quorum_handle_t handle,
  237. uint32_t quorate,
  238. uint64_t ring_id,
  239. uint32_t view_list_entries,
  240. uint32_t *view_list)
  241. {
  242. g_called = 1;
  243. g_quorate = quorate;
  244. g_ring_id = ring_id;
  245. g_view_list_entries = view_list_entries;
  246. if (g_view_list) {
  247. free(g_view_list);
  248. }
  249. g_view_list = malloc(sizeof(uint32_t) * view_list_entries);
  250. if (g_view_list) {
  251. memcpy(g_view_list, view_list,sizeof(uint32_t) * view_list_entries);
  252. }
  253. }
  254. /*
  255. * return 1 if quorate
  256. * 0 if not quorate
  257. * -1 on error
  258. */
  259. static int show_status(void)
  260. {
  261. struct votequorum_info info;
  262. int is_quorate;
  263. int err;
  264. char quorum_type[256];
  265. err=quorum_getquorate(q_handle, &is_quorate);
  266. if (err != CS_OK) {
  267. fprintf(stderr, "quorum_getquorate FAILED: %d\n", err);
  268. goto quorum_err;
  269. }
  270. err=quorum_trackstart(q_handle, CS_TRACK_CURRENT);
  271. if (err != CS_OK) {
  272. fprintf(stderr, "quorum_trackstart FAILED: %d\n", err);
  273. goto quorum_err;
  274. }
  275. g_called = 0;
  276. while (g_called == 0 && err == CS_OK) {
  277. err = quorum_dispatch(q_handle, CS_DISPATCH_ONE);
  278. if (err != CS_OK) {
  279. fprintf(stderr, "quorum_dispatch FAILED: %d\n", err);
  280. }
  281. }
  282. if (quorum_trackstop(q_handle) != CS_OK) {
  283. fprintf(stderr, "quorum_trackstop FAILED: %d\n", err);
  284. }
  285. quorum_err:
  286. if (err < 0) {
  287. return err;
  288. }
  289. printf("Version: %s\n", VERSION);
  290. printf("Nodes: %d\n", g_view_list_entries);
  291. printf("Ring ID: %" PRIu64 "\n", g_ring_id);
  292. if (get_quorum_type(quorum_type, sizeof(quorum_type))) {
  293. strncpy(quorum_type, "Not configured", sizeof(quorum_type) - 1);
  294. }
  295. printf("Quorum type: %s\n", quorum_type);
  296. printf("Quorate: %s\n", is_quorate?"Yes":"No");
  297. if (!v_handle) {
  298. return is_quorate;
  299. }
  300. if ((err=votequorum_getinfo(v_handle, 0, &info)) == CS_OK) {
  301. printf("Node votes: %d\n", info.node_votes);
  302. printf("Expected votes: %d\n", info.node_expected_votes);
  303. printf("Highest expected: %d\n", info.highest_expected);
  304. printf("Total votes: %d\n", info.total_votes);
  305. printf("Quorum: %d %s\n", info.quorum, info.flags & VOTEQUORUM_INFO_FLAG_QUORATE?" ":"Activity blocked");
  306. printf("Flags: ");
  307. if (info.flags & VOTEQUORUM_INFO_FLAG_HASSTATE) printf("HasState ");
  308. if (info.flags & VOTEQUORUM_INFO_FLAG_DISALLOWED) printf("DisallowedNodes ");
  309. if (info.flags & VOTEQUORUM_INFO_FLAG_TWONODE) printf("2Node ");
  310. if (info.flags & VOTEQUORUM_INFO_FLAG_QUORATE) printf("Quorate ");
  311. printf("\n");
  312. } else {
  313. fprintf(stderr, "votequorum_getinfo FAILED: %d\n", err);
  314. }
  315. if (err != CS_OK) {
  316. return err;
  317. }
  318. return is_quorate;
  319. }
  320. static int monitor_status(nodeid_format_t nodeid_format, name_format_t name_format) {
  321. int err;
  322. show_status();
  323. printf("starting monitoring loop\n");
  324. err=quorum_trackstart(q_handle, CS_TRACK_CHANGES);
  325. if (err != CS_OK) {
  326. fprintf(stderr, "quorum_trackstart FAILED: %d\n", err);
  327. goto quorum_err;
  328. }
  329. while (1) {
  330. time_t t;
  331. int i;
  332. err = quorum_dispatch(q_handle, CS_DISPATCH_ONE);
  333. if (err != CS_OK) {
  334. fprintf(stderr, "quorum_dispatch FAILED: %d\n", err);
  335. goto quorum_err;
  336. }
  337. time(&t);
  338. printf("\ndate: %s", ctime((const time_t *)&t));
  339. printf("Nodes: %d\n", g_view_list_entries);
  340. printf("Ring ID: %" PRIu64 "\n", g_ring_id);
  341. printf("Quorate: %s\n", g_quorate?"Yes":"No");
  342. printf("Nodeid\tName\n");
  343. for (i=0; i < g_view_list_entries; i++) {
  344. if (nodeid_format == NODEID_FORMAT_DECIMAL) {
  345. printf("%4u\t", g_view_list[i]);
  346. } else {
  347. printf("0x%04x\t", g_view_list[i]);
  348. }
  349. printf("%s\n", node_name(g_view_list[i], name_format));
  350. }
  351. free(g_view_list);
  352. g_view_list = NULL;
  353. }
  354. quorum_err:
  355. return err;
  356. }
  357. static int show_nodes(nodeid_format_t nodeid_format, name_format_t name_format)
  358. {
  359. int i;
  360. int err;
  361. int result = EXIT_FAILURE;
  362. err = quorum_trackstart(q_handle, CS_TRACK_CURRENT);
  363. if (err != CS_OK) {
  364. fprintf(stderr, "quorum_trackstart FAILED: %d\n", err);
  365. goto err_exit;
  366. }
  367. g_called = 0;
  368. while (g_called == 0) {
  369. quorum_dispatch(q_handle, CS_DISPATCH_ONE);
  370. }
  371. quorum_finalize(q_handle);
  372. q_handle = 0;
  373. err = corosync_cfg_initialize(&c_handle, &c_callbacks);
  374. if (err != CS_OK) {
  375. fprintf(stderr, "Cannot initialise CFG service\n");
  376. c_handle = 0;
  377. goto err_exit;
  378. }
  379. if (v_handle) {
  380. printf("Nodeid Votes Name\n");
  381. } else {
  382. printf("Nodeid Name\n");
  383. }
  384. for (i=0; i < g_view_list_entries; i++) {
  385. if (nodeid_format == NODEID_FORMAT_DECIMAL) {
  386. printf("%4u ", g_view_list[i]);
  387. } else {
  388. printf("0x%04x ", g_view_list[i]);
  389. }
  390. if (v_handle) {
  391. printf("%3d %s\n", get_votes(g_view_list[i]), node_name(g_view_list[i], name_format));
  392. } else {
  393. printf("%s\n", node_name(g_view_list[i], name_format));
  394. }
  395. }
  396. result = EXIT_SUCCESS;
  397. err_exit:
  398. return result;
  399. }
  400. /*
  401. * return -1 on error
  402. * 0 if OK
  403. */
  404. static int init_all(void) {
  405. cmap_handle = 0;
  406. q_handle = 0;
  407. v_handle = 0;
  408. c_handle = 0;
  409. if (cmap_initialize(&cmap_handle) != CS_OK) {
  410. fprintf(stderr, "Cannot initialize CMAP service\n");
  411. cmap_handle = 0;
  412. goto out;
  413. }
  414. if (quorum_initialize(&q_handle, &q_callbacks) != CS_OK) {
  415. fprintf(stderr, "Cannot initialize QUORUM service\n");
  416. q_handle = 0;
  417. goto out;
  418. }
  419. if (corosync_cfg_initialize(&c_handle, &c_callbacks) != CS_OK) {
  420. fprintf(stderr, "Cannot initialise CFG service\n");
  421. c_handle = 0;
  422. goto out;
  423. }
  424. if (using_votequorum() <= 0) {
  425. return 0;
  426. }
  427. if (votequorum_initialize(&v_handle, &v_callbacks) != CS_OK) {
  428. fprintf(stderr, "Cannot initialise VOTEQUORUM service\n");
  429. v_handle = 0;
  430. goto out;
  431. }
  432. return 0;
  433. out:
  434. return -1;
  435. }
  436. static void close_all(void) {
  437. if (cmap_handle) {
  438. cmap_finalize(cmap_handle);
  439. }
  440. if (q_handle) {
  441. quorum_finalize(q_handle);
  442. }
  443. if (c_handle) {
  444. corosync_cfg_finalize(c_handle);
  445. }
  446. if (v_handle) {
  447. votequorum_finalize(v_handle);
  448. }
  449. }
  450. int main (int argc, char *argv[]) {
  451. const char *options = "VHslme:v:hin:d:";
  452. char *endptr;
  453. int opt;
  454. int votes = 0;
  455. int ret = 0;
  456. uint32_t nodeid = VOTEQUORUM_NODEID_US;
  457. nodeid_format_t nodeid_format = NODEID_FORMAT_DECIMAL;
  458. name_format_t address_format = ADDRESS_FORMAT_NAME;
  459. command_t command_opt = CMD_UNKNOWN;
  460. if (argc == 1) {
  461. show_usage (argv[0]);
  462. exit(0);
  463. }
  464. if (init_all()) {
  465. close_all();
  466. exit(1);
  467. }
  468. while ( (opt = getopt(argc, argv, options)) != -1 ) {
  469. switch (opt) {
  470. case 's':
  471. command_opt = CMD_SHOWSTATUS;
  472. break;
  473. case 'm':
  474. command_opt = CMD_MONITOR;
  475. break;
  476. case 'i':
  477. address_format = ADDRESS_FORMAT_IP;
  478. break;
  479. case 'H':
  480. nodeid_format = NODEID_FORMAT_HEX;
  481. break;
  482. case 'l':
  483. command_opt = CMD_SHOWNODES;
  484. break;
  485. case 'e':
  486. if (using_votequorum() > 0) {
  487. votes = strtol(optarg, &endptr, 0);
  488. if ((votes == 0 && endptr == optarg) || votes <= 0) {
  489. fprintf(stderr, "New expected votes value was not valid, try a positive number\n");
  490. } else {
  491. command_opt = CMD_SETEXPECTED;
  492. }
  493. } else {
  494. fprintf(stderr, "You cannot change expected votes, corosync is not using votequorum\n");
  495. exit(2);
  496. }
  497. break;
  498. case 'n':
  499. nodeid = strtol(optarg, &endptr, 0);
  500. if ((nodeid == 0 && endptr == optarg) || nodeid <= 0) {
  501. fprintf(stderr, "The nodeid was not valid, try a positive number\n");
  502. }
  503. break;
  504. case 'v':
  505. if (using_votequorum() > 0) {
  506. votes = strtol(optarg, &endptr, 0);
  507. if ((votes == 0 && endptr == optarg) || votes < 0) {
  508. fprintf(stderr, "New votes value was not valid, try a positive number or zero\n");
  509. } else {
  510. command_opt = CMD_SETVOTES;
  511. }
  512. }
  513. else {
  514. fprintf(stderr, "You cannot change node votes, corosync is not using votequorum\n");
  515. exit(2);
  516. }
  517. break;
  518. case 'h':
  519. case '?':
  520. default:
  521. break;
  522. }
  523. }
  524. switch (command_opt) {
  525. case CMD_UNKNOWN:
  526. show_usage(argv[0]);
  527. ret = -1;
  528. break;
  529. case CMD_SHOWNODES:
  530. ret = show_nodes(nodeid_format, address_format);
  531. break;
  532. case CMD_SHOWSTATUS:
  533. ret = show_status();
  534. break;
  535. case CMD_SETVOTES:
  536. ret = set_votes(nodeid, votes);
  537. break;
  538. case CMD_SETEXPECTED:
  539. ret = set_expected(votes);
  540. break;
  541. case CMD_MONITOR:
  542. ret = monitor_status(nodeid_format, address_format);
  543. break;
  544. }
  545. close_all();
  546. return (ret);
  547. }