qdevice-net-ipc-cmd.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Copyright (c) 2015-2016 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Jan Friesse (jfriesse@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 Red Hat, 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 "log.h"
  35. #include "qdevice-net-ipc-cmd.h"
  36. #include "dynar-str.h"
  37. #include "qdevice-net-algorithm.h"
  38. #include "utils.h"
  39. static int
  40. qdevice_net_ipc_cmd_status_add_header(struct dynar *outbuf, int verbose)
  41. {
  42. return ((dynar_str_catf(outbuf, "Qdevice-net information\n") != -1) &&
  43. (dynar_str_catf(outbuf, "----------------------\n") != -1));
  44. }
  45. static int
  46. qdevice_net_ipc_cmd_status_add_tie_breaker(struct qdevice_net_instance *instance,
  47. struct dynar *outbuf, int verbose)
  48. {
  49. if (dynar_str_catf(outbuf, "Tie-breaker:\t\t") == -1) {
  50. return (0);
  51. }
  52. switch (instance->tie_breaker.mode) {
  53. case TLV_TIE_BREAKER_MODE_LOWEST:
  54. if (dynar_str_catf(outbuf, "Node with lowest node ID") == -1) {
  55. return (0);
  56. }
  57. break;
  58. case TLV_TIE_BREAKER_MODE_HIGHEST:
  59. if (dynar_str_catf(outbuf, "Node with highest node ID") == -1) {
  60. return (0);
  61. }
  62. break;
  63. case TLV_TIE_BREAKER_MODE_NODE_ID:
  64. if (dynar_str_catf(outbuf, "Node with node ID "UTILS_PRI_NODE_ID,
  65. instance->tie_breaker.node_id) == -1) {
  66. return (0);
  67. }
  68. break;
  69. }
  70. return (dynar_str_catf(outbuf, "\n") != -1);
  71. }
  72. static int
  73. qdevice_net_ipc_cmd_status_add_basic_info(struct qdevice_net_instance *instance,
  74. struct dynar *outbuf, int verbose)
  75. {
  76. if (dynar_str_catf(outbuf, "Cluster name:\t\t%s\n", instance->cluster_name) == -1) {
  77. return (0);
  78. }
  79. if (dynar_str_catf(outbuf, "QNetd host:\t\t%s:%"PRIu16"\n",
  80. instance->host_addr, instance->host_port) == -1) {
  81. return (0);
  82. }
  83. if (verbose && instance->force_ip_version != 0) {
  84. if (dynar_str_catf(outbuf, "Force IP version:\t%u\n",
  85. instance->force_ip_version) == -1) {
  86. return (0);
  87. }
  88. }
  89. if (verbose) {
  90. if ((dynar_str_catf(outbuf, "Connect timeout:\t%"PRIu32"ms\n",
  91. instance->connect_timeout) == -1) ||
  92. (dynar_str_catf(outbuf, "HB interval:\t\t%"PRIu32"ms\n",
  93. instance->heartbeat_interval) == -1) ||
  94. (dynar_str_catf(outbuf, "VQ vote timer interval:\t%"PRIu32"ms\n",
  95. instance->cast_vote_timer_interval) == -1)) {
  96. return (0);
  97. }
  98. if (dynar_str_catf(outbuf, "TLS:\t\t\t%s\n",
  99. tlv_tls_supported_to_str(instance->tls_supported)) == -1) {
  100. return (0);
  101. }
  102. }
  103. if (dynar_str_catf(outbuf, "Algorithm:\t\t%s\n",
  104. tlv_decision_algorithm_type_to_str(instance->decision_algorithm)) == -1) {
  105. return (0);
  106. }
  107. return (1);
  108. }
  109. static int
  110. qdevice_net_ipc_cmd_status_add_poll_timer_status(struct qdevice_net_instance *instance,
  111. struct dynar *outbuf, int verbose)
  112. {
  113. if (!verbose) {
  114. return (1);
  115. }
  116. if (dynar_str_catf(outbuf, "Poll timer running:\t%s",
  117. (instance->cast_vote_timer != NULL ? "Yes" : "No")) == -1) {
  118. return (0);
  119. }
  120. if (instance->cast_vote_timer != NULL && instance->cast_vote_timer_vote == TLV_VOTE_ACK) {
  121. if (dynar_str_catf(outbuf, " (cast vote)") == -1) {
  122. return (0);
  123. }
  124. }
  125. return (dynar_str_catf(outbuf, "\n") != -1);
  126. }
  127. static int
  128. qdevice_net_ipc_cmd_status_add_state(struct qdevice_net_instance *instance,
  129. struct dynar *outbuf, int verbose)
  130. {
  131. const char *state;
  132. if (instance->schedule_disconnect) {
  133. state = "Disconnected";
  134. } else {
  135. if (instance->state == QDEVICE_NET_INSTANCE_STATE_WAITING_VOTEQUORUM_CMAP_EVENTS) {
  136. state = "Connected";
  137. } else {
  138. if (instance->state != QDEVICE_NET_INSTANCE_STATE_WAITING_CONNECT ||
  139. !instance->non_blocking_client.destroyed) {
  140. state = "Connecting";
  141. } else {
  142. state = "Connect failed";
  143. }
  144. }
  145. }
  146. return (dynar_str_catf(outbuf, "State:\t\t\t%s\n", state) != -1);
  147. }
  148. static int
  149. qdevice_net_ipc_cmd_status_add_heuristics(struct qdevice_net_instance *instance,
  150. struct dynar *outbuf, int verbose)
  151. {
  152. enum qdevice_heuristics_mode active_heuristics_mode;
  153. int heuristics_enabled;
  154. active_heuristics_mode = instance->qdevice_instance_ptr->heuristics_instance.mode;
  155. heuristics_enabled = (active_heuristics_mode == QDEVICE_HEURISTICS_MODE_ENABLED ||
  156. active_heuristics_mode == QDEVICE_HEURISTICS_MODE_SYNC);
  157. if (!heuristics_enabled) {
  158. return (1);
  159. }
  160. if (dynar_str_catf(outbuf, "Heuristics result:\t%s",
  161. tlv_heuristics_to_str(instance->latest_heuristics_result)) == -1) {
  162. return (0);
  163. }
  164. if (verbose) {
  165. if (dynar_str_catf(outbuf, " (regular: %s, membership: %s, connect: %s)",
  166. tlv_heuristics_to_str(instance->latest_regular_heuristics_result),
  167. tlv_heuristics_to_str(instance->latest_vq_heuristics_result),
  168. tlv_heuristics_to_str(instance->latest_connect_heuristics_result)) == -1) {
  169. return (0);
  170. }
  171. }
  172. return (dynar_str_catf(outbuf, "\n") != -1);
  173. }
  174. static int
  175. qdevice_net_ipc_cmd_status_add_tls_state(struct qdevice_net_instance *instance,
  176. struct dynar *outbuf, int verbose)
  177. {
  178. if (!verbose || instance->state != QDEVICE_NET_INSTANCE_STATE_WAITING_VOTEQUORUM_CMAP_EVENTS) {
  179. return (1);
  180. }
  181. if (dynar_str_catf(outbuf, "TLS active:\t\t%s", (instance->using_tls ? "Yes" : "No")) == -1) {
  182. return (0);
  183. }
  184. if (instance->using_tls && instance->tls_client_cert_sent) {
  185. if (dynar_str_catf(outbuf, " (client certificate sent)") == -1) {
  186. return (0);
  187. }
  188. }
  189. return (dynar_str_catf(outbuf, "\n") != -1);
  190. }
  191. static int
  192. qdevice_net_ipc_cmd_status_add_times(struct qdevice_net_instance *instance,
  193. struct dynar *outbuf, int verbose)
  194. {
  195. struct tm tm_res;
  196. if (!verbose || instance->state != QDEVICE_NET_INSTANCE_STATE_WAITING_VOTEQUORUM_CMAP_EVENTS) {
  197. return (1);
  198. }
  199. if (instance->connected_since_time != ((time_t) -1)) {
  200. localtime_r(&instance->connected_since_time, &tm_res);
  201. if (dynar_str_catf(outbuf, "Connected since:\t%04d-%02d-%02dT%02d:%02d:%02d\n",
  202. tm_res.tm_year + 1900, tm_res.tm_mon + 1, tm_res.tm_mday,
  203. tm_res.tm_hour, tm_res.tm_min, tm_res.tm_sec) == -1) {
  204. return (0);
  205. }
  206. }
  207. if (instance->last_echo_reply_received_time != ((time_t) -1)) {
  208. localtime_r(&instance->last_echo_reply_received_time, &tm_res);
  209. if (dynar_str_catf(outbuf, "Echo reply received:\t%04d-%02d-%02dT%02d:%02d:%02d\n",
  210. tm_res.tm_year + 1900, tm_res.tm_mon + 1, tm_res.tm_mday,
  211. tm_res.tm_hour, tm_res.tm_min, tm_res.tm_sec) == -1) {
  212. return (0);
  213. }
  214. }
  215. return (1);
  216. }
  217. int
  218. qdevice_net_ipc_cmd_status(struct qdevice_net_instance *instance, struct dynar *outbuf, int verbose)
  219. {
  220. if (qdevice_net_ipc_cmd_status_add_header(outbuf, verbose) &&
  221. qdevice_net_ipc_cmd_status_add_basic_info(instance, outbuf, verbose) &&
  222. qdevice_net_ipc_cmd_status_add_tie_breaker(instance, outbuf, verbose) &&
  223. qdevice_net_ipc_cmd_status_add_poll_timer_status(instance, outbuf, verbose) &&
  224. qdevice_net_ipc_cmd_status_add_state(instance, outbuf, verbose) &&
  225. qdevice_net_ipc_cmd_status_add_heuristics(instance, outbuf, verbose) &&
  226. qdevice_net_ipc_cmd_status_add_tls_state(instance, outbuf, verbose) &&
  227. qdevice_net_ipc_cmd_status_add_times(instance, outbuf, verbose)) {
  228. return (1);
  229. }
  230. return (0);
  231. }