corosync-quorumtool.c 19 KB

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