corosync-quorumtool.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213
  1. /*
  2. * Copyright (c) 2009-2020 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Authors: Christine Caulfield <ccaulfie@redhat.com>
  7. * Fabio M. Di Nitto (fdinitto@redhat.com)
  8. *
  9. * This software licensed under BSD license, the text of which follows:
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * - Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * - Neither the name of the Red Hat Inc. nor the names of its
  20. * contributors may be used to endorse or promote products derived from this
  21. * software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  33. * THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. #include <config.h>
  36. #include <ctype.h>
  37. #include <stdbool.h>
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #include <sys/types.h>
  42. #include <sys/socket.h>
  43. #include <netdb.h>
  44. #include <limits.h>
  45. #include <corosync/totem/totem.h>
  46. #include <corosync/cfg.h>
  47. #include <corosync/cmap.h>
  48. #include <corosync/quorum.h>
  49. #include <corosync/votequorum.h>
  50. #include "util.h"
  51. typedef enum {
  52. NODEID_FORMAT_DECIMAL,
  53. NODEID_FORMAT_HEX
  54. } nodeid_format_t;
  55. typedef enum {
  56. ADDRESS_FORMAT_NAME,
  57. ADDRESS_FORMAT_IP
  58. } name_format_t;
  59. typedef enum {
  60. CMD_SHOWNODES,
  61. CMD_SHOWSTATUS,
  62. CMD_SETVOTES,
  63. CMD_SETEXPECTED,
  64. CMD_MONITOR,
  65. CMD_WATCH_NOTIFICATIONS,
  66. CMD_UNREGISTER_QDEVICE,
  67. CMD_SET_EXTRA_INFO
  68. } command_t;
  69. typedef enum {
  70. SORT_ADDR,
  71. SORT_NODEID,
  72. SORT_NODENAME
  73. } sorttype_t;
  74. typedef enum {
  75. WATCH_NONE = 0,
  76. WATCH_QUORUM = (1 << 0),
  77. WATCH_EXTRA_INFO = (1 << 1),
  78. } notification_watchtype_t;
  79. #define EXIT_NOT_QUORATE 2
  80. /*
  81. * global vars
  82. */
  83. /*
  84. * cmap bits
  85. */
  86. static cmap_handle_t cmap_handle;
  87. /*
  88. * quorum bits
  89. */
  90. static void quorum_notification_fn(
  91. quorum_handle_t handle,
  92. uint32_t quorate,
  93. uint64_t ring_id,
  94. uint32_t view_list_entries,
  95. uint32_t *view_list);
  96. static quorum_handle_t q_handle;
  97. static uint32_t q_type;
  98. static quorum_callbacks_t q_callbacks = {
  99. .quorum_notify_fn = quorum_notification_fn
  100. };
  101. /*
  102. * quorum call back vars
  103. */
  104. /* Containing struct to keep votequorum & normal quorum bits together */
  105. typedef struct {
  106. struct votequorum_info *vq_info; /* Might be NULL if votequorum not present */
  107. char *name; /* Might be IP address or NULL */
  108. int node_id; /* Always present */
  109. } view_list_entry_t;
  110. static view_list_entry_t *g_view_list;
  111. static uint32_t g_quorate;
  112. static uint64_t g_ring_id;
  113. static uint32_t g_ring_id_rep_node;
  114. static uint32_t g_view_list_entries;
  115. static uint32_t g_called;
  116. static uint32_t g_vq_called;
  117. static uint32_t g_show_all_addrs = 0;
  118. static notification_watchtype_t g_watch_notifications = WATCH_NONE;
  119. /*
  120. * votequorum bits
  121. */
  122. static void votequorum_notification_fn(
  123. votequorum_handle_t handle,
  124. uint64_t context,
  125. votequorum_ring_id_t ring_id,
  126. uint32_t node_list_entries,
  127. uint32_t node_list[]);
  128. static void votequorum_qdevice_extra_info_notification_fn(
  129. votequorum_handle_t handle,
  130. uint64_t context,
  131. uint32_t nodeid,
  132. uint32_t ei_size,
  133. void *extra_info);
  134. static votequorum_handle_t v_handle;
  135. static votequorum_model_v1_data_t v_callbacks = {
  136. .model = VOTEQUORUM_MODEL_V1,
  137. .votequorum_quorum_notify_fn = NULL,
  138. .votequorum_expectedvotes_notify_fn = NULL,
  139. .votequorum_nodelist_notify_fn = votequorum_notification_fn,
  140. .votequorum_qdevice_extra_info_fn = votequorum_qdevice_extra_info_notification_fn,
  141. };
  142. static uint32_t our_nodeid = 0;
  143. /*
  144. * cfg bits
  145. */
  146. static corosync_cfg_handle_t c_handle;
  147. static corosync_cfg_callbacks_t c_callbacks = {
  148. .corosync_cfg_shutdown_callback = NULL
  149. };
  150. /*
  151. * global
  152. */
  153. static int machine_parsable = 0;
  154. static void show_usage(const char *name)
  155. {
  156. printf("usage: \n");
  157. printf("%s <options>\n", name);
  158. printf("\n");
  159. printf(" options:\n");
  160. printf("\n");
  161. printf(" -s show quorum status\n");
  162. printf(" -m constantly monitor quorum status\n");
  163. printf(" -l list nodes\n");
  164. printf(" -a show all names or addresses for each node\n");
  165. printf(" -p when used with -s or -l, generates machine parsable output\n");
  166. printf(" -v <votes> change the number of votes for a node (*)\n");
  167. printf(" -n <nodeid> optional nodeid of node for -v\n");
  168. printf(" -e <expected> change expected votes for the cluster (*)\n");
  169. printf(" -H show nodeids in hexadecimal rather than decimal\n");
  170. printf(" -i show node IP addresses instead of the resolved name\n");
  171. printf(" -o <a|n|i> order by [a] IP address (default), [n] name, [i] nodeid\n");
  172. printf(" -f forcefully unregister a quorum device *DANGEROUS* (*)\n");
  173. printf(" -W <q|x|qx> watch for update messages. q quorum status, x extra info (votequorum only)\n");
  174. printf(" -X <string> set qdevice extra info on the node running the command (votequorum only)\n");
  175. printf(" -C clear qdevice extra info on the node running the command (votequorum only)\n");
  176. printf(" -h show this help text\n");
  177. printf(" -V show version and exit\n");
  178. printf("\n");
  179. printf(" (*) Starred items only work if votequorum is the quorum provider for corosync\n");
  180. printf("\n");
  181. }
  182. static int get_quorum_type(char *quorum_type, size_t quorum_type_len)
  183. {
  184. int err;
  185. char *str = NULL;
  186. if ((!quorum_type) || (quorum_type_len <= 0)) {
  187. return -1;
  188. }
  189. if (q_type == QUORUM_FREE) {
  190. return -1;
  191. }
  192. if ((err = cmap_get_string(cmap_handle, "quorum.provider", &str)) != CS_OK) {
  193. goto out;
  194. }
  195. if (!str) {
  196. return -1;
  197. }
  198. strncpy(quorum_type, str, quorum_type_len - 1);
  199. free(str);
  200. return 0;
  201. out:
  202. return err;
  203. }
  204. /*
  205. * Returns 1 if 'votequorum' is active. The called then knows that
  206. * votequorum calls should work and can provide extra information
  207. */
  208. static int using_votequorum(void)
  209. {
  210. char quorumtype[256];
  211. int using_voteq;
  212. memset(quorumtype, 0, sizeof(quorumtype));
  213. if (get_quorum_type(quorumtype, sizeof(quorumtype))) {
  214. return -1;
  215. }
  216. if (strcmp(quorumtype, "corosync_votequorum") == 0) {
  217. using_voteq = 1;
  218. } else {
  219. using_voteq = 0;
  220. }
  221. return using_voteq;
  222. }
  223. static int set_votes(uint32_t nodeid, int votes)
  224. {
  225. int err;
  226. if ((err=votequorum_setvotes(v_handle, nodeid, votes)) != CS_OK) {
  227. fprintf(stderr, "Unable to set votes %d for nodeid: " CS_PRI_NODE_ID ": %s\n",
  228. votes, nodeid, cs_strerror(err));
  229. }
  230. return (err == CS_OK ? EXIT_SUCCESS : EXIT_FAILURE);
  231. }
  232. static int set_expected(int expected_votes)
  233. {
  234. int err;
  235. if ((err=votequorum_setexpected(v_handle, expected_votes)) != CS_OK) {
  236. fprintf(stderr, "Unable to set expected votes: %s\n", cs_strerror(err));
  237. }
  238. return (err == CS_OK ? EXIT_SUCCESS : EXIT_FAILURE);
  239. }
  240. /*
  241. * node name by nodelist
  242. */
  243. static const char *node_name_by_nodelist(uint32_t nodeid)
  244. {
  245. cmap_iter_handle_t iter;
  246. char key_name[CMAP_KEYNAME_MAXLEN + 1];
  247. char tmp_key[CMAP_KEYNAME_MAXLEN + 1];
  248. static char ret_buf[_POSIX_HOST_NAME_MAX];
  249. char *str = NULL;
  250. uint32_t node_pos, cur_nodeid;
  251. int res = 0;
  252. if (cmap_iter_init(cmap_handle, "nodelist.node.", &iter) != CS_OK) {
  253. return "";
  254. }
  255. memset(ret_buf, 0, sizeof(ret_buf));
  256. while ((cmap_iter_next(cmap_handle, iter, key_name, NULL, NULL)) == CS_OK) {
  257. res = sscanf(key_name, "nodelist.node.%u.%s", &node_pos, tmp_key);
  258. if (res != 2) {
  259. continue;
  260. }
  261. if (strcmp(tmp_key, "nodeid") != 0) {
  262. continue;
  263. }
  264. snprintf(tmp_key, CMAP_KEYNAME_MAXLEN, "nodelist.node.%u.nodeid", node_pos);
  265. if (cmap_get_uint32(cmap_handle, tmp_key, &cur_nodeid) != CS_OK) {
  266. continue;
  267. }
  268. if (cur_nodeid != nodeid) {
  269. continue;
  270. }
  271. snprintf(tmp_key, CMAP_KEYNAME_MAXLEN, "nodelist.node.%u.name", node_pos);
  272. if (cmap_get_string(cmap_handle, tmp_key, &str) != CS_OK) {
  273. continue;
  274. }
  275. if (!str) {
  276. continue;
  277. }
  278. strncpy(ret_buf, str, sizeof(ret_buf) - 1);
  279. free(str);
  280. break;
  281. }
  282. cmap_iter_finalize(cmap_handle, iter);
  283. return ret_buf;
  284. }
  285. /*
  286. * This resolves the first address assigned to a node
  287. * and returns the name or IP address. Use cfgtool if you need more information.
  288. */
  289. static const char *node_name(uint32_t nodeid, name_format_t name_format)
  290. {
  291. int err;
  292. int numaddrs;
  293. corosync_cfg_node_address_t addrs[INTERFACE_MAX];
  294. static char buf[(INET6_ADDRSTRLEN + 1) * KNET_MAX_LINK];
  295. const char *nodelist_name = NULL;
  296. socklen_t addrlen;
  297. struct sockaddr_storage *ss;
  298. int start_addr = 0;
  299. int i;
  300. int bufptr = 0;
  301. buf[0] = '\0';
  302. /* If a name is required, always look for the nodelist node0_addr name first */
  303. if (name_format == ADDRESS_FORMAT_NAME) {
  304. nodelist_name = node_name_by_nodelist(nodeid);
  305. }
  306. if ((nodelist_name) &&
  307. (strlen(nodelist_name) > 0)) {
  308. start_addr = 1;
  309. assert(strlen(nodelist_name) < sizeof(buf));
  310. strcpy(buf, nodelist_name);
  311. bufptr = strlen(buf);
  312. }
  313. err = corosync_cfg_get_node_addrs(c_handle, nodeid, INTERFACE_MAX, &numaddrs, addrs);
  314. if (err != CS_OK) {
  315. fprintf(stderr, "Unable to get node address for nodeid " CS_PRI_NODE_ID ": %s\n", nodeid, cs_strerror(err));
  316. return "";
  317. }
  318. /* Don't show all addressess */
  319. if (!g_show_all_addrs) {
  320. numaddrs = 1;
  321. }
  322. for (i=start_addr; i<numaddrs; i++) {
  323. ss = (struct sockaddr_storage *)addrs[i].address;
  324. if (!ss->ss_family) {
  325. continue;
  326. }
  327. if (ss->ss_family == AF_INET6) {
  328. addrlen = sizeof(struct sockaddr_in6);
  329. } else {
  330. addrlen = sizeof(struct sockaddr_in);
  331. }
  332. if (i) {
  333. buf[bufptr++] = ',';
  334. buf[bufptr++] = ' ';
  335. }
  336. if (!getnameinfo(
  337. (struct sockaddr *)addrs[i].address, addrlen,
  338. buf+bufptr, sizeof(buf)-bufptr,
  339. NULL, 0,
  340. (name_format == ADDRESS_FORMAT_IP)?NI_NUMERICHOST:0)) {
  341. bufptr += strlen(buf+bufptr);
  342. }
  343. }
  344. return buf;
  345. }
  346. static void print_extra_info(uint32_t ei_size, const char *extra_info);
  347. static void votequorum_qdevice_extra_info_notification_fn(
  348. votequorum_handle_t handle,
  349. uint64_t context,
  350. uint32_t nodeid,
  351. uint32_t ei_size,
  352. void *extra_info)
  353. {
  354. if (g_watch_notifications & WATCH_EXTRA_INFO) {
  355. printf(CS_PRI_NODE_ID_PADDED " ", nodeid);
  356. print_extra_info(ei_size, extra_info);
  357. printf("\n");
  358. }
  359. }
  360. static void votequorum_notification_fn(
  361. votequorum_handle_t handle,
  362. uint64_t context,
  363. votequorum_ring_id_t ring_id,
  364. uint32_t node_list_entries,
  365. uint32_t node_list[])
  366. {
  367. g_ring_id_rep_node = ring_id.nodeid;
  368. g_vq_called = 1;
  369. }
  370. static void quorum_notification_fn(
  371. quorum_handle_t handle,
  372. uint32_t quorate,
  373. uint64_t ring_id,
  374. uint32_t view_list_entries,
  375. uint32_t *view_list)
  376. {
  377. int i;
  378. g_called = 1;
  379. g_quorate = quorate;
  380. g_ring_id = ring_id;
  381. g_view_list_entries = view_list_entries;
  382. if (g_view_list) {
  383. free(g_view_list);
  384. }
  385. g_view_list = malloc(sizeof(view_list_entry_t) * view_list_entries);
  386. if (g_view_list) {
  387. for (i=0; i< view_list_entries; i++) {
  388. g_view_list[i].node_id = view_list[i];
  389. g_view_list[i].name = NULL;
  390. g_view_list[i].vq_info = NULL;
  391. }
  392. }
  393. if(g_watch_notifications & WATCH_QUORUM) {
  394. printf("%s\n", (quorate?"Quorate":"Not quorate"));
  395. }
  396. }
  397. static void print_string_padded(const char *buf)
  398. {
  399. int len, delta;
  400. len = strlen(buf);
  401. delta = 10 - len;
  402. while (delta > 0) {
  403. printf(" ");
  404. delta--;
  405. }
  406. printf("%s ", buf);
  407. }
  408. static void print_uint32_padded(uint32_t value)
  409. {
  410. char buf[12];
  411. snprintf(buf, sizeof(buf) - 1, "%u", value);
  412. print_string_padded(buf);
  413. }
  414. /* for qsort */
  415. static int compare_nodeids(const void *one, const void *two)
  416. {
  417. const view_list_entry_t *info1 = one;
  418. const view_list_entry_t *info2 = two;
  419. if (info1->node_id == info2->node_id) {
  420. return 0;
  421. }
  422. if (info1->node_id > info2->node_id) {
  423. return 1;
  424. }
  425. return -1;
  426. }
  427. static int compare_nodenames(const void *one, const void *two)
  428. {
  429. const view_list_entry_t *info1 = one;
  430. const view_list_entry_t *info2 = two;
  431. return strcmp(info1->name, info2->name);
  432. }
  433. static void print_extra_info(uint32_t ei_size, const char *extra_info)
  434. {
  435. if (ei_size == 0) {
  436. printf("<unset>");
  437. return;
  438. }
  439. bool is_printable = extra_info[ei_size-1] == '\0';
  440. for (uint32_t i = 0; i < ei_size - 1; i++) {
  441. if(!isprint((unsigned char)extra_info[i])) {
  442. is_printable = false;
  443. }
  444. }
  445. if (is_printable) {
  446. printf("%s", extra_info);
  447. return;
  448. }
  449. printf("%" PRIu32 " bytes: ", ei_size);
  450. for (uint32_t i = 0; i < ei_size; i++) {
  451. printf("%02hhX ", (unsigned char)extra_info[i]);
  452. }
  453. }
  454. static void display_nodes_data(nodeid_format_t nodeid_format, name_format_t name_format, sorttype_t sort_type)
  455. {
  456. int i, display_qdevice = 0;
  457. unsigned int our_flags = 0;
  458. struct votequorum_info info[g_view_list_entries];
  459. /*
  460. * cache node info because we need to parse them twice
  461. */
  462. if (v_handle) {
  463. for (i=0; i < g_view_list_entries; i++) {
  464. if (votequorum_getinfo(v_handle, g_view_list[i].node_id, &info[i]) != CS_OK) {
  465. printf("Unable to get node " CS_PRI_NODE_ID " info\n", g_view_list[i].node_id);
  466. }
  467. g_view_list[i].vq_info = &info[i];
  468. if (info[i].flags & VOTEQUORUM_INFO_QDEVICE_REGISTERED) {
  469. display_qdevice = 1;
  470. }
  471. }
  472. }
  473. /*
  474. * Get node names
  475. */
  476. for (i=0; i < g_view_list_entries; i++) {
  477. g_view_list[i].name = strdup(node_name(g_view_list[i].node_id, name_format));
  478. }
  479. printf("\nMembership information\n");
  480. printf("----------------------\n");
  481. print_string_padded("Nodeid");
  482. if (v_handle) {
  483. print_string_padded("Votes");
  484. if ((display_qdevice) || (machine_parsable)) {
  485. print_string_padded("Qdevice");
  486. }
  487. }
  488. printf("Name\n");
  489. /* corosync sends them already sorted by address */
  490. if (sort_type == SORT_NODEID) {
  491. qsort(g_view_list, g_view_list_entries, sizeof(view_list_entry_t), compare_nodeids);
  492. }
  493. if (sort_type == SORT_NODENAME) {
  494. qsort(g_view_list, g_view_list_entries, sizeof(view_list_entry_t), compare_nodenames);
  495. }
  496. for (i=0; i < g_view_list_entries; i++) {
  497. if (nodeid_format == NODEID_FORMAT_DECIMAL) {
  498. print_uint32_padded(g_view_list[i].node_id);
  499. } else {
  500. printf("0x%08x ", g_view_list[i].node_id);
  501. }
  502. if (v_handle) {
  503. int votes = -1;
  504. votes = info[i].node_votes;
  505. print_uint32_padded(votes);
  506. if ((display_qdevice) || (machine_parsable)) {
  507. if (info[i].flags & VOTEQUORUM_INFO_QDEVICE_REGISTERED) {
  508. char buf[10];
  509. snprintf(buf, sizeof(buf),
  510. "%s,%s,%s",
  511. info[i].flags & VOTEQUORUM_INFO_QDEVICE_ALIVE?"A":"NA",
  512. info[i].flags & VOTEQUORUM_INFO_QDEVICE_CAST_VOTE?"V":"NV",
  513. info[i].flags & VOTEQUORUM_INFO_QDEVICE_MASTER_WINS?"MW":"NMW");
  514. print_string_padded(buf);
  515. } else {
  516. print_string_padded("NR");
  517. }
  518. }
  519. }
  520. printf("%s", g_view_list[i].name);
  521. if (display_qdevice
  522. && (info[i].flags & VOTEQUORUM_INFO_QDEVICE_REGISTERED)
  523. && (info[i].flags & VOTEQUORUM_INFO_QDEVICE_HAS_EXTRA_INFO)) {
  524. uint32_t ei_size = 0;
  525. char extra_info[VOTEQUORUM_QDEVICE_EXTRA_NODEINFO_MAXSIZE+1] = { 0 };
  526. if(votequorum_get_qdevice_extra_info (v_handle, info[i].node_id, &ei_size, extra_info) == CS_OK) {
  527. printf(" [");
  528. print_extra_info(ei_size, extra_info);
  529. printf("]");
  530. }
  531. }
  532. if (g_view_list[i].node_id == our_nodeid) {
  533. printf(" (local)");
  534. if (v_handle) {
  535. our_flags = info[i].flags;
  536. }
  537. }
  538. printf("\n");
  539. }
  540. if (g_view_list_entries) {
  541. for (i= 0; i < g_view_list_entries; i++) {
  542. free(g_view_list[i].name);
  543. }
  544. free(g_view_list);
  545. g_view_list = NULL;
  546. }
  547. if (display_qdevice) {
  548. if (nodeid_format == NODEID_FORMAT_DECIMAL) {
  549. print_uint32_padded(VOTEQUORUM_QDEVICE_NODEID);
  550. } else {
  551. printf("0x%08x ", VOTEQUORUM_QDEVICE_NODEID);
  552. }
  553. /* If the quorum device is inactive on this node then show votes as 0
  554. so that the display is not confusing */
  555. if (our_flags & VOTEQUORUM_INFO_QDEVICE_CAST_VOTE) {
  556. print_uint32_padded(info[0].qdevice_votes);
  557. }
  558. else {
  559. print_uint32_padded(0);
  560. }
  561. printf(" %s", info[0].qdevice_name);
  562. if (our_flags & VOTEQUORUM_INFO_QDEVICE_CAST_VOTE) {
  563. printf("\n");
  564. }
  565. else {
  566. printf(" (votes %d)\n", info[0].qdevice_votes);
  567. }
  568. }
  569. }
  570. static int display_quorum_data(int is_quorate,
  571. nodeid_format_t nodeid_format, name_format_t name_format, sorttype_t sort_type,
  572. int loop)
  573. {
  574. struct votequorum_info info;
  575. int err;
  576. char quorumtype[256];
  577. time_t t;
  578. memset(quorumtype, 0, sizeof(quorumtype));
  579. printf("Quorum information\n");
  580. printf("------------------\n");
  581. time(&t);
  582. printf("Date: %s", ctime((const time_t *)&t));
  583. if (get_quorum_type(quorumtype, sizeof(quorumtype))) {
  584. strncpy(quorumtype, "Not configured", sizeof(quorumtype) - 1);
  585. }
  586. printf("Quorum provider: %s\n", quorumtype);
  587. printf("Nodes: %d\n", g_view_list_entries);
  588. if (nodeid_format == NODEID_FORMAT_DECIMAL) {
  589. printf("Node ID: " CS_PRI_NODE_ID "\n", our_nodeid);
  590. } else {
  591. printf("Node ID: 0x%08x\n", our_nodeid);
  592. }
  593. if (v_handle) {
  594. printf("Ring ID: " CS_PRI_RING_ID "\n", g_ring_id_rep_node, g_ring_id);
  595. }
  596. else {
  597. printf("Ring ID: " CS_PRI_RING_ID_SEQ "\n", g_ring_id);
  598. }
  599. printf("Quorate: %s\n", is_quorate?"Yes":"No");
  600. if (!v_handle) {
  601. return CS_OK;
  602. }
  603. err=votequorum_getinfo(v_handle, our_nodeid, &info);
  604. if ((err == CS_OK) || (err == CS_ERR_NOT_EXIST)) {
  605. printf("\nVotequorum information\n");
  606. printf("----------------------\n");
  607. printf("Expected votes: %d\n", info.node_expected_votes);
  608. printf("Highest expected: %d\n", info.highest_expected);
  609. printf("Total votes: %d\n", info.total_votes);
  610. printf("Quorum: %d %s\n", info.quorum, info.flags & VOTEQUORUM_INFO_QUORATE?" ":"Activity blocked");
  611. printf("Flags: ");
  612. if (info.flags & VOTEQUORUM_INFO_TWONODE) printf("2Node ");
  613. if (info.flags & VOTEQUORUM_INFO_QUORATE) printf("Quorate ");
  614. if (info.flags & VOTEQUORUM_INFO_WAIT_FOR_ALL) printf("WaitForAll ");
  615. if (info.flags & VOTEQUORUM_INFO_LAST_MAN_STANDING) printf("LastManStanding ");
  616. if (info.flags & VOTEQUORUM_INFO_AUTO_TIE_BREAKER) printf("AutoTieBreaker ");
  617. if (info.flags & VOTEQUORUM_INFO_ALLOW_DOWNSCALE) printf("AllowDownscale ");
  618. if (info.flags & VOTEQUORUM_INFO_QDEVICE_REGISTERED) printf("Qdevice ");
  619. printf("\n");
  620. } else {
  621. fprintf(stderr, "Unable to get node info: %s\n", cs_strerror(err));
  622. }
  623. display_nodes_data(nodeid_format, name_format, sort_type);
  624. return err;
  625. }
  626. /*
  627. * return EXIT_SUCCESS if quorate
  628. * EXIT_NOT_QUORATE if not quorate
  629. * EXIT_FAILURE on error
  630. */
  631. static int show_status(nodeid_format_t nodeid_format, name_format_t name_format, sorttype_t sort_type)
  632. {
  633. int is_quorate;
  634. int err;
  635. err=quorum_getquorate(q_handle, &is_quorate);
  636. if (err != CS_OK) {
  637. fprintf(stderr, "Unable to get cluster quorate status: %s\n", cs_strerror(err));
  638. goto quorum_err;
  639. }
  640. err=quorum_trackstart(q_handle, CS_TRACK_CURRENT);
  641. if (err != CS_OK) {
  642. fprintf(stderr, "Unable to start quorum status tracking: %s\n", cs_strerror(err));
  643. goto quorum_err;
  644. }
  645. g_called = 0;
  646. while (g_called == 0 && err == CS_OK) {
  647. err = quorum_dispatch(q_handle, CS_DISPATCH_ONE);
  648. if (err != CS_OK) {
  649. fprintf(stderr, "Unable to dispatch quorum status: %s\n", cs_strerror(err));
  650. }
  651. }
  652. if (quorum_trackstop(q_handle) != CS_OK) {
  653. fprintf(stderr, "Unable to stop quorum status tracking: %s\n", cs_strerror(err));
  654. }
  655. if (using_votequorum()) {
  656. if ( (err=votequorum_trackstart(v_handle, 0LL, CS_TRACK_CURRENT)) != CS_OK) {
  657. fprintf(stderr, "Unable to start votequorum status tracking: %s\n", cs_strerror(err));
  658. goto quorum_err;
  659. }
  660. g_vq_called = 0;
  661. while (g_vq_called == 0 && err == CS_OK) {
  662. err = votequorum_dispatch(v_handle, CS_DISPATCH_ONE);
  663. if (err != CS_OK) {
  664. fprintf(stderr, "Unable to dispatch votequorum status: %s\n", cs_strerror(err));
  665. }
  666. }
  667. }
  668. quorum_err:
  669. if (err != CS_OK) {
  670. return EXIT_FAILURE;
  671. }
  672. err = display_quorum_data(is_quorate, nodeid_format, name_format, sort_type, 0);
  673. if (err != CS_OK) {
  674. return EXIT_FAILURE;
  675. }
  676. return (is_quorate ? EXIT_SUCCESS : EXIT_NOT_QUORATE);
  677. }
  678. static int monitor_status(nodeid_format_t nodeid_format, name_format_t name_format, sorttype_t sort_type) {
  679. int err;
  680. int loop = 0;
  681. if (q_type == QUORUM_FREE) {
  682. printf("\nQuorum is not configured - cannot monitor\n");
  683. return show_status(nodeid_format, name_format, sort_type);
  684. }
  685. err=quorum_trackstart(q_handle, CS_TRACK_CHANGES);
  686. if (err != CS_OK) {
  687. fprintf(stderr, "Unable to start quorum status tracking: %s\n", cs_strerror(err));
  688. goto quorum_err;
  689. }
  690. if (using_votequorum()) {
  691. if ( (err=votequorum_trackstart(v_handle, 0LL, CS_TRACK_CHANGES)) != CS_OK) {
  692. fprintf(stderr, "Unable to start votequorum status tracking: %s\n", cs_strerror(err));
  693. goto quorum_err;
  694. }
  695. }
  696. while (1) {
  697. err = quorum_dispatch(q_handle, CS_DISPATCH_ONE);
  698. if (err != CS_OK) {
  699. fprintf(stderr, "Unable to dispatch quorum status: %s\n", cs_strerror(err));
  700. goto quorum_err;
  701. }
  702. if (using_votequorum()) {
  703. g_vq_called = 0;
  704. while (!g_vq_called) {
  705. err = votequorum_dispatch(v_handle, CS_DISPATCH_ONE);
  706. if (err != CS_OK) {
  707. fprintf(stderr, "Unable to dispatch votequorum status: %s\n", cs_strerror(err));
  708. goto quorum_err;
  709. }
  710. }
  711. }
  712. err = display_quorum_data(g_quorate, nodeid_format, name_format, sort_type, loop);
  713. printf("\n");
  714. loop = 1;
  715. if (err != CS_OK) {
  716. fprintf(stderr, "Unable to display quorum data: %s\n", cs_strerror(err));
  717. goto quorum_err;
  718. }
  719. }
  720. quorum_err:
  721. return EXIT_FAILURE;
  722. }
  723. static int watch_notifications(notification_watchtype_t watch) {
  724. int err;
  725. g_watch_notifications = watch;
  726. if (q_type == QUORUM_FREE) {
  727. printf("\nQuorum is not configured - cannot monitor\n");
  728. return EXIT_FAILURE;
  729. }
  730. err=quorum_trackstart(q_handle, CS_TRACK_CHANGES);
  731. if (err != CS_OK) {
  732. fprintf(stderr, "Unable to start quorum status tracking: %s\n", cs_strerror(err));
  733. goto quorum_err;
  734. }
  735. if (using_votequorum()) {
  736. if ( (err=votequorum_trackstart(v_handle, 0LL, CS_TRACK_CHANGES)) != CS_OK) {
  737. fprintf(stderr, "Unable to start votequorum status tracking: %s\n", cs_strerror(err));
  738. goto quorum_err;
  739. }
  740. }
  741. printf("Watching for updates:\n");
  742. while (1) {
  743. if (using_votequorum()) {
  744. err = quorum_dispatch(q_handle, CS_DISPATCH_ALL);
  745. if (err != CS_OK) {
  746. fprintf(stderr, "Unable to dispatch quorum status: %s\n", cs_strerror(err));
  747. goto quorum_err;
  748. }
  749. err = votequorum_dispatch(v_handle, CS_DISPATCH_ONE);
  750. if (err != CS_OK) {
  751. fprintf(stderr, "Unable to dispatch votequorum status: %s\n", cs_strerror(err));
  752. goto quorum_err;
  753. }
  754. } else {
  755. err = quorum_dispatch(q_handle, CS_DISPATCH_ONE);
  756. if (err != CS_OK) {
  757. fprintf(stderr, "Unable to dispatch quorum status: %s\n", cs_strerror(err));
  758. goto quorum_err;
  759. }
  760. }
  761. }
  762. quorum_err:
  763. return EXIT_FAILURE;
  764. }
  765. static int show_nodes(nodeid_format_t nodeid_format, name_format_t name_format, sorttype_t sort_type)
  766. {
  767. int err;
  768. int result = EXIT_FAILURE;
  769. err = quorum_trackstart(q_handle, CS_TRACK_CURRENT);
  770. if (err != CS_OK) {
  771. fprintf(stderr, "Unable to start quorum status tracking: %s\n", cs_strerror(err));
  772. goto err_exit;
  773. }
  774. g_called = 0;
  775. while (g_called == 0) {
  776. err = quorum_dispatch(q_handle, CS_DISPATCH_ONE);
  777. if (err != CS_OK) {
  778. fprintf(stderr, "Unable to dispatch quorum status: %s\n", cs_strerror(err));
  779. goto err_exit;
  780. }
  781. }
  782. display_nodes_data(nodeid_format, name_format, sort_type);
  783. result = EXIT_SUCCESS;
  784. err_exit:
  785. return result;
  786. }
  787. static int unregister_qdevice(void)
  788. {
  789. int err;
  790. struct votequorum_info info;
  791. int result;
  792. result = EXIT_FAILURE;
  793. err = votequorum_getinfo(v_handle, our_nodeid, &info);
  794. if (err != CS_OK) {
  795. fprintf(stderr, "Unable to get quorum device info: %s\n", cs_strerror(err));
  796. goto err_exit;
  797. }
  798. if (!(info.flags & VOTEQUORUM_INFO_QDEVICE_REGISTERED)) {
  799. result = EXIT_SUCCESS;
  800. goto err_exit;
  801. }
  802. err = votequorum_qdevice_unregister(v_handle, info.qdevice_name);
  803. if (err != CS_OK) {
  804. fprintf(stderr, "Unable to unregister quorum device: %s\n", cs_strerror(err));
  805. goto err_exit;
  806. }
  807. result = EXIT_SUCCESS;
  808. err_exit:
  809. return result;
  810. }
  811. static int set_qdevice_extra_info(const char *extra_info)
  812. {
  813. int err;
  814. uint32_t ei_size = 0;
  815. if (extra_info) {
  816. ei_size = strlen(extra_info) + 1;
  817. }
  818. err = votequorum_set_qdevice_extra_info(v_handle, ei_size, extra_info);
  819. if (err != CS_OK) {
  820. fprintf(stderr, "Failed to set extra info %s\n", cs_strerror(err));
  821. return EXIT_FAILURE;
  822. }
  823. return EXIT_SUCCESS;
  824. }
  825. /*
  826. * return -1 on error
  827. * 0 if OK
  828. */
  829. static int init_all(void) {
  830. cmap_handle = 0;
  831. q_handle = 0;
  832. v_handle = 0;
  833. c_handle = 0;
  834. if (cmap_initialize(&cmap_handle) != CS_OK) {
  835. fprintf(stderr, "Cannot initialize CMAP service\n");
  836. cmap_handle = 0;
  837. goto out;
  838. }
  839. if (quorum_initialize(&q_handle, &q_callbacks, &q_type) != CS_OK) {
  840. fprintf(stderr, "Cannot initialize QUORUM service\n");
  841. q_handle = 0;
  842. goto out;
  843. }
  844. if (corosync_cfg_initialize(&c_handle, &c_callbacks) != CS_OK) {
  845. fprintf(stderr, "Cannot initialise CFG service\n");
  846. c_handle = 0;
  847. goto out;
  848. }
  849. if (using_votequorum() <= 0) {
  850. return 0;
  851. }
  852. if (votequorum_model_initialize(&v_handle, &v_callbacks) != CS_OK) {
  853. fprintf(stderr, "Cannot initialise VOTEQUORUM service\n");
  854. v_handle = 0;
  855. goto out;
  856. }
  857. if (cmap_get_uint32(cmap_handle, "runtime.votequorum.this_node_id", &our_nodeid) != CS_OK) {
  858. fprintf(stderr, "Unable to retrieve this_node_id\n");
  859. goto out;
  860. }
  861. return 0;
  862. out:
  863. return -1;
  864. }
  865. static void close_all(void) {
  866. if (cmap_handle) {
  867. cmap_finalize(cmap_handle);
  868. }
  869. if (q_handle) {
  870. quorum_finalize(q_handle);
  871. }
  872. if (c_handle) {
  873. corosync_cfg_finalize(c_handle);
  874. }
  875. if (v_handle) {
  876. votequorum_finalize(v_handle);
  877. }
  878. }
  879. int main (int argc, char *argv[]) {
  880. const char *options = "VHaslpmfe:v:hin:o:W:X:C";
  881. int opt;
  882. int votes = 0;
  883. int ret = 0;
  884. uint32_t nodeid = 0;
  885. uint32_t nodeid_set = 0;
  886. nodeid_format_t nodeid_format = NODEID_FORMAT_DECIMAL;
  887. name_format_t address_format = ADDRESS_FORMAT_NAME;
  888. command_t command_opt = CMD_SHOWSTATUS;
  889. sorttype_t sort_opt = SORT_ADDR;
  890. notification_watchtype_t watchtype = WATCH_NONE;
  891. long long int l;
  892. char *extra_info = NULL;
  893. while ( (opt = getopt(argc, argv, options)) != -1 ) {
  894. switch (opt) {
  895. case 'f':
  896. command_opt = CMD_UNREGISTER_QDEVICE;
  897. break;
  898. case 's':
  899. command_opt = CMD_SHOWSTATUS;
  900. break;
  901. case 'a':
  902. g_show_all_addrs = 1;
  903. break;
  904. case 'm':
  905. command_opt = CMD_MONITOR;
  906. break;
  907. case 'i':
  908. address_format = ADDRESS_FORMAT_IP;
  909. break;
  910. case 'H':
  911. nodeid_format = NODEID_FORMAT_HEX;
  912. break;
  913. case 'l':
  914. command_opt = CMD_SHOWNODES;
  915. break;
  916. case 'W':
  917. command_opt = CMD_WATCH_NOTIFICATIONS;
  918. if (strchr(optarg, 'q')) {
  919. watchtype |= WATCH_QUORUM;
  920. }
  921. if (strchr(optarg, 'x')) {
  922. watchtype |= WATCH_EXTRA_INFO;
  923. }
  924. if(watchtype == WATCH_NONE) {
  925. fprintf(stderr, "Invalid watch option. valid watch options are q(quorum) or x(qdevice extra info)\n");
  926. exit(EXIT_FAILURE);
  927. }
  928. break;
  929. case 'X':
  930. command_opt = CMD_SET_EXTRA_INFO;
  931. if (extra_info) {
  932. free(extra_info);
  933. }
  934. extra_info = strdup(optarg);
  935. break;
  936. case 'C':
  937. command_opt = CMD_SET_EXTRA_INFO;
  938. if (extra_info) {
  939. free(extra_info);
  940. }
  941. extra_info = NULL;
  942. break;
  943. case 'p':
  944. machine_parsable = 1;
  945. break;
  946. case 'e':
  947. if (util_strtonum(optarg, 1, INT_MAX, &l) == -1) {
  948. fprintf(stderr, "New expected votes value was not valid, try a positive number\n");
  949. exit(EXIT_FAILURE);
  950. }
  951. votes = l;
  952. command_opt = CMD_SETEXPECTED;
  953. break;
  954. case 'n':
  955. if (util_strtonum(optarg, 1, UINT_MAX, &l) == -1) {
  956. fprintf(stderr, "The nodeid was not valid, try a positive number\n");
  957. exit(EXIT_FAILURE);
  958. }
  959. nodeid = l;
  960. nodeid_set = 1;
  961. break;
  962. case 'v':
  963. if (util_strtonum(optarg, 0, INT_MAX, &l) == -1) {
  964. fprintf(stderr, "New votes value was not valid, try a positive number or zero\n");
  965. exit(EXIT_FAILURE);
  966. }
  967. votes = l;
  968. command_opt = CMD_SETVOTES;
  969. break;
  970. case 'o':
  971. if (strcmp(optarg, "a") == 0) {
  972. sort_opt = SORT_ADDR;
  973. } else if (strcmp(optarg, "i") == 0) {
  974. sort_opt = SORT_NODEID;
  975. } else if (strcmp(optarg, "n") == 0) {
  976. sort_opt = SORT_NODENAME;
  977. } else {
  978. fprintf(stderr, "Invalid ordering option. valid orders are a(address), i(node ID) or n(name)\n");
  979. exit(EXIT_FAILURE);
  980. }
  981. break;
  982. case 'V':
  983. printf("corosync-quorumtool version: %s\n", VERSION);
  984. exit(EXIT_SUCCESS);
  985. break;
  986. case 'h':
  987. show_usage(argv[0]);
  988. exit(EXIT_SUCCESS);
  989. break;
  990. case ':':
  991. case '?':
  992. default:
  993. show_usage(argv[0]);
  994. exit(EXIT_FAILURE);
  995. break;
  996. }
  997. }
  998. if (init_all()) {
  999. close_all();
  1000. exit(EXIT_FAILURE);
  1001. }
  1002. switch (command_opt) {
  1003. case CMD_SHOWNODES:
  1004. ret = show_nodes(nodeid_format, address_format, sort_opt);
  1005. break;
  1006. case CMD_SHOWSTATUS:
  1007. ret = show_status(nodeid_format, address_format, sort_opt);
  1008. break;
  1009. case CMD_SETVOTES:
  1010. if (using_votequorum() > 0) {
  1011. if (!nodeid_set) {
  1012. nodeid = our_nodeid;
  1013. }
  1014. ret = set_votes(nodeid, votes);
  1015. } else {
  1016. fprintf(stderr, "You cannot change node votes, corosync is not using votequorum\n");
  1017. ret = EXIT_FAILURE;
  1018. }
  1019. break;
  1020. case CMD_SETEXPECTED:
  1021. if (using_votequorum() > 0) {
  1022. ret = set_expected(votes);
  1023. } else {
  1024. fprintf(stderr, "You cannot change expected votes, corosync is not using votequorum\n");
  1025. ret = EXIT_FAILURE;
  1026. }
  1027. break;
  1028. case CMD_MONITOR:
  1029. ret = monitor_status(nodeid_format, address_format, sort_opt);
  1030. break;
  1031. case CMD_WATCH_NOTIFICATIONS:
  1032. ret = watch_notifications(watchtype);
  1033. break;
  1034. case CMD_UNREGISTER_QDEVICE:
  1035. if (using_votequorum() > 0) {
  1036. ret = unregister_qdevice();
  1037. } else {
  1038. fprintf(stderr, "You cannot unregister quorum device, corosync is not using votequorum\n");
  1039. ret = EXIT_FAILURE;
  1040. }
  1041. break;
  1042. case CMD_SET_EXTRA_INFO:
  1043. if (using_votequorum() > 0) {
  1044. ret = set_qdevice_extra_info(extra_info);
  1045. } else {
  1046. fprintf(stderr, "You cannot set quorum device extra info, corosync is not using votequorum\n");
  1047. ret = EXIT_FAILURE;
  1048. }
  1049. break;
  1050. }
  1051. if (extra_info) {
  1052. free(extra_info);
  1053. }
  1054. close_all();
  1055. return (ret);
  1056. }