corosync-quorumtool.c 14 KB

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