Linux: Grafana可视化
- TAGS: Linux
添加变量
全局变量
- https://grafana.com/docs/grafana/latest/dashboards/variables/add-template-variables/#global-variables
- https://grafana.org.cn/docs/grafana/latest/dashboards/variables/
$__dashboard #当前仪表板的名称 $__from and $__to #时间戳。 &from=${__from}&to=${__to} 或from=now-24h&to=now $__interval #Grafana 自动计算一个间隔 $__interval_ms #以毫秒为单位的$__interval变量 $__name #仅在 Singlestat 面板中可用 $__org #当前组织的 ID $__user #${__user.id} ${__user.login} ${__user.email} $__range #通过to - from计算的. 它有一个毫秒和一个秒表示,称为$__range_ms和$__range_s $__rate_interval #仅支持 Prometheus 数据源. 用于速率函数 $__rate_interval_ms #以毫秒为单位的$__rate_interval变量 $timeFilter or $__timeFilter #以表达式的形式返回当前选定的时间范围.例如,时间范围间隔Last 7 days表达式为time > now() - 7d $__timezone #当前选择的时区
自定义时间范围
${__from} 1594671549254
${__from:date} 2020-07-13T20:19:09.254Z
${__from:date:iso} 2020-07-13T20:19:09.254Z
${__from:date:seconds} 1594671549
${__from:date:YYYY-MM} 2020-07
范例
/d/$clusterUid-k8s-deploy/kubernetes-deployment?\ orgId=$__org&var-namespace=default&var-name=${deployment}\ &from=${__from}&to=${__to}&showTitle&kiosk=$__kiosk
隐藏常量
如果你的面板uid设置的571136-k8s-overview,统一有571136前缀,那么可以添加一个隐藏常量 clusterUid值为571136 方便调用。
- Variable type: Constant
- Name: clusterUid
- Value: 571136
https://a/d/$clusterUid-k8s-overview/
自定义
- 变量类型:自定义
- 名称:My
自定义选项: 以逗号分隔的值. 如 1, 10, mykey : myvalue, myvalue, escaped\,value
例: '
A : aaa|AAA,B : bbb|BBB,C : ccc|CCCC'
数据链接变量
- https://grafana.com/docs/grafana/latest/panels-visualizations/configure-data-links/
- https://grafana.org.cn/docs/grafana/latest/panels-visualizations/configure-data-links/
数据链接中的变量可让您将人们发送到带有保留数据过滤器的详细仪表板。例如,您可以使用变量来指定标签、时间范围、系列或变量选择。
在Data link面板使用$来补全显示所有可用变量
__url_time_range #当前仪表板的时间范围。 例如, ?from=now-6h&to=now __series.name #查询页面下Options中Legend取值 __field.name #指标名称 __field.labels.<LABEL> #指标的标签 __value.numeric #指标的值 __data.fields.name #表格字段。或者 __data.fields["NameOfField"]
范例
/d/$clusterUid-k8s-deploy/kubernetes-deployment?orgId=$__org&var-namespace=default&var-name=${__series.name}&${__url_time_range} #Table图表中Namespace字段取值 # 1 - 按字段合并 pod # 2 - 按名称组织字段 namespace改为Namespace /d/$clusterUid-k8s-namespace/k8s-namespace?var-Namespace=${__data.fields.Namespace}&kiosk=$__kiosk&showTitle
注意:直接在 Data Link 的 URL 中对变量进行数学运算目前不被支持
查询语法引用变量
编辑查询页面下Options选项,Legend 用来覆盖查询结果标签,{{xxx}} 引用某个标签名称
变量语法
如 编辑 Variables,
- Variable type: Custom
- Name: My
- Custom options: key1 : value1,key2 : value2
- 勾选可多选 Multi-value
value
Custom options: A : aaa|AAA,B : bbb|BBB,C : ccc|CCCC 执行 String to interpolate: '${My:value}' Interpolation result: '{aaa|AAA,bbb|BBB,ccc|CCC}' #不适合多选 表达式: sum(sum by(url, game, code) (rate(Fb_Net_Code_count{game=~".*",url=~".*(${My:value})",url!~".*\\.(png|jpg|jpeg|json)",prom_cluster="prod-eks-client"}[30s])))by (url) 解析后: 可以看到|分隔成 {aaa、AAA,bbb、ccc|CCCC} sum(sum by(url, game, code) (rate(Fb_Net_Code_count{game=~".*",url=~".*({aaa|AAA,bbb|BBB,ccc|CCCC})",url!~".*\\.(png|jpg|jpeg|json)",prom_cluster="prod-eks-client"}[30s])))by (url)
pip
Custom options: A : aaa|AAA,B : bbb|BBB,C : ccc|CCCC #执行 String to interpolate: '${My:pipe}' Interpolation result: 'aaa|AAA|bbb|BBB|ccc|CCC' 表达式: sum(sum by(url, game, code) (rate(Fb_Net_Code_count{game=~".*",url=~".*(${My:pipe})",url!~".*\\.(png|jpg|jpeg|json)",prom_cluster="prod-eks-client"}[30s])))by (url) 解析后: sum(sum by(url, game, code) (rate(Fb_Net_Code_count{game=~".*",url=~".*(aaa|AAA|bbb|BBB|ccc|CCCC)",url!~".*\\.(png|jpg|jpeg|json)",prom_cluster="prod-eks-client"}[30s])))by (url)
图例
AI Assistant
13.1 版本
安装步骤
前置:得有个 Grafana Cloud 账号(免费版就行,带 Assistant 额度)。另外完成 OAuth 的是你的浏览器,不是 105,所以你本机要能打开 grafana.com。
- 浏览器开 http://10.0.0.105:3000,用 org admin 账号登录
- Administration → Plugins and data → Plugins
- 搜 Grafana Assistant,或直接开 http://10.0.0.105:3000/plugins/grafana-assistant-app
- 点 Install
- 装完刷新页面 —— 不刷新的话 Connection 标签不会出现,官方文档专门点了这个坑
- 进 Connection 标签 → Connect to Grafana Cloud
- 授权页里选目标 org 和 stack → Authorize connection
- 页面重载后,Assistant 就出现在 UI 里了
用户授权
需求:
- 研发账号,用于看指定的面板。 指定只读面板,同时可以使用 drilldown下的 Logs 查看日志也可以使用 Loki 的 explore 自己写查询语句。
先建 team——权限授给 team,以后加人只需把人拉进 team,不用再碰权限。
- Grafana 版本:13.2.2 Open Source,没有 Enterprise 的细粒度 RBAC
- Viewer 默认权限:有
datasources:query、plugins.app:access(Drilldown 能用),没有datasources:explore viewers_can_edit=true:仍然有效(网上说 Grafana 12 移除了,不准)。开启后 Viewer 只多出datasources:explore,dashboards:write/dashboards:create。仍然没有——Explore 能用,但存不了盘- Editor 角色:有 Explore,但能新建看板、也能覆盖你的看板。太宽
- 文件夹权限: OSS 下可以把默认的 Viewer/Editor 权限整个去掉,只授给 team。实测该用户只看到授权文件夹
所以选 Viewer + viewers_can_edit ,不选 Editor——Editor 要靠文件夹权限反过来堵
操作步骤
#步骤 1 开 viewers_can_edit cp /etc/grafana/grafana.ini /etc/grafana/grafana.ini.bak-$(date +%Y%m%d) sed -i '/^\[users\]/a viewers_can_edit = true' /etc/grafana/grafana.ini systemctl restart grafana-server sleep 8 curl -s -u admin:admin http://10.0.0.101:3000/api/frontend/settings \ | python3 -c 'import sys,json; print("viewersCanEdit =", json.load(sys.stdin)["viewersCanEdit"])' # 最后一条要输出 True。 #步骤 2 建团队 curl -s -u admin:admin -H 'Content-Type: application/json' \ -X POST http://10.0.0.101:3000/api/teams \ -d '{"name":"研发"}' #{"message":"Team created","teamId":2,"uid":"eg03qrhofqvb4f"} #记下返回的 teamId(第一个团队通常是 1)。 #步骤 3 建两个文件夹 for f in '{"uid":"dev-visible","title":"研发看板"}' '{"uid":"ops-only","title":"运维看板"}'; do curl -s -u admin:admin -H 'Content-Type: application/json' \ -X POST http://10.0.0.101:3000/api/folders -d "$f"; echo done #清掉四个看板的自身 ACL for d in loki-log-overview loki-k8s-events loki-dev-troubleshoot loki-gateway-access; do printf '%-24s ' "$d" curl -s -u admin:admin -H 'Content-Type: application/json' \ -X POST "http://10.0.0.101:3000/api/dashboards/uid/$d/permissions" \ -d '{"items":[]}' echo done #将对应面板放到不同文件夹 #应该看到研发排障和 Gateway 在「研发看板」,另两个在「运维看板」,没有 General。 #步骤 4 设文件夹权限 #"items" 是整体替换——不写基础角色就等于把默认的 Viewer/Editor 权限去掉。把 TEAMID 换成步骤 2 的值。 TEAMID=2 curl -s -u admin:admin -H 'Content-Type: application/json' \ -X POST http://10.0.0.101:3000/api/folders/dev-visible/permissions \ -d "{\"items\":[{\"teamId\":$TEAMID,\"permission\":1}]}"; echo curl -s -u admin:admin -H 'Content-Type: application/json' \ -X POST http://10.0.0.101:3000/api/folders/ops-only/permissions \ -d '{"items":[]}'; echo #permission: 1 = View(2 = Edit,4 = Admin)。 # 步骤 5 建用户并加入团队 read -rsp '研发账号密码: ' PW; echo curl -s -u admin:admin -H 'Content-Type: application/json' \ -X POST http://10.0.0.101:3000/api/admin/users \ -d "{\"name\":\"研发\",\"login\":\"dev\",\"password\":\"$PW\"}"; echo unset PW #{"id":6,"uid":"fg03s1dkaevb4b","message":"User created"} #记下返回的用户 id,然后(UID/TEAMID 换成实际值): GFUID=6; TEAMID=2 curl -s -u admin:admin -H 'Content-Type: application/json' \ -X PATCH http://10.0.0.101:3000/api/org/users/$GFUID -d '{"role":"Viewer"}'; echo curl -s -u admin:admin -H 'Content-Type: application/json' \ -X POST http://10.0.0.101:3000/api/teams/$TEAMID/members -d "{\"userId\":$GFUID}"; echo #步骤 6 验收 DEV="dev:<上面设的密码>" echo "--- 能看到的文件夹(应只有 研发看板 和 Shared with me)---" curl -s -u "$DEV" http://10.0.0.101:3000/api/folders \ | python3 -c 'import sys,json; print([f["title"] for f in json.load(sys.stdin)])' echo "--- 关键权限 ---" curl -s -u "$DEV" http://10.0.0.101:3000/api/access-control/user/permissions \ | python3 -c 'import sys,json p=json.load(sys.stdin) for k in ("datasources:explore","datasources:query","plugins.app:access","dashboards:write","dashboards:create"): print(" %-22s %s" % (k, p.get(k,"— 没有")))' #团队「研发」显示成员数 curl -s -u admin:admin http://10.0.0.101:3000/api/teams/2/members \ | python3 -c 'import sys,json; [print(" %-10s id=%s" % (m["login"], m["userId"])) for m in json.load(sys.stdin)]' #余的成员用 DELETE /api/teams/2/members/<userId> 移除 # 登录查看