grafana 连接 influxdb 1.x 和 2.x
influxdb 中文文档 https://jasper-zhang1.gitbooks.io/influxdb/content/,该文档还是 1.x 版本
influxdb 官方文档 https://docs.influxdata.com/influxdb/v2.0/,可以选择版本
influxdb 的概念基础语法可以查看官方文档,下面讲讲安装使用方法,1.x 版本使用 influxQL 查询语言,2.x 和 1.8+(beta) 使用 flux 查询语法
一、安装 influxdb
参考官方文档 https://docs.influxdata.com/influxdb/v2.0/install/?t=Docker,在右上方选择文档版本,然后选择指定的系统,按照指定命令或者下载安装包安装,下面讲讲 docker 安装方法
influx 2.x 版本安装成功自带 UI 界面,可访问 http://127.0.0.1:8086 查看配置
Ⅰ、docker 安装
选取拉取的镜像版本,可在 https://hub.docker.com/_/influxdb 中选择指定版本,不指定版本默认安装最新的
docker pull influxdb
- 1
创建并启动容器,将容器/var/lib/influxdb2 内部的数据持久化到主机 /var/docker/influxdb2.0 目录。
docker run -d \
--name influxdb \
-p 8086:8086 \
--volume /var/docker/influxdb2.0:/var/lib/influxdb2 \
influxdb:2.0.9
- 1
- 2
- 3
- 4
- 5
使用 docker ps -a 查看是否已经启动,如果未启动使用 docker logs -f influxdb 查看错误日志
进入influxdb 容器使用 docker exec -it influxdb /bin/bash 查看
二、常用操作
Ⅰ、influxdb 1.x版本添加用户认证
1、添加一个管理员用户,all privileges 拥有所有权限就是管理员用户
create user admin with password '..ye123456' with all privileges
- 1
2、修改 influxdb.conf 文件,开启用户认证 auth-enabled = true
3、启动运行
重新启动 influx 服务,重新运行 influxd.exe,Linux可以使用 systemctl restart influxd 或 service restart influxd
注意:网上说的是直接运行 influxd.exe 就可以,但我没有成功,我在启动服务时指定了配置文件
# influxdb.conf 为你配置文件的路径
influxd -config influxdb.conf
- 1
- 2
4、测试
未认证
认证
Ⅱ、influxdb 2.x 使用命令行
命令如下
官方文档地址 https://docs.influxdata.com/influxdb/v2.0/reference/cli/influx/
Ⅲ、CLI 配置token
influx cli 配置token,执行语句时不用传递 token
influx config create --config-name <config-name> \
--host-url http://localhost:8086 \
--org <your-org> \
--token <your-api-token> \
--active
- 1
- 2
- 3
- 4
- 5
比如我的设置
influx config create -n first -u http://1.117.165.107:8086 -o first_org -t tNJ7xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxU2xJjhGqV2cw== -a
- 1
其中 token 以及 org 在你的 influx UI 界面查看 http://127.0.0.1:8086/
查看配置的 token
比如查看所有的
influx config ls
- 1
可以看到我激活的是 first 的配置
切换环境可以使用 influx config set -n second -a
Ⅴ、CLI 查询测试
例如,查询 test 数据库 temperature 表,field 为西安的最近 5 分钟的数据
influx query 'from(bucket: "test")
|> range(start: -5m,stop:now())
|> filter(fn: (r) => r["_measurement"] == "temperature")
|> filter(fn: (r) => r["_field"] == "西安")
|> yield(name: "mean")'
- 1
- 2
- 3
- 4
- 5
查询结果
相比 influx 1.x 命令行变化很大,1.x 版本命令行类似常用的 mysql 命令行写法
三、grafana 安装使用
安装 grafana,上一篇讲了 window 从源码运行 grafana https://blog.csdn.net/qq_41538097/article/details/120956231,有需要的可以看看
window和Linux安装 grafana 比较简单,下载解压即可,可以参考 grafana 官方文档https://grafana.com/docs/grafana/v8.1/installation/
Ⅱ、docker 安装 grafana
去dockerhub 查看需要的版本https://hub.docker.com/r/grafana/grafana
使用如下命令拉去镜像
docker pull grafana/grafana
- 1
创建启动容器
docker run -d -p 3000:3000 --name=grafana grafana/grafana
- 1
进入容器内部使用命令
docker exec -it -u root grafana /bin/sh
- 1
启动成功使用 http://127.0.0.1:3000 访问
如果需要修改 grafana 默认端口号,使用上面命令进入容器,进入 /usr/share/grafana/conf 目录,使用 cp default.ini grafana.ini 复制一份配置文件,grafana 官方不建议直接修改 default.ini 文件,具体修改方法可以参考我上一篇博客。
四、grafana 连接 influx
Ⅰ、influx 1.x
1、添加数据源
grafana 连接 influx 1.x ,点击设置里面的 datasources ,名字自己制定,查询语言需要选择 influxQL,设置链接地址 URL
设置连接数据库、用户名以及密码,点击保存并测试,如下表示配置成功
2、添加数据
需要数据的可以执行如下代码,需要使用 npm install influx
安装包
const Influx = require('influx'); //导包
const influxdbClient = new Influx.InfluxDB({
database: 'my_data',
username: 'root',
password: '123456',
hosts: [{host: 'localhost'}],
schema: [
{
measurement: 'cpu', //类似于数据表的概念
fields: { //数据表的字段,定义类型,FLOAT/INTEGER/STRING/BOOLEAN
enabled: Influx.FieldType.BOOLEAN,
host: Influx.FieldType.STRING,
temperature: Influx.FieldType.FLOAT
}, // tag 也是里面的字段,是自带索引光环。查询速度杠杠的。
tags: ['model', 'system']
}
]
});
// 插入数据
function insert(field1, field2, field3, tag1, tag2) {
influxdbClient.writePoints([
{
measurement: 'cpu',
fields: {
enabled: field1,
host: field2,
temperature: field3
},
tags: {
model: tag1,
system: tag2
}
}
])
}
let j = 0;
setInterval(() => {
for (let i = 0; i < 5; i++) {
let field1, field2, field3, tag1, tag2;
let arg1 = Math.round((Math.random() 100).valueOf());
let arg2 = Math.round((Math.random() 100).valueOf());
let arg3 = Math.round((Math.random() 100).valueOf());
let arg4 = (Math.random() 100).toFixed(2);
field1 <span class="token operator">=</span> arg1 <span class="token operator">%</span> <span class="token number">2</span> <span class="token operator">===</span> <span class="token number">0</span> <span class="token operator">?</span> <span class="token boolean">true</span> <span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
field2 <span class="token operator">=</span> <span class="token string">'192.'</span> <span class="token operator">+</span> arg1 <span class="token operator">+</span> <span class="token string">'.'</span> <span class="token operator">+</span> arg2 <span class="token operator">+</span> <span class="token string">'.'</span> <span class="token operator">+</span> arg3<span class="token punctuation">;</span>
field3 <span class="token operator">=</span> arg4<span class="token punctuation">;</span>
<span class="token comment">// 模拟告警(测试告警数据需要)</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>i <span class="token operator">===</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>j <span class="token operator">%</span> <span class="token number">5</span> <span class="token operator">!==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
field3 <span class="token operator">=</span> <span class="token punctuation">(</span>Math<span class="token punctuation">.</span><span class="token function">random</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token number">20</span> <span class="token operator">+</span> <span class="token number">80</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toFixed</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
j<span class="token operator">++</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">switch</span> <span class="token punctuation">(</span>i<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
<span class="token keyword">case</span> <span class="token number">0</span><span class="token operator">:</span>tag1 <span class="token operator">=</span> <span class="token string">'window'</span><span class="token punctuation">;</span>tag2 <span class="token operator">=</span> <span class="token string">'notebook'</span><span class="token punctuation">;</span><span class="token keyword">break</span><span class="token punctuation">;</span>
<span class="token keyword">case</span> <span class="token number">1</span><span class="token operator">:</span>tag1 <span class="token operator">=</span> <span class="token string">'window'</span><span class="token punctuation">;</span>tag2 <span class="token operator">=</span> <span class="token string">'desktop'</span><span class="token punctuation">;</span><span class="token keyword">break</span><span class="token punctuation">;</span>
<span class="token keyword">case</span> <span class="token number">2</span><span class="token operator">:</span>tag1 <span class="token operator">=</span> <span class="token string">'linux'</span><span class="token punctuation">;</span>tag2 <span class="token operator">=</span> <span class="token string">'notebook'</span><span class="token punctuation">;</span><span class="token keyword">break</span><span class="token punctuation">;</span>
<span class="token keyword">case</span> <span class="token number">3</span><span class="token operator">:</span>tag1 <span class="token operator">=</span> <span class="token string">'linux'</span><span class="token punctuation">;</span>tag2 <span class="token operator">=</span> <span class="token string">'desktop'</span><span class="token punctuation">;</span><span class="token keyword">break</span><span class="token punctuation">;</span>
<span class="token keyword">case</span> <span class="token number">4</span><span class="token operator">:</span> tag1 <span class="token operator">=</span> <span class="token string">'mac'</span><span class="token punctuation">;</span>tag2 <span class="token operator">=</span> <span class="token string">'notebook'</span><span class="token punctuation">;</span><span class="token keyword">break</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>field1<span class="token punctuation">,</span> field2<span class="token punctuation">,</span> field3<span class="token punctuation">,</span> tag1<span class="token punctuation">,</span> tag2<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">insert</span><span class="token punctuation">(</span>field1<span class="token punctuation">,</span> field2<span class="token punctuation">,</span> field3<span class="token punctuation">,</span> tag1<span class="token punctuation">,</span> tag2<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
}, 1000 * 30);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
插入数据如下所示
3、grafana 测试连接
- 新建仪表盘
- 选择刚才新建的数据源
- 添加查询语句
SELECT "temperature" FROM "cpu" WHERE $timeFilter GROUP BY "model", "system"
Ⅱ、influx 2.x
1、添加数据源
- 2.x 和 1.x 差别还是挺大的,查询语言使用 flux,相关语法可参考 https://docs.influxdata.com/flux/v0.x/
- 连接不在使用用户名密码连接,而是使用 token
Organization,选择创建组织
Token
Default Bucket,默认桶,即数据库
2、添加数据
如果需要添加数据的可以在 cli ui 界面选择客户端或其他数据源,选择自己熟悉的语言,复制其中的代码即可
3、grafana测试连接
- grafana 新建仪表盘
- 选定 influx2.x 数据源
- 添加 flux 查询语句
from(bucket: "test")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "temperature")
|> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
|> yield(name: "mean")
- 1
- 2
- 3
- 4
- 5
结果如下