corosync-cfgtool.c 9.5 KB

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