You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbstractSystemInfoWriter.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.platform;
  21. import java.util.Collection;
  22. import org.sonar.api.utils.text.JsonWriter;
  23. import org.sonar.process.systeminfo.SystemInfoUtils;
  24. import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo;
  25. import org.sonar.server.health.Health;
  26. import org.sonar.server.telemetry.TelemetryDataLoader;
  27. import static org.sonar.server.telemetry.TelemetryDataJsonWriter.writeTelemetryData;
  28. public abstract class AbstractSystemInfoWriter implements SystemInfoWriter {
  29. private static final String[] ORDERED_SECTION_NAMES = {
  30. // standalone
  31. "System", "Database", "Plugins",
  32. // cluster
  33. "Web JVM State", "Web Database Connection", "Web Logging", "Web JVM Properties",
  34. "Compute Engine Tasks", "Compute Engine JVM State", "Compute Engine Database Connection", "Compute Engine Logging", "Compute Engine JVM Properties",
  35. "Search State", "Search Indexes"};
  36. private final TelemetryDataLoader telemetry;
  37. AbstractSystemInfoWriter(TelemetryDataLoader telemetry) {
  38. this.telemetry = telemetry;
  39. }
  40. protected void writeSections(Collection<ProtobufSystemInfo.Section> sections, JsonWriter json) {
  41. SystemInfoUtils
  42. .order(sections, ORDERED_SECTION_NAMES)
  43. .forEach(section -> writeSection(section, json));
  44. }
  45. private void writeSection(ProtobufSystemInfo.Section section, JsonWriter json) {
  46. json.name(section.getName());
  47. json.beginObject();
  48. for (ProtobufSystemInfo.Attribute attribute : section.getAttributesList()) {
  49. writeAttribute(attribute, json);
  50. }
  51. json.endObject();
  52. }
  53. private void writeAttribute(ProtobufSystemInfo.Attribute attribute, JsonWriter json) {
  54. switch (attribute.getValueCase()) {
  55. case BOOLEAN_VALUE:
  56. json.prop(attribute.getKey(), attribute.getBooleanValue());
  57. break;
  58. case LONG_VALUE:
  59. json.prop(attribute.getKey(), attribute.getLongValue());
  60. break;
  61. case DOUBLE_VALUE:
  62. json.prop(attribute.getKey(), attribute.getDoubleValue());
  63. break;
  64. case STRING_VALUE:
  65. json.prop(attribute.getKey(), attribute.getStringValue());
  66. break;
  67. case VALUE_NOT_SET:
  68. json.name(attribute.getKey()).beginArray().values(attribute.getStringValuesList()).endArray();
  69. break;
  70. default:
  71. throw new IllegalArgumentException("Unsupported type: " + attribute.getValueCase());
  72. }
  73. }
  74. protected void writeHealth(Health health, JsonWriter json) {
  75. json.prop("Health", health.getStatus().name());
  76. json.name("Health Causes").beginArray().values(health.getCauses()).endArray();
  77. }
  78. protected void writeTelemetry(JsonWriter json) {
  79. json.name("Statistics");
  80. writeTelemetryData(json, telemetry.load());
  81. }
  82. }