4
0

corosync-cfgtool.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * Copyright (c) 2006-2012 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. static int ringstatusget_do (char *interface_name)
  51. {
  52. cs_error_t result;
  53. corosync_cfg_handle_t handle;
  54. unsigned int interface_count;
  55. char **interface_names;
  56. char **interface_status;
  57. unsigned int i;
  58. unsigned int nodeid;
  59. int rc = 0;
  60. printf ("Printing ring status.\n");
  61. result = corosync_cfg_initialize (&handle, NULL);
  62. if (result != CS_OK) {
  63. printf ("Could not initialize corosync configuration API error %d\n", result);
  64. exit (1);
  65. }
  66. result = corosync_cfg_local_get(handle, &nodeid);
  67. if (result != CS_OK) {
  68. printf ("Could not get the local node id, the error is: %d\n", result);
  69. }
  70. else {
  71. printf ("Local node ID %d\n", nodeid);
  72. }
  73. result = corosync_cfg_ring_status_get (handle,
  74. &interface_names,
  75. &interface_status,
  76. &interface_count);
  77. if (result != CS_OK) {
  78. printf ("Could not get the ring status, the error is: %d\n", result);
  79. } else {
  80. for (i = 0; i < interface_count; i++) {
  81. if ( (interface_name &&
  82. (interface_name[0]=='\0' ||
  83. strcasecmp (interface_name, interface_names[i]) == 0)) ||
  84. !interface_name ) {
  85. printf ("RING ID %d\n", i);
  86. printf ("\tid\t= %s\n", interface_names[i]);
  87. printf ("\tstatus\t= %s\n", interface_status[i]);
  88. if (strstr(interface_status[i], "FAULTY")) {
  89. rc = 1;
  90. }
  91. }
  92. }
  93. }
  94. (void)corosync_cfg_finalize (handle);
  95. return rc;
  96. }
  97. static void ringreenable_do (void)
  98. {
  99. cs_error_t result;
  100. corosync_cfg_handle_t handle;
  101. printf ("Re-enabling all failed rings.\n");
  102. result = corosync_cfg_initialize (&handle, NULL);
  103. if (result != CS_OK) {
  104. printf ("Could not initialize corosync configuration API error %d\n", result);
  105. exit (1);
  106. }
  107. result = corosync_cfg_ring_reenable (handle);
  108. if (result != CS_OK) {
  109. printf ("Could not reenable ring error %d\n", result);
  110. }
  111. (void)corosync_cfg_finalize (handle);
  112. }
  113. static void shutdown_do(void)
  114. {
  115. cs_error_t result;
  116. corosync_cfg_handle_t handle;
  117. corosync_cfg_callbacks_t callbacks;
  118. callbacks.corosync_cfg_shutdown_callback = NULL;
  119. result = corosync_cfg_initialize (&handle, &callbacks);
  120. if (result != CS_OK) {
  121. printf ("Could not initialize corosync configuration API error %d\n", result);
  122. exit (1);
  123. }
  124. printf ("Shutting down corosync\n");
  125. result = corosync_cfg_try_shutdown (handle, COROSYNC_CFG_SHUTDOWN_FLAG_REQUEST);
  126. if (result != CS_OK) {
  127. printf ("Could not shutdown (error = %d)\n", result);
  128. }
  129. (void)corosync_cfg_finalize (handle);
  130. }
  131. static void showaddrs_do(int nodeid)
  132. {
  133. cs_error_t result;
  134. corosync_cfg_handle_t handle;
  135. corosync_cfg_callbacks_t callbacks;
  136. int numaddrs;
  137. int i;
  138. corosync_cfg_node_address_t addrs[INTERFACE_MAX];
  139. result = corosync_cfg_initialize (&handle, &callbacks);
  140. if (result != CS_OK) {
  141. printf ("Could not initialize corosync configuration API error %d\n", result);
  142. exit (1);
  143. }
  144. if (corosync_cfg_get_node_addrs(handle, nodeid, INTERFACE_MAX, &numaddrs, addrs) == CS_OK) {
  145. for (i=0; i<numaddrs; i++) {
  146. char buf[INET6_ADDRSTRLEN];
  147. struct sockaddr_storage *ss = (struct sockaddr_storage *)addrs[i].address;
  148. struct sockaddr_in *sin = (struct sockaddr_in *)addrs[i].address;
  149. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addrs[i].address;
  150. void *saddr;
  151. if (ss->ss_family == AF_INET6)
  152. saddr = &sin6->sin6_addr;
  153. else
  154. saddr = &sin->sin_addr;
  155. inet_ntop(ss->ss_family, saddr, buf, sizeof(buf));
  156. if (i != 0) {
  157. printf(" ");
  158. }
  159. printf("%s", buf);
  160. }
  161. printf("\n");
  162. }
  163. (void)corosync_cfg_finalize (handle);
  164. }
  165. static void killnode_do(unsigned int nodeid)
  166. {
  167. cs_error_t result;
  168. corosync_cfg_handle_t handle;
  169. printf ("Killing node %d\n", nodeid);
  170. result = corosync_cfg_initialize (&handle, NULL);
  171. if (result != CS_OK) {
  172. printf ("Could not initialize corosync configuration API error %d\n", result);
  173. exit (1);
  174. }
  175. result = corosync_cfg_kill_node (handle, nodeid, "Killed by corosync-cfgtool");
  176. if (result != CS_OK) {
  177. printf ("Could not kill node (error = %d)\n", result);
  178. }
  179. (void)corosync_cfg_finalize (handle);
  180. }
  181. static void usage_do (void)
  182. {
  183. printf ("corosync-cfgtool [-i <interface ip>] -s] [-r] [-H] [service_name] [-k] [nodeid] [-a] [nodeid]\n\n");
  184. printf ("A tool for displaying and configuring active parameters within corosync.\n");
  185. printf ("options:\n");
  186. printf ("\t-s\tDisplays the status of the current rings on this node.\n");
  187. printf ("\t-r\tReset redundant ring state cluster wide after a fault to\n");
  188. printf ("\t\tre-enable redundant ring operation.\n");
  189. printf ("\t-a\tDisplay the IP address(es) of a node\n");
  190. printf ("\t-k\tKill a node identified by node id.\n");
  191. printf ("\t-H\tShutdown corosync cleanly on this node.\n");
  192. }
  193. int main (int argc, char *argv[]) {
  194. const char *options = "i:srk:a:hH";
  195. int opt;
  196. unsigned int nodeid;
  197. char interface_name[128] = "";
  198. int rc=0;
  199. if (argc == 1) {
  200. usage_do ();
  201. }
  202. while ( (opt = getopt(argc, argv, options)) != -1 ) {
  203. switch (opt) {
  204. case 'i':
  205. strncpy(interface_name, optarg, sizeof(interface_name));
  206. break;
  207. case 's':
  208. rc = ringstatusget_do (interface_name);
  209. break;
  210. case 'r':
  211. ringreenable_do ();
  212. break;
  213. case 'k':
  214. nodeid = atoi (optarg);
  215. killnode_do(nodeid);
  216. break;
  217. case 'H':
  218. shutdown_do();
  219. break;
  220. case 'a':
  221. showaddrs_do( atoi(optarg) );
  222. break;
  223. case 'h':
  224. usage_do();
  225. break;
  226. }
  227. }
  228. return (rc);
  229. }