corosync-quorumtool.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  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: " CS_PRI_NODE_ID ": %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. assert(strlen(nodelist_name) < sizeof(buf));
  289. strcpy(buf, nodelist_name);
  290. bufptr = strlen(buf);
  291. }
  292. err = corosync_cfg_get_node_addrs(c_handle, nodeid, INTERFACE_MAX, &numaddrs, addrs);
  293. if (err != CS_OK) {
  294. fprintf(stderr, "Unable to get node address for nodeid " CS_PRI_NODE_ID ": %s\n", nodeid, cs_strerror(err));
  295. return "";
  296. }
  297. /* Don't show all addressess */
  298. if (!g_show_all_addrs) {
  299. numaddrs = 1;
  300. }
  301. for (i=start_addr; i<numaddrs; i++) {
  302. ss = (struct sockaddr_storage *)addrs[i].address;
  303. if (!ss->ss_family) {
  304. continue;
  305. }
  306. if (ss->ss_family == AF_INET6) {
  307. addrlen = sizeof(struct sockaddr_in6);
  308. } else {
  309. addrlen = sizeof(struct sockaddr_in);
  310. }
  311. if (i) {
  312. buf[bufptr++] = ',';
  313. buf[bufptr++] = ' ';
  314. }
  315. if (!getnameinfo(
  316. (struct sockaddr *)addrs[i].address, addrlen,
  317. buf+bufptr, sizeof(buf)-bufptr,
  318. NULL, 0,
  319. (name_format == ADDRESS_FORMAT_IP)?NI_NUMERICHOST:0)) {
  320. bufptr += strlen(buf+bufptr);
  321. }
  322. }
  323. return buf;
  324. }
  325. static void votequorum_notification_fn(
  326. votequorum_handle_t handle,
  327. uint64_t context,
  328. votequorum_ring_id_t ring_id,
  329. uint32_t node_list_entries,
  330. uint32_t node_list[])
  331. {
  332. g_ring_id_rep_node = ring_id.nodeid;
  333. g_vq_called = 1;
  334. }
  335. static void quorum_notification_fn(
  336. quorum_handle_t handle,
  337. uint32_t quorate,
  338. uint64_t ring_id,
  339. uint32_t view_list_entries,
  340. uint32_t *view_list)
  341. {
  342. int i;
  343. g_called = 1;
  344. g_quorate = quorate;
  345. g_ring_id = ring_id;
  346. g_view_list_entries = view_list_entries;
  347. if (g_view_list) {
  348. free(g_view_list);
  349. }
  350. g_view_list = malloc(sizeof(view_list_entry_t) * view_list_entries);
  351. if (g_view_list) {
  352. for (i=0; i< view_list_entries; i++) {
  353. g_view_list[i].node_id = view_list[i];
  354. g_view_list[i].name = NULL;
  355. g_view_list[i].vq_info = NULL;
  356. }
  357. }
  358. }
  359. static void print_string_padded(const char *buf)
  360. {
  361. int len, delta;
  362. len = strlen(buf);
  363. delta = 10 - len;
  364. while (delta > 0) {
  365. printf(" ");
  366. delta--;
  367. }
  368. printf("%s ", buf);
  369. }
  370. static void print_uint32_padded(uint32_t value)
  371. {
  372. char buf[12];
  373. snprintf(buf, sizeof(buf) - 1, "%u", value);
  374. print_string_padded(buf);
  375. }
  376. /* for qsort */
  377. static int compare_nodeids(const void *one, const void *two)
  378. {
  379. const view_list_entry_t *info1 = one;
  380. const view_list_entry_t *info2 = two;
  381. if (info1->node_id == info2->node_id) {
  382. return 0;
  383. }
  384. if (info1->node_id > info2->node_id) {
  385. return 1;
  386. }
  387. return -1;
  388. }
  389. static int compare_nodenames(const void *one, const void *two)
  390. {
  391. const view_list_entry_t *info1 = one;
  392. const view_list_entry_t *info2 = two;
  393. return strcmp(info1->name, info2->name);
  394. }
  395. static void display_nodes_data(nodeid_format_t nodeid_format, name_format_t name_format, sorttype_t sort_type)
  396. {
  397. int i, display_qdevice = 0;
  398. unsigned int our_flags = 0;
  399. struct votequorum_info info[g_view_list_entries];
  400. /*
  401. * cache node info because we need to parse them twice
  402. */
  403. if (v_handle) {
  404. for (i=0; i < g_view_list_entries; i++) {
  405. if (votequorum_getinfo(v_handle, g_view_list[i].node_id, &info[i]) != CS_OK) {
  406. printf("Unable to get node " CS_PRI_NODE_ID " info\n", g_view_list[i].node_id);
  407. }
  408. g_view_list[i].vq_info = &info[i];
  409. if (info[i].flags & VOTEQUORUM_INFO_QDEVICE_REGISTERED) {
  410. display_qdevice = 1;
  411. }
  412. }
  413. }
  414. /*
  415. * Get node names
  416. */
  417. for (i=0; i < g_view_list_entries; i++) {
  418. g_view_list[i].name = strdup(node_name(g_view_list[i].node_id, name_format));
  419. }
  420. printf("\nMembership information\n");
  421. printf("----------------------\n");
  422. print_string_padded("Nodeid");
  423. if (v_handle) {
  424. print_string_padded("Votes");
  425. if ((display_qdevice) || (machine_parsable)) {
  426. print_string_padded("Qdevice");
  427. }
  428. }
  429. printf("Name\n");
  430. /* corosync sends them already sorted by address */
  431. if (sort_type == SORT_NODEID) {
  432. qsort(g_view_list, g_view_list_entries, sizeof(view_list_entry_t), compare_nodeids);
  433. }
  434. if (sort_type == SORT_NODENAME) {
  435. qsort(g_view_list, g_view_list_entries, sizeof(view_list_entry_t), compare_nodenames);
  436. }
  437. for (i=0; i < g_view_list_entries; i++) {
  438. if (nodeid_format == NODEID_FORMAT_DECIMAL) {
  439. print_uint32_padded(g_view_list[i].node_id);
  440. } else {
  441. printf("0x%08x ", g_view_list[i].node_id);
  442. }
  443. if (v_handle) {
  444. int votes = -1;
  445. votes = info[i].node_votes;
  446. print_uint32_padded(votes);
  447. if ((display_qdevice) || (machine_parsable)) {
  448. if (info[i].flags & VOTEQUORUM_INFO_QDEVICE_REGISTERED) {
  449. char buf[10];
  450. snprintf(buf, sizeof(buf),
  451. "%s,%s,%s",
  452. info[i].flags & VOTEQUORUM_INFO_QDEVICE_ALIVE?"A":"NA",
  453. info[i].flags & VOTEQUORUM_INFO_QDEVICE_CAST_VOTE?"V":"NV",
  454. info[i].flags & VOTEQUORUM_INFO_QDEVICE_MASTER_WINS?"MW":"NMW");
  455. print_string_padded(buf);
  456. } else {
  457. print_string_padded("NR");
  458. }
  459. }
  460. }
  461. printf("%s", g_view_list[i].name);
  462. if (g_view_list[i].node_id == our_nodeid) {
  463. printf(" (local)");
  464. if (v_handle) {
  465. our_flags = info[i].flags;
  466. }
  467. }
  468. printf("\n");
  469. }
  470. if (g_view_list_entries) {
  471. for (i=0; i < g_view_list_entries; i++) {
  472. free(g_view_list[i].name);
  473. }
  474. free(g_view_list);
  475. g_view_list = NULL;
  476. }
  477. if (display_qdevice) {
  478. if (nodeid_format == NODEID_FORMAT_DECIMAL) {
  479. print_uint32_padded(VOTEQUORUM_QDEVICE_NODEID);
  480. } else {
  481. printf("0x%08x ", VOTEQUORUM_QDEVICE_NODEID);
  482. }
  483. /* If the quorum device is inactive on this node then show votes as 0
  484. so that the display is not confusing */
  485. if (our_flags & VOTEQUORUM_INFO_QDEVICE_CAST_VOTE) {
  486. print_uint32_padded(info[0].qdevice_votes);
  487. }
  488. else {
  489. print_uint32_padded(0);
  490. }
  491. printf(" %s", info[0].qdevice_name);
  492. if (our_flags & VOTEQUORUM_INFO_QDEVICE_CAST_VOTE) {
  493. printf("\n");
  494. }
  495. else {
  496. printf(" (votes %d)\n", info[0].qdevice_votes);
  497. }
  498. }
  499. }
  500. static int display_quorum_data(int is_quorate,
  501. nodeid_format_t nodeid_format, name_format_t name_format, sorttype_t sort_type,
  502. int loop)
  503. {
  504. struct votequorum_info info;
  505. int err;
  506. char quorumtype[256];
  507. time_t t;
  508. memset(quorumtype, 0, sizeof(quorumtype));
  509. printf("Quorum information\n");
  510. printf("------------------\n");
  511. time(&t);
  512. printf("Date: %s", ctime((const time_t *)&t));
  513. if (get_quorum_type(quorumtype, sizeof(quorumtype))) {
  514. strncpy(quorumtype, "Not configured", sizeof(quorumtype) - 1);
  515. }
  516. printf("Quorum provider: %s\n", quorumtype);
  517. printf("Nodes: %d\n", g_view_list_entries);
  518. if (nodeid_format == NODEID_FORMAT_DECIMAL) {
  519. printf("Node ID: " CS_PRI_NODE_ID "\n", our_nodeid);
  520. } else {
  521. printf("Node ID: 0x%08x\n", our_nodeid);
  522. }
  523. if (v_handle) {
  524. printf("Ring ID: " CS_PRI_RING_ID "\n", g_ring_id_rep_node, g_ring_id);
  525. }
  526. else {
  527. printf("Ring ID: " CS_PRI_RING_ID_SEQ "\n", g_ring_id);
  528. }
  529. printf("Quorate: %s\n", is_quorate?"Yes":"No");
  530. if (!v_handle) {
  531. return CS_OK;
  532. }
  533. err=votequorum_getinfo(v_handle, our_nodeid, &info);
  534. if ((err == CS_OK) || (err == CS_ERR_NOT_EXIST)) {
  535. printf("\nVotequorum information\n");
  536. printf("----------------------\n");
  537. printf("Expected votes: %d\n", info.node_expected_votes);
  538. printf("Highest expected: %d\n", info.highest_expected);
  539. printf("Total votes: %d\n", info.total_votes);
  540. printf("Quorum: %d %s\n", info.quorum, info.flags & VOTEQUORUM_INFO_QUORATE?" ":"Activity blocked");
  541. printf("Flags: ");
  542. if (info.flags & VOTEQUORUM_INFO_TWONODE) printf("2Node ");
  543. if (info.flags & VOTEQUORUM_INFO_QUORATE) printf("Quorate ");
  544. if (info.flags & VOTEQUORUM_INFO_WAIT_FOR_ALL) printf("WaitForAll ");
  545. if (info.flags & VOTEQUORUM_INFO_LAST_MAN_STANDING) printf("LastManStanding ");
  546. if (info.flags & VOTEQUORUM_INFO_AUTO_TIE_BREAKER) printf("AutoTieBreaker ");
  547. if (info.flags & VOTEQUORUM_INFO_ALLOW_DOWNSCALE) printf("AllowDownscale ");
  548. if (info.flags & VOTEQUORUM_INFO_QDEVICE_REGISTERED) printf("Qdevice ");
  549. printf("\n");
  550. } else {
  551. fprintf(stderr, "Unable to get node info: %s\n", cs_strerror(err));
  552. }
  553. display_nodes_data(nodeid_format, name_format, sort_type);
  554. return err;
  555. }
  556. /*
  557. * return EXIT_SUCCESS if quorate
  558. * EXIT_NOT_QUORATE if not quorate
  559. * EXIT_FAILURE on error
  560. */
  561. static int show_status(nodeid_format_t nodeid_format, name_format_t name_format, sorttype_t sort_type)
  562. {
  563. int is_quorate;
  564. int err;
  565. err=quorum_getquorate(q_handle, &is_quorate);
  566. if (err != CS_OK) {
  567. fprintf(stderr, "Unable to get cluster quorate status: %s\n", cs_strerror(err));
  568. goto quorum_err;
  569. }
  570. err=quorum_trackstart(q_handle, CS_TRACK_CURRENT);
  571. if (err != CS_OK) {
  572. fprintf(stderr, "Unable to start quorum status tracking: %s\n", cs_strerror(err));
  573. goto quorum_err;
  574. }
  575. g_called = 0;
  576. while (g_called == 0 && err == CS_OK) {
  577. err = quorum_dispatch(q_handle, CS_DISPATCH_ONE);
  578. if (err != CS_OK) {
  579. fprintf(stderr, "Unable to dispatch quorum status: %s\n", cs_strerror(err));
  580. }
  581. }
  582. if (quorum_trackstop(q_handle) != CS_OK) {
  583. fprintf(stderr, "Unable to stop quorum status tracking: %s\n", cs_strerror(err));
  584. }
  585. if (using_votequorum()) {
  586. if ( (err=votequorum_trackstart(v_handle, 0LL, CS_TRACK_CURRENT)) != CS_OK) {
  587. fprintf(stderr, "Unable to start votequorum status tracking: %s\n", cs_strerror(err));
  588. goto quorum_err;
  589. }
  590. g_vq_called = 0;
  591. while (g_vq_called == 0 && err == CS_OK) {
  592. err = votequorum_dispatch(v_handle, CS_DISPATCH_ONE);
  593. if (err != CS_OK) {
  594. fprintf(stderr, "Unable to dispatch votequorum status: %s\n", cs_strerror(err));
  595. }
  596. }
  597. }
  598. quorum_err:
  599. if (err != CS_OK) {
  600. return EXIT_FAILURE;
  601. }
  602. err = display_quorum_data(is_quorate, nodeid_format, name_format, sort_type, 0);
  603. if (err != CS_OK) {
  604. return EXIT_FAILURE;
  605. }
  606. return (is_quorate ? EXIT_SUCCESS : EXIT_NOT_QUORATE);
  607. }
  608. static int monitor_status(nodeid_format_t nodeid_format, name_format_t name_format, sorttype_t sort_type) {
  609. int err;
  610. int loop = 0;
  611. if (q_type == QUORUM_FREE) {
  612. printf("\nQuorum is not configured - cannot monitor\n");
  613. return show_status(nodeid_format, name_format, sort_type);
  614. }
  615. err=quorum_trackstart(q_handle, CS_TRACK_CHANGES);
  616. if (err != CS_OK) {
  617. fprintf(stderr, "Unable to start quorum status tracking: %s\n", cs_strerror(err));
  618. goto quorum_err;
  619. }
  620. if (using_votequorum()) {
  621. if ( (err=votequorum_trackstart(v_handle, 0LL, CS_TRACK_CHANGES)) != CS_OK) {
  622. fprintf(stderr, "Unable to start votequorum status tracking: %s\n", cs_strerror(err));
  623. goto quorum_err;
  624. }
  625. }
  626. while (1) {
  627. err = quorum_dispatch(q_handle, CS_DISPATCH_ONE);
  628. if (err != CS_OK) {
  629. fprintf(stderr, "Unable to dispatch quorum status: %s\n", cs_strerror(err));
  630. goto quorum_err;
  631. }
  632. if (using_votequorum()) {
  633. g_vq_called = 0;
  634. while (!g_vq_called) {
  635. err = votequorum_dispatch(v_handle, CS_DISPATCH_ONE);
  636. if (err != CS_OK) {
  637. fprintf(stderr, "Unable to dispatch votequorum status: %s\n", cs_strerror(err));
  638. goto quorum_err;
  639. }
  640. }
  641. }
  642. err = display_quorum_data(g_quorate, nodeid_format, name_format, sort_type, loop);
  643. printf("\n");
  644. loop = 1;
  645. if (err != CS_OK) {
  646. fprintf(stderr, "Unable to display quorum data: %s\n", cs_strerror(err));
  647. goto quorum_err;
  648. }
  649. }
  650. quorum_err:
  651. return EXIT_FAILURE;
  652. }
  653. static int show_nodes(nodeid_format_t nodeid_format, name_format_t name_format, sorttype_t sort_type)
  654. {
  655. int err;
  656. int result = EXIT_FAILURE;
  657. err = quorum_trackstart(q_handle, CS_TRACK_CURRENT);
  658. if (err != CS_OK) {
  659. fprintf(stderr, "Unable to start quorum status tracking: %s\n", cs_strerror(err));
  660. goto err_exit;
  661. }
  662. g_called = 0;
  663. while (g_called == 0) {
  664. err = quorum_dispatch(q_handle, CS_DISPATCH_ONE);
  665. if (err != CS_OK) {
  666. fprintf(stderr, "Unable to dispatch quorum status: %s\n", cs_strerror(err));
  667. goto err_exit;
  668. }
  669. }
  670. display_nodes_data(nodeid_format, name_format, sort_type);
  671. result = EXIT_SUCCESS;
  672. err_exit:
  673. return result;
  674. }
  675. static int unregister_qdevice(void)
  676. {
  677. int err;
  678. struct votequorum_info info;
  679. int result;
  680. result = EXIT_FAILURE;
  681. err = votequorum_getinfo(v_handle, our_nodeid, &info);
  682. if (err != CS_OK) {
  683. fprintf(stderr, "Unable to get quorum device info: %s\n", cs_strerror(err));
  684. goto err_exit;
  685. }
  686. if (!(info.flags & VOTEQUORUM_INFO_QDEVICE_REGISTERED)) {
  687. result = EXIT_SUCCESS;
  688. goto err_exit;
  689. }
  690. err = votequorum_qdevice_unregister(v_handle, info.qdevice_name);
  691. if (err != CS_OK) {
  692. fprintf(stderr, "Unable to unregister quorum device: %s\n", cs_strerror(err));
  693. goto err_exit;
  694. }
  695. result = EXIT_SUCCESS;
  696. err_exit:
  697. return result;
  698. }
  699. /*
  700. * return -1 on error
  701. * 0 if OK
  702. */
  703. static int init_all(void) {
  704. cmap_handle = 0;
  705. q_handle = 0;
  706. v_handle = 0;
  707. c_handle = 0;
  708. if (cmap_initialize(&cmap_handle) != CS_OK) {
  709. fprintf(stderr, "Cannot initialize CMAP service\n");
  710. cmap_handle = 0;
  711. goto out;
  712. }
  713. if (quorum_initialize(&q_handle, &q_callbacks, &q_type) != CS_OK) {
  714. fprintf(stderr, "Cannot initialize QUORUM service\n");
  715. q_handle = 0;
  716. goto out;
  717. }
  718. if (corosync_cfg_initialize(&c_handle, &c_callbacks) != CS_OK) {
  719. fprintf(stderr, "Cannot initialise CFG service\n");
  720. c_handle = 0;
  721. goto out;
  722. }
  723. if (using_votequorum() <= 0) {
  724. return 0;
  725. }
  726. if (votequorum_initialize(&v_handle, &v_callbacks) != CS_OK) {
  727. fprintf(stderr, "Cannot initialise VOTEQUORUM service\n");
  728. v_handle = 0;
  729. goto out;
  730. }
  731. if (cmap_get_uint32(cmap_handle, "runtime.votequorum.this_node_id", &our_nodeid) != CS_OK) {
  732. fprintf(stderr, "Unable to retrieve this_node_id\n");
  733. goto out;
  734. }
  735. return 0;
  736. out:
  737. return -1;
  738. }
  739. static void close_all(void) {
  740. if (cmap_handle) {
  741. cmap_finalize(cmap_handle);
  742. }
  743. if (q_handle) {
  744. quorum_finalize(q_handle);
  745. }
  746. if (c_handle) {
  747. corosync_cfg_finalize(c_handle);
  748. }
  749. if (v_handle) {
  750. votequorum_finalize(v_handle);
  751. }
  752. }
  753. int main (int argc, char *argv[]) {
  754. const char *options = "VHaslpmfe:v:hin:o:";
  755. char *endptr;
  756. int opt;
  757. int votes = 0;
  758. int ret = 0;
  759. uint32_t nodeid = 0;
  760. uint32_t nodeid_set = 0;
  761. nodeid_format_t nodeid_format = NODEID_FORMAT_DECIMAL;
  762. name_format_t address_format = ADDRESS_FORMAT_NAME;
  763. command_t command_opt = CMD_SHOWSTATUS;
  764. sorttype_t sort_opt = SORT_ADDR;
  765. char sortchar;
  766. long int l;
  767. if (init_all()) {
  768. close_all();
  769. exit(EXIT_FAILURE);
  770. }
  771. while ( (opt = getopt(argc, argv, options)) != -1 ) {
  772. switch (opt) {
  773. case 'f':
  774. if (using_votequorum() > 0) {
  775. command_opt = CMD_UNREGISTER_QDEVICE;
  776. } else {
  777. fprintf(stderr, "You cannot unregister quorum device, corosync is not using votequorum\n");
  778. exit(EXIT_FAILURE);
  779. }
  780. break;
  781. case 's':
  782. command_opt = CMD_SHOWSTATUS;
  783. break;
  784. case 'a':
  785. g_show_all_addrs = 1;
  786. break;
  787. case 'm':
  788. command_opt = CMD_MONITOR;
  789. break;
  790. case 'i':
  791. address_format = ADDRESS_FORMAT_IP;
  792. break;
  793. case 'H':
  794. nodeid_format = NODEID_FORMAT_HEX;
  795. break;
  796. case 'l':
  797. command_opt = CMD_SHOWNODES;
  798. break;
  799. case 'p':
  800. machine_parsable = 1;
  801. break;
  802. case 'e':
  803. if (using_votequorum() > 0) {
  804. votes = strtol(optarg, &endptr, 0);
  805. if ((votes == 0 && endptr == optarg) || votes <= 0) {
  806. fprintf(stderr, "New expected votes value was not valid, try a positive number\n");
  807. } else {
  808. command_opt = CMD_SETEXPECTED;
  809. }
  810. } else {
  811. fprintf(stderr, "You cannot change expected votes, corosync is not using votequorum\n");
  812. exit(EXIT_FAILURE);
  813. }
  814. break;
  815. case 'n':
  816. l = strtol(optarg, &endptr, 0);
  817. if ((l == 0 && endptr == optarg) || l < 0) {
  818. fprintf(stderr, "The nodeid was not valid, try a positive number\n");
  819. exit(EXIT_FAILURE);
  820. }
  821. nodeid = l;
  822. nodeid_set = 1;
  823. break;
  824. case 'v':
  825. if (using_votequorum() > 0) {
  826. votes = strtol(optarg, &endptr, 0);
  827. if ((votes == 0 && endptr == optarg) || votes < 0) {
  828. fprintf(stderr, "New votes value was not valid, try a positive number or zero\n");
  829. exit(EXIT_FAILURE);
  830. } else {
  831. command_opt = CMD_SETVOTES;
  832. }
  833. }
  834. else {
  835. fprintf(stderr, "You cannot change node votes, corosync is not using votequorum\n");
  836. exit(EXIT_FAILURE);
  837. }
  838. break;
  839. case 'o':
  840. sortchar = optarg[0];
  841. switch (sortchar) {
  842. case 'a': sort_opt = SORT_ADDR;
  843. break;
  844. case 'i': sort_opt = SORT_NODEID;
  845. break;
  846. case 'n': sort_opt = SORT_NODENAME;
  847. break;
  848. default:
  849. fprintf(stderr, "Invalid ordering option. valid orders are a(address), i(node ID) or n(name)\n");
  850. exit(EXIT_FAILURE);
  851. break;
  852. }
  853. break;
  854. case 'V':
  855. printf("corosync-quorumtool version: %s\n", VERSION);
  856. exit(0);
  857. case ':':
  858. case 'h':
  859. case '?':
  860. default:
  861. command_opt = CMD_UNKNOWN;
  862. break;
  863. }
  864. }
  865. switch (command_opt) {
  866. case CMD_UNKNOWN:
  867. show_usage(argv[0]);
  868. ret = EXIT_FAILURE;
  869. break;
  870. case CMD_SHOWNODES:
  871. ret = show_nodes(nodeid_format, address_format, sort_opt);
  872. break;
  873. case CMD_SHOWSTATUS:
  874. ret = show_status(nodeid_format, address_format, sort_opt);
  875. break;
  876. case CMD_SETVOTES:
  877. if (!nodeid_set) {
  878. nodeid = our_nodeid;
  879. }
  880. ret = set_votes(nodeid, votes);
  881. break;
  882. case CMD_SETEXPECTED:
  883. ret = set_expected(votes);
  884. break;
  885. case CMD_MONITOR:
  886. ret = monitor_status(nodeid_format, address_format, sort_opt);
  887. break;
  888. case CMD_UNREGISTER_QDEVICE:
  889. ret = unregister_qdevice();
  890. break;
  891. }
  892. close_all();
  893. return (ret);
  894. }