corosync-cfgtool.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /*
  2. * Copyright (c) 2006-2019 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Steven Dake <sdake@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 MontaVista Software, 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 <sys/types.h>
  42. #include <sys/socket.h>
  43. #include <sys/select.h>
  44. #include <sys/un.h>
  45. #include <netinet/in.h>
  46. #include <arpa/inet.h>
  47. #include <corosync/corotypes.h>
  48. #include <corosync/totem/totem.h>
  49. #include <corosync/cfg.h>
  50. #include <corosync/cmap.h>
  51. #define cs_repeat(result, max, code) \
  52. do { \
  53. int counter = 0; \
  54. do { \
  55. result = code; \
  56. if (result == CS_ERR_TRY_AGAIN) { \
  57. sleep(1); \
  58. counter++; \
  59. } else { \
  60. break; \
  61. } \
  62. } while (counter < max); \
  63. } while (0)
  64. enum user_action {
  65. ACTION_NOOP=0,
  66. ACTION_LINKSTATUS_GET,
  67. ACTION_RELOAD_CONFIG,
  68. ACTION_REOPEN_LOG_FILES,
  69. ACTION_SHUTDOW,
  70. ACTION_SHOWADDR,
  71. ACTION_KILL_NODE,
  72. };
  73. static int node_compare(const void *aptr, const void *bptr)
  74. {
  75. uint32_t a,b;
  76. a = *(uint32_t *)aptr;
  77. b = *(uint32_t *)bptr;
  78. return a > b;
  79. }
  80. static int
  81. linkstatusget_do (char *interface_name, int brief)
  82. {
  83. cs_error_t result;
  84. corosync_cfg_handle_t handle;
  85. cmap_handle_t cmap_handle;
  86. unsigned int interface_count;
  87. char **interface_names;
  88. char **interface_status;
  89. uint32_t nodeid_list[KNET_MAX_HOST];
  90. char iter_key[CMAP_KEYNAME_MAXLEN];
  91. unsigned int i;
  92. cmap_iter_handle_t iter;
  93. unsigned int nodeid;
  94. unsigned int node_pos;
  95. cmap_value_types_t type;
  96. size_t value_len;
  97. int rc = 0;
  98. int len, s = 0, t;
  99. printf ("Printing link status.\n");
  100. result = corosync_cfg_initialize (&handle, NULL);
  101. if (result != CS_OK) {
  102. printf ("Could not initialize corosync configuration API error %d\n", result);
  103. exit (1);
  104. }
  105. result = cmap_initialize (&cmap_handle);
  106. if (result != CS_OK) {
  107. printf ("Could not initialize corosync cmap API error %d\n", result);
  108. exit (1);
  109. }
  110. /* Get a list of nodes. We do it this way rather than using votequorum as cfgtool
  111. * needs to be independent of quorum type
  112. */
  113. result = cmap_iter_init(cmap_handle, "nodelist.node.", &iter);
  114. if (result != CS_OK) {
  115. printf ("Could not get nodelist from cmap. error %d\n", result);
  116. exit (1);
  117. }
  118. while ((cmap_iter_next(cmap_handle, iter, iter_key, &value_len, &type)) == CS_OK) {
  119. result = sscanf(iter_key, "nodelist.node.%u.nodeid", &node_pos);
  120. if (result != 1) {
  121. continue;
  122. }
  123. if (cmap_get_uint32(cmap_handle, iter_key, &nodeid) == CS_OK) {
  124. nodeid_list[s++] = nodeid;
  125. }
  126. }
  127. /* totemknet returns nodes in nodeid order - even though it doesn't tell us
  128. what the nodeid is. So sort our node list and we can then look up
  129. knet node pos to get an actual nodeid.
  130. Yep, I really should have totally rewritten the cfg interface for this.
  131. */
  132. qsort(nodeid_list, s, sizeof(uint32_t), node_compare);
  133. result = corosync_cfg_local_get(handle, &nodeid);
  134. if (result != CS_OK) {
  135. printf ("Could not get the local node id, the error is: %d\n", result);
  136. }
  137. else {
  138. printf ("Local node ID %u\n", nodeid);
  139. }
  140. result = corosync_cfg_ring_status_get (handle,
  141. &interface_names,
  142. &interface_status,
  143. &interface_count);
  144. if (result != CS_OK) {
  145. printf ("Could not get the link status, the error is: %d\n", result);
  146. } else {
  147. for (i = 0; i < interface_count; i++) {
  148. char *cur_iface_name_space = strchr(interface_names[i], ' ');
  149. int show_current_iface;
  150. s = 0;
  151. /*
  152. * Interface_name is "<linkid> <IP address>"
  153. * separate them out
  154. */
  155. if (!cur_iface_name_space) {
  156. continue;
  157. }
  158. *cur_iface_name_space = '\0';
  159. show_current_iface = 1;
  160. if (interface_name != NULL && interface_name[0] != '\0' &&
  161. strcmp(interface_name, interface_names[i]) != 0 &&
  162. strcmp(interface_name, cur_iface_name_space + 1) != 0) {
  163. show_current_iface = 0;
  164. }
  165. if (show_current_iface) {
  166. printf ("LINK ID %s\n", interface_names[i]);
  167. printf ("\taddr\t= %s\n", cur_iface_name_space + 1);
  168. if((!brief) && (strcmp(interface_status[i], "OK") != 0) &&
  169. (!strstr(interface_status[i], "FAULTY"))) {
  170. len = strlen(interface_status[i]);
  171. printf ("\tstatus:\n");
  172. while (s < len) {
  173. nodeid = nodeid_list[s];
  174. t = interface_status[i][s] - '0';
  175. s++;
  176. printf("\t\tnodeid %2d:\t", nodeid);
  177. printf("link enabled:%d\t", t&1? 1 : 0);
  178. printf("link connected:%d\n", t&2? 1: 0);
  179. }
  180. } else {
  181. printf ("\tstatus\t= %s\n", interface_status[i]);
  182. if (strstr(interface_status[i], "FAULTY")) {
  183. rc = 1;
  184. }
  185. }
  186. }
  187. }
  188. for (i = 0; i < interface_count; i++) {
  189. free(interface_status[i]);
  190. free(interface_names[i]);
  191. }
  192. free(interface_status);
  193. free(interface_names);
  194. }
  195. (void)cmap_finalize (cmap_handle);
  196. (void)corosync_cfg_finalize (handle);
  197. return rc;
  198. }
  199. static int reload_config_do (void)
  200. {
  201. cs_error_t result;
  202. corosync_cfg_handle_t handle;
  203. int rc;
  204. rc = 0;
  205. printf ("Reloading corosync.conf...\n");
  206. result = corosync_cfg_initialize (&handle, NULL);
  207. if (result != CS_OK) {
  208. printf ("Could not initialize corosync configuration API error %s\n", cs_strerror(result));
  209. exit (1);
  210. }
  211. result = corosync_cfg_reload_config (handle);
  212. if (result != CS_OK) {
  213. printf ("Could not reload configuration. Error %s\n", cs_strerror(result));
  214. rc = (int)result;
  215. }
  216. else {
  217. printf ("Done\n");
  218. }
  219. (void)corosync_cfg_finalize (handle);
  220. return (rc);
  221. }
  222. static int reopen_log_files_do (void)
  223. {
  224. cs_error_t result;
  225. corosync_cfg_handle_t handle;
  226. int rc;
  227. rc = 0;
  228. result = corosync_cfg_initialize (&handle, NULL);
  229. if (result != CS_OK) {
  230. fprintf (stderr, "Could not initialize corosync configuration API error %s\n", cs_strerror(result));
  231. exit (1);
  232. }
  233. result = corosync_cfg_reopen_log_files (handle);
  234. if (result != CS_OK) {
  235. fprintf (stderr, "Could not reopen corosync logging files. Error %s\n", cs_strerror(result));
  236. rc = (int)result;
  237. }
  238. (void)corosync_cfg_finalize (handle);
  239. return (rc);
  240. }
  241. static void shutdown_do(void)
  242. {
  243. cs_error_t result;
  244. corosync_cfg_handle_t handle;
  245. corosync_cfg_callbacks_t callbacks;
  246. callbacks.corosync_cfg_shutdown_callback = NULL;
  247. result = corosync_cfg_initialize (&handle, &callbacks);
  248. if (result != CS_OK) {
  249. printf ("Could not initialize corosync configuration API error %d\n", result);
  250. exit (1);
  251. }
  252. printf ("Shutting down corosync\n");
  253. cs_repeat(result, 30, corosync_cfg_try_shutdown (handle, COROSYNC_CFG_SHUTDOWN_FLAG_REQUEST));
  254. if (result != CS_OK) {
  255. printf ("Could not shutdown (error = %d)\n", result);
  256. }
  257. (void)corosync_cfg_finalize (handle);
  258. }
  259. static void showaddrs_do(unsigned int nodeid)
  260. {
  261. cs_error_t result;
  262. corosync_cfg_handle_t handle;
  263. corosync_cfg_callbacks_t callbacks;
  264. int numaddrs;
  265. int i;
  266. corosync_cfg_node_address_t addrs[INTERFACE_MAX];
  267. result = corosync_cfg_initialize (&handle, &callbacks);
  268. if (result != CS_OK) {
  269. printf ("Could not initialize corosync configuration API error %d\n", result);
  270. exit (1);
  271. }
  272. if (corosync_cfg_get_node_addrs(handle, nodeid, INTERFACE_MAX, &numaddrs, addrs) == CS_OK) {
  273. for (i=0; i<numaddrs; i++) {
  274. char buf[INET6_ADDRSTRLEN];
  275. struct sockaddr_storage *ss = (struct sockaddr_storage *)addrs[i].address;
  276. struct sockaddr_in *sin = (struct sockaddr_in *)addrs[i].address;
  277. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addrs[i].address;
  278. void *saddr;
  279. if (!ss->ss_family) {
  280. continue;
  281. }
  282. if (ss->ss_family == AF_INET6) {
  283. saddr = &sin6->sin6_addr;
  284. } else {
  285. saddr = &sin->sin_addr;
  286. }
  287. inet_ntop(ss->ss_family, saddr, buf, sizeof(buf));
  288. if (i != 0) {
  289. printf(" ");
  290. }
  291. printf("%s", buf);
  292. }
  293. printf("\n");
  294. }
  295. (void)corosync_cfg_finalize (handle);
  296. }
  297. static void killnode_do(unsigned int nodeid)
  298. {
  299. cs_error_t result;
  300. corosync_cfg_handle_t handle;
  301. printf ("Killing node %d\n", nodeid);
  302. result = corosync_cfg_initialize (&handle, NULL);
  303. if (result != CS_OK) {
  304. printf ("Could not initialize corosync configuration API error %d\n", result);
  305. exit (1);
  306. }
  307. result = corosync_cfg_kill_node (handle, nodeid, "Killed by corosync-cfgtool");
  308. if (result != CS_OK) {
  309. printf ("Could not kill node (error = %d)\n", result);
  310. }
  311. (void)corosync_cfg_finalize (handle);
  312. }
  313. static void usage_do (void)
  314. {
  315. printf ("corosync-cfgtool [[-i <interface ip>] [-b] -s] [-R] [-L] [-k nodeid] [-a nodeid] [-h] [-H]\n\n");
  316. printf ("A tool for displaying and configuring active parameters within corosync.\n");
  317. printf ("options:\n");
  318. printf ("\t-i\tFinds only information about the specified interface IP address or link id when used with -s..\n");
  319. printf ("\t-s\tDisplays the status of the current links on this node(UDP/UDPU), with extended status for KNET.\n");
  320. printf ("\t-b\tDisplays the brief status of the current links on this node when used with -s.(KNET only)\n");
  321. printf ("\t-R\tTell all instances of corosync in this cluster to reload corosync.conf.\n");
  322. printf ("\t-L\tTell corosync to reopen all logging files.\n");
  323. printf ("\t-k\tKill a node identified by node id.\n");
  324. printf ("\t-a\tDisplay the IP address(es) of a node\n");
  325. printf ("\t-h\tPrint basic usage.\n");
  326. printf ("\t-H\tShutdown corosync cleanly on this node.\n");
  327. }
  328. int main (int argc, char *argv[]) {
  329. const char *options = "i:sbrRLk:a:hH";
  330. int opt;
  331. unsigned int nodeid = 0;
  332. char interface_name[128] = "";
  333. int rc = 0;
  334. enum user_action action = ACTION_NOOP;
  335. int brief = 0;
  336. while ( (opt = getopt(argc, argv, options)) != -1 ) {
  337. switch (opt) {
  338. case 'i':
  339. strncpy(interface_name, optarg, sizeof(interface_name));
  340. interface_name[sizeof(interface_name) - 1] = '\0';
  341. break;
  342. case 's':
  343. action = ACTION_LINKSTATUS_GET;
  344. break;
  345. case 'b':
  346. brief = 1;
  347. break;
  348. case 'R':
  349. action = ACTION_RELOAD_CONFIG;
  350. break;
  351. case 'L':
  352. action = ACTION_REOPEN_LOG_FILES;
  353. break;
  354. case 'k':
  355. nodeid = atoi (optarg);
  356. action = ACTION_KILL_NODE;
  357. break;
  358. case 'H':
  359. action = ACTION_SHUTDOW;
  360. break;
  361. case 'a':
  362. nodeid = atoi (optarg);
  363. action = ACTION_SHOWADDR;
  364. break;
  365. case '?':
  366. return (EXIT_FAILURE);
  367. break;
  368. case 'h':
  369. default:
  370. break;
  371. }
  372. }
  373. switch(action) {
  374. case ACTION_LINKSTATUS_GET:
  375. rc = linkstatusget_do(interface_name, brief);
  376. break;
  377. case ACTION_RELOAD_CONFIG:
  378. rc = reload_config_do();
  379. break;
  380. case ACTION_REOPEN_LOG_FILES:
  381. rc = reopen_log_files_do();
  382. break;
  383. case ACTION_KILL_NODE:
  384. killnode_do(nodeid);
  385. break;
  386. case ACTION_SHUTDOW:
  387. shutdown_do();
  388. break;
  389. case ACTION_SHOWADDR:
  390. showaddrs_do(nodeid);
  391. break;
  392. case ACTION_NOOP:
  393. default:
  394. usage_do();
  395. break;
  396. }
  397. return (rc);
  398. }