public static String getResourceAsString(String name) { try { return toString(getResourceAsStream(name)); } catch (IOException e) { throw new RuntimeException(e); } }
ObjectNode getDashboardForElasticsearch(String dashboardPath) throws IOException { final ObjectMapper mapper = JsonUtils.getMapper(); final ObjectNode dashboard = (ObjectNode) mapper.readTree(IOUtils.getResourceAsStream(dashboardPath)); dashboard.put("editable", false); ObjectNode dashboardElasticsearchFormat = mapper.createObjectNode(); dashboardElasticsearchFormat.put("user", "guest"); dashboardElasticsearchFormat.put("group", "guest"); dashboardElasticsearchFormat.set(TITLE, dashboard.get(TITLE)); dashboardElasticsearchFormat.set("tags", dashboard.get("tags")); dashboardElasticsearchFormat.put("dashboard", dashboard.toString()); return dashboardElasticsearchFormat; }
private void createKibana5IndexAndMappings(ElasticsearchClient elasticsearchClient) { // makes sure the .kibana index is present and has the right mapping. // otherwise it leads to problems if stagemonitor sends the dashboards to the // .kibana index before it has been properly created by kibana final String resourcePath = elasticsearchClient.getElasticsearchResourcePath(); elasticsearchClient.createIndexAndSendMapping(".kibana", "index-pattern", IOUtils.getResourceAsStream(resourcePath + "kibana-index-index-pattern.json")); elasticsearchClient.createIndexAndSendMapping(".kibana", "search", IOUtils.getResourceAsStream(resourcePath + "kibana-index-search.json")); elasticsearchClient.createIndexAndSendMapping(".kibana", "dashboard", IOUtils.getResourceAsStream(resourcePath + "kibana-index-dashboard.json")); elasticsearchClient.createIndexAndSendMapping(".kibana", "visualization", IOUtils.getResourceAsStream(resourcePath + "kibana-index-visualization.json")); }
private String getFields(String fieldsJsonPath) throws IOException { final JsonNode fields = JsonUtils.getMapper().readTree(IOUtils.getResourceAsStream(fieldsJsonPath)); for (JsonNode field : fields) { if (!field.has("readFromDocValues")) { // if a field mapping does not contain readFromDocValues (previously named doc_values) // kibana refreshes the mapping based on the contents of the index // if the index is empty or contains documents which don't have all possible properties set (like username) // the value for readFromDocValues can't be determined // thus these field mappings are "deleted" (or rather can't be recreated) logger.warn("Field {} in {} does not have property readFromDocValues", field.get("name"), fieldsJsonPath); } } return JsonUtils.getMapper().writeValueAsString(fields); }
public static String modifyIndexTemplate(String templatePath, int moveToColdNodesAfterDays, Integer numberOfReplicas, Integer numberOfShards) { final JsonNode json; try { json = JsonUtils.getMapper().readTree(IOUtils.getResourceAsStream(templatePath)); ObjectNode indexSettings = (ObjectNode) json.get("settings").get("index"); if (moveToColdNodesAfterDays > 0) { indexSettings.put("routing.allocation.require.box_type", "hot"); } if (numberOfReplicas != null && numberOfReplicas >= 0) { indexSettings.put("number_of_replicas", numberOfReplicas); } if (numberOfShards != null && numberOfShards > 0) { indexSettings.put("number_of_shards", numberOfShards); } } catch (Exception e) { throw new RuntimeException(e); } return json.toString(); }
public static void sendConfigurationMapping(ElasticsearchClient elasticsearchClient) { final String mappingJson; try { mappingJson = IOUtils.toString(IOUtils.getResourceAsStream("stagemonitor-configuration-elasticsearch-mapping.json")); elasticsearchClient.sendMappingTemplate(mappingJson, "stagemonitor-configuration"); } catch (IOException e) { logger.warn("Suppressed exception:", e); } }
ObjectNode getGrafanaDashboard(String classPathLocation) throws IOException { final ObjectNode dashboard = (ObjectNode) JsonUtils.getMapper().readTree(IOUtils.getResourceAsStream(classPathLocation)); dashboard.put("editable", false); addMinIntervalToPanels(dashboard, corePlugin.getElasticsearchReportingInterval() + "s"); return dashboard; }
public void updateKibanaIndexPattern(final String indexName, final String indexPatternLocation) { final String elasticsearchKibanaIndexPatternPath = isElasticsearch6Compatible() ? "/.kibana/doc/index-pattern:" + indexName : "/.kibana/index-pattern/" + indexName; logger.debug("Sending index pattern {} to {}", indexPatternLocation, elasticsearchKibanaIndexPatternPath); try { ObjectNode stagemonitorPattern = JsonUtils.getMapper().readTree(IOUtils.getResourceAsStream(indexPatternLocation)).deepCopy(); if (isElasticsearch6Compatible()) { ObjectNode indexPatternNode = (ObjectNode) stagemonitorPattern.get("index-pattern"); indexPatternNode.put("fields", getFields(stagemonitorPattern.get("index-pattern").get("fields").asText())); } else { stagemonitorPattern.put("fields", getFields(stagemonitorPattern.get("fields").asText())); } JsonNode currentPattern = fetchCurrentKibanaIndexPatternConfiguration(elasticsearchKibanaIndexPatternPath); JsonNode mergedDefinition = JsonMerger.merge(currentPattern, stagemonitorPattern, mergeStrategy().mergeEncodedObjects("fieldFormatMap").encodedArrayWithKey("fields", "name")); sendAsJson("PUT", elasticsearchKibanaIndexPatternPath, mergedDefinition); } catch (IOException e) { logger.warn("Error while updating kibana index pattern, definition = {}, pattern path = {}", indexPatternLocation, elasticsearchKibanaIndexPatternPath, e); } catch (IllegalArgumentException e) { logger.warn("Error while preparing data for kibana index pattern update, definition = {}, pattern path = {}", indexPatternLocation, elasticsearchKibanaIndexPatternPath); } }
@Override public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { IOUtils.copy(IOUtils.getResourceAsStream("wsdl.xml"), response.getOutputStream()); baseRequest.setHandled(true); } });
@Override protected void onElasticsearchFirstAvailable(ElasticsearchClient elasticsearchClient) { if (corePlugin.isInitializeElasticsearch()) { if (elasticsearchClient.isElasticsearch6Compatible()) { logger.debug("creating KibanaIndexAndMapping for ES 6..."); elasticsearchClient.createIndexAndSendMapping(".kibana", "doc", IOUtils.getResourceAsStream(elasticsearchClient.getElasticsearchResourcePath() + "kibana-index-doc.json")); logger.debug("created KibanaIndexAndMapping for ES 6"); } else { logger.debug("creating KibanaIndexAndMapping for ES 5..."); createKibana5IndexAndMappings(elasticsearchClient); sendConfigurationMapping(elasticsearchClient); logger.debug("created KibanaIndexAndMapping for ES 5"); } manageMetricsIndex(elasticsearchClient, corePlugin); } elasticsearchClient.scheduleIndexManagement(ElasticsearchReporter.STAGEMONITOR_METRICS_INDEX_PREFIX, corePlugin.getMoveToColdNodesAfterDays(), corePlugin.getDeleteElasticsearchMetricsAfterDays()); reportToElasticsearch(corePlugin.getMetricRegistry(), corePlugin.getElasticsearchReportingInterval(), corePlugin.getMeasurementSession()); }