目标
- 配置 Grafana,允许匿名访问
- 通过 rest 接口注册 dashbord
- 获取 dashbord url 进行可视化
mac 安装配置
- 安装
brew update
brew install grafana
- 配置匿名访问:/usr/local/etc/grafana/grafana.ini
[auth]
# 要删掉前边的分号
disable_login_form = true
[auth.anonymous]
enabled = true
org_name = Main Org.
org_role = Viewer
- 启停
// 启动
brew services start grafana
// 关闭
brew services stop grafana
// 重启
brew services restart grafana
注册 Dashboard
https://github.com/grafana/grafana/blob/master/docs/sources/http_api/dashboard.md
-
Post请求Url:/api/dashboards/db
-
需要首先创建一个 Api key,在 Configuration -> API Keys 中创建一个,会得到一个字符串,添加到post请求报头。
-
以下几个重要项说明
- targets.target:查询的时间序列
- id:dashboard 在单Grafana实例中的id
- uid:dashboard 在多Grafana实例中的uidid,这两个搞成一样就行
- title:dashboard 名称
- refresh:刷新频率
- time:查询时间段,如最近五分钟
- panel:一个时间序列的仪表板,这个例子的panel都是折线图
- datasource:数据源,需要提前创建
- gridPos:仪表板的位置,x,y是起始位置,h是高度,w是宽,多个panel需要自己排版。
- title:panel 名称
-
注册完成后,可以通过 http://ip:3000/d/uid/title 查看 dashboard。
-
创建 dashboard 的 post 请求 body
{
"dashboard": {
"id": "imei",
"uid": "imei",
"title": "userName",
"timezone": "browser",
"refresh": "5s",
"schemaVersion": 16,
"time": {
"from": "now-5m",
"to": "now"
},
"panels": [
{
"aliasColors": {},
"bars": false,
"dashLength": 10,
"dashes": false,
"datasource": "IoTDB-Raspberry",
"fill": 1,
"gridPos": {
"h": 6,
"w": 24,
"x": 0,
"y": 0
},
"id": 2,
"legend": {
"avg": false,
"current": false,
"max": false,
"min": false,
"show": true,
"total": false,
"values": false
},
"lines": true,
"linewidth": 1,
"links": [],
"nullPointMode": "null",
"percentage": false,
"pointradius": 5,
"points": false,
"renderer": "flot",
"seriesOverrides": [],
"spaceLength": 10,
"stack": false,
"steppedLine": false,
"targets": [
{
"refId": "A",
"target": "raspberry.pi1.distance",
"type": "timeserie"
}
],
"thresholds": [],
"timeFrom": null,
"timeShift": null,
"title": "超声波测距(米)",
"tooltip": {
"shared": true,
"sort": 0,
"value_type": "individual"
},
"type": "graph",
"xaxis": {
"buckets": null,
"mode": "time",
"name": null,
"show": true,
"values": []
},
"yaxes": [
{
"format": "short",
"label": null,
"logBase": 1,
"max": null,
"min": null,
"show": true
},
{
"format": "short",
"label": null,
"logBase": 1,
"max": null,
"min": null,
"show": true
}
],
"yaxis": {
"align": false,
"alignLevel": null
}
}
],
"version": 0
},
"folderId": 0,
"overwrite": true
}
java 构造 post 请求发送
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
URL url = new URL(getGrafanaUrl);
URLConnection connection = url.openConnection();
connection.setRequestProperty("accept", "application/json");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// apiKey 是上边生成的那个
connection.setRequestProperty("Authorization", "Bearer " + apiKey);
connection.setDoInput(true);
connection.setDoOutput(true);
// request 是上边那个json
try (PrintWriter out = new PrintWriter(connection.getOutputStream())){
out.print(request.toString());
out.flush();
}
try (BufferedReader in = new BufferedReader(
new InputStreamReader(connection.getInputStream()))) {
StringBuilder result = new StringBuilder();
String line;
while ((line = in.readLine()) != null) {
result.append(line);
}
logger.info("Received response: {}", result);
}
Dashboard URL
Grafana 的 dashboard 的 url 都是一个风格 http://localhost:3000/d/good/goodgood?refresh=5s&orgId=1
直接把那些 good 替换成自己 的就可以了