grafana dashboard 分享的几种处理方法

prometheus+grafan 已经成为了大家进行系统监控的标配了,但是很多时候我们为了给予用户一个方便的dashboard查看系统
的metrics 请求,可以会进行grafan dashboard 的分享,大家的解决方法可能很多,基于grafana 分享的(需要修改配置)也有
基于grafana 的script 的(官方不太推荐的),还有直接基于grafana 的auth_proxy 模式的,个人也比较推荐基于auth_proxy 的
因为我们会通过一个proxy 解决header 传参,那么我们就可以方便的控制请求了,openresty 是一个很不错的选择,以下是一些
实践以及扩展说明

一张参考图

 

 

参考配置

  • grafana
    grafana.ini
 
[auth.proxy]
enabled = true
header_name = X-WEBAUTH-USER
;header_property = username
auto_sign_up = true
  • nginx 参考配置
location / {
            proxy_pass http://grafana:3000;
            proxy_set_header X-WEBAUTH-USER dalongrong;
            proxy_set_header   Host $host;
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Host $server_name;
            proxy_set_header   X-Forwarded-Proto $scheme;
}
 
 

扩展说明

默认上边的很简单,尽管用户不需要输入密码了,但是没有安全控制了(直接打开浏览器就能看dashboard)
尽管官方提供了enable_login_token 但是不是很灵活,一些解决方法

  • 基于openresty 的access_by_lua 阶段进行用户的认证以及请求资源的处理
    很多时候我们嵌入dashboard 的时候需要传递参数的,我们可以多附带一个token的参数,在这个阶段我们处理用户的
    token 以及用户的机器资源,这样可以详细的控制访问(参考上图),同时可以动态的进行proxy 以及设置用户请求头
  • 基于nginx 的auth_request 模块
    原理上与openresty 的access_by_lua 类似,基于模块进行权限的与机器资源的控制,但是从灵活性上不如openresty

说明

以上是grafana auth_proxy +openresty 的一些集成尝试,实际上,prometheus 也暴露了rest api,我们也可以基于rest api +
charts 开发自己的dashboard 集成方案(更灵活,但是成本比较高了),chartjs-plugin-datasource-prometheus 是一个不错的
选择但是目前太简单了,我们可以自己扩展下

参考资料

https://grafana.com/docs/grafana/latest/reference/scripting/
https://grafana.com/docs/grafana/latest/auth/auth-proxy/#auth-proxy-authentication
https://grafana.com/docs/grafana/latest/reference/share_dashboard/
https://github.com/samber/chartjs-plugin-datasource-prometheus
http://nginx.org/en/docs/http/ngx_http_auth_request_module.html
https://www.cnblogs.com/rongfengliang/p/13150413.html

posted on 2020-08-17 17:48  荣锋亮  阅读(4607)  评论(0编辑  收藏  举报

导航