corosync-cfgtool.c 8.1 KB

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