corosync-quorumtool.c 15 KB

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