资料
数据流
grafana ——> mongodb-grafana插件 ——> mongodb-proxy ——> mongodb
- 一个mongdb-proxy对应一个mongodb
部署
-
目录
├── control.sh ├── Dockerfile (grafana) ├── mongodb-grafana └── mongodb-proxy ├── Dockerfile (mongodb-proxy) └── mongodb-grafana
-
下载
git clone https://github.com/JamesOsgood/mongodb-grafana.git
-
Dockerfile(grafana)
From grafana/grafana:6.5.0 ADD mongodb-grafana /var/lib/grafan plugins/mongodb-grafana
-
Dockerfile(mongodb-proxy)
From node:14.8.0 ADD ./mongodb-grafana /usr/localmongodb-grafana WORKDIR /usr/local/mongodb-grafana RUN npm install --registry=https:/registry.npm.taobao.org EXPOSE 3333 CMD ["npm","run","server"]
-
安装 (control.sh)
#!/bin/bash # date: 2020/08/29 # description: grafana support mongodb datasource # auth: vera function install_grafana(){ # 3000 docker pull grafana/grafana:6.5.0 docker build -t grafana/grafana:6.5.1 . docker stop grafana docker rm grafana docker run -d --name grafana --net=host grafana/grafana:6.5.1 } function install_mongodb(){ #27017 docker pull mongo:4.2.9 docker stop mongo docker rm mongo docker run --name mongo --net=host -d mongo:4.2.9 } function install_mongodb_proxy(){ # 3333 docker pull node:14.8.0 cd mongodb-proxy && docker build -t grafana/mongodb-proxy:0.0.1 . docker stop mongodb-proxy docker rm mongodb-proxy docker run -d --name mongodb-proxy --net=host grafana/mongodb-proxy:0.0.1 } install_grafana install_mongodb install_mongodb_proxy
验证
-
创建mongodb数据
# docker exec -it mongo /bin/bash root@9:/# mongodb show dbs; use test db.fruit.insert("name":"apple") db.fruit.insert({"name":"orange"}) db.fruit.insert({"name":"banana"}) db.fruit.insert({"name":"banana"}) db.fruit.insert({"name":"banana"}) db.fruit.insert({"name":"xigua","city":"xian","num":6,"ts":new Date()}) db.fruit.insert({"name":"xigua","city":"xian","num":6,"ts":new Date()}) db.fruit.insert({"name":"xigua","city":"xian","num":6,"ts":new Date()}) db.fruit.insert({"name":"xigua","city":"xian","num":6,"ts":new Date()})
-
聚合查询
mongodb聚合查询有管道的概念,可以传递。
# 根据name为维度分组,查询每组出现的次数 db.fruit.aggregate([{ "$group": { _id: "$name", num:{ "$sum": 1} } }]) # 匹配查询 db.fruit.aggregate([ { "$match":{"name":"xigua"} } ,{ "$group": { _id: "$name", num:{ "$sum": 1} } } ] )
-
grafana配置数据源
HTTP的Access方式选择: Servcer(Dafault) mongoDB URL: mong0db://127.0.0.1:27017 mongDB Database: test
-
grafana查询mongodb
-
变量
# 查询所有name类型 db.fruit.aggregate([ { "$group": { "_id": "$name" }} ])
-
table格式数据
# 适用于 mongodb存储时没有时间字段数据 db.fruit.aggregate([ { "$match": { "name": "xigua" } }, { "$project": { "name": "$name", "value": "$num", "_id": 0 } } ])
-
timeserie时间序列数据
# $from 和 $to 变量是grafana的UI的时间序列范围 # 适用于 mongodb存储有时间字段(ts)数据 db.fruit.aggregate([ { "$match": { "name": "xigua", "ts": { "$gte": "$from", "$lte": "$to" } } }, { "$sort": { "ts": 1 } }, { "$project": { "name": "value", "value": "$num", "_id": 0, "ts": "$ts" } } ])
-
网友评论