# 设置可嵌入 iframe

找到 grafana.ini,

security:
allow_embedding: true

参考: https://grafana.com/docs/grafana/v7.5/administration/configuration/#allow_embedding (将 grafana 嵌入 iframe)

# 设置免密登录,方便在项目中将 grafana 以 iframe 形式嵌入进去

使用 grafana admin 账号设置一个只有 Viewer 权限的用户

使用在线 base64 工具如 https://base64.us 来 base 以下字符串 (注意中间用分号隔开): username:password

base64 后得到: dXNlcm5hbWU6cGFzc3dvcmQ=

在 nginx/conf/nginx.conf 下面配置

include /opt/nginx/conf.d/*.conf;

/opt/nginx/conf.d 下新建 grafana.conf,写入下面内容

server {
    listen 1801;
    server_name app-grafana.jalen.com;

    location / {
        proxy_pass http://xx.xxx.xx.xx:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $proxy_host;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header  X-Real-IP         $remote_addr; # pass on real client's IP
        proxy_set_header  X-Forwarded-For   $proxy_add_x_forwarded_for;
        proxy_set_header  X-Forwarded-Proto $scheme;
        proxy_set_header  Authorization "Basic dXNlcm5hbWU6cGFzc3dvcmQ=";
        proxy_hide_header 'X-Frame-Options';
        add_header X-Frame-Options ALLOWALL;
    }
}

参考:
http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_bypass
http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_set_header

# Iframe 隐藏左侧边栏,只显示 dashboard 主体

方法一:

参考: https://github.com/grafana/grafana/issues/13493#issuecomment-426304112

添加 url 参数 & kiosk=tv

如: https://app-proxy-grafana.jalen.com/d/XumeirY7z/site-admin-features?orgId=1&kiosk=tv

虽然左侧边栏没有了,但是这个时候 dashboard 配置的 variables 不显示了,不符合需求

参考 issue:

https://github.com/grafana/grafana/issues/15546

https://github.com/grafana/grafana/issues/20176

方法二:

使用 css

参考: https://github.com/grafana/grafana/issues/13493#issuecomment-504309808

<iframe style="margin-left:-30px" id="dashboardIframe" src="http://domain/dashboard?orgId=1&theme=light&from=1553319207005&to=1561095207008&var-domain=All&var-tribe=All&var-squad=All&var-site=All&var-self_service_type=All" class="" width="100%" height="1000px" frameborder="0"></iframe>

.iframe-usage{
  position: relative;
  left: -58px;
  width: calc(100% + 58px);
  height: 650px;
  top: -5px;
}

方法三:

改 grafana 的主题 css

参考: https://github.com/grafana/grafana/issues/15546#issuecomment-658661495

[root@chumingcheng grafana]# locate grafana.dark
/usr/share/grafana/public/build/grafana.dark.b5d3c4da30d141a6181f.css
/usr/share/grafana/public/sass/grafana.dark.scss
[root@chumingcheng grafana]# locate grafana.light
/usr/share/grafana/public/build/grafana.light.b5d3c4da30d141a6181f.css
/usr/share/grafana/public/sass/grafana.light.scss
[root@chumingcheng build]# cp grafana.dark.b5d3c4da30d141a6181f.css/users/jalchu/  #备份
[root@chumingcheng build]# vi grafana.dark.b5d3c4da30d141a6181f.css
.view-mode--tv .submenu-controls{display:none}
.view-mode--tv .submenu-controls{display: flex}
:.,$ s/str1/str2/g 用字符串 str2 替换正文当前行到末尾所有出现的字符串 str1
:1,$ s/str1/str2/g 用字符串 str2 替换正文中所有出现的字符串 str1

:.,$ s/.view-mode--tv .submenu-controls{display:none}/.view-mode--tv .submenu-controls{display: flex}/g

不用重启 grafana,改过即时生效

更新于 阅读次数

请我喝[茶]~( ̄▽ ̄)~*

Jalen Chu 微信支付

微信支付

Jalen Chu 支付宝

支付宝

Jalen Chu 公众号

公众号