corosync-quorumtool.c 14 KB

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