corosync-quorumtool.c 14 KB

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