corosync-cfgtool.c 10 KB

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