corosync-quorumtool.c 18 KB

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