corosync-quorumtool.c 19 KB

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