corosync-quorumtool.c 25 KB

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