corosync-cfgtool.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * Copyright (c) 2006-2017 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
  64. linkstatusget_do (char *interface_name, int brief)
  65. {
  66. cs_error_t result;
  67. corosync_cfg_handle_t handle;
  68. unsigned int interface_count;
  69. char **interface_names;
  70. char **interface_status;
  71. unsigned int i;
  72. unsigned int nodeid;
  73. int rc = 0;
  74. int len, s = 0, t;
  75. printf ("Printing link status.\n");
  76. result = corosync_cfg_initialize (&handle, NULL);
  77. if (result != CS_OK) {
  78. printf ("Could not initialize corosync configuration API error %d\n", result);
  79. exit (1);
  80. }
  81. result = corosync_cfg_local_get(handle, &nodeid);
  82. if (result != CS_OK) {
  83. printf ("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. printf ("Could not get the link status, the error is: %d\n", result);
  94. } else {
  95. for (i = 0; i < interface_count; i++) {
  96. if ( (interface_name &&
  97. interface_names[i][0] != '\0' &&
  98. (interface_name[0]=='\0' ||
  99. strcasecmp (interface_name, interface_names[i]) == 0)) ||
  100. !interface_name ) {
  101. printf ("LINK ID %d\n", i);
  102. printf ("\tid\t= %s\n", interface_names[i]);
  103. if((!brief) && (strcmp(interface_status[i], "OK") != 0) &&
  104. (!strstr(interface_status[i], "FAULTY"))) {
  105. len = strlen(interface_status[i]);
  106. printf ("\tstatus:\n");
  107. while(s < len) {
  108. t = interface_status[i][s] - '0';
  109. printf("\t\tnode %d:\n", s++);
  110. printf("link enabled:%d\t", t&1? 1 : 0);
  111. printf("link connected:%d\n", t&2? 1: 0);
  112. }
  113. } else {
  114. printf ("\tstatus\t= %s\n", interface_status[i]);
  115. if (strstr(interface_status[i], "FAULTY")) {
  116. rc = 1;
  117. }
  118. }
  119. }
  120. }
  121. for (i = 0; i < interface_count; i++) {
  122. free(interface_status[i]);
  123. free(interface_names[i]);
  124. }
  125. free(interface_status);
  126. free(interface_names);
  127. }
  128. (void)corosync_cfg_finalize (handle);
  129. return rc;
  130. }
  131. static int reload_config_do (void)
  132. {
  133. cs_error_t result;
  134. corosync_cfg_handle_t handle;
  135. int rc;
  136. rc = 0;
  137. printf ("Reloading corosync.conf...\n");
  138. result = corosync_cfg_initialize (&handle, NULL);
  139. if (result != CS_OK) {
  140. printf ("Could not initialize corosync configuration API error %s\n", cs_strerror(result));
  141. exit (1);
  142. }
  143. result = corosync_cfg_reload_config (handle);
  144. if (result != CS_OK) {
  145. printf ("Could not reload configuration. Error %s\n", cs_strerror(result));
  146. rc = (int)result;
  147. }
  148. else {
  149. printf ("Done\n");
  150. }
  151. (void)corosync_cfg_finalize (handle);
  152. return (rc);
  153. }
  154. static void shutdown_do(void)
  155. {
  156. cs_error_t result;
  157. corosync_cfg_handle_t handle;
  158. corosync_cfg_callbacks_t callbacks;
  159. callbacks.corosync_cfg_shutdown_callback = NULL;
  160. result = corosync_cfg_initialize (&handle, &callbacks);
  161. if (result != CS_OK) {
  162. printf ("Could not initialize corosync configuration API error %d\n", result);
  163. exit (1);
  164. }
  165. printf ("Shutting down corosync\n");
  166. cs_repeat(result, 30, corosync_cfg_try_shutdown (handle, COROSYNC_CFG_SHUTDOWN_FLAG_REQUEST));
  167. if (result != CS_OK) {
  168. printf ("Could not shutdown (error = %d)\n", result);
  169. }
  170. (void)corosync_cfg_finalize (handle);
  171. }
  172. static void showaddrs_do(unsigned int nodeid)
  173. {
  174. cs_error_t result;
  175. corosync_cfg_handle_t handle;
  176. corosync_cfg_callbacks_t callbacks;
  177. int numaddrs;
  178. int i;
  179. corosync_cfg_node_address_t addrs[INTERFACE_MAX];
  180. result = corosync_cfg_initialize (&handle, &callbacks);
  181. if (result != CS_OK) {
  182. printf ("Could not initialize corosync configuration API error %d\n", result);
  183. exit (1);
  184. }
  185. if (corosync_cfg_get_node_addrs(handle, nodeid, INTERFACE_MAX, &numaddrs, addrs) == CS_OK) {
  186. for (i=0; i<numaddrs; i++) {
  187. char buf[INET6_ADDRSTRLEN];
  188. struct sockaddr_storage *ss = (struct sockaddr_storage *)addrs[i].address;
  189. struct sockaddr_in *sin = (struct sockaddr_in *)addrs[i].address;
  190. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addrs[i].address;
  191. void *saddr;
  192. if (ss->ss_family == AF_INET6)
  193. saddr = &sin6->sin6_addr;
  194. else
  195. saddr = &sin->sin_addr;
  196. inet_ntop(ss->ss_family, saddr, buf, sizeof(buf));
  197. if (i != 0) {
  198. printf(" ");
  199. }
  200. printf("%s", buf);
  201. }
  202. printf("\n");
  203. }
  204. (void)corosync_cfg_finalize (handle);
  205. }
  206. static void killnode_do(unsigned int nodeid)
  207. {
  208. cs_error_t result;
  209. corosync_cfg_handle_t handle;
  210. printf ("Killing node %d\n", nodeid);
  211. result = corosync_cfg_initialize (&handle, NULL);
  212. if (result != CS_OK) {
  213. printf ("Could not initialize corosync configuration API error %d\n", result);
  214. exit (1);
  215. }
  216. result = corosync_cfg_kill_node (handle, nodeid, "Killed by corosync-cfgtool");
  217. if (result != CS_OK) {
  218. printf ("Could not kill node (error = %d)\n", result);
  219. }
  220. (void)corosync_cfg_finalize (handle);
  221. }
  222. static void usage_do (void)
  223. {
  224. printf ("corosync-cfgtool [-i <interface ip>] [[-b] -s] [-R] [-k nodeid] [-a nodeid] [-h] [-H]\n\n");
  225. printf ("A tool for displaying and configuring active parameters within corosync.\n");
  226. printf ("options:\n");
  227. printf ("\t-i\tFinds only information about the specified interface IP address.\n");
  228. printf ("\t-s\tDisplays the status of the current links on this node(UDP/UDPU), while extended status for KNET.\n");
  229. printf ("\t-b\tDisplays the brief status of the current links on this node when used with -s.(KNET only)\n");
  230. printf ("\t-R\tTell all instances of corosync in this cluster to reload corosync.conf.\n");
  231. printf ("\t-k\tKill a node identified by node id.\n");
  232. printf ("\t-a\tDisplay the IP address(es) of a node\n");
  233. printf ("\t-h\tPrint basic usage.\n");
  234. printf ("\t-H\tShutdown corosync cleanly on this node.\n");
  235. }
  236. int main (int argc, char *argv[]) {
  237. const char *options = "i:sbrRk:a:hH";
  238. int opt;
  239. unsigned int nodeid;
  240. char interface_name[128] = "";
  241. int rc=0;
  242. int getstatus=0, brief=0;
  243. if (argc == 1) {
  244. usage_do ();
  245. }
  246. while ( (opt = getopt(argc, argv, options)) != -1 ) {
  247. switch (opt) {
  248. case 'i':
  249. strncpy(interface_name, optarg, sizeof(interface_name));
  250. interface_name[sizeof(interface_name) - 1] = '\0';
  251. break;
  252. case 's':
  253. getstatus=1;
  254. break;
  255. case 'b':
  256. brief = 1;
  257. break;
  258. case 'R':
  259. rc = reload_config_do ();
  260. break;
  261. case 'k':
  262. nodeid = atoi (optarg);
  263. killnode_do(nodeid);
  264. break;
  265. case 'H':
  266. shutdown_do();
  267. break;
  268. case 'a':
  269. nodeid = atoi (optarg);
  270. showaddrs_do(nodeid);
  271. break;
  272. case 'h':
  273. usage_do();
  274. break;
  275. }
  276. }
  277. if(getstatus) {
  278. linkstatusget_do(interface_name, brief);
  279. } else if (brief) {
  280. usage_do();
  281. }
  282. return (rc);
  283. }