4
0

corosync-quorumtool.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /*
  2. * Copyright (c) 2009 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Christine Caulfield <ccaulfie@redhat.com>
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the Red Hat Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <config.h>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <errno.h>
  38. #include <unistd.h>
  39. #include <string.h>
  40. #include <pthread.h>
  41. #include <inttypes.h>
  42. #include <sys/types.h>
  43. #include <sys/socket.h>
  44. #include <sys/select.h>
  45. #include <sys/un.h>
  46. #include <netinet/in.h>
  47. #include <arpa/inet.h>
  48. #include <netdb.h>
  49. #include <corosync/corotypes.h>
  50. #include <corosync/totem/totem.h>
  51. #include <corosync/cfg.h>
  52. #include <corosync/confdb.h>
  53. #include <corosync/quorum.h>
  54. #include <corosync/votequorum.h>
  55. typedef enum { NODEID_FORMAT_DECIMAL, NODEID_FORMAT_HEX } nodeid_format_t;
  56. typedef enum { ADDRESS_FORMAT_NAME, ADDRESS_FORMAT_IP } name_format_t;
  57. typedef enum { CMD_UNKNOWN, CMD_SHOWNODES, CMD_SHOWSTATUS, CMD_SETVOTES, CMD_SETEXPECTED } command_t;
  58. static void show_usage(const char *name)
  59. {
  60. printf("usage: \n");
  61. printf("%s <options>\n", name);
  62. printf("\n");
  63. printf(" options:\n");
  64. printf("\n");
  65. printf(" -s show quorum status\n");
  66. printf(" -l list nodes\n");
  67. printf(" -v <votes> change the number of votes for a node *\n");
  68. printf(" -n <nodeid> optional nodeid of node for -v\n");
  69. printf(" -e <expected> change expected votes for the cluster *\n");
  70. printf(" -h show nodeids in hexadecimal rather than decimal\n");
  71. printf(" -i show node IP addresses instead of the resolved name\n");
  72. printf(" -h show this help text\n");
  73. printf("\n");
  74. printf(" * Starred items only work if votequorum is the quorum provider for corosync\n");
  75. printf("\n");
  76. }
  77. /*
  78. * Caller should free the returned string
  79. */
  80. static const char *get_quorum_type(void)
  81. {
  82. const char *qtype = NULL;
  83. int result;
  84. char buf[255];
  85. size_t namelen = sizeof(buf);
  86. hdb_handle_t quorum_handle;
  87. confdb_handle_t confdb_handle;
  88. confdb_callbacks_t callbacks = {
  89. .confdb_key_change_notify_fn = NULL,
  90. .confdb_object_create_change_notify_fn = NULL,
  91. .confdb_object_delete_change_notify_fn = NULL
  92. };
  93. if (confdb_initialize(&confdb_handle, &callbacks) != CS_OK) {
  94. errno = EINVAL;
  95. return NULL;
  96. }
  97. result = confdb_object_find_start(confdb_handle, OBJECT_PARENT_HANDLE);
  98. if (result != CS_OK)
  99. goto out;
  100. result = confdb_object_find(confdb_handle, OBJECT_PARENT_HANDLE, (void *)"quorum", strlen("quorum"), &quorum_handle);
  101. if (result != CS_OK)
  102. goto out;
  103. result = confdb_key_get(confdb_handle, quorum_handle, (void *)"provider", strlen("provider"), buf, &namelen);
  104. if (result != CS_OK)
  105. goto out;
  106. buf[namelen] = '\0';
  107. /* If strdup returns NULL then we just assume no quorum provider ?? */
  108. qtype = strdup(buf);
  109. out:
  110. confdb_finalize(confdb_handle);
  111. return qtype;
  112. }
  113. /*
  114. * Returns 1 if 'votequorum' is active. The called then knows that
  115. * votequorum calls should work and can provide extra information
  116. */
  117. static int using_votequorum(void)
  118. {
  119. const char *quorumtype = get_quorum_type();
  120. int using_voteq;
  121. if (!quorumtype)
  122. return 0;
  123. if (strcmp(quorumtype, "corosync_votequorum") == 0)
  124. using_voteq = 1;
  125. else
  126. using_voteq = 0;
  127. free((void *)quorumtype);
  128. return using_voteq;
  129. }
  130. static int set_votes(uint32_t nodeid, int votes)
  131. {
  132. votequorum_handle_t v_handle;
  133. votequorum_callbacks_t v_callbacks;
  134. int err;
  135. v_callbacks.votequorum_notify_fn = NULL;
  136. v_callbacks.votequorum_expectedvotes_notify_fn = NULL;
  137. if ( (err=votequorum_initialize(&v_handle, &v_callbacks)) != CS_OK) {
  138. fprintf(stderr, "votequorum_initialize FAILED: %d, this is probably a configuration error\n", err);
  139. return err;
  140. }
  141. if ( (err=votequorum_setvotes(v_handle, nodeid, votes)) != CS_OK)
  142. fprintf(stderr, "set votes FAILED: %d\n", err);
  143. votequorum_finalize(v_handle);
  144. return err==CS_OK?0:err;
  145. }
  146. static int set_expected(int expected_votes)
  147. {
  148. votequorum_handle_t v_handle;
  149. votequorum_callbacks_t v_callbacks;
  150. int err;
  151. v_callbacks.votequorum_notify_fn = NULL;
  152. v_callbacks.votequorum_expectedvotes_notify_fn = NULL;
  153. if ( (err=votequorum_initialize(&v_handle, &v_callbacks)) != CS_OK) {
  154. fprintf(stderr, "votequorum_initialize FAILED: %d, this is probably a configuration error\n", err);
  155. return err;
  156. }
  157. if ( (err=votequorum_setexpected(v_handle, expected_votes)) != CS_OK)
  158. fprintf(stderr, "set expected votes FAILED: %d\n", err);
  159. votequorum_finalize(v_handle);
  160. return err==CS_OK?0:err;
  161. }
  162. static int get_votes(votequorum_handle_t v_handle, uint32_t nodeid)
  163. {
  164. int votes = -1;
  165. struct votequorum_info info;
  166. if (votequorum_getinfo(v_handle, nodeid, &info) == CS_OK)
  167. votes = info.node_votes;
  168. return votes;
  169. }
  170. /*
  171. * This resolves the first address assigned to a node
  172. * and returns the name or IP address. Use cfgtool if you need more information.
  173. */
  174. static const char *node_name(corosync_cfg_handle_t c_handle, uint32_t nodeid, name_format_t name_format)
  175. {
  176. int ret;
  177. int numaddrs;
  178. corosync_cfg_node_address_t addrs[INTERFACE_MAX];
  179. if (corosync_cfg_get_node_addrs(c_handle, nodeid, INTERFACE_MAX, &numaddrs, addrs) == CS_OK) {
  180. static char buf[INET6_ADDRSTRLEN];
  181. socklen_t addrlen;
  182. struct sockaddr_storage *ss = (struct sockaddr_storage *)addrs[0].address;
  183. if (ss->ss_family == AF_INET6)
  184. addrlen = sizeof(struct sockaddr_in6);
  185. else
  186. addrlen = sizeof(struct sockaddr_in);
  187. ret = getnameinfo((struct sockaddr *)addrs[0].address, addrlen,
  188. buf, sizeof(buf),
  189. NULL, 0,
  190. (name_format == ADDRESS_FORMAT_IP)?NI_NUMERICHOST:0);
  191. if (!ret)
  192. return buf;
  193. }
  194. return "";
  195. }
  196. /*
  197. * Static variables to pass back to calling routine
  198. */
  199. static uint32_t g_quorate;
  200. static uint64_t g_ring_id;
  201. static uint32_t g_view_list_entries;
  202. static uint32_t *g_view_list;
  203. static uint32_t g_called;
  204. static void quorum_notification_fn(
  205. quorum_handle_t handle,
  206. uint32_t quorate,
  207. uint64_t ring_id,
  208. uint32_t view_list_entries,
  209. uint32_t *view_list)
  210. {
  211. g_called = 1;
  212. g_quorate = quorate;
  213. g_ring_id = ring_id;
  214. g_view_list_entries = view_list_entries;
  215. g_view_list = malloc(sizeof(uint32_t) * view_list_entries);
  216. if (g_view_list) {
  217. memcpy(g_view_list, view_list,sizeof(uint32_t) * view_list_entries);
  218. }
  219. }
  220. static void show_status(void)
  221. {
  222. quorum_handle_t q_handle;
  223. votequorum_handle_t v_handle;
  224. votequorum_callbacks_t v_callbacks;
  225. quorum_callbacks_t callbacks;
  226. struct votequorum_info info;
  227. int is_quorate;
  228. int err;
  229. callbacks.quorum_notify_fn = quorum_notification_fn;
  230. err=quorum_initialize(&q_handle, &callbacks);
  231. if (err != CS_OK) {
  232. fprintf(stderr, "Cannot connect to quorum service, is it loaded?\n");
  233. return;
  234. }
  235. err=quorum_getquorate(q_handle, &is_quorate);
  236. if (err != CS_OK) {
  237. fprintf(stderr, "quorum_getquorate FAILED: %d\n", err);
  238. return;
  239. }
  240. err=quorum_trackstart(q_handle, CS_TRACK_CURRENT);
  241. if (err != CS_OK) {
  242. fprintf(stderr, "quorum_trackstart FAILED: %d\n", err);
  243. return;
  244. }
  245. g_called = 0;
  246. while (g_called == 0)
  247. quorum_dispatch(q_handle, CS_DISPATCH_ONE);
  248. quorum_finalize(q_handle);
  249. printf("Version: %s\n", VERSION);
  250. printf("Nodes: %d\n", g_view_list_entries);
  251. printf("Ring ID: %" PRIu64 "\n", g_ring_id);
  252. printf("Quorum type: %s\n", get_quorum_type());
  253. printf("Quorate: %s\n", is_quorate?"Yes":"No");
  254. if (using_votequorum()) {
  255. v_callbacks.votequorum_notify_fn = NULL;
  256. v_callbacks.votequorum_expectedvotes_notify_fn = NULL;
  257. if ( (err=votequorum_initialize(&v_handle, &v_callbacks)) != CS_OK) {
  258. fprintf(stderr, "votequorum_initialize FAILED: %d, this is probably a configuration error\n", err);
  259. goto err_exit;
  260. }
  261. if ( (err=votequorum_getinfo(v_handle, 0, &info)) != CS_OK)
  262. fprintf(stderr, "votequorum_getinfo FAILED: %d\n", err);
  263. else {
  264. printf("Node votes: %d\n", info.node_votes);
  265. printf("Expected votes: %d\n", info.node_expected_votes);
  266. printf("Highest expected: %d\n", info.highest_expected);
  267. printf("Total votes: %d\n", info.total_votes);
  268. printf("Quorum: %d %s\n", info.quorum, info.flags & VOTEQUORUM_INFO_FLAG_QUORATE?" ":"Activity blocked");
  269. printf("Flags: ");
  270. if (info.flags & VOTEQUORUM_INFO_FLAG_HASSTATE) printf("HasState ");
  271. if (info.flags & VOTEQUORUM_INFO_FLAG_DISALLOWED) printf("DisallowedNodes ");
  272. if (info.flags & VOTEQUORUM_INFO_FLAG_TWONODE) printf("2Node ");
  273. if (info.flags & VOTEQUORUM_INFO_FLAG_QUORATE) printf("Quorate ");
  274. printf("\n");
  275. }
  276. }
  277. err_exit:
  278. return;
  279. }
  280. static void show_nodes(nodeid_format_t nodeid_format, name_format_t name_format)
  281. {
  282. quorum_handle_t q_handle;
  283. votequorum_handle_t v_handle;
  284. corosync_cfg_handle_t c_handle;
  285. corosync_cfg_callbacks_t c_callbacks;
  286. int i;
  287. int using_vq = 0;
  288. quorum_callbacks_t q_callbacks;
  289. votequorum_callbacks_t v_callbacks;
  290. int err;
  291. q_callbacks.quorum_notify_fn = quorum_notification_fn;
  292. err=quorum_initialize(&q_handle, &q_callbacks);
  293. if (err != CS_OK) {
  294. fprintf(stderr, "Cannot connect to quorum service, is it loaded?\n");
  295. return;
  296. }
  297. v_callbacks.votequorum_notify_fn = NULL;
  298. v_callbacks.votequorum_expectedvotes_notify_fn = NULL;
  299. using_vq = using_votequorum();
  300. if (using_vq) {
  301. if ( (err=votequorum_initialize(&v_handle, &v_callbacks)) != CS_OK) {
  302. fprintf(stderr, "votequorum_initialize FAILED: %d, this is probably a configuration error\n", err);
  303. goto err_exit;
  304. }
  305. }
  306. err = quorum_trackstart(q_handle, CS_TRACK_CURRENT);
  307. if (err != CS_OK) {
  308. fprintf(stderr, "quorum_trackstart FAILED: %d\n", err);
  309. return;
  310. }
  311. g_called = 0;
  312. while (g_called == 0)
  313. quorum_dispatch(q_handle, CS_DISPATCH_ONE);
  314. quorum_finalize(q_handle);
  315. err = corosync_cfg_initialize(&c_handle, &c_callbacks);
  316. if (err != CS_OK) {
  317. fprintf(stderr, "Cannot initialise CFG service\n");
  318. goto err_exit;
  319. }
  320. if (using_vq)
  321. printf("Nodeid Votes Name\n");
  322. else
  323. printf("Nodeid Name\n");
  324. for (i=0; i < g_view_list_entries; i++) {
  325. if (nodeid_format == NODEID_FORMAT_DECIMAL) {
  326. printf("%4u ", g_view_list[i]);
  327. }
  328. else {
  329. printf("0x%04x ", g_view_list[i]);
  330. }
  331. if (using_vq) {
  332. printf("%3d %s\n", get_votes(v_handle, g_view_list[i]), node_name(c_handle, g_view_list[i], name_format));
  333. }
  334. else {
  335. printf("%s\n", node_name(c_handle, g_view_list[i], name_format));
  336. }
  337. }
  338. if (using_vq)
  339. votequorum_finalize(v_handle);
  340. err_exit:
  341. corosync_cfg_finalize(c_handle);
  342. }
  343. int main (int argc, char *argv[]) {
  344. const char *options = "VHsle:v:hin:d:";
  345. char *endptr;
  346. int opt;
  347. int votes = 0;
  348. int ret = 0;
  349. uint32_t nodeid = VOTEQUORUM_NODEID_US;
  350. nodeid_format_t nodeid_format = NODEID_FORMAT_DECIMAL;
  351. name_format_t address_format = ADDRESS_FORMAT_NAME;
  352. command_t command_opt = CMD_UNKNOWN;
  353. if (argc == 1) {
  354. show_usage (argv[0]);
  355. exit(0);
  356. }
  357. while ( (opt = getopt(argc, argv, options)) != -1 ) {
  358. switch (opt) {
  359. case 's':
  360. command_opt = CMD_SHOWSTATUS;
  361. break;
  362. case 'i':
  363. address_format = ADDRESS_FORMAT_IP;
  364. break;
  365. case 'h':
  366. nodeid_format = NODEID_FORMAT_HEX;
  367. break;
  368. case 'l':
  369. command_opt = CMD_SHOWNODES;
  370. break;
  371. case 'e':
  372. if (using_votequorum()) {
  373. votes = strtol(optarg, &endptr, 0);
  374. if ((votes == 0 && endptr == optarg) || votes <= 0) {
  375. fprintf(stderr, "New expected votes value was not valid, try a positive number\n");
  376. }
  377. else {
  378. command_opt = CMD_SETEXPECTED;
  379. }
  380. }
  381. else {
  382. fprintf(stderr, "You cannot change expected votes, corosync is not using votequorum\n");
  383. exit(2);
  384. }
  385. break;
  386. case 'n':
  387. nodeid = strtol(optarg, &endptr, 0);
  388. if ((nodeid == 0 && endptr == optarg) || nodeid <= 0) {
  389. fprintf(stderr, "The nodeid was not valid, try a positive number\n");
  390. }
  391. break;
  392. case 'v':
  393. if (using_votequorum()) {
  394. votes = strtol(optarg, &endptr, 0);
  395. if ((votes == 0 && endptr == optarg) || votes < 0) {
  396. fprintf(stderr, "New votes value was not valid, try a positive number or zero\n");
  397. }
  398. else {
  399. command_opt = CMD_SETVOTES;
  400. }
  401. }
  402. else {
  403. fprintf(stderr, "You cannot change node votes, corosync is not using votequorum\n");
  404. exit(2);
  405. }
  406. break;
  407. case 'H':
  408. case '?':
  409. default:
  410. break;
  411. }
  412. }
  413. switch (command_opt) {
  414. case CMD_UNKNOWN:
  415. show_usage(argv[0]);
  416. break;
  417. case CMD_SHOWNODES:
  418. show_nodes(nodeid_format, address_format);
  419. break;
  420. case CMD_SHOWSTATUS:
  421. show_status();
  422. break;
  423. case CMD_SETVOTES:
  424. ret = set_votes(nodeid, votes);
  425. break;
  426. case CMD_SETEXPECTED:
  427. ret = set_expected(votes);
  428. break;
  429. }
  430. return (ret);
  431. }