corosync-quorumtool.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  1. /*
  2. * Copyright (c) 2009-2019 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 <stdlib.h>
  38. #include <string.h>
  39. #include <sys/types.h>
  40. #include <sys/socket.h>
  41. #include <netdb.h>
  42. #include <limits.h>
  43. #include <corosync/totem/totem.h>
  44. #include <corosync/cfg.h>
  45. #include <corosync/cmap.h>
  46. #include <corosync/quorum.h>
  47. #include <corosync/votequorum.h>
  48. typedef enum {
  49. NODEID_FORMAT_DECIMAL,
  50. NODEID_FORMAT_HEX
  51. } nodeid_format_t;
  52. typedef enum {
  53. ADDRESS_FORMAT_NAME,
  54. ADDRESS_FORMAT_IP
  55. } name_format_t;
  56. typedef enum {
  57. CMD_UNKNOWN,
  58. CMD_SHOWNODES,
  59. CMD_SHOWSTATUS,
  60. CMD_SETVOTES,
  61. CMD_SETEXPECTED,
  62. CMD_MONITOR,
  63. CMD_UNREGISTER_QDEVICE
  64. } command_t;
  65. typedef enum {
  66. SORT_ADDR,
  67. SORT_NODEID,
  68. SORT_NODENAME
  69. } sorttype_t;
  70. #define EXIT_NOT_QUORATE 2
  71. /*
  72. * global vars
  73. */
  74. /*
  75. * cmap bits
  76. */
  77. static cmap_handle_t cmap_handle;
  78. /*
  79. * quorum bits
  80. */
  81. static void quorum_notification_fn(
  82. quorum_handle_t handle,
  83. uint32_t quorate,
  84. uint64_t ring_id,
  85. uint32_t view_list_entries,
  86. uint32_t *view_list);
  87. static quorum_handle_t q_handle;
  88. static uint32_t q_type;
  89. static quorum_callbacks_t q_callbacks = {
  90. .quorum_notify_fn = quorum_notification_fn
  91. };
  92. /*
  93. * quorum call back vars
  94. */
  95. /* Containing struct to keep votequorum & normal quorum bits together */
  96. typedef struct {
  97. struct votequorum_info *vq_info; /* Might be NULL if votequorum not present */
  98. char *name; /* Might be IP address or NULL */
  99. int node_id; /* Always present */
  100. } view_list_entry_t;
  101. static view_list_entry_t *g_view_list;
  102. static uint32_t g_quorate;
  103. static uint64_t g_ring_id;
  104. static uint32_t g_ring_id_rep_node;
  105. static uint32_t g_view_list_entries;
  106. static uint32_t g_called;
  107. static uint32_t g_vq_called;
  108. static uint32_t g_show_all_addrs = 0;
  109. /*
  110. * votequorum bits
  111. */
  112. static void votequorum_notification_fn(
  113. votequorum_handle_t handle,
  114. uint64_t context,
  115. votequorum_ring_id_t ring_id,
  116. uint32_t node_list_entries,
  117. uint32_t node_list[]);
  118. static votequorum_handle_t v_handle;
  119. static votequorum_callbacks_t v_callbacks = {
  120. .votequorum_quorum_notify_fn = NULL,
  121. .votequorum_expectedvotes_notify_fn = NULL,
  122. .votequorum_nodelist_notify_fn = votequorum_notification_fn,
  123. };
  124. static uint32_t our_nodeid = 0;
  125. /*
  126. * cfg bits
  127. */
  128. static corosync_cfg_handle_t c_handle;
  129. static corosync_cfg_callbacks_t c_callbacks = {
  130. .corosync_cfg_shutdown_callback = NULL
  131. };
  132. /*
  133. * global
  134. */
  135. static int machine_parsable = 0;
  136. static void show_usage(const char *name)
  137. {
  138. printf("usage: \n");
  139. printf("%s <options>\n", name);
  140. printf("\n");
  141. printf(" options:\n");
  142. printf("\n");
  143. printf(" -s show quorum status\n");
  144. printf(" -m constantly monitor quorum status\n");
  145. printf(" -l list nodes\n");
  146. printf(" -a show all names or addresses for each node\n");
  147. printf(" -p when used with -s or -l, generates machine parsable output\n");
  148. printf(" -v <votes> change the number of votes for a node (*)\n");
  149. printf(" -n <nodeid> optional nodeid of node for -v\n");
  150. printf(" -e <expected> change expected votes for the cluster (*)\n");
  151. printf(" -H show nodeids in hexadecimal rather than decimal\n");
  152. printf(" -i show node IP addresses instead of the resolved name\n");
  153. printf(" -o <a|n|i> order by [a] IP address (default), [n] name, [i] nodeid\n");
  154. printf(" -f forcefully unregister a quorum device *DANGEROUS* (*)\n");
  155. printf(" -h show this help text\n");
  156. printf(" -V show version and exit\n");
  157. printf("\n");
  158. printf(" (*) Starred items only work if votequorum is the quorum provider for corosync\n");
  159. printf("\n");
  160. }
  161. static int get_quorum_type(char *quorum_type, size_t quorum_type_len)
  162. {
  163. int err;
  164. char *str = NULL;
  165. if ((!quorum_type) || (quorum_type_len <= 0)) {
  166. return -1;
  167. }
  168. if (q_type == QUORUM_FREE) {
  169. return -1;
  170. }
  171. if ((err = cmap_get_string(cmap_handle, "quorum.provider", &str)) != CS_OK) {
  172. goto out;
  173. }
  174. if (!str) {
  175. return -1;
  176. }
  177. strncpy(quorum_type, str, quorum_type_len - 1);
  178. free(str);
  179. return 0;
  180. out:
  181. return err;
  182. }
  183. /*
  184. * Returns 1 if 'votequorum' is active. The called then knows that
  185. * votequorum calls should work and can provide extra information
  186. */
  187. static int using_votequorum(void)
  188. {
  189. char quorumtype[256];
  190. int using_voteq;
  191. memset(quorumtype, 0, sizeof(quorumtype));
  192. if (get_quorum_type(quorumtype, sizeof(quorumtype))) {
  193. return -1;
  194. }
  195. if (strcmp(quorumtype, "corosync_votequorum") == 0) {
  196. using_voteq = 1;
  197. } else {
  198. using_voteq = 0;
  199. }
  200. return using_voteq;
  201. }
  202. static int set_votes(uint32_t nodeid, int votes)
  203. {
  204. int err;
  205. if ((err=votequorum_setvotes(v_handle, nodeid, votes)) != CS_OK) {
  206. fprintf(stderr, "Unable to set votes %d for nodeid: %u: %s\n",
  207. votes, nodeid, cs_strerror(err));
  208. }
  209. return (err == CS_OK ? EXIT_SUCCESS : EXIT_FAILURE);
  210. }
  211. static int set_expected(int expected_votes)
  212. {
  213. int err;
  214. if ((err=votequorum_setexpected(v_handle, expected_votes)) != CS_OK) {
  215. fprintf(stderr, "Unable to set expected votes: %s\n", cs_strerror(err));
  216. }
  217. return (err == CS_OK ? EXIT_SUCCESS : EXIT_FAILURE);
  218. }
  219. /*
  220. * node name by nodelist
  221. */
  222. static const char *node_name_by_nodelist(uint32_t nodeid)
  223. {
  224. cmap_iter_handle_t iter;
  225. char key_name[CMAP_KEYNAME_MAXLEN + 1];
  226. char tmp_key[CMAP_KEYNAME_MAXLEN + 1];
  227. static char ret_buf[_POSIX_HOST_NAME_MAX];
  228. char *str = NULL;
  229. uint32_t node_pos, cur_nodeid;
  230. int res = 0;
  231. if (cmap_iter_init(cmap_handle, "nodelist.node.", &iter) != CS_OK) {
  232. return "";
  233. }
  234. memset(ret_buf, 0, sizeof(ret_buf));
  235. while ((cmap_iter_next(cmap_handle, iter, key_name, NULL, NULL)) == CS_OK) {
  236. res = sscanf(key_name, "nodelist.node.%u.%s", &node_pos, tmp_key);
  237. if (res != 2) {
  238. continue;
  239. }
  240. if (strcmp(tmp_key, "nodeid") != 0) {
  241. continue;
  242. }
  243. snprintf(tmp_key, CMAP_KEYNAME_MAXLEN, "nodelist.node.%u.nodeid", node_pos);
  244. if (cmap_get_uint32(cmap_handle, tmp_key, &cur_nodeid) != CS_OK) {
  245. continue;
  246. }
  247. if (cur_nodeid != nodeid) {
  248. continue;
  249. }
  250. snprintf(tmp_key, CMAP_KEYNAME_MAXLEN, "nodelist.node.%u.name", node_pos);
  251. if (cmap_get_string(cmap_handle, tmp_key, &str) != CS_OK) {
  252. continue;
  253. }
  254. if (!str) {
  255. continue;
  256. }
  257. strncpy(ret_buf, str, sizeof(ret_buf) - 1);
  258. free(str);
  259. break;
  260. }
  261. cmap_iter_finalize(cmap_handle, iter);
  262. return ret_buf;
  263. }
  264. /*
  265. * This resolves the first address assigned to a node
  266. * and returns the name or IP address. Use cfgtool if you need more information.
  267. */
  268. static const char *node_name(uint32_t nodeid, name_format_t name_format)
  269. {
  270. int err;
  271. int numaddrs;
  272. corosync_cfg_node_address_t addrs[INTERFACE_MAX];
  273. static char buf[(INET6_ADDRSTRLEN + 1) * KNET_MAX_LINK];
  274. const char *nodelist_name = NULL;
  275. socklen_t addrlen;
  276. struct sockaddr_storage *ss;
  277. int start_addr = 0;
  278. int i;
  279. int bufptr = 0;
  280. buf[0] = '\0';
  281. /* If a name is required, always look for the nodelist node0_addr name first */
  282. if (name_format == ADDRESS_FORMAT_NAME) {
  283. nodelist_name = node_name_by_nodelist(nodeid);
  284. }
  285. if ((nodelist_name) &&
  286. (strlen(nodelist_name) > 0)) {
  287. start_addr = 1;
  288. strcpy(buf, nodelist_name);
  289. bufptr = strlen(buf);
  290. }
  291. err = corosync_cfg_get_node_addrs(c_handle, nodeid, INTERFACE_MAX, &numaddrs, addrs);
  292. if (err != CS_OK) {
  293. fprintf(stderr, "Unable to get node address for nodeid %u: %s\n", nodeid, cs_strerror(err));
  294. return "";
  295. }
  296. /* Don't show all addressess */
  297. if (!g_show_all_addrs) {
  298. numaddrs = 1;
  299. }
  300. for (i=start_addr; i<numaddrs; i++) {
  301. ss = (struct sockaddr_storage *)addrs[i].address;
  302. if (!ss->ss_family) {
  303. continue;
  304. }
  305. if (ss->ss_family == AF_INET6) {
  306. addrlen = sizeof(struct sockaddr_in6);
  307. } else {
  308. addrlen = sizeof(struct sockaddr_in);
  309. }
  310. if (i) {
  311. buf[bufptr++] = ',';
  312. buf[bufptr++] = ' ';
  313. }
  314. if (!getnameinfo(
  315. (struct sockaddr *)addrs[i].address, addrlen,
  316. buf+bufptr, sizeof(buf)-bufptr,
  317. NULL, 0,
  318. (name_format == ADDRESS_FORMAT_IP)?NI_NUMERICHOST:0)) {
  319. bufptr += strlen(buf+bufptr);
  320. }
  321. }
  322. return buf;
  323. }
  324. static void votequorum_notification_fn(
  325. votequorum_handle_t handle,
  326. uint64_t context,
  327. votequorum_ring_id_t ring_id,
  328. uint32_t node_list_entries,
  329. uint32_t node_list[])
  330. {
  331. g_ring_id_rep_node = ring_id.nodeid;
  332. g_vq_called = 1;
  333. }
  334. static void quorum_notification_fn(
  335. quorum_handle_t handle,
  336. uint32_t quorate,
  337. uint64_t ring_id,
  338. uint32_t view_list_entries,
  339. uint32_t *view_list)
  340. {
  341. int i;
  342. g_called = 1;
  343. g_quorate = quorate;
  344. g_ring_id = ring_id;
  345. g_view_list_entries = view_list_entries;
  346. if (g_view_list) {
  347. free(g_view_list);
  348. }
  349. g_view_list = malloc(sizeof(view_list_entry_t) * view_list_entries);
  350. if (g_view_list) {
  351. for (i=0; i< view_list_entries; i++) {
  352. g_view_list[i].node_id = view_list[i];
  353. g_view_list[i].name = NULL;
  354. g_view_list[i].vq_info = NULL;
  355. }
  356. }
  357. }
  358. static void print_string_padded(const char *buf)
  359. {
  360. int len, delta;
  361. len = strlen(buf);
  362. delta = 10 - len;
  363. while (delta > 0) {
  364. printf(" ");
  365. delta--;
  366. }
  367. printf("%s ", buf);
  368. }
  369. static void print_uint32_padded(uint32_t value)
  370. {
  371. char buf[12];
  372. snprintf(buf, sizeof(buf) - 1, "%u", value);
  373. print_string_padded(buf);
  374. }
  375. /* for qsort */
  376. static int compare_nodeids(const void *one, const void *two)
  377. {
  378. const view_list_entry_t *info1 = one;
  379. const view_list_entry_t *info2 = two;
  380. if (info1->node_id == info2->node_id) {
  381. return 0;
  382. }
  383. if (info1->node_id > info2->node_id) {
  384. return 1;
  385. }
  386. return -1;
  387. }
  388. static int compare_nodenames(const void *one, const void *two)
  389. {
  390. const view_list_entry_t *info1 = one;
  391. const view_list_entry_t *info2 = two;
  392. return strcmp(info1->name, info2->name);
  393. }
  394. static void display_nodes_data(nodeid_format_t nodeid_format, name_format_t name_format, sorttype_t sort_type)
  395. {
  396. int i, display_qdevice = 0;
  397. unsigned int our_flags = 0;
  398. struct votequorum_info info[g_view_list_entries];
  399. /*
  400. * cache node info because we need to parse them twice
  401. */
  402. if (v_handle) {
  403. for (i=0; i < g_view_list_entries; i++) {
  404. if (votequorum_getinfo(v_handle, g_view_list[i].node_id, &info[i]) != CS_OK) {
  405. printf("Unable to get node %u info\n", g_view_list[i].node_id);
  406. }
  407. g_view_list[i].vq_info = &info[i];
  408. if (info[i].flags & VOTEQUORUM_INFO_QDEVICE_REGISTERED) {
  409. display_qdevice = 1;
  410. }
  411. }
  412. }
  413. /*
  414. * Get node names
  415. */
  416. for (i=0; i < g_view_list_entries; i++) {
  417. g_view_list[i].name = strdup(node_name(g_view_list[i].node_id, name_format));
  418. }
  419. printf("\nMembership information\n");
  420. printf("----------------------\n");
  421. print_string_padded("Nodeid");
  422. if (v_handle) {
  423. print_string_padded("Votes");
  424. if ((display_qdevice) || (machine_parsable)) {
  425. print_string_padded("Qdevice");
  426. }
  427. }
  428. printf("Name\n");
  429. /* corosync sends them already sorted by address */
  430. if (sort_type == SORT_NODEID) {
  431. qsort(g_view_list, g_view_list_entries, sizeof(view_list_entry_t), compare_nodeids);
  432. }
  433. if (sort_type == SORT_NODENAME) {
  434. qsort(g_view_list, g_view_list_entries, sizeof(view_list_entry_t), compare_nodenames);
  435. }
  436. for (i=0; i < g_view_list_entries; i++) {
  437. if (nodeid_format == NODEID_FORMAT_DECIMAL) {
  438. print_uint32_padded(g_view_list[i].node_id);
  439. } else {
  440. printf("0x%08x ", g_view_list[i].node_id);
  441. }
  442. if (v_handle) {
  443. int votes = -1;
  444. votes = info[i].node_votes;
  445. print_uint32_padded(votes);
  446. if ((display_qdevice) || (machine_parsable)) {
  447. if (info[i].flags & VOTEQUORUM_INFO_QDEVICE_REGISTERED) {
  448. char buf[10];
  449. snprintf(buf, sizeof(buf),
  450. "%s,%s,%s",
  451. info[i].flags & VOTEQUORUM_INFO_QDEVICE_ALIVE?"A":"NA",
  452. info[i].flags & VOTEQUORUM_INFO_QDEVICE_CAST_VOTE?"V":"NV",
  453. info[i].flags & VOTEQUORUM_INFO_QDEVICE_MASTER_WINS?"MW":"NMW");
  454. print_string_padded(buf);
  455. } else {
  456. print_string_padded("NR");
  457. }
  458. }
  459. }
  460. printf("%s", g_view_list[i].name);
  461. if (g_view_list[i].node_id == our_nodeid) {
  462. printf(" (local)");
  463. if (v_handle) {
  464. our_flags = info[i].flags;
  465. }
  466. }
  467. printf("\n");
  468. }
  469. if (g_view_list_entries) {
  470. for (i=0; i < g_view_list_entries; i++) {
  471. free(g_view_list[i].name);
  472. }
  473. free(g_view_list);
  474. g_view_list = NULL;
  475. }
  476. if (display_qdevice) {
  477. if (nodeid_format == NODEID_FORMAT_DECIMAL) {
  478. print_uint32_padded(VOTEQUORUM_QDEVICE_NODEID);
  479. } else {
  480. printf("0x%08x ", VOTEQUORUM_QDEVICE_NODEID);
  481. }
  482. /* If the quorum device is inactive on this node then show votes as 0
  483. so that the display is not confusing */
  484. if (our_flags & VOTEQUORUM_INFO_QDEVICE_CAST_VOTE) {
  485. print_uint32_padded(info[0].qdevice_votes);
  486. }
  487. else {
  488. print_uint32_padded(0);
  489. }
  490. printf(" %s", info[0].qdevice_name);
  491. if (our_flags & VOTEQUORUM_INFO_QDEVICE_CAST_VOTE) {
  492. printf("\n");
  493. }
  494. else {
  495. printf(" (votes %d)\n", info[0].qdevice_votes);
  496. }
  497. }
  498. }
  499. static int display_quorum_data(int is_quorate,
  500. nodeid_format_t nodeid_format, name_format_t name_format, sorttype_t sort_type,
  501. int loop)
  502. {
  503. struct votequorum_info info;
  504. int err;
  505. char quorumtype[256];
  506. time_t t;
  507. memset(quorumtype, 0, sizeof(quorumtype));
  508. printf("Quorum information\n");
  509. printf("------------------\n");
  510. time(&t);
  511. printf("Date: %s", ctime((const time_t *)&t));
  512. if (get_quorum_type(quorumtype, sizeof(quorumtype))) {
  513. strncpy(quorumtype, "Not configured", sizeof(quorumtype) - 1);
  514. }
  515. printf("Quorum provider: %s\n", quorumtype);
  516. printf("Nodes: %d\n", g_view_list_entries);
  517. if (nodeid_format == NODEID_FORMAT_DECIMAL) {
  518. printf("Node ID: %u\n", our_nodeid);
  519. } else {
  520. printf("Node ID: 0x%08x\n", our_nodeid);
  521. }
  522. if (v_handle) {
  523. printf("Ring ID: %d/%" PRIu64 "\n", g_ring_id_rep_node, g_ring_id);
  524. }
  525. else {
  526. printf("Ring ID: %" PRIu64 "\n", g_ring_id);
  527. }
  528. printf("Quorate: %s\n", is_quorate?"Yes":"No");
  529. if (!v_handle) {
  530. return CS_OK;
  531. }
  532. err=votequorum_getinfo(v_handle, our_nodeid, &info);
  533. if ((err == CS_OK) || (err == CS_ERR_NOT_EXIST)) {
  534. printf("\nVotequorum information\n");
  535. printf("----------------------\n");
  536. printf("Expected votes: %d\n", info.node_expected_votes);
  537. printf("Highest expected: %d\n", info.highest_expected);
  538. printf("Total votes: %d\n", info.total_votes);
  539. printf("Quorum: %d %s\n", info.quorum, info.flags & VOTEQUORUM_INFO_QUORATE?" ":"Activity blocked");
  540. printf("Flags: ");
  541. if (info.flags & VOTEQUORUM_INFO_TWONODE) printf("2Node ");
  542. if (info.flags & VOTEQUORUM_INFO_QUORATE) printf("Quorate ");
  543. if (info.flags & VOTEQUORUM_INFO_WAIT_FOR_ALL) printf("WaitForAll ");
  544. if (info.flags & VOTEQUORUM_INFO_LAST_MAN_STANDING) printf("LastManStanding ");
  545. if (info.flags & VOTEQUORUM_INFO_AUTO_TIE_BREAKER) printf("AutoTieBreaker ");
  546. if (info.flags & VOTEQUORUM_INFO_ALLOW_DOWNSCALE) printf("AllowDownscale ");
  547. if (info.flags & VOTEQUORUM_INFO_QDEVICE_REGISTERED) printf("Qdevice ");
  548. printf("\n");
  549. } else {
  550. fprintf(stderr, "Unable to get node info: %s\n", cs_strerror(err));
  551. }
  552. display_nodes_data(nodeid_format, name_format, sort_type);
  553. return err;
  554. }
  555. /*
  556. * return EXIT_SUCCESS if quorate
  557. * EXIT_NOT_QUORATE if not quorate
  558. * EXIT_FAILURE on error
  559. */
  560. static int show_status(nodeid_format_t nodeid_format, name_format_t name_format, sorttype_t sort_type)
  561. {
  562. int is_quorate;
  563. int err;
  564. err=quorum_getquorate(q_handle, &is_quorate);
  565. if (err != CS_OK) {
  566. fprintf(stderr, "Unable to get cluster quorate status: %s\n", cs_strerror(err));
  567. goto quorum_err;
  568. }
  569. err=quorum_trackstart(q_handle, CS_TRACK_CURRENT);
  570. if (err != CS_OK) {
  571. fprintf(stderr, "Unable to start quorum status tracking: %s\n", cs_strerror(err));
  572. goto quorum_err;
  573. }
  574. g_called = 0;
  575. while (g_called == 0 && err == CS_OK) {
  576. err = quorum_dispatch(q_handle, CS_DISPATCH_ONE);
  577. if (err != CS_OK) {
  578. fprintf(stderr, "Unable to dispatch quorum status: %s\n", cs_strerror(err));
  579. }
  580. }
  581. if (quorum_trackstop(q_handle) != CS_OK) {
  582. fprintf(stderr, "Unable to stop quorum status tracking: %s\n", cs_strerror(err));
  583. }
  584. if (using_votequorum()) {
  585. if ( (err=votequorum_trackstart(v_handle, 0LL, CS_TRACK_CURRENT)) != CS_OK) {
  586. fprintf(stderr, "Unable to start votequorum status tracking: %s\n", cs_strerror(err));
  587. goto quorum_err;
  588. }
  589. g_vq_called = 0;
  590. while (g_vq_called == 0 && err == CS_OK) {
  591. err = votequorum_dispatch(v_handle, CS_DISPATCH_ONE);
  592. if (err != CS_OK) {
  593. fprintf(stderr, "Unable to dispatch votequorum status: %s\n", cs_strerror(err));
  594. }
  595. }
  596. }
  597. quorum_err:
  598. if (err != CS_OK) {
  599. return EXIT_FAILURE;
  600. }
  601. err = display_quorum_data(is_quorate, nodeid_format, name_format, sort_type, 0);
  602. if (err != CS_OK) {
  603. return EXIT_FAILURE;
  604. }
  605. return (is_quorate ? EXIT_SUCCESS : EXIT_NOT_QUORATE);
  606. }
  607. static int monitor_status(nodeid_format_t nodeid_format, name_format_t name_format, sorttype_t sort_type) {
  608. int err;
  609. int loop = 0;
  610. if (q_type == QUORUM_FREE) {
  611. printf("\nQuorum is not configured - cannot monitor\n");
  612. return show_status(nodeid_format, name_format, sort_type);
  613. }
  614. err=quorum_trackstart(q_handle, CS_TRACK_CHANGES);
  615. if (err != CS_OK) {
  616. fprintf(stderr, "Unable to start quorum status tracking: %s\n", cs_strerror(err));
  617. goto quorum_err;
  618. }
  619. if (using_votequorum()) {
  620. if ( (err=votequorum_trackstart(v_handle, 0LL, CS_TRACK_CHANGES)) != CS_OK) {
  621. fprintf(stderr, "Unable to start votequorum status tracking: %s\n", cs_strerror(err));
  622. goto quorum_err;
  623. }
  624. }
  625. while (1) {
  626. err = quorum_dispatch(q_handle, CS_DISPATCH_ONE);
  627. if (err != CS_OK) {
  628. fprintf(stderr, "Unable to dispatch quorum status: %s\n", cs_strerror(err));
  629. goto quorum_err;
  630. }
  631. if (using_votequorum()) {
  632. g_vq_called = 0;
  633. while (!g_vq_called) {
  634. err = votequorum_dispatch(v_handle, CS_DISPATCH_ONE);
  635. if (err != CS_OK) {
  636. fprintf(stderr, "Unable to dispatch votequorum status: %s\n", cs_strerror(err));
  637. goto quorum_err;
  638. }
  639. }
  640. }
  641. err = display_quorum_data(g_quorate, nodeid_format, name_format, sort_type, loop);
  642. printf("\n");
  643. loop = 1;
  644. if (err != CS_OK) {
  645. fprintf(stderr, "Unable to display quorum data: %s\n", cs_strerror(err));
  646. goto quorum_err;
  647. }
  648. }
  649. quorum_err:
  650. return EXIT_FAILURE;
  651. }
  652. static int show_nodes(nodeid_format_t nodeid_format, name_format_t name_format, sorttype_t sort_type)
  653. {
  654. int err;
  655. int result = EXIT_FAILURE;
  656. err = quorum_trackstart(q_handle, CS_TRACK_CURRENT);
  657. if (err != CS_OK) {
  658. fprintf(stderr, "Unable to start quorum status tracking: %s\n", cs_strerror(err));
  659. goto err_exit;
  660. }
  661. g_called = 0;
  662. while (g_called == 0) {
  663. err = quorum_dispatch(q_handle, CS_DISPATCH_ONE);
  664. if (err != CS_OK) {
  665. fprintf(stderr, "Unable to dispatch quorum status: %s\n", cs_strerror(err));
  666. goto err_exit;
  667. }
  668. }
  669. display_nodes_data(nodeid_format, name_format, sort_type);
  670. result = EXIT_SUCCESS;
  671. err_exit:
  672. return result;
  673. }
  674. static int unregister_qdevice(void)
  675. {
  676. int err;
  677. struct votequorum_info info;
  678. int result;
  679. result = EXIT_FAILURE;
  680. err = votequorum_getinfo(v_handle, our_nodeid, &info);
  681. if (err != CS_OK) {
  682. fprintf(stderr, "Unable to get quorum device info: %s\n", cs_strerror(err));
  683. goto err_exit;
  684. }
  685. if (!(info.flags & VOTEQUORUM_INFO_QDEVICE_REGISTERED)) {
  686. result = EXIT_SUCCESS;
  687. goto err_exit;
  688. }
  689. err = votequorum_qdevice_unregister(v_handle, info.qdevice_name);
  690. if (err != CS_OK) {
  691. fprintf(stderr, "Unable to unregister quorum device: %s\n", cs_strerror(err));
  692. goto err_exit;
  693. }
  694. result = EXIT_SUCCESS;
  695. err_exit:
  696. return result;
  697. }
  698. /*
  699. * return -1 on error
  700. * 0 if OK
  701. */
  702. static int init_all(void) {
  703. cmap_handle = 0;
  704. q_handle = 0;
  705. v_handle = 0;
  706. c_handle = 0;
  707. if (cmap_initialize(&cmap_handle) != CS_OK) {
  708. fprintf(stderr, "Cannot initialize CMAP service\n");
  709. cmap_handle = 0;
  710. goto out;
  711. }
  712. if (quorum_initialize(&q_handle, &q_callbacks, &q_type) != CS_OK) {
  713. fprintf(stderr, "Cannot initialize QUORUM service\n");
  714. q_handle = 0;
  715. goto out;
  716. }
  717. if (corosync_cfg_initialize(&c_handle, &c_callbacks) != CS_OK) {
  718. fprintf(stderr, "Cannot initialise CFG service\n");
  719. c_handle = 0;
  720. goto out;
  721. }
  722. if (using_votequorum() <= 0) {
  723. return 0;
  724. }
  725. if (votequorum_initialize(&v_handle, &v_callbacks) != CS_OK) {
  726. fprintf(stderr, "Cannot initialise VOTEQUORUM service\n");
  727. v_handle = 0;
  728. goto out;
  729. }
  730. if (cmap_get_uint32(cmap_handle, "runtime.votequorum.this_node_id", &our_nodeid) != CS_OK) {
  731. fprintf(stderr, "Unable to retrieve this_node_id\n");
  732. goto out;
  733. }
  734. return 0;
  735. out:
  736. return -1;
  737. }
  738. static void close_all(void) {
  739. if (cmap_handle) {
  740. cmap_finalize(cmap_handle);
  741. }
  742. if (q_handle) {
  743. quorum_finalize(q_handle);
  744. }
  745. if (c_handle) {
  746. corosync_cfg_finalize(c_handle);
  747. }
  748. if (v_handle) {
  749. votequorum_finalize(v_handle);
  750. }
  751. }
  752. int main (int argc, char *argv[]) {
  753. const char *options = "VHaslpmfe:v:hin:o:";
  754. char *endptr;
  755. int opt;
  756. int votes = 0;
  757. int ret = 0;
  758. uint32_t nodeid = 0;
  759. uint32_t nodeid_set = 0;
  760. nodeid_format_t nodeid_format = NODEID_FORMAT_DECIMAL;
  761. name_format_t address_format = ADDRESS_FORMAT_NAME;
  762. command_t command_opt = CMD_SHOWSTATUS;
  763. sorttype_t sort_opt = SORT_ADDR;
  764. char sortchar;
  765. long int l;
  766. if (init_all()) {
  767. close_all();
  768. exit(EXIT_FAILURE);
  769. }
  770. while ( (opt = getopt(argc, argv, options)) != -1 ) {
  771. switch (opt) {
  772. case 'f':
  773. if (using_votequorum() > 0) {
  774. command_opt = CMD_UNREGISTER_QDEVICE;
  775. } else {
  776. fprintf(stderr, "You cannot unregister quorum device, corosync is not using votequorum\n");
  777. exit(EXIT_FAILURE);
  778. }
  779. break;
  780. case 's':
  781. command_opt = CMD_SHOWSTATUS;
  782. break;
  783. case 'a':
  784. g_show_all_addrs = 1;
  785. break;
  786. case 'm':
  787. command_opt = CMD_MONITOR;
  788. break;
  789. case 'i':
  790. address_format = ADDRESS_FORMAT_IP;
  791. break;
  792. case 'H':
  793. nodeid_format = NODEID_FORMAT_HEX;
  794. break;
  795. case 'l':
  796. command_opt = CMD_SHOWNODES;
  797. break;
  798. case 'p':
  799. machine_parsable = 1;
  800. break;
  801. case 'e':
  802. if (using_votequorum() > 0) {
  803. votes = strtol(optarg, &endptr, 0);
  804. if ((votes == 0 && endptr == optarg) || votes <= 0) {
  805. fprintf(stderr, "New expected votes value was not valid, try a positive number\n");
  806. } else {
  807. command_opt = CMD_SETEXPECTED;
  808. }
  809. } else {
  810. fprintf(stderr, "You cannot change expected votes, corosync is not using votequorum\n");
  811. exit(EXIT_FAILURE);
  812. }
  813. break;
  814. case 'n':
  815. l = strtol(optarg, &endptr, 0);
  816. if ((l == 0 && endptr == optarg) || l < 0) {
  817. fprintf(stderr, "The nodeid was not valid, try a positive number\n");
  818. exit(EXIT_FAILURE);
  819. }
  820. nodeid = l;
  821. nodeid_set = 1;
  822. break;
  823. case 'v':
  824. if (using_votequorum() > 0) {
  825. votes = strtol(optarg, &endptr, 0);
  826. if ((votes == 0 && endptr == optarg) || votes < 0) {
  827. fprintf(stderr, "New votes value was not valid, try a positive number or zero\n");
  828. exit(EXIT_FAILURE);
  829. } else {
  830. command_opt = CMD_SETVOTES;
  831. }
  832. }
  833. else {
  834. fprintf(stderr, "You cannot change node votes, corosync is not using votequorum\n");
  835. exit(EXIT_FAILURE);
  836. }
  837. break;
  838. case 'o':
  839. sortchar = optarg[0];
  840. switch (sortchar) {
  841. case 'a': sort_opt = SORT_ADDR;
  842. break;
  843. case 'i': sort_opt = SORT_NODEID;
  844. break;
  845. case 'n': sort_opt = SORT_NODENAME;
  846. break;
  847. default:
  848. fprintf(stderr, "Invalid ordering option. valid orders are a(address), i(node ID) or n(name)\n");
  849. exit(EXIT_FAILURE);
  850. break;
  851. }
  852. break;
  853. case 'V':
  854. printf("corosync-quorumtool version: %s\n", VERSION);
  855. exit(0);
  856. case ':':
  857. case 'h':
  858. case '?':
  859. default:
  860. command_opt = CMD_UNKNOWN;
  861. break;
  862. }
  863. }
  864. switch (command_opt) {
  865. case CMD_UNKNOWN:
  866. show_usage(argv[0]);
  867. ret = EXIT_FAILURE;
  868. break;
  869. case CMD_SHOWNODES:
  870. ret = show_nodes(nodeid_format, address_format, sort_opt);
  871. break;
  872. case CMD_SHOWSTATUS:
  873. ret = show_status(nodeid_format, address_format, sort_opt);
  874. break;
  875. case CMD_SETVOTES:
  876. if (!nodeid_set) {
  877. nodeid = our_nodeid;
  878. }
  879. ret = set_votes(nodeid, votes);
  880. break;
  881. case CMD_SETEXPECTED:
  882. ret = set_expected(votes);
  883. break;
  884. case CMD_MONITOR:
  885. ret = monitor_status(nodeid_format, address_format, sort_opt);
  886. break;
  887. case CMD_UNREGISTER_QDEVICE:
  888. ret = unregister_qdevice();
  889. break;
  890. }
  891. close_all();
  892. return (ret);
  893. }