corosync-quorumtool.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  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 <string.h>
  38. #include <sys/types.h>
  39. #include <sys/socket.h>
  40. #include <netdb.h>
  41. #include <limits.h>
  42. #include <corosync/totem/totem.h>
  43. #include <corosync/cfg.h>
  44. #include <corosync/cmap.h>
  45. #include <corosync/quorum.h>
  46. #include <corosync/votequorum.h>
  47. typedef enum {
  48. NODEID_FORMAT_DECIMAL,
  49. NODEID_FORMAT_HEX
  50. } nodeid_format_t;
  51. typedef enum {
  52. ADDRESS_FORMAT_NAME,
  53. ADDRESS_FORMAT_IP
  54. } name_format_t;
  55. typedef enum {
  56. CMD_UNKNOWN,
  57. CMD_SHOWNODES,
  58. CMD_SHOWSTATUS,
  59. CMD_SETVOTES,
  60. CMD_SETEXPECTED,
  61. CMD_MONITOR,
  62. CMD_UNREGISTER_QDEVICE
  63. } command_t;
  64. /*
  65. * global vars
  66. */
  67. /*
  68. * cmap bits
  69. */
  70. static cmap_handle_t cmap_handle;
  71. /*
  72. * quorum bits
  73. */
  74. static void quorum_notification_fn(
  75. quorum_handle_t handle,
  76. uint32_t quorate,
  77. uint64_t ring_id,
  78. uint32_t view_list_entries,
  79. uint32_t *view_list);
  80. static quorum_handle_t q_handle;
  81. static uint32_t q_type;
  82. static quorum_callbacks_t q_callbacks = {
  83. .quorum_notify_fn = quorum_notification_fn
  84. };
  85. /*
  86. * quorum call back vars
  87. */
  88. static uint32_t g_quorate;
  89. static uint64_t g_ring_id;
  90. static uint32_t g_view_list_entries;
  91. static uint32_t *g_view_list = NULL;
  92. static uint32_t g_called;
  93. /*
  94. * votequorum bits
  95. */
  96. static votequorum_handle_t v_handle;
  97. static votequorum_callbacks_t v_callbacks = {
  98. .votequorum_notify_fn = NULL,
  99. .votequorum_expectedvotes_notify_fn = NULL
  100. };
  101. static uint32_t our_nodeid = 0;
  102. /*
  103. * cfg bits
  104. */
  105. static corosync_cfg_handle_t c_handle;
  106. static corosync_cfg_callbacks_t c_callbacks = {
  107. .corosync_cfg_shutdown_callback = NULL
  108. };
  109. static void show_usage(const char *name)
  110. {
  111. printf("usage: \n");
  112. printf("%s <options>\n", name);
  113. printf("\n");
  114. printf(" options:\n");
  115. printf("\n");
  116. printf(" -s show quorum status\n");
  117. printf(" -m monitor quorum status\n");
  118. printf(" -l list nodes\n");
  119. printf(" -v <votes> change the number of votes for a node (*)\n");
  120. printf(" -n <nodeid> optional nodeid of node for -v (*)\n");
  121. printf(" -e <expected> change expected votes for the cluster (*)\n");
  122. printf(" -H show nodeids in hexadecimal rather than decimal\n");
  123. printf(" -i show node IP addresses instead of the resolved name\n");
  124. printf(" -f forcefully unregister a quorum device *DANGEROUS* (*)\n");
  125. printf(" -h show this help text\n");
  126. printf(" -V show version and exit\n");
  127. printf("\n");
  128. printf(" (*) Starred items only work if votequorum is the quorum provider for corosync\n");
  129. printf("\n");
  130. }
  131. static int get_quorum_type(char *quorum_type, size_t quorum_type_len)
  132. {
  133. int err;
  134. char *str = NULL;
  135. if ((!quorum_type) || (quorum_type_len <= 0)) {
  136. return -1;
  137. }
  138. if (q_type == QUORUM_FREE) {
  139. return -1;
  140. }
  141. if ((err = cmap_get_string(cmap_handle, "quorum.provider", &str)) != CS_OK) {
  142. goto out;
  143. }
  144. if (!str) {
  145. return -1;
  146. }
  147. strncpy(quorum_type, str, quorum_type_len - 1);
  148. free(str);
  149. return 0;
  150. out:
  151. return err;
  152. }
  153. /*
  154. * Returns 1 if 'votequorum' is active. The called then knows that
  155. * votequorum calls should work and can provide extra information
  156. */
  157. static int using_votequorum(void)
  158. {
  159. char quorumtype[256];
  160. int using_voteq;
  161. memset(quorumtype, 0, sizeof(quorumtype));
  162. if (get_quorum_type(quorumtype, sizeof(quorumtype))) {
  163. return -1;
  164. }
  165. if (strcmp(quorumtype, "corosync_votequorum") == 0) {
  166. using_voteq = 1;
  167. } else {
  168. using_voteq = 0;
  169. }
  170. return using_voteq;
  171. }
  172. static int set_votes(uint32_t nodeid, int votes)
  173. {
  174. int err;
  175. if ((err=votequorum_setvotes(v_handle, nodeid, votes)) != CS_OK) {
  176. fprintf(stderr, "Unable to set votes %d for nodeid: %u: %s\n",
  177. votes, nodeid, cs_strerror(err));
  178. }
  179. return err==CS_OK?0:err;
  180. }
  181. static int set_expected(int expected_votes)
  182. {
  183. int err;
  184. if ((err=votequorum_setexpected(v_handle, expected_votes)) != CS_OK) {
  185. fprintf(stderr, "Unable to set expected votes: %s\n", cs_strerror(err));
  186. }
  187. return err==CS_OK?0:err;
  188. }
  189. /*
  190. * node name by nodelist
  191. */
  192. static const char *node_name_by_nodelist(uint32_t nodeid)
  193. {
  194. cmap_iter_handle_t iter;
  195. char key_name[CMAP_KEYNAME_MAXLEN];
  196. char tmp_key[CMAP_KEYNAME_MAXLEN];
  197. static char ret_buf[_POSIX_HOST_NAME_MAX];
  198. char *str = NULL;
  199. uint32_t node_pos, cur_nodeid;
  200. int res = 0;
  201. if (cmap_iter_init(cmap_handle, "nodelist.node.", &iter) != CS_OK) {
  202. return "";
  203. }
  204. memset(ret_buf, 0, sizeof(ret_buf));
  205. while ((cmap_iter_next(cmap_handle, iter, key_name, NULL, NULL)) == CS_OK) {
  206. res = sscanf(key_name, "nodelist.node.%u.%s", &node_pos, tmp_key);
  207. if (res != 2) {
  208. continue;
  209. }
  210. if (strcmp(tmp_key, "ring0_addr") != 0) {
  211. continue;
  212. }
  213. snprintf(tmp_key, CMAP_KEYNAME_MAXLEN, "nodelist.node.%u.nodeid", node_pos);
  214. if (cmap_get_uint32(cmap_handle, tmp_key, &cur_nodeid) != CS_OK) {
  215. continue;
  216. }
  217. if (cur_nodeid != nodeid) {
  218. continue;
  219. }
  220. snprintf(tmp_key, CMAP_KEYNAME_MAXLEN, "nodelist.node.%u.ring0_addr", node_pos);
  221. if (cmap_get_string(cmap_handle, tmp_key, &str) != CS_OK) {
  222. continue;
  223. }
  224. if (!str) {
  225. continue;
  226. }
  227. strncpy(ret_buf, str, sizeof(ret_buf) - 1);
  228. free(str);
  229. break;
  230. }
  231. cmap_iter_finalize(cmap_handle, iter);
  232. return ret_buf;
  233. }
  234. /*
  235. * This resolves the first address assigned to a node
  236. * and returns the name or IP address. Use cfgtool if you need more information.
  237. */
  238. static const char *node_name(uint32_t nodeid, name_format_t name_format)
  239. {
  240. int err;
  241. int numaddrs;
  242. corosync_cfg_node_address_t addrs[INTERFACE_MAX];
  243. static char buf[INET6_ADDRSTRLEN];
  244. const char *nodelist_name = NULL;
  245. socklen_t addrlen;
  246. struct sockaddr_storage *ss;
  247. if (name_format == ADDRESS_FORMAT_NAME) {
  248. nodelist_name = node_name_by_nodelist(nodeid);
  249. }
  250. if ((nodelist_name) &&
  251. (strlen(nodelist_name) > 0)) {
  252. return nodelist_name;
  253. }
  254. err = corosync_cfg_get_node_addrs(c_handle, nodeid, INTERFACE_MAX, &numaddrs, addrs);
  255. if (err != CS_OK) {
  256. fprintf(stderr, "Unable to get node address for nodeid %u: %s\n", nodeid, cs_strerror(err));
  257. return "";
  258. }
  259. ss = (struct sockaddr_storage *)addrs[0].address;
  260. if (ss->ss_family == AF_INET6) {
  261. addrlen = sizeof(struct sockaddr_in6);
  262. } else {
  263. addrlen = sizeof(struct sockaddr_in);
  264. }
  265. if (!getnameinfo(
  266. (struct sockaddr *)addrs[0].address, addrlen,
  267. buf, sizeof(buf),
  268. NULL, 0,
  269. (name_format == ADDRESS_FORMAT_IP)?NI_NUMERICHOST:0)) {
  270. return buf;
  271. }
  272. return "";
  273. }
  274. static void quorum_notification_fn(
  275. quorum_handle_t handle,
  276. uint32_t quorate,
  277. uint64_t ring_id,
  278. uint32_t view_list_entries,
  279. uint32_t *view_list)
  280. {
  281. g_called = 1;
  282. g_quorate = quorate;
  283. g_ring_id = ring_id;
  284. g_view_list_entries = view_list_entries;
  285. if (g_view_list) {
  286. free(g_view_list);
  287. }
  288. g_view_list = malloc(sizeof(uint32_t) * view_list_entries);
  289. if (g_view_list) {
  290. memcpy(g_view_list, view_list,sizeof(uint32_t) * view_list_entries);
  291. }
  292. }
  293. static void print_string_padded(const char *buf)
  294. {
  295. int len, delta;
  296. len = strlen(buf);
  297. delta = 10 - len;
  298. while (delta > 0) {
  299. printf(" ");
  300. delta--;
  301. }
  302. printf("%s ", buf);
  303. }
  304. static void print_uint32_padded(uint32_t value)
  305. {
  306. char buf[12];
  307. snprintf(buf, sizeof(buf) - 1, "%u", value);
  308. print_string_padded(buf);
  309. }
  310. static void display_nodes_data(nodeid_format_t nodeid_format, name_format_t name_format)
  311. {
  312. int i, display_qdevice = 0;
  313. struct votequorum_info info;
  314. printf("\nMembership information\n");
  315. printf("----------------------\n");
  316. print_string_padded("Nodeid");
  317. if (v_handle) {
  318. print_string_padded("Votes");
  319. print_string_padded("Qdevice");
  320. }
  321. printf("Name\n");
  322. for (i=0; i < g_view_list_entries; i++) {
  323. if (nodeid_format == NODEID_FORMAT_DECIMAL) {
  324. print_uint32_padded(g_view_list[i]);
  325. } else {
  326. printf("0x%08x ", g_view_list[i]);
  327. }
  328. if (v_handle) {
  329. int votes = -1;
  330. if (votequorum_getinfo(v_handle, g_view_list[i], &info) == CS_OK) {
  331. votes = info.node_votes;
  332. }
  333. print_uint32_padded(votes);
  334. if (info.flags & VOTEQUORUM_INFO_QDEVICE_REGISTERED) {
  335. char buf[10];
  336. display_qdevice = 1;
  337. snprintf(buf, sizeof(buf) - 1,
  338. "%s,%s,%s",
  339. info.flags & VOTEQUORUM_INFO_QDEVICE_ALIVE?"A":"NA",
  340. info.flags & VOTEQUORUM_INFO_QDEVICE_CAST_VOTE?"V":"NV",
  341. info.flags & VOTEQUORUM_INFO_QDEVICE_MASTER_WINS?"MW":"NMW");
  342. print_string_padded(buf);
  343. } else {
  344. print_string_padded("NR");
  345. }
  346. }
  347. printf("%s", node_name(g_view_list[i], name_format));
  348. if (g_view_list[i] == our_nodeid) {
  349. printf(" (local)");
  350. }
  351. printf("\n");
  352. }
  353. if (g_view_list_entries) {
  354. free(g_view_list);
  355. g_view_list = NULL;
  356. }
  357. if (display_qdevice) {
  358. if (nodeid_format == NODEID_FORMAT_DECIMAL) {
  359. print_uint32_padded(VOTEQUORUM_QDEVICE_NODEID);
  360. } else {
  361. printf("0x%08x ", VOTEQUORUM_QDEVICE_NODEID);
  362. }
  363. print_uint32_padded(info.qdevice_votes);
  364. printf(" %s\n", info.qdevice_name);
  365. }
  366. }
  367. static int display_quorum_data(int is_quorate,
  368. nodeid_format_t nodeid_format, name_format_t name_format,
  369. int loop)
  370. {
  371. struct votequorum_info info;
  372. int err;
  373. char quorumtype[256];
  374. time_t t;
  375. memset(quorumtype, 0, sizeof(quorumtype));
  376. printf("Quorum information\n");
  377. printf("------------------\n");
  378. time(&t);
  379. printf("Date: %s", ctime((const time_t *)&t));
  380. if (get_quorum_type(quorumtype, sizeof(quorumtype))) {
  381. strncpy(quorumtype, "Not configured", sizeof(quorumtype) - 1);
  382. }
  383. printf("Quorum provider: %s\n", quorumtype);
  384. printf("Nodes: %d\n", g_view_list_entries);
  385. if (nodeid_format == NODEID_FORMAT_DECIMAL) {
  386. printf("Node ID: %u\n", our_nodeid);
  387. } else {
  388. printf("Node ID: 0x%08x\n", our_nodeid);
  389. }
  390. printf("Ring ID: %" PRIu64 "\n", g_ring_id);
  391. printf("Quorate: %s\n", is_quorate?"Yes":"No");
  392. if (!v_handle) {
  393. return CS_OK;
  394. }
  395. err=votequorum_getinfo(v_handle, our_nodeid, &info);
  396. if ((err == CS_OK) || (err == CS_ERR_NOT_EXIST)) {
  397. printf("\nVotequorum information\n");
  398. printf("----------------------\n");
  399. printf("Expected votes: %d\n", info.node_expected_votes);
  400. printf("Highest expected: %d\n", info.highest_expected);
  401. printf("Total votes: %d\n", info.total_votes);
  402. printf("Quorum: %d %s\n", info.quorum, info.flags & VOTEQUORUM_INFO_QUORATE?" ":"Activity blocked");
  403. printf("Flags: ");
  404. if (info.flags & VOTEQUORUM_INFO_TWONODE) printf("2Node ");
  405. if (info.flags & VOTEQUORUM_INFO_QUORATE) printf("Quorate ");
  406. if (info.flags & VOTEQUORUM_INFO_WAIT_FOR_ALL) printf("WaitForAll ");
  407. if (info.flags & VOTEQUORUM_INFO_LAST_MAN_STANDING) printf("LastManStanding ");
  408. if (info.flags & VOTEQUORUM_INFO_AUTO_TIE_BREAKER) printf("AutoTieBreaker ");
  409. if (info.flags & VOTEQUORUM_INFO_ALLOW_DOWNSCALE) printf("AllowDownscale ");
  410. if (info.flags & VOTEQUORUM_INFO_QDEVICE_REGISTERED) printf("Qdevice ");
  411. printf("\n");
  412. } else {
  413. fprintf(stderr, "Unable to get node info: %s\n", cs_strerror(err));
  414. }
  415. display_nodes_data(nodeid_format, name_format);
  416. return err;
  417. }
  418. /*
  419. * return 1 if quorate
  420. * 0 if not quorate
  421. * -1 on error
  422. */
  423. static int show_status(nodeid_format_t nodeid_format, name_format_t name_format)
  424. {
  425. int is_quorate;
  426. int err;
  427. err=quorum_getquorate(q_handle, &is_quorate);
  428. if (err != CS_OK) {
  429. fprintf(stderr, "Unable to get cluster quorate status: %s\n", cs_strerror(err));
  430. goto quorum_err;
  431. }
  432. err=quorum_trackstart(q_handle, CS_TRACK_CURRENT);
  433. if (err != CS_OK) {
  434. fprintf(stderr, "Unable to start quorum status tracking: %s\n", cs_strerror(err));
  435. goto quorum_err;
  436. }
  437. g_called = 0;
  438. while (g_called == 0 && err == CS_OK) {
  439. err = quorum_dispatch(q_handle, CS_DISPATCH_ONE);
  440. if (err != CS_OK) {
  441. fprintf(stderr, "Unable to dispatch quorum status: %s\n", cs_strerror(err));
  442. }
  443. }
  444. if (quorum_trackstop(q_handle) != CS_OK) {
  445. fprintf(stderr, "Unable to stop quorum status tracking: %s\n", cs_strerror(err));
  446. }
  447. quorum_err:
  448. if (err != CS_OK) {
  449. return -1;
  450. }
  451. err = display_quorum_data(is_quorate, nodeid_format, name_format, 0);
  452. if (err != CS_OK) {
  453. return -1;
  454. }
  455. return is_quorate;
  456. }
  457. static int monitor_status(nodeid_format_t nodeid_format, name_format_t name_format) {
  458. int err;
  459. int loop = 0;
  460. if (q_type == QUORUM_FREE) {
  461. printf("\nQuorum is not configured - cannot monitor\n");
  462. return show_status(nodeid_format, name_format);
  463. }
  464. err=quorum_trackstart(q_handle, CS_TRACK_CHANGES);
  465. if (err != CS_OK) {
  466. fprintf(stderr, "Unable to start quorum status tracking: %s\n", cs_strerror(err));
  467. goto quorum_err;
  468. }
  469. while (1) {
  470. err = quorum_dispatch(q_handle, CS_DISPATCH_ONE);
  471. if (err != CS_OK) {
  472. fprintf(stderr, "Unable to dispatch quorum status: %s\n", cs_strerror(err));
  473. goto quorum_err;
  474. }
  475. err = display_quorum_data(g_quorate, nodeid_format, name_format, loop);
  476. printf("\n");
  477. loop = 1;
  478. if (err != CS_OK) {
  479. fprintf(stderr, "Unable to display quorum data: %s\n", cs_strerror(err));
  480. goto quorum_err;
  481. }
  482. }
  483. quorum_err:
  484. return -1;
  485. }
  486. static int show_nodes(nodeid_format_t nodeid_format, name_format_t name_format)
  487. {
  488. int err;
  489. int result = EXIT_FAILURE;
  490. err = quorum_trackstart(q_handle, CS_TRACK_CURRENT);
  491. if (err != CS_OK) {
  492. fprintf(stderr, "Unable to start quorum status tracking: %s\n", cs_strerror(err));
  493. goto err_exit;
  494. }
  495. g_called = 0;
  496. while (g_called == 0) {
  497. err = quorum_dispatch(q_handle, CS_DISPATCH_ONE);
  498. if (err != CS_OK) {
  499. fprintf(stderr, "Unable to dispatch quorum status: %s\n", cs_strerror(err));
  500. goto err_exit;
  501. }
  502. }
  503. display_nodes_data(nodeid_format, name_format);
  504. result = EXIT_SUCCESS;
  505. err_exit:
  506. return result;
  507. }
  508. static int unregister_qdevice(void)
  509. {
  510. int err;
  511. struct votequorum_info info;
  512. err = votequorum_getinfo(v_handle, our_nodeid, &info);
  513. if (err != CS_OK) {
  514. fprintf(stderr, "Unable to get quorum device info: %s\n", cs_strerror(err));
  515. return -1;
  516. }
  517. if (!(info.flags & VOTEQUORUM_INFO_QDEVICE_REGISTERED)) {
  518. return 0;
  519. }
  520. err = votequorum_qdevice_unregister(v_handle, info.qdevice_name);
  521. if (err != CS_OK) {
  522. fprintf(stderr, "Unable to unregister quorum device: %s\n", cs_strerror(err));
  523. return -1;
  524. }
  525. return 0;
  526. }
  527. /*
  528. * return -1 on error
  529. * 0 if OK
  530. */
  531. static int init_all(void) {
  532. cmap_handle = 0;
  533. q_handle = 0;
  534. v_handle = 0;
  535. c_handle = 0;
  536. if (cmap_initialize(&cmap_handle) != CS_OK) {
  537. fprintf(stderr, "Cannot initialize CMAP service\n");
  538. cmap_handle = 0;
  539. goto out;
  540. }
  541. if (quorum_initialize(&q_handle, &q_callbacks, &q_type) != CS_OK) {
  542. fprintf(stderr, "Cannot initialize QUORUM service\n");
  543. q_handle = 0;
  544. goto out;
  545. }
  546. if (corosync_cfg_initialize(&c_handle, &c_callbacks) != CS_OK) {
  547. fprintf(stderr, "Cannot initialise CFG service\n");
  548. c_handle = 0;
  549. goto out;
  550. }
  551. if (using_votequorum() <= 0) {
  552. return 0;
  553. }
  554. if (votequorum_initialize(&v_handle, &v_callbacks) != CS_OK) {
  555. fprintf(stderr, "Cannot initialise VOTEQUORUM service\n");
  556. v_handle = 0;
  557. goto out;
  558. }
  559. if (cmap_get_uint32(cmap_handle, "runtime.votequorum.this_node_id", &our_nodeid) != CS_OK) {
  560. fprintf(stderr, "Unable to retrive this node nodeid\n");
  561. goto out;
  562. }
  563. return 0;
  564. out:
  565. return -1;
  566. }
  567. static void close_all(void) {
  568. if (cmap_handle) {
  569. cmap_finalize(cmap_handle);
  570. }
  571. if (q_handle) {
  572. quorum_finalize(q_handle);
  573. }
  574. if (c_handle) {
  575. corosync_cfg_finalize(c_handle);
  576. }
  577. if (v_handle) {
  578. votequorum_finalize(v_handle);
  579. }
  580. }
  581. int main (int argc, char *argv[]) {
  582. const char *options = "VHslmfe:v:hin:";
  583. char *endptr;
  584. int opt;
  585. int votes = 0;
  586. int ret = 0;
  587. uint32_t nodeid = 0;
  588. uint32_t nodeid_set = 0;
  589. nodeid_format_t nodeid_format = NODEID_FORMAT_DECIMAL;
  590. name_format_t address_format = ADDRESS_FORMAT_NAME;
  591. command_t command_opt = CMD_SHOWSTATUS;
  592. if (init_all()) {
  593. close_all();
  594. exit(1);
  595. }
  596. while ( (opt = getopt(argc, argv, options)) != -1 ) {
  597. switch (opt) {
  598. case 'f':
  599. if (using_votequorum() > 0) {
  600. command_opt = CMD_UNREGISTER_QDEVICE;
  601. } else {
  602. fprintf(stderr, "You cannot unregister quorum device, corosync is not using votequorum\n");
  603. exit(2);
  604. }
  605. break;
  606. case 's':
  607. command_opt = CMD_SHOWSTATUS;
  608. break;
  609. case 'm':
  610. command_opt = CMD_MONITOR;
  611. break;
  612. case 'i':
  613. address_format = ADDRESS_FORMAT_IP;
  614. break;
  615. case 'H':
  616. nodeid_format = NODEID_FORMAT_HEX;
  617. break;
  618. case 'l':
  619. command_opt = CMD_SHOWNODES;
  620. break;
  621. case 'e':
  622. if (using_votequorum() > 0) {
  623. votes = strtol(optarg, &endptr, 0);
  624. if ((votes == 0 && endptr == optarg) || votes <= 0) {
  625. fprintf(stderr, "New expected votes value was not valid, try a positive number\n");
  626. } else {
  627. command_opt = CMD_SETEXPECTED;
  628. }
  629. } else {
  630. fprintf(stderr, "You cannot change expected votes, corosync is not using votequorum\n");
  631. exit(2);
  632. }
  633. break;
  634. case 'n':
  635. nodeid = strtol(optarg, &endptr, 0);
  636. if ((nodeid == 0 && endptr == optarg) || nodeid < 0) {
  637. fprintf(stderr, "The nodeid was not valid, try a positive number\n");
  638. exit(2);
  639. }
  640. nodeid_set = 1;
  641. break;
  642. case 'v':
  643. if (using_votequorum() > 0) {
  644. votes = strtol(optarg, &endptr, 0);
  645. if ((votes == 0 && endptr == optarg) || votes < 0) {
  646. fprintf(stderr, "New votes value was not valid, try a positive number or zero\n");
  647. exit(2);
  648. } else {
  649. command_opt = CMD_SETVOTES;
  650. }
  651. }
  652. else {
  653. fprintf(stderr, "You cannot change node votes, corosync is not using votequorum\n");
  654. exit(2);
  655. }
  656. break;
  657. case 'V':
  658. printf("corosync-quorumtool version: %s\n", VERSION);
  659. exit(0);
  660. case ':':
  661. case 'h':
  662. case '?':
  663. default:
  664. command_opt = CMD_UNKNOWN;
  665. break;
  666. }
  667. }
  668. switch (command_opt) {
  669. case CMD_UNKNOWN:
  670. show_usage(argv[0]);
  671. ret = -1;
  672. break;
  673. case CMD_SHOWNODES:
  674. ret = show_nodes(nodeid_format, address_format);
  675. break;
  676. case CMD_SHOWSTATUS:
  677. ret = show_status(nodeid_format, address_format);
  678. break;
  679. case CMD_SETVOTES:
  680. if (!nodeid_set) {
  681. nodeid = our_nodeid;
  682. }
  683. ret = set_votes(nodeid, votes);
  684. break;
  685. case CMD_SETEXPECTED:
  686. ret = set_expected(votes);
  687. break;
  688. case CMD_MONITOR:
  689. ret = monitor_status(nodeid_format, address_format);
  690. break;
  691. case CMD_UNREGISTER_QDEVICE:
  692. ret = unregister_qdevice();
  693. break;
  694. }
  695. close_all();
  696. return (ret);
  697. }