corosync-quorumtool.c 16 KB

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