corosync-quorumtool.c 24 KB

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