corosync-cfgtool.c 8.4 KB

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