corosync-quorumtool.c 15 KB

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