corosync-quorumtool.c 25 KB

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