corosync-quorumtool.c 22 KB

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