├── lombok.config ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .gitignore ├── grafana-api.iml ├── src ├── test │ └── java │ │ └── com │ │ └── grafana │ │ └── api │ │ └── GrafanaClientTest.java ├── main │ └── java │ │ └── com │ │ └── grafana │ │ └── api │ │ ├── client │ │ ├── models │ │ │ ├── Templating.java │ │ │ ├── Time.java │ │ │ ├── Annotations.java │ │ │ ├── Group.java │ │ │ ├── Current.java │ │ │ ├── GrafanaDashboard.java │ │ │ ├── ValueMap.java │ │ │ ├── GridPos.java │ │ │ ├── Timepicker.java │ │ │ ├── Legend.java │ │ │ ├── Target.java │ │ │ ├── GrafanaSearchResult.java │ │ │ ├── TreeMap.java │ │ │ ├── List.java │ │ │ ├── Panel.java │ │ │ ├── Meta.java │ │ │ └── Dashboard.java │ │ ├── GrafanaService.java │ │ └── GrafanaClient.java │ │ ├── configuration │ │ └── GrafanaConfiguration.java │ │ └── exceptions │ │ ├── GrafanaException.java │ │ └── GrafanaDashboardDoesNotExistException.java └── integration-test │ └── java │ └── com.grafana.api.client │ └── GrafanaClientTest.java ├── README.md ├── settings.gradle ├── .circleci └── config.yml ├── gradlew.bat └── gradlew /lombok.config: -------------------------------------------------------------------------------- 1 | lombok.addLombokGeneratedAnnotation=true -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ohmrefresh/grafana-client/master/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore Gradle project-specific cache directory 2 | .gradle 3 | .idea 4 | .iml 5 | 6 | # Ignore Gradle build output directory 7 | build 8 | out -------------------------------------------------------------------------------- /grafana-api.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Tue Oct 15 22:05:31 ICT 2019 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-all.zip 7 | -------------------------------------------------------------------------------- /src/test/java/com/grafana/api/GrafanaClientTest.java: -------------------------------------------------------------------------------- 1 | package com.grafana.api; 2 | 3 | import static org.junit.Assert.assertTrue; 4 | 5 | import org.junit.Test; 6 | 7 | public class GrafanaClientTest { 8 | 9 | @Test 10 | public void test001() { 11 | assertTrue(true); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [Build status] [![CircleCI](https://circleci.com/gh/ohmrefresh/grafana-api.svg?style=svg)](https://circleci.com/gh/ohmrefresh/grafana-api) 2 | 3 | [code coverage] [![codecov](https://codecov.io/gh/ohmrefresh/grafana-api/branch/master/graph/badge.svg)](https://codecov.io/gh/ohmrefresh/grafana-api) 4 | 5 | Refer to https://github.com/appnexus/grafana-api-java-client 6 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * This file was generated by the Gradle 'init' task. 3 | * 4 | * The settings file is used to specify which projects to include in your build. 5 | * 6 | * Detailed information about configuring a multi-project build in Gradle can be found 7 | * in the user manual at https://docs.gradle.org/5.6.2/userguide/multi_project_builds.html 8 | */ 9 | 10 | rootProject.name = 'grafana-api' 11 | -------------------------------------------------------------------------------- /src/main/java/com/grafana/api/client/models/Templating.java: -------------------------------------------------------------------------------- 1 | package com.grafana.api.client.models; 2 | 3 | import java.io.Serializable; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | import lombok.experimental.Accessors; 7 | 8 | @Data 9 | @NoArgsConstructor 10 | @Accessors(fluent = true) 11 | public class Templating implements Serializable { 12 | 13 | private java.util.List list; 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/grafana/api/client/models/Time.java: -------------------------------------------------------------------------------- 1 | package com.grafana.api.client.models; 2 | 3 | import java.io.Serializable; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | import lombok.experimental.Accessors; 7 | 8 | @Data 9 | @NoArgsConstructor 10 | @Accessors(fluent = true) 11 | public class Time implements Serializable { 12 | 13 | private String from; 14 | private String to; 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/grafana/api/client/models/Annotations.java: -------------------------------------------------------------------------------- 1 | package com.grafana.api.client.models; 2 | 3 | import java.io.Serializable; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | import lombok.experimental.Accessors; 7 | 8 | @Data 9 | @NoArgsConstructor 10 | @Accessors(fluent = true) 11 | public class Annotations implements Serializable { 12 | 13 | private java.util.List list; 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/grafana/api/client/models/Group.java: -------------------------------------------------------------------------------- 1 | package com.grafana.api.client.models; 2 | 3 | import java.io.Serializable; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | import lombok.experimental.Accessors; 7 | 8 | @Data 9 | @NoArgsConstructor 10 | @Accessors(fluent = true) 11 | public class Group implements Serializable { 12 | 13 | private String key; 14 | private String value; 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/grafana/api/client/models/Current.java: -------------------------------------------------------------------------------- 1 | package com.grafana.api.client.models; 2 | 3 | import java.io.Serializable; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | import lombok.experimental.Accessors; 7 | 8 | @Data 9 | @NoArgsConstructor 10 | @Accessors(fluent = true) 11 | public class Current implements Serializable { 12 | 13 | private String text; 14 | private String value; 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/grafana/api/client/models/GrafanaDashboard.java: -------------------------------------------------------------------------------- 1 | package com.grafana.api.client.models; 2 | 3 | import java.io.Serializable; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | import lombok.experimental.Accessors; 7 | 8 | @Data 9 | @NoArgsConstructor 10 | @Accessors(fluent = true) 11 | public class GrafanaDashboard implements Serializable { 12 | private Dashboard dashboard; 13 | private Meta meta; 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/grafana/api/client/models/ValueMap.java: -------------------------------------------------------------------------------- 1 | package com.grafana.api.client.models; 2 | 3 | import java.io.Serializable; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | import lombok.experimental.Accessors; 7 | 8 | @Data 9 | @NoArgsConstructor 10 | @Accessors(fluent = true) 11 | public class ValueMap implements Serializable { 12 | 13 | private String op; 14 | private String text; 15 | private String value; 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/grafana/api/client/models/GridPos.java: -------------------------------------------------------------------------------- 1 | package com.grafana.api.client.models; 2 | 3 | import java.io.Serializable; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | import lombok.experimental.Accessors; 7 | 8 | @Data 9 | @NoArgsConstructor 10 | @Accessors(fluent = true) 11 | public class GridPos implements Serializable { 12 | 13 | private long h; 14 | private long w; 15 | private long x; 16 | private long y; 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/grafana/api/client/models/Timepicker.java: -------------------------------------------------------------------------------- 1 | package com.grafana.api.client.models; 2 | 3 | import java.io.Serializable; 4 | import java.util.List; 5 | import lombok.Data; 6 | import lombok.NoArgsConstructor; 7 | import lombok.experimental.Accessors; 8 | 9 | @Data 10 | @NoArgsConstructor 11 | @Accessors(fluent = true) 12 | public class Timepicker implements Serializable { 13 | 14 | private List refreshIntervals; 15 | private List timeOptions; 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/grafana/api/configuration/GrafanaConfiguration.java: -------------------------------------------------------------------------------- 1 | /* Licensed under Apache-2.0 */ 2 | package com.grafana.api.configuration; 3 | 4 | import lombok.AllArgsConstructor; 5 | import lombok.Getter; 6 | import lombok.NoArgsConstructor; 7 | import lombok.Setter; 8 | import lombok.experimental.Accessors; 9 | 10 | @Getter 11 | @Setter 12 | @AllArgsConstructor 13 | @NoArgsConstructor 14 | @Accessors(fluent = true) 15 | public class GrafanaConfiguration { 16 | private String host; 17 | private String apiKey; 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/grafana/api/client/models/Legend.java: -------------------------------------------------------------------------------- 1 | package com.grafana.api.client.models; 2 | 3 | import java.io.Serializable; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | import lombok.experimental.Accessors; 7 | 8 | @Data 9 | @NoArgsConstructor 10 | @Accessors(fluent = true) 11 | public class Legend implements Serializable { 12 | 13 | private Boolean avg; 14 | private Boolean current; 15 | private Boolean max; 16 | private Boolean min; 17 | private Boolean show; 18 | private Boolean total; 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/grafana/api/client/models/Target.java: -------------------------------------------------------------------------------- 1 | package com.grafana.api.client.models; 2 | 3 | import java.io.Serializable; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | import lombok.experimental.Accessors; 7 | 8 | @Data 9 | @NoArgsConstructor 10 | @Accessors(fluent = true) 11 | public class Target implements Serializable { 12 | 13 | private String expr; 14 | private String format; 15 | private long intervalFactor; 16 | private String legendFormat; 17 | private String metric; 18 | private String refId; 19 | private long step; 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/grafana/api/client/models/GrafanaSearchResult.java: -------------------------------------------------------------------------------- 1 | /* Licensed under Apache-2.0 */ 2 | package com.grafana.api.client.models; 3 | 4 | import java.util.List; 5 | import lombok.Data; 6 | import lombok.experimental.Accessors; 7 | 8 | @Data 9 | @Accessors(fluent = true) 10 | public class GrafanaSearchResult { 11 | private long id; 12 | private Boolean isStarred; 13 | private String slug; 14 | private List tags; 15 | private String title; 16 | private String type; 17 | private String uid; 18 | private String uri; 19 | private String url; 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/grafana/api/client/models/TreeMap.java: -------------------------------------------------------------------------------- 1 | package com.grafana.api.client.models; 2 | 3 | import java.io.Serializable; 4 | import java.util.List; 5 | import lombok.Data; 6 | import lombok.NoArgsConstructor; 7 | import lombok.experimental.Accessors; 8 | 9 | @Data 10 | @NoArgsConstructor 11 | @Accessors(fluent = true) 12 | public class TreeMap implements Serializable { 13 | 14 | private String colorByFunction; 15 | private Boolean debug; 16 | private long depth; 17 | private Boolean enableGrouping; 18 | private Boolean enableTimeBlocks; 19 | private List groups; 20 | private List ids; 21 | private String mode; 22 | private String nodeSizeProperty; 23 | private String sizeByFunction; 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/grafana/api/exceptions/GrafanaException.java: -------------------------------------------------------------------------------- 1 | /* Licensed under Apache-2.0 */ 2 | package com.grafana.api.exceptions; 3 | 4 | import java.io.IOException; 5 | import okhttp3.ResponseBody; 6 | 7 | public class GrafanaException extends Exception { 8 | 9 | public static GrafanaException withErrorBody(ResponseBody body) throws IOException { 10 | return body != null 11 | ? new GrafanaException("Unexpected Grafana error; " + body.string()) 12 | : new GrafanaException("Unexpected Grafana error"); 13 | } 14 | 15 | public GrafanaException(String message) { 16 | super(message); 17 | } 18 | 19 | public GrafanaException(String message, Throwable cause) { 20 | super(message, cause); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | version: 2.0 2 | 3 | jobs: 4 | build: 5 | docker: 6 | - image: circleci/openjdk:11.0.3-jdk-stretch 7 | steps: 8 | - checkout 9 | - restore_cache: 10 | keys: 11 | - gradle-{{ checksum "build.gradle" }} 12 | - run: gradle assemble 13 | - run: gradle run 14 | - run: gradle test 15 | - run: gradle build jacocoTestReport 16 | - run: bash <(curl -s https://codecov.io/bash) 17 | - save_cache: 18 | paths: 19 | - ~/.gradle 20 | key: gradle-{{ checksum "build.gradle" }} 21 | - store_artifacts: 22 | path: build/distributions 23 | destination: distributions 24 | - store_artifacts: 25 | path: build/libs 26 | destination: libs -------------------------------------------------------------------------------- /src/main/java/com/grafana/api/client/models/List.java: -------------------------------------------------------------------------------- 1 | package com.grafana.api.client.models; 2 | 3 | import java.io.Serializable; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | import lombok.experimental.Accessors; 7 | 8 | @Data 9 | @NoArgsConstructor 10 | @Accessors(fluent = true) 11 | public class List implements Serializable { 12 | 13 | private String allFormat; 14 | private long builtIn; 15 | private Current current; 16 | private String datasource; 17 | private Boolean enable; 18 | private Boolean hide; 19 | private String iconColor; 20 | private Boolean includeAll; 21 | private String label; 22 | private Boolean multi; 23 | private String name; 24 | private java.util.List options; 25 | private String query; 26 | private long refresh; 27 | private String regex; 28 | private Boolean skipUrlSync; 29 | private String type; 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/grafana/api/exceptions/GrafanaDashboardDoesNotExistException.java: -------------------------------------------------------------------------------- 1 | /* Licensed under Apache-2.0 */ 2 | package com.grafana.api.exceptions; 3 | 4 | import java.io.IOException; 5 | import okhttp3.ResponseBody; 6 | 7 | public class GrafanaDashboardDoesNotExistException extends GrafanaException { 8 | 9 | public static GrafanaDashboardDoesNotExistException withErrorBody(ResponseBody body) 10 | throws IOException { 11 | return body != null 12 | ? new GrafanaDashboardDoesNotExistException("Unexpected Grafana error; " + body.string()) 13 | : new GrafanaDashboardDoesNotExistException("Unexpected Grafana error"); 14 | } 15 | 16 | public GrafanaDashboardDoesNotExistException(String message) { 17 | super(message); 18 | } 19 | 20 | public GrafanaDashboardDoesNotExistException(String message, Throwable cause) { 21 | super(message, cause); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/grafana/api/client/GrafanaService.java: -------------------------------------------------------------------------------- 1 | /* Licensed under Apache-2.0 */ 2 | package com.grafana.api.client; 3 | 4 | import com.grafana.api.client.models.GrafanaDashboard; 5 | import com.grafana.api.client.models.GrafanaSearchResult; 6 | import java.util.List; 7 | import retrofit2.Call; 8 | import retrofit2.http.*; 9 | 10 | public interface GrafanaService { 11 | String GRAFANA_DASHBOARDS = "api/dashboards/db/"; 12 | String GRAFANA_SEARCH = "api/search/"; 13 | 14 | String AUTHORIZATION = "Authorization"; 15 | 16 | @GET(GRAFANA_DASHBOARDS + "{dashboard}") 17 | Call getDashboard( 18 | @Header(AUTHORIZATION) String authorization, @Path("dashboard") String dashboard); 19 | 20 | @GET(GRAFANA_SEARCH) 21 | Call> search( 22 | @Header(AUTHORIZATION) String authorization, 23 | @Query("query") String query, 24 | @Query("tag") String tag); 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/grafana/api/client/models/Panel.java: -------------------------------------------------------------------------------- 1 | package com.grafana.api.client.models; 2 | 3 | import java.io.Serializable; 4 | import java.util.List; 5 | import lombok.Data; 6 | import lombok.NoArgsConstructor; 7 | import lombok.experimental.Accessors; 8 | 9 | @Data 10 | @NoArgsConstructor 11 | @Accessors(fluent = true) 12 | public class Panel implements Serializable { 13 | 14 | private String chartId; 15 | private List colors; 16 | private String datasource; 17 | private String format; 18 | private GridPos gridPos; 19 | private long id; 20 | private Legend legend; 21 | private List links; 22 | private long mappingType; 23 | private long maxDataPoints; 24 | private String nullPointMode; 25 | private List seriesOverrides; 26 | private List targets; 27 | // private String thresholds; 28 | private String title; 29 | private TreeMap treeMap; 30 | private String type; 31 | private List valueMaps; 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/grafana/api/client/models/Meta.java: -------------------------------------------------------------------------------- 1 | package com.grafana.api.client.models; 2 | 3 | import java.io.Serializable; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | import lombok.experimental.Accessors; 7 | 8 | @Data 9 | @NoArgsConstructor 10 | @Accessors(fluent = true) 11 | public class Meta implements Serializable { 12 | 13 | private Boolean canAdmin; 14 | private Boolean canEdit; 15 | private Boolean canSave; 16 | private Boolean canStar; 17 | private String created; 18 | private String createdBy; 19 | private String expires; 20 | private long folderId; 21 | private String folderTitle; 22 | private String folderUrl; 23 | private Boolean hasAcl; 24 | private Boolean isFolder; 25 | private Boolean provisioned; 26 | private String provisionedExternalId; 27 | private String slug; 28 | private String type; 29 | private String updated; 30 | private String updatedBy; 31 | private String url; 32 | private long version; 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/grafana/api/client/models/Dashboard.java: -------------------------------------------------------------------------------- 1 | package com.grafana.api.client.models; 2 | 3 | import java.io.Serializable; 4 | import java.util.List; 5 | import lombok.Data; 6 | import lombok.NoArgsConstructor; 7 | import lombok.experimental.Accessors; 8 | 9 | @Data 10 | @NoArgsConstructor 11 | @Accessors(fluent = true) 12 | public class Dashboard implements Serializable { 13 | 14 | private Annotations annotations; 15 | private Boolean editable; 16 | private Object gnetId; 17 | private long graphTooltip; 18 | private long id; 19 | private long iteration; 20 | private List links; 21 | private List panels; 22 | private String refresh; 23 | private long schemaVersion; 24 | private String style; 25 | private List tags; 26 | private Templating templating; 27 | private Time time; 28 | private Timepicker timepicker; 29 | private String timezone; 30 | private String title; 31 | private String uid; 32 | private long version; 33 | } 34 | -------------------------------------------------------------------------------- /src/integration-test/java/com.grafana.api.client/GrafanaClientTest.java: -------------------------------------------------------------------------------- 1 | package com.grafana.api.client; 2 | 3 | import com.grafana.api.client.models.GrafanaDashboard; 4 | import com.grafana.api.client.models.GrafanaSearchResult; 5 | import com.grafana.api.configuration.GrafanaConfiguration; 6 | import com.grafana.api.exceptions.GrafanaException; 7 | import java.io.IOException; 8 | import java.util.List; 9 | import org.junit.After; 10 | import org.junit.Before; 11 | import org.junit.Test; 12 | 13 | public class GrafanaClientTest { 14 | 15 | private final String GRAFANA_API_KEY = "Bearer xxxx=="; // Replace with valid API key 16 | private final String GRAFANA_HOST = "https://xxxx:3000"; // Replace with valid host 17 | 18 | private GrafanaConfiguration grafanaConfiguration = 19 | new GrafanaConfiguration().host(GRAFANA_HOST).apiKey(GRAFANA_API_KEY); 20 | 21 | private final String tags = "app"; 22 | private final String dashboardName = "app-dash"; 23 | private GrafanaClient grafanaClient; 24 | 25 | @Before 26 | public void setup() { 27 | grafanaClient = new GrafanaClient(grafanaConfiguration); 28 | } 29 | 30 | @After 31 | public void clean() {} 32 | 33 | @Test 34 | public void testGeDashboardShouldReturnResponse() throws GrafanaException, IOException { 35 | grafanaClient = new GrafanaClient(grafanaConfiguration); 36 | GrafanaDashboard dash = grafanaClient.getDashboard(dashboardName); 37 | assert dash != null; 38 | // ObjectMapper om = new ObjectMapper(); 39 | // om.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY); 40 | // String json = om.writerWithDefaultPrettyPrinter().writeValueAsString(dash); 41 | // System.out.println(json); 42 | } 43 | 44 | @Test 45 | public void testSearchDashboardWithTagsShouldReturnResponse() 46 | throws GrafanaException, IOException { 47 | grafanaClient = new GrafanaClient(grafanaConfiguration); 48 | List dash = grafanaClient.search("", tags); 49 | assert dash.size() > 0; 50 | // 51 | // ObjectMapper om = new ObjectMapper(); 52 | // om.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY); 53 | // String json = om.writerWithDefaultPrettyPrinter().writeValueAsString(dash); 54 | // System.out.println(json); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 33 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 34 | 35 | @rem Find java.exe 36 | if defined JAVA_HOME goto findJavaFromJavaHome 37 | 38 | set JAVA_EXE=java.exe 39 | %JAVA_EXE% -version >NUL 2>&1 40 | if "%ERRORLEVEL%" == "0" goto init 41 | 42 | echo. 43 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 44 | echo. 45 | echo Please set the JAVA_HOME variable in your environment to match the 46 | echo location of your Java installation. 47 | 48 | goto fail 49 | 50 | :findJavaFromJavaHome 51 | set JAVA_HOME=%JAVA_HOME:"=% 52 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 53 | 54 | if exist "%JAVA_EXE%" goto init 55 | 56 | echo. 57 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 58 | echo. 59 | echo Please set the JAVA_HOME variable in your environment to match the 60 | echo location of your Java installation. 61 | 62 | goto fail 63 | 64 | :init 65 | @rem Get command-line arguments, handling Windows variants 66 | 67 | if not "%OS%" == "Windows_NT" goto win9xME_args 68 | 69 | :win9xME_args 70 | @rem Slurp the command line arguments. 71 | set CMD_LINE_ARGS= 72 | set _SKIP=2 73 | 74 | :win9xME_args_slurp 75 | if "x%~1" == "x" goto execute 76 | 77 | set CMD_LINE_ARGS=%* 78 | 79 | :execute 80 | @rem Setup the command line 81 | 82 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 83 | 84 | @rem Execute Gradle 85 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 86 | 87 | :end 88 | @rem End local scope for the variables with windows NT shell 89 | if "%ERRORLEVEL%"=="0" goto mainEnd 90 | 91 | :fail 92 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 93 | rem the _cmd.exe /c_ return code! 94 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 95 | exit /b 1 96 | 97 | :mainEnd 98 | if "%OS%"=="Windows_NT" endlocal 99 | 100 | :omega 101 | -------------------------------------------------------------------------------- /src/main/java/com/grafana/api/client/GrafanaClient.java: -------------------------------------------------------------------------------- 1 | /* Licensed under Apache-2.0 */ 2 | package com.grafana.api.client; 3 | 4 | import static java.net.HttpURLConnection.HTTP_NOT_FOUND; 5 | 6 | import com.fasterxml.jackson.annotation.JsonAutoDetect; 7 | import com.fasterxml.jackson.annotation.JsonInclude; 8 | import com.fasterxml.jackson.annotation.PropertyAccessor; 9 | import com.fasterxml.jackson.databind.DeserializationFeature; 10 | import com.fasterxml.jackson.databind.ObjectMapper; 11 | import com.grafana.api.client.models.GrafanaDashboard; 12 | import com.grafana.api.client.models.GrafanaSearchResult; 13 | import com.grafana.api.configuration.GrafanaConfiguration; 14 | import com.grafana.api.exceptions.GrafanaDashboardDoesNotExistException; 15 | import com.grafana.api.exceptions.GrafanaException; 16 | import java.io.IOException; 17 | import java.util.List; 18 | import javax.net.ssl.SSLContext; 19 | import javax.net.ssl.SSLSocketFactory; 20 | import javax.net.ssl.TrustManager; 21 | import javax.net.ssl.X509TrustManager; 22 | import okhttp3.OkHttpClient; 23 | import retrofit2.Response; 24 | import retrofit2.Retrofit; 25 | import retrofit2.converter.jackson.JacksonConverterFactory; 26 | 27 | public class GrafanaClient { 28 | 29 | private final String host; 30 | private final String apiKey; 31 | private final GrafanaService service; 32 | 33 | private static final ObjectMapper mapper = 34 | new ObjectMapper() 35 | .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) 36 | .setSerializationInclusion(JsonInclude.Include.NON_NULL) 37 | .setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY); 38 | 39 | /** @param configuration the information needed to communicate with Grafana. */ 40 | public GrafanaClient(GrafanaConfiguration configuration) { 41 | this(configuration, createOkHttpClient()); 42 | } 43 | 44 | private static OkHttpClient createOkHttpClient() { 45 | try { 46 | // Create a trust manager that does not validate certificate chains 47 | final TrustManager[] trustAllCerts = 48 | new TrustManager[] { 49 | new X509TrustManager() { 50 | @Override 51 | public void checkClientTrusted( 52 | java.security.cert.X509Certificate[] chain, String authType) {} 53 | 54 | @Override 55 | public void checkServerTrusted( 56 | java.security.cert.X509Certificate[] chain, String authType) {} 57 | 58 | @Override 59 | public java.security.cert.X509Certificate[] getAcceptedIssuers() { 60 | return new java.security.cert.X509Certificate[] {}; 61 | } 62 | } 63 | }; 64 | 65 | // Install the all-trusting trust manager 66 | final SSLContext sslContext = SSLContext.getInstance("SSL"); 67 | sslContext.init(null, trustAllCerts, new java.security.SecureRandom()); 68 | 69 | // Create an ssl socket factory with our all-trusting manager 70 | final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory(); 71 | 72 | return new OkHttpClient.Builder() 73 | .sslSocketFactory(sslSocketFactory, (X509TrustManager) trustAllCerts[0]) 74 | .hostnameVerifier((hostname, session) -> true) 75 | .build(); 76 | } catch (Exception e) { 77 | throw new RuntimeException(e); 78 | } 79 | } 80 | 81 | public GrafanaClient(GrafanaConfiguration configuration, OkHttpClient client) { 82 | 83 | this.apiKey = configuration.apiKey(); 84 | this.host = configuration.host(); 85 | 86 | Retrofit retrofit = 87 | new Retrofit.Builder() 88 | .baseUrl(host) 89 | .client(client) 90 | .addConverterFactory(JacksonConverterFactory.create(mapper)) 91 | .build(); 92 | 93 | service = retrofit.create(GrafanaService.class); 94 | } 95 | 96 | public String getHost() { 97 | return host; 98 | } 99 | 100 | /** 101 | * Searches for an existing dashboard by name. 102 | * 103 | * @param dashboardName the name of the dashboard to search for. 104 | * @return {@link GrafanaDashboard} with matching name. 105 | * @throws GrafanaDashboardDoesNotExistException if a dashboard with matching name does not exist. 106 | * @throws GrafanaException if Grafana returns an error when trying to retrieve the dashboard. 107 | * @throws IOException if a problem occurred talking to the server. 108 | */ 109 | public GrafanaDashboard getDashboard(String dashboardName) 110 | throws GrafanaDashboardDoesNotExistException, GrafanaException, IOException { 111 | 112 | Response response = service.getDashboard(apiKey, dashboardName).execute(); 113 | 114 | if (response.isSuccessful()) { 115 | return response.body(); 116 | } else if (response.code() == HTTP_NOT_FOUND) { 117 | throw new GrafanaDashboardDoesNotExistException( 118 | "Dashboard " + dashboardName + " does not exist"); 119 | } else { 120 | throw GrafanaException.withErrorBody(response.errorBody()); 121 | } 122 | } 123 | 124 | public List search(String query, String tag) 125 | throws GrafanaException, IOException { 126 | 127 | Response> response = service.search(apiKey, query, tag).execute(); 128 | 129 | if (response.isSuccessful()) { 130 | return response.body(); 131 | } else { 132 | throw GrafanaException.withErrorBody(response.errorBody()); 133 | } 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | # 4 | # Copyright 2015 the original author or authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | ## 21 | ## Gradle start up script for UN*X 22 | ## 23 | ############################################################################## 24 | 25 | # Attempt to set APP_HOME 26 | # Resolve links: $0 may be a link 27 | PRG="$0" 28 | # Need this for relative symlinks. 29 | while [ -h "$PRG" ] ; do 30 | ls=`ls -ld "$PRG"` 31 | link=`expr "$ls" : '.*-> \(.*\)$'` 32 | if expr "$link" : '/.*' > /dev/null; then 33 | PRG="$link" 34 | else 35 | PRG=`dirname "$PRG"`"/$link" 36 | fi 37 | done 38 | SAVED="`pwd`" 39 | cd "`dirname \"$PRG\"`/" >/dev/null 40 | APP_HOME="`pwd -P`" 41 | cd "$SAVED" >/dev/null 42 | 43 | APP_NAME="Gradle" 44 | APP_BASE_NAME=`basename "$0"` 45 | 46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 48 | 49 | # Use the maximum available, or set MAX_FD != -1 to use that value. 50 | MAX_FD="maximum" 51 | 52 | warn () { 53 | echo "$*" 54 | } 55 | 56 | die () { 57 | echo 58 | echo "$*" 59 | echo 60 | exit 1 61 | } 62 | 63 | # OS specific support (must be 'true' or 'false'). 64 | cygwin=false 65 | msys=false 66 | darwin=false 67 | nonstop=false 68 | case "`uname`" in 69 | CYGWIN* ) 70 | cygwin=true 71 | ;; 72 | Darwin* ) 73 | darwin=true 74 | ;; 75 | MINGW* ) 76 | msys=true 77 | ;; 78 | NONSTOP* ) 79 | nonstop=true 80 | ;; 81 | esac 82 | 83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 84 | 85 | # Determine the Java command to use to start the JVM. 86 | if [ -n "$JAVA_HOME" ] ; then 87 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 88 | # IBM's JDK on AIX uses strange locations for the executables 89 | JAVACMD="$JAVA_HOME/jre/sh/java" 90 | else 91 | JAVACMD="$JAVA_HOME/bin/java" 92 | fi 93 | if [ ! -x "$JAVACMD" ] ; then 94 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 95 | 96 | Please set the JAVA_HOME variable in your environment to match the 97 | location of your Java installation." 98 | fi 99 | else 100 | JAVACMD="java" 101 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 102 | 103 | Please set the JAVA_HOME variable in your environment to match the 104 | location of your Java installation." 105 | fi 106 | 107 | # Increase the maximum file descriptors if we can. 108 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 109 | MAX_FD_LIMIT=`ulimit -H -n` 110 | if [ $? -eq 0 ] ; then 111 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 112 | MAX_FD="$MAX_FD_LIMIT" 113 | fi 114 | ulimit -n $MAX_FD 115 | if [ $? -ne 0 ] ; then 116 | warn "Could not set maximum file descriptor limit: $MAX_FD" 117 | fi 118 | else 119 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 120 | fi 121 | fi 122 | 123 | # For Darwin, add options to specify how the application appears in the dock 124 | if $darwin; then 125 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 126 | fi 127 | 128 | # For Cygwin or MSYS, switch paths to Windows format before running java 129 | if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then 130 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 131 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 132 | JAVACMD=`cygpath --unix "$JAVACMD"` 133 | 134 | # We build the pattern for arguments to be converted via cygpath 135 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 136 | SEP="" 137 | for dir in $ROOTDIRSRAW ; do 138 | ROOTDIRS="$ROOTDIRS$SEP$dir" 139 | SEP="|" 140 | done 141 | OURCYGPATTERN="(^($ROOTDIRS))" 142 | # Add a user-defined pattern to the cygpath arguments 143 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 144 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 145 | fi 146 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 147 | i=0 148 | for arg in "$@" ; do 149 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 150 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 151 | 152 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 153 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 154 | else 155 | eval `echo args$i`="\"$arg\"" 156 | fi 157 | i=$((i+1)) 158 | done 159 | case $i in 160 | (0) set -- ;; 161 | (1) set -- "$args0" ;; 162 | (2) set -- "$args0" "$args1" ;; 163 | (3) set -- "$args0" "$args1" "$args2" ;; 164 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 165 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 166 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 167 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 168 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 169 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 170 | esac 171 | fi 172 | 173 | # Escape application args 174 | save () { 175 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 176 | echo " " 177 | } 178 | APP_ARGS=$(save "$@") 179 | 180 | # Collect all arguments for the java command, following the shell quoting and substitution rules 181 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 182 | 183 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 184 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 185 | cd "$(dirname "$0")" 186 | fi 187 | 188 | exec "$JAVACMD" "$@" 189 | --------------------------------------------------------------------------------