corosync-cfgtool.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 <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 service_load_do (const char *service, unsigned int version)
  114. {
  115. cs_error_t result;
  116. corosync_cfg_handle_t handle;
  117. printf ("Loading service '%s' version '%d'\n", service, version);
  118. result = corosync_cfg_initialize (&handle, NULL);
  119. if (result != CS_OK) {
  120. printf ("Could not initialize corosync configuration API error %d\n", result);
  121. exit (1);
  122. }
  123. result = corosync_cfg_service_load (handle, service, version);
  124. if (result != CS_OK) {
  125. printf ("Could not load service (error = %d)\n", result);
  126. }
  127. (void)corosync_cfg_finalize (handle);
  128. }
  129. static void service_unload_do (const char *service, unsigned int version)
  130. {
  131. cs_error_t result;
  132. corosync_cfg_handle_t handle;
  133. printf ("Unloading service '%s' version '%d'\n", service, version);
  134. result = corosync_cfg_initialize (&handle, NULL);
  135. if (result != CS_OK) {
  136. printf ("Could not initialize corosync configuration API error %d\n", result);
  137. exit (1);
  138. }
  139. result = corosync_cfg_service_unload (handle, service, version);
  140. if (result != CS_OK) {
  141. printf ("Could not unload service (error = %d)\n", result);
  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. result = 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 crypto_do(unsigned int type)
  198. {
  199. cs_error_t result;
  200. corosync_cfg_handle_t handle;
  201. printf ("Setting crypto to mode %d\n", type);
  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_crypto_set (handle, type);
  208. if (result != CS_OK) {
  209. printf ("Could not set crypto mode (error = %d)\n", result);
  210. }
  211. (void)corosync_cfg_finalize (handle);
  212. }
  213. static void killnode_do(unsigned int nodeid)
  214. {
  215. cs_error_t result;
  216. corosync_cfg_handle_t handle;
  217. printf ("Killing node %d\n", nodeid);
  218. result = corosync_cfg_initialize (&handle, NULL);
  219. if (result != CS_OK) {
  220. printf ("Could not initialize corosync configuration API error %d\n", result);
  221. exit (1);
  222. }
  223. result = corosync_cfg_kill_node (handle, nodeid, "Killed by corosync-cfgtool");
  224. if (result != CS_OK) {
  225. printf ("Could not kill node (error = %d)\n", result);
  226. }
  227. (void)corosync_cfg_finalize (handle);
  228. }
  229. static void usage_do (void)
  230. {
  231. printf ("corosync-cfgtool [[-i <interface ip>] -s] [-r] [-l] [-u] [-H] [service_name] [-v] [version] [-k] [nodeid] [-a] [nodeid]\n\n");
  232. printf ("A tool for displaying and configuring active parameters within corosync.\n");
  233. printf ("options:\n");
  234. printf ("\t-s\tDisplays the status of the current rings on this node.\n");
  235. printf ("\t-r\tReset redundant ring state cluster wide after a fault to\n");
  236. printf ("\t\tre-enable redundant ring operation.\n");
  237. printf ("\t-l\tLoad a service identified by name.\n");
  238. printf ("\t-u\tUnload a service identified by name.\n");
  239. printf ("\t-a\tDisplay the IP address(es) of a node\n");
  240. printf ("\t-c\tSet the cryptography mode of cluster communications\n");
  241. printf ("\t-k\tKill a node identified by node id.\n");
  242. printf ("\t-H\tShutdown corosync cleanly on this node.\n");
  243. }
  244. static char *
  245. xstrdup (char const *s)
  246. {
  247. char *p = strdup (s);
  248. if (p)
  249. return (char *) p;
  250. printf ("exhausted virtual memory\n");
  251. exit (1);
  252. }
  253. int main (int argc, char *argv[]) {
  254. const char *options = "i:srl:u:v:k:a:c:hH";
  255. int opt;
  256. int service_load = 0;
  257. unsigned int nodeid;
  258. int service_unload = 0;
  259. char *service = NULL;
  260. unsigned int version = 0;
  261. char interface_name[128] = "";
  262. int rc=0;
  263. if (argc == 1) {
  264. usage_do ();
  265. }
  266. while ( (opt = getopt(argc, argv, options)) != -1 ) {
  267. switch (opt) {
  268. case 'i':
  269. strncpy(interface_name, optarg, sizeof(interface_name));
  270. break;
  271. case 's':
  272. rc = ringstatusget_do (interface_name);
  273. break;
  274. case 'r':
  275. ringreenable_do ();
  276. break;
  277. case 'l':
  278. service_load = 1;
  279. service = xstrdup (optarg);
  280. break;
  281. case 'u':
  282. service_unload = 1;
  283. service = xstrdup (optarg);
  284. break;
  285. case 'k':
  286. nodeid = atoi (optarg);
  287. killnode_do(nodeid);
  288. break;
  289. case 'H':
  290. shutdown_do();
  291. break;
  292. case 'a':
  293. showaddrs_do( atoi(optarg) );
  294. break;
  295. case 'c':
  296. crypto_do( atoi(optarg) );
  297. break;
  298. case 'v':
  299. version = atoi (optarg);
  300. break;
  301. case 'h':
  302. usage_do();
  303. break;
  304. }
  305. }
  306. if (service_load) {
  307. service_load_do (service, version);
  308. } else
  309. if (service_unload) {
  310. service_unload_do (service, version);
  311. }
  312. return (rc);
  313. }