@Test public void load_info_from_all_nodes() throws Exception { DistributedAnswer<SystemInfo> answer = new DistributedAnswer<>(); answer.setAnswer(newMember("foo"), SystemInfo.newBuilder().addSections(Section.newBuilder().build()).build()); answer.setTimedOut(newMember("bar")); answer.setFailed(newMember("baz"), new IOException("BOOM")); when(hzMember.call(any(DistributedCall.class), any(MemberSelector.class), anyLong())).thenReturn(answer); Collection<NodeInfo> nodes = underTest.load(); assertThat(nodes).hasSize(3); NodeInfo successfulNodeInfo = findNode(nodes, "foo"); assertThat(successfulNodeInfo.getName()).isEqualTo("foo"); assertThat(successfulNodeInfo.getHost()).hasValue(AN_ADDRESS.getHostAddress()); assertThat(successfulNodeInfo.getErrorMessage()).isEmpty(); assertThat(successfulNodeInfo.getSections()).hasSize(1); NodeInfo timedOutNodeInfo = findNode(nodes, "bar"); assertThat(timedOutNodeInfo.getName()).isEqualTo("bar"); assertThat(timedOutNodeInfo.getErrorMessage()).hasValue("Failed to retrieve information on time"); assertThat(timedOutNodeInfo.getSections()).isEmpty(); NodeInfo failedNodeInfo = findNode(nodes, "baz"); assertThat(failedNodeInfo.getName()).isEqualTo("baz"); assertThat(failedNodeInfo.getErrorMessage()).hasValue("Failed to retrieve information: BOOM"); assertThat(failedNodeInfo.getSections()).isEmpty(); }
@Test public void write_json() { logInAsSystemAdministrator(); ProtobufSystemInfo.Section.Builder attributes1 = ProtobufSystemInfo.Section.newBuilder() .setName("Section One"); setAttribute(attributes1, "foo", "bar"); when(section1.toProtobuf()).thenReturn(attributes1.build()); ProtobufSystemInfo.Section.Builder attributes2 = ProtobufSystemInfo.Section.newBuilder() .setName("Section Two"); setAttribute(attributes2, "one", 1); setAttribute(attributes2, "two", 2); when(section2.toProtobuf()).thenReturn(attributes2.build()); when(ceHttpClient.retrieveSystemInfo()).thenReturn(Optional.empty()); StringWriter writer = new StringWriter(); JsonWriter jsonWriter = JsonWriter.of(writer); jsonWriter.beginObject(); underTest.write(jsonWriter); jsonWriter.endObject(); // response does not contain empty "Section Three" assertThat(writer.toString()).isEqualTo("{\"Health\":\"GREEN\",\"Health Causes\":[],\"Section One\":{\"foo\":\"bar\"},\"Section Two\":{\"one\":1,\"two\":2}," + "\"Statistics\":{\"id\":\"\",\"version\":\"\",\"database\":{\"name\":\"\",\"version\":\"\"},\"plugins\":[],\"userCount\":0,\"projectCount\":0,\"usingBranches\":false," + "\"ncloc\":0,\"projectCountByLanguage\":[],\"nclocByLanguage\":[]}}"); }
public static void setAttribute(Section.Builder section, String key, @Nullable Collection<String> values) { if (values != null) { section.addAttributesBuilder() .setKey(key) .addAllStringValues(values) .build(); } }
@Override public ProtobufSystemInfo.Section toProtobuf() { ProtobufSystemInfo.Section.Builder protobuf = ProtobufSystemInfo.Section.newBuilder(); protobuf.setName("System"); setAttribute(protobuf, "Server ID", server.getId()); setAttribute(protobuf, "Version", getVersion()); setAttribute(protobuf, "External User Authentication", getExternalUserAuthentication()); addIfNotEmpty(protobuf, "Accepted external identity providers", getEnabledIdentityProviders()); addIfNotEmpty(protobuf, "External identity providers whose users are allowed to sign themselves up", getAllowsToSignUpEnabledIdentityProviders()); setAttribute(protobuf, "High Availability", false); setAttribute(protobuf, "Official Distribution", officialDistribution.check()); setAttribute(protobuf, "Force authentication", getForceAuthentication()); setAttribute(protobuf, "Home Dir", config.get(PATH_HOME.getKey()).orElse(null)); setAttribute(protobuf, "Data Dir", config.get(PATH_DATA.getKey()).orElse(null)); setAttribute(protobuf, "Temp Dir", config.get(PATH_TEMP.getKey()).orElse(null)); setAttribute(protobuf, "Processors", Runtime.getRuntime().availableProcessors()); return protobuf.build(); }
@Override public ProtobufSystemInfo.Section toProtobuf() { ProtobufSystemInfo.Section.Builder builder = ProtobufSystemInfo.Section.newBuilder(); builder.setName("Compute Engine Database Connection"); builder.addAttributesBuilder().setKey("Pool Initial Size").setLongValue(getPoolInitialSize()).build(); builder.addAttributesBuilder().setKey("Pool Active Connections").setLongValue(getPoolActiveConnections()).build(); builder.addAttributesBuilder().setKey("Pool Idle Connections").setLongValue(getPoolIdleConnections()).build(); builder.addAttributesBuilder().setKey("Pool Max Active Connections").setLongValue(getPoolMaxActiveConnections()).build(); builder.addAttributesBuilder().setKey("Pool Max Idle Connections").setLongValue(getPoolMaxIdleConnections()).build(); builder.addAttributesBuilder().setKey("Pool Min Idle Connections").setLongValue(getPoolMinIdleConnections()).build(); builder.addAttributesBuilder().setKey("Pool Max Wait (ms)").setLongValue(getPoolMaxWaitMillis()).build(); builder.addAttributesBuilder().setKey("Pool Remove Abandoned").setBooleanValue(getPoolRemoveAbandoned()).build(); builder.addAttributesBuilder().setKey("Pool Remove Abandoned Timeout (sec)").setLongValue(getPoolRemoveAbandonedTimeoutSeconds()).build(); return builder.build(); } }
@Override public Section toProtobuf() { Section.Builder protobuf = Section.newBuilder(); protobuf.setName("Database"); try (DbSession dbSession = dbClient.openSession(false)) { DatabaseMetaData metadata = dbSession.getConnection().getMetaData(); setAttribute(protobuf, "Database", metadata.getDatabaseProductName()); setAttribute(protobuf, "Database Version", metadata.getDatabaseProductVersion()); setAttribute(protobuf, "Username", metadata.getUserName()); setAttribute(protobuf, "URL", metadata.getURL()); setAttribute(protobuf, "Driver", metadata.getDriverName()); setAttribute(protobuf, "Driver Version", metadata.getDriverVersion()); } catch (SQLException e) { throw new IllegalStateException("Fail to get DB metadata", e); } return protobuf.build(); } }
@Override public ProtobufSystemInfo.Section toProtobuf() { ProtobufSystemInfo.Section.Builder protobuf = ProtobufSystemInfo.Section.newBuilder(); protobuf.setName("Plugins"); for (PluginInfo plugin : repository.getPluginInfos()) { String label = "[" + plugin.getName() + "]"; Version version = plugin.getVersion(); if (version != null) { label = version.getName() + " " + label; } setAttribute(protobuf, plugin.getKey(), label); } return protobuf.build(); } }
@Override public ProtobufSystemInfo.Section toProtobuf() { ProtobufSystemInfo.Section.Builder protobuf = ProtobufSystemInfo.Section.newBuilder(); protobuf.setName("Search State"); try { setAttribute(protobuf, "State", getStateAsEnum().name()); completeNodeAttributes(protobuf); } catch (Exception es) { Loggers.get(EsStateSection.class).warn("Failed to retrieve ES attributes. There will be only a single \"state\" attribute.", es); setAttribute(protobuf, "State", es.getCause() instanceof ElasticsearchException ? es.getCause().getMessage() : es.getMessage()); } return protobuf.build(); }
@Override public ProtobufSystemInfo.Section toProtobuf() { ProtobufSystemInfo.Section.Builder protobuf = ProtobufSystemInfo.Section.newBuilder(); if (runtime.getSonarQubeSide() == SonarQubeSide.COMPUTE_ENGINE) { protobuf.setName("Compute Engine Logging"); } else { protobuf.setName("Web Logging"); } SystemInfoUtils.setAttribute(protobuf, "Logs Level", logging.getRootLoggerLevel().name()); SystemInfoUtils.setAttribute(protobuf, "Logs Dir", logging.getLogsDir().getAbsolutePath()); return protobuf.build(); } }
@Override public ProtobufSystemInfo.Section toProtobuf() { ProtobufSystemInfo.Section.Builder protobuf = ProtobufSystemInfo.Section.newBuilder(); protobuf.setName("System"); setAttribute(protobuf, "Version", server.getVersion()); setAttribute(protobuf, "Official Distribution", officialDistribution.check()); setAttribute(protobuf, "Home Dir", config.get(PATH_HOME.getKey()).orElse(null)); setAttribute(protobuf, "Data Dir", config.get(PATH_DATA.getKey()).orElse(null)); setAttribute(protobuf, "Temp Dir", config.get(PATH_TEMP.getKey()).orElse(null)); setAttribute(protobuf, "Processors", Runtime.getRuntime().availableProcessors()); return protobuf.build(); }
private static NodeInfo toNodeInfo(NodeStats stat) { String nodeName = stat.getNode().getName(); NodeInfo info = new NodeInfo(nodeName); info.setHost(stat.getHostname()); ProtobufSystemInfo.Section.Builder section = ProtobufSystemInfo.Section.newBuilder(); section.setName("Search State"); EsStateSection.toProtobuf(stat, section); info.addSection(section.build()); return info; }
@Override public ProtobufSystemInfo.Section toProtobuf() { ProtobufSystemInfo.Section.Builder protobuf = ProtobufSystemInfo.Section.newBuilder(); protobuf.setName("Settings"); PropertyDefinitions definitions = settings.getDefinitions(); TreeMap<String, String> orderedProps = new TreeMap<>(settings.getProperties()); for (Map.Entry<String, String> prop : orderedProps.entrySet()) { String key = prop.getKey(); String value = obfuscateValue(definitions, key, prop.getValue()); setAttribute(protobuf, key, value); } return protobuf.build(); }
@Override public ProtobufSystemInfo.Section toProtobuf() { ProtobufSystemInfo.Section.Builder builder = ProtobufSystemInfo.Section.newBuilder(); builder.setName("Compute Engine Tasks"); builder.addAttributesBuilder().setKey("Pending").setLongValue(getPendingCount()).build(); builder.addAttributesBuilder().setKey("In Progress").setLongValue(getInProgressCount()).build(); builder.addAttributesBuilder().setKey("Processed With Error").setLongValue(getErrorCount()).build(); builder.addAttributesBuilder().setKey("Processed With Success").setLongValue(getSuccessCount()).build(); builder.addAttributesBuilder().setKey("Processing Time (ms)").setLongValue(getProcessingTime()).build(); builder.addAttributesBuilder().setKey("Worker Count").setLongValue(getWorkerCount()).build(); builder.addAttributesBuilder().setKey("Max Worker Count").setLongValue(getWorkerMaxCount()).build(); return builder.build(); } }
@Override public ProtobufSystemInfo.Section toProtobuf() { ProtobufSystemInfo.Section.Builder protobuf = ProtobufSystemInfo.Section.newBuilder(); protobuf.setName("Search Indexes"); try { completeIndexAttributes(protobuf); } catch (Exception es) { Loggers.get(EsIndexesSection.class).warn("Failed to retrieve ES attributes. There will be only a single \"Error\" attribute.", es); setAttribute(protobuf, "Error", es.getCause() instanceof ElasticsearchException ? es.getCause().getMessage() : es.getMessage()); } return protobuf.build(); }
@Override public ProtobufSystemInfo.Section toProtobuf() { ProtobufSystemInfo.Section.Builder protobuf = ProtobufSystemInfo.Section.newBuilder(); protobuf.setName("Compute Engine Tasks"); try (DbSession dbSession = dbClient.openSession(false)) { setAttribute(protobuf, "Total Pending", dbClient.ceQueueDao().countByStatus(dbSession, CeQueueDto.Status.PENDING)); setAttribute(protobuf, "Total In Progress", dbClient.ceQueueDao().countByStatus(dbSession, CeQueueDto.Status.IN_PROGRESS)); setAttribute(protobuf, "Max Workers per Node", workerCountProvider == null ? DEFAULT_NB_OF_WORKERS : workerCountProvider.get()); setAttribute(protobuf, "Workers Paused", "true".equals(dbClient.internalPropertiesDao().selectByKey(dbSession, InternalProperties.COMPUTE_ENGINE_PAUSE).orElse(null))); } return protobuf.build(); } }
@Override public ProtobufSystemInfo.Section toProtobuf() { ProtobufSystemInfo.Section.Builder protobuf = ProtobufSystemInfo.Section.newBuilder(); protobuf.setName("System"); setAttribute(protobuf, "Server ID", server.getId()); setAttribute(protobuf, "High Availability", true); setAttribute(protobuf, "External User Authentication", getExternalUserAuthentication()); addIfNotEmpty(protobuf, "Accepted external identity providers", getEnabledIdentityProviders()); addIfNotEmpty(protobuf, "External identity providers whose users are allowed to sign themselves up", getAllowsToSignUpEnabledIdentityProviders()); setAttribute(protobuf, "Force authentication", getForceAuthentication()); return protobuf.build(); }
@Override public ProtobufSystemInfo.Section toProtobuf() { ProtobufSystemInfo.Section.Builder protobuf = ProtobufSystemInfo.Section.newBuilder(); protobuf.setName("Search State"); ClusterStatsResponse stats = esClient.prepareClusterStats().get(); setAttribute(protobuf, "State", stats.getStatus().name()); setAttribute(protobuf, "Nodes", stats.getNodesStats().getCounts().getTotal()); return protobuf.build(); }
@Override public Section toProtobuf() { Section.Builder protobuf = Section.newBuilder(); protobuf.setName("Database"); try (DbSession dbSession = dbClient.openSession(false)) { DatabaseMetaData metadata = dbSession.getConnection().getMetaData(); setAttribute(protobuf, "Database", metadata.getDatabaseProductName()); setAttribute(protobuf, "Database Version", metadata.getDatabaseProductVersion()); setAttribute(protobuf, "Username", metadata.getUserName()); setAttribute(protobuf, "URL", metadata.getURL()); setAttribute(protobuf, "Driver", metadata.getDriverName()); setAttribute(protobuf, "Driver Version", metadata.getDriverVersion()); } catch (SQLException e) { throw new IllegalStateException("Fail to get DB metadata", e); } return protobuf.build(); } }
ProtobufSystemInfo.Section toProtobuf(MemoryMXBean memoryBean) { ProtobufSystemInfo.Section.Builder protobuf = ProtobufSystemInfo.Section.newBuilder(); protobuf.setName(name); addAttributeInMb(protobuf,"Max Memory (MB)", Runtime.getRuntime().maxMemory()); addAttributeInMb(protobuf, "Free Memory (MB)", Runtime.getRuntime().freeMemory()); MemoryUsage heap = memoryBean.getHeapMemoryUsage(); addAttributeInMb(protobuf, "Heap Committed (MB)", heap.getCommitted()); addAttributeInMb(protobuf, "Heap Init (MB)", heap.getInit()); addAttributeInMb(protobuf, "Heap Max (MB)", heap.getMax()); addAttributeInMb(protobuf, "Heap Used (MB)", heap.getUsed()); MemoryUsage nonHeap = memoryBean.getNonHeapMemoryUsage(); addAttributeInMb(protobuf, "Non Heap Committed (MB)", nonHeap.getCommitted()); addAttributeInMb(protobuf, "Non Heap Init (MB)", nonHeap.getInit()); addAttributeInMb(protobuf, "Non Heap Max (MB)", nonHeap.getMax()); addAttributeInMb(protobuf, "Non Heap Used (MB)", nonHeap.getUsed()); OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean(); setAttribute(protobuf,"System Load Average", format(Locale.ENGLISH, "%.1f%% (last minute)", os.getSystemLoadAverage() * 100.0)); ThreadMXBean thread = ManagementFactory.getThreadMXBean(); setAttribute(protobuf, "Threads", thread.getThreadCount()); return protobuf.build(); }