qdevice-ipc-cmd.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * Copyright (c) 2015-2020 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-ipc-cmd.h"
  36. #include "qdevice-model.h"
  37. #include "dynar-str.h"
  38. #include "utils.h"
  39. static int
  40. qdevice_ipc_cmd_status_add_header(struct qdevice_instance *instance, struct dynar *outbuf,
  41. int verbose)
  42. {
  43. return ((dynar_str_catf(outbuf, "Qdevice information\n") != -1) &&
  44. (dynar_str_catf(outbuf, "-------------------\n") != -1));
  45. }
  46. static int
  47. qdevice_ipc_cmd_status_add_model(struct qdevice_instance *instance, struct dynar *outbuf,
  48. int verbose)
  49. {
  50. return (dynar_str_catf(outbuf, "Model:\t\t\t%s\n",
  51. qdevice_model_type_to_str(instance->model_type)) != -1);
  52. }
  53. static int
  54. qdevice_ipc_cmd_status_add_nodeid(struct qdevice_instance *instance, struct dynar *outbuf,
  55. int verbose)
  56. {
  57. return (dynar_str_catf(outbuf, "Node ID:\t\t"UTILS_PRI_NODE_ID"\n",
  58. instance->node_id) != -1);
  59. }
  60. static int
  61. qdevice_ipc_cmd_status_add_intervals(struct qdevice_instance *instance, struct dynar *outbuf,
  62. int verbose)
  63. {
  64. if (!verbose) {
  65. return (1);
  66. }
  67. return ((dynar_str_catf(outbuf, "HB interval:\t\t%"PRIu32"ms\n",
  68. instance->heartbeat_interval) != -1) &&
  69. (dynar_str_catf(outbuf, "Sync HB interval:\t%"PRIu32"ms\n",
  70. instance->sync_heartbeat_interval) != -1));
  71. }
  72. static int
  73. qdevice_ipc_cmd_status_add_config_node_list(struct qdevice_instance *instance, struct dynar *outbuf,
  74. int verbose)
  75. {
  76. struct node_list_entry *node_info;
  77. size_t zi;
  78. if (instance->config_node_list_version_set) {
  79. if (dynar_str_catf(outbuf, "Configuration version:\t"UTILS_PRI_CONFIG_VERSION"\n",
  80. instance->config_node_list_version) == -1) {
  81. return (0);
  82. }
  83. }
  84. if (dynar_str_catf(outbuf, "Configured node list:\n") == -1) {
  85. return (0);
  86. }
  87. zi = 0;
  88. TAILQ_FOREACH(node_info, &instance->config_node_list, entries) {
  89. if ((dynar_str_catf(outbuf, " %zu\tNode ID = "UTILS_PRI_NODE_ID, zi,
  90. node_info->node_id) == -1) ||
  91. (node_info->data_center_id != 0 && dynar_str_catf(outbuf, ", Data center ID = "
  92. UTILS_PRI_DATACENTER_ID, node_info->data_center_id) == -1) ||
  93. (dynar_str_catf(outbuf, "\n") == -1)) {
  94. return (0);
  95. }
  96. zi++;
  97. }
  98. return (1);
  99. }
  100. static int
  101. qdevice_ipc_cmd_status_add_membership_node_list(struct qdevice_instance *instance, struct dynar *outbuf,
  102. int verbose)
  103. {
  104. uint32_t u32;
  105. if (verbose && dynar_str_catf(outbuf, "Ring ID:\t\t"UTILS_PRI_RING_ID"\n",
  106. instance->vq_node_list_ring_id.nodeid, instance->vq_node_list_ring_id.seq) == -1) {
  107. return (0);
  108. }
  109. if (dynar_str_catf(outbuf, "Membership node list:\t") == -1) {
  110. return (0);
  111. }
  112. for (u32 = 0; u32 < instance->vq_node_list_entries; u32++) {
  113. if (u32 != 0) {
  114. if (dynar_str_catf(outbuf, ", ") == -1) {
  115. return (0);
  116. }
  117. }
  118. if (dynar_str_catf(outbuf, UTILS_PRI_NODE_ID, instance->vq_node_list[u32]) == -1) {
  119. return (0);
  120. }
  121. }
  122. if (dynar_str_catf(outbuf, "\n") == -1) {
  123. return (0);
  124. }
  125. return (1);
  126. }
  127. static const char *
  128. qdevice_ipc_cmd_vq_nodestate_to_str(uint32_t state)
  129. {
  130. switch (state) {
  131. case VOTEQUORUM_NODESTATE_MEMBER: return ("member"); break;
  132. case VOTEQUORUM_NODESTATE_DEAD: return ("dead"); break;
  133. case VOTEQUORUM_NODESTATE_LEAVING: return ("leaving"); break;
  134. default:
  135. log(LOG_ERR, "qdevice_ipc_cmd_vq_nodestate_to_str: Unhandled votequorum "
  136. "node state %"PRIu32, state);
  137. exit(EXIT_FAILURE);
  138. break;
  139. }
  140. return ("Unhandled votequorum node state");
  141. }
  142. static int
  143. qdevice_ipc_cmd_status_add_quorum_node_list(struct qdevice_instance *instance, struct dynar *outbuf,
  144. int verbose)
  145. {
  146. uint32_t u32;
  147. votequorum_node_t *node;
  148. if (!verbose) {
  149. return (1);
  150. }
  151. if (dynar_str_catf(outbuf, "Quorate:\t\t%s\n",
  152. (instance->vq_quorum_quorate ? "Yes" : "No")) == -1) {
  153. return (0);
  154. }
  155. if (dynar_str_catf(outbuf, "Quorum node list:\n") == -1) {
  156. return (0);
  157. }
  158. for (u32 = 0; u32 < instance->vq_quorum_node_list_entries; u32++) {
  159. node = &instance->vq_quorum_node_list[u32];
  160. if (node->nodeid == 0) {
  161. continue;
  162. }
  163. if (dynar_str_catf(outbuf, " %"PRIu32"\tNode ID = "UTILS_PRI_NODE_ID
  164. ", State = %s\n", u32, node->nodeid,
  165. qdevice_ipc_cmd_vq_nodestate_to_str(node->state)) == -1) {
  166. return (0);
  167. }
  168. }
  169. return (1);
  170. }
  171. static int
  172. qdevice_ipc_cmd_status_add_expected_votes(struct qdevice_instance *instance, struct dynar *outbuf,
  173. int verbose)
  174. {
  175. if (!verbose) {
  176. return (1);
  177. }
  178. return (dynar_str_catf(outbuf, "Expected votes:\t\t"UTILS_PRI_EXPECTED_VOTES"\n",
  179. instance->vq_expected_votes) != -1);
  180. }
  181. static int
  182. qdevice_ipc_cmd_status_add_last_poll(struct qdevice_instance *instance, struct dynar *outbuf,
  183. int verbose)
  184. {
  185. struct tm tm_res;
  186. if (!verbose) {
  187. return (1);
  188. }
  189. if (instance->vq_last_poll == ((time_t) -1)) {
  190. return (dynar_str_catf(outbuf, "Last poll call:\t\tNever\n") != -1);
  191. }
  192. localtime_r(&instance->vq_last_poll, &tm_res);
  193. if (dynar_str_catf(outbuf, "Last poll call:\t\t%04d-%02d-%02dT%02d:%02d:%02d%s\n",
  194. tm_res.tm_year + 1900, tm_res.tm_mon + 1, tm_res.tm_mday,
  195. tm_res.tm_hour, tm_res.tm_min, tm_res.tm_sec,
  196. (instance->vq_last_poll_cast_vote ? " (cast vote)" : "")) == -1) {
  197. return (0);
  198. }
  199. return (1);
  200. }
  201. static int
  202. qdevice_ipc_cmd_status_add_heuristics(struct qdevice_instance *instance, struct dynar *outbuf,
  203. int verbose)
  204. {
  205. if (!verbose) {
  206. return (1);
  207. }
  208. return (dynar_str_catf(outbuf, "Heuristics:\t\t%s\n",
  209. qdevice_heuristics_mode_to_str(instance->heuristics_instance.mode)) != 0);
  210. }
  211. int
  212. qdevice_ipc_cmd_status(struct qdevice_instance *instance, struct dynar *outbuf, int verbose)
  213. {
  214. if (qdevice_ipc_cmd_status_add_header(instance, outbuf, verbose) &&
  215. qdevice_ipc_cmd_status_add_model(instance, outbuf, verbose) &&
  216. qdevice_ipc_cmd_status_add_nodeid(instance, outbuf, verbose) &&
  217. qdevice_ipc_cmd_status_add_intervals(instance, outbuf, verbose) &&
  218. qdevice_ipc_cmd_status_add_config_node_list(instance, outbuf, verbose) &&
  219. qdevice_ipc_cmd_status_add_heuristics(instance, outbuf, verbose) &&
  220. qdevice_ipc_cmd_status_add_membership_node_list(instance, outbuf, verbose) &&
  221. qdevice_ipc_cmd_status_add_quorum_node_list(instance, outbuf, verbose) &&
  222. qdevice_ipc_cmd_status_add_expected_votes(instance, outbuf, verbose) &&
  223. qdevice_ipc_cmd_status_add_last_poll(instance, outbuf, verbose) &&
  224. dynar_str_catf(outbuf, "\n") != -1 &&
  225. qdevice_model_ipc_cmd_status(instance, outbuf, verbose) != -1) {
  226. return (0);
  227. }
  228. return (-1);
  229. }