corosync-cfgtool.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * Copyright (c) 2006-2013 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 <limits.h>
  48. #include <corosync/corotypes.h>
  49. #include <corosync/totem/totem.h>
  50. #include <corosync/cfg.h>
  51. #include "util.h"
  52. #define cs_repeat(result, max, code) \
  53. do { \
  54. int counter = 0; \
  55. do { \
  56. result = code; \
  57. if (result == CS_ERR_TRY_AGAIN) { \
  58. sleep(1); \
  59. counter++; \
  60. } else { \
  61. break; \
  62. } \
  63. } while (counter < max); \
  64. } while (0)
  65. static int ringstatusget_do (char *interface_name)
  66. {
  67. cs_error_t result;
  68. corosync_cfg_handle_t handle;
  69. unsigned int interface_count;
  70. char **interface_names;
  71. char **interface_status;
  72. unsigned int i;
  73. unsigned int nodeid;
  74. int rc = EXIT_SUCCESS;
  75. printf ("Printing ring status.\n");
  76. result = corosync_cfg_initialize (&handle, NULL);
  77. if (result != CS_OK) {
  78. fprintf (stderr, "Could not initialize corosync configuration API error %d\n", result);
  79. exit (EXIT_FAILURE);
  80. }
  81. result = corosync_cfg_local_get(handle, &nodeid);
  82. if (result != CS_OK) {
  83. fprintf (stderr, "Could not get the local node id, the error is: %d\n", result);
  84. }
  85. else {
  86. printf ("Local node ID %u\n", nodeid);
  87. }
  88. result = corosync_cfg_ring_status_get (handle,
  89. &interface_names,
  90. &interface_status,
  91. &interface_count);
  92. if (result != CS_OK) {
  93. fprintf (stderr, "Could not get the ring status, the error is: %d\n", result);
  94. } else {
  95. for (i = 0; i < interface_count; i++) {
  96. if ( (interface_name &&
  97. (interface_name[0]=='\0' ||
  98. strcasecmp (interface_name, interface_names[i]) == 0)) ||
  99. !interface_name ) {
  100. printf ("RING ID %d\n", i);
  101. printf ("\tid\t= %s\n", interface_names[i]);
  102. printf ("\tstatus\t= %s\n", interface_status[i]);
  103. if (strstr(interface_status[i], "FAULTY")) {
  104. rc = EXIT_FAILURE;
  105. }
  106. }
  107. }
  108. for (i = 0; i < interface_count; i++) {
  109. free(interface_status[i]);
  110. free(interface_names[i]);
  111. }
  112. free(interface_status);
  113. free(interface_names);
  114. }
  115. (void)corosync_cfg_finalize (handle);
  116. return rc;
  117. }
  118. static void ringreenable_do (void)
  119. {
  120. cs_error_t result;
  121. corosync_cfg_handle_t handle;
  122. printf ("Re-enabling all failed rings.\n");
  123. result = corosync_cfg_initialize (&handle, NULL);
  124. if (result != CS_OK) {
  125. fprintf (stderr, "Could not initialize corosync configuration API error %d\n", result);
  126. exit (EXIT_FAILURE);
  127. }
  128. result = corosync_cfg_ring_reenable (handle);
  129. if (result != CS_OK) {
  130. fprintf (stderr, "Could not re-enable ring error %d\n", result);
  131. }
  132. (void)corosync_cfg_finalize (handle);
  133. }
  134. static int reload_config_do (void)
  135. {
  136. cs_error_t result;
  137. corosync_cfg_handle_t handle;
  138. int rc;
  139. rc = EXIT_SUCCESS;
  140. printf ("Reloading corosync.conf...\n");
  141. result = corosync_cfg_initialize (&handle, NULL);
  142. if (result != CS_OK) {
  143. fprintf (stderr, "Could not initialize corosync configuration API error %s\n", cs_strerror(result));
  144. exit (EXIT_FAILURE);
  145. }
  146. result = corosync_cfg_reload_config (handle);
  147. if (result != CS_OK) {
  148. fprintf (stderr, "Could not reload configuration. Error %s\n", cs_strerror(result));
  149. rc = (int)result;
  150. }
  151. else {
  152. printf ("Done\n");
  153. }
  154. (void)corosync_cfg_finalize (handle);
  155. return (rc);
  156. }
  157. static void shutdown_do(void)
  158. {
  159. cs_error_t result;
  160. corosync_cfg_handle_t handle;
  161. corosync_cfg_callbacks_t callbacks;
  162. callbacks.corosync_cfg_shutdown_callback = NULL;
  163. result = corosync_cfg_initialize (&handle, &callbacks);
  164. if (result != CS_OK) {
  165. fprintf (stderr, "Could not initialize corosync configuration API error %d\n", result);
  166. exit (EXIT_FAILURE);
  167. }
  168. printf ("Shutting down corosync\n");
  169. cs_repeat(result, 30, corosync_cfg_try_shutdown (handle, COROSYNC_CFG_SHUTDOWN_FLAG_REQUEST));
  170. if (result != CS_OK) {
  171. fprintf (stderr, "Could not shutdown (error = %d)\n", result);
  172. }
  173. (void)corosync_cfg_finalize (handle);
  174. }
  175. static void showaddrs_do(unsigned int nodeid)
  176. {
  177. cs_error_t result;
  178. corosync_cfg_handle_t handle;
  179. int numaddrs;
  180. int i;
  181. corosync_cfg_node_address_t addrs[INTERFACE_MAX];
  182. result = corosync_cfg_initialize (&handle, NULL);
  183. if (result != CS_OK) {
  184. fprintf (stderr, "Could not initialize corosync configuration API error %d\n", result);
  185. exit (EXIT_FAILURE);
  186. }
  187. if (corosync_cfg_get_node_addrs(handle, nodeid, INTERFACE_MAX, &numaddrs, addrs) == CS_OK) {
  188. for (i=0; i<numaddrs; i++) {
  189. char buf[INET6_ADDRSTRLEN];
  190. struct sockaddr_storage *ss = (struct sockaddr_storage *)addrs[i].address;
  191. struct sockaddr_in *sin = (struct sockaddr_in *)addrs[i].address;
  192. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addrs[i].address;
  193. void *saddr;
  194. if (ss->ss_family == AF_INET6)
  195. saddr = &sin6->sin6_addr;
  196. else
  197. saddr = &sin->sin_addr;
  198. inet_ntop(ss->ss_family, saddr, buf, sizeof(buf));
  199. if (i != 0) {
  200. printf(" ");
  201. }
  202. printf("%s", buf);
  203. }
  204. printf("\n");
  205. }
  206. (void)corosync_cfg_finalize (handle);
  207. }
  208. static void killnode_do(unsigned int nodeid)
  209. {
  210. cs_error_t result;
  211. corosync_cfg_handle_t handle;
  212. printf ("Killing node %d\n", nodeid);
  213. result = corosync_cfg_initialize (&handle, NULL);
  214. if (result != CS_OK) {
  215. fprintf (stderr, "Could not initialize corosync configuration API error %d\n", result);
  216. exit (EXIT_FAILURE);
  217. }
  218. result = corosync_cfg_kill_node (handle, nodeid, "Killed by corosync-cfgtool");
  219. if (result != CS_OK) {
  220. fprintf (stderr, "Could not kill node (error = %d)\n", result);
  221. }
  222. (void)corosync_cfg_finalize (handle);
  223. }
  224. static void usage_do (void)
  225. {
  226. printf ("corosync-cfgtool [-i <interface ip>] [-s] [-r] [-R] [-k nodeid] [-a nodeid] [-h] [-H]\n\n");
  227. printf ("A tool for displaying and configuring active parameters within corosync.\n");
  228. printf ("options:\n");
  229. printf ("\t-i\tFinds only information about the specified interface IP address.\n");
  230. printf ("\t-s\tDisplays the status of the current rings on this node.\n");
  231. printf ("\t-r\tReset redundant ring state cluster wide after a fault to\n");
  232. printf ("\t\tre-enable redundant ring operation.\n");
  233. printf ("\t-R\tTell all instances of corosync in this cluster to reload corosync.conf.\n");
  234. printf ("\t-k\tKill a node identified by node id.\n");
  235. printf ("\t-a\tDisplay the IP address(es) of a node\n");
  236. printf ("\t-h\tPrint basic usage.\n");
  237. printf ("\t-H\tShutdown corosync cleanly on this node.\n");
  238. }
  239. int main (int argc, char *argv[]) {
  240. const char *options = "i:srRk:a:hH";
  241. int opt;
  242. unsigned int nodeid;
  243. char interface_name[128] = "";
  244. int rc = EXIT_SUCCESS;
  245. long long int l;
  246. if (argc == 1) {
  247. usage_do ();
  248. }
  249. while ( (opt = getopt(argc, argv, options)) != -1 ) {
  250. switch (opt) {
  251. case 'i':
  252. strncpy(interface_name, optarg, sizeof(interface_name));
  253. interface_name[sizeof(interface_name) - 1] = '\0';
  254. break;
  255. case 's':
  256. rc = ringstatusget_do (interface_name);
  257. break;
  258. case 'R':
  259. rc = reload_config_do ();
  260. break;
  261. case 'r':
  262. ringreenable_do ();
  263. break;
  264. case 'k':
  265. if (util_strtonum(optarg, 1, UINT_MAX, &l) == -1) {
  266. fprintf(stderr, "The nodeid was not valid, try a positive number\n");
  267. exit(EXIT_FAILURE);
  268. }
  269. nodeid = l;
  270. killnode_do(nodeid);
  271. break;
  272. case 'H':
  273. shutdown_do();
  274. break;
  275. case 'a':
  276. if (util_strtonum(optarg, 1, UINT_MAX, &l) == -1) {
  277. fprintf(stderr, "The nodeid was not valid, try a positive number\n");
  278. exit(EXIT_FAILURE);
  279. }
  280. nodeid = l;
  281. showaddrs_do(nodeid);
  282. break;
  283. case 'h':
  284. usage_do();
  285. break;
  286. }
  287. }
  288. return (rc);
  289. }