首页 > 解决方案 > 为什么我的 Grafana 仪表板中的面板重叠?

问题描述

我正在尝试创建一个 Grafana 脚本仪表板,其中包含指示服务器是否已启动的面板。这样做的原因是我们不断地添加一个代理列表,并且每次创建一个新面板变得重复。

目前,我有这个作为我的脚本(注意我的 JavaScript/JQuery 技能很糟糕..):

'use strict';

// accessible variables in this scope
var window, document, ARGS, $, jQuery, moment, kbn;

return function(callback) {

  // Setup some variables
  var dashboard;

  // Initialize a skeleton with nothing but a rows array and service object
  dashboard = {
    annotations: {
      list: [
        {
          builtIn: 1,
          datasource: "-- Grafana --",
          enable: true,
          hide: false,
          iconColor: "rgba(0, 211, 255, 1)",
          name: "Annotations & Alerts",
          type: "dashboard"
        }
      ]
    },
    editable: true,
    gnetId: null,
    graphTooltip: 0,
    id: 15,
    links: [],
    panels : [],
    refresh: false,
    schemaVersion: 16,
    style: "dark",
    tags: [],
    templating: {
      list: []
    },
    time: {
      from: "now-5m",
      to: "now"
    },
    timepicker: {
      refresh_intervals: [
        "5s",
        "10s",
        "30s",
        "1m",
        "5m",
        "15m",
        "30m",
        "1h",
        "2h",
        "1d"
      ],
      time_options: [
        "5m",
        "15m",
        "1h",
        "6h",
        "12h",
        "24h",
        "2d",
        "7d",
        "30d"
      ]
    },
    timezone: "",
    title: "My Agents",
    //uid: "000000019",
    version: 2
  };

  // Set a title
  dashboard.title = 'Scripted dash';

  // Set default time
  // time can be overridden in the url using from/to parameters, but this is
  // handled automatically in grafana core during dashboard initialization
  dashboard.time = {
      from: "now-6h",
      to: "now"
  };

  var rows = 1;
  var seriesName = 'argName';

  if(!_.isUndefined(ARGS.rows)) {
    rows = parseInt(ARGS.rows, 10);
  }

  if(!_.isUndefined(ARGS.name)) {
    seriesName = ARGS.name;
  }

  $.ajax({
    method: 'GET',
    url: 'http://my_server/api/my_rest_call_to_get_agents'
  })
  .done(function(result) {
    //console.log(JSON.stringify(result));
    var x = 0;
    var y = 0;
    var id = 0;
    $.each(result.data.result, function(index, value) {
      //console.log(value.metric.instance);
      var panel = {
        clusterName: "My Agent",
        colorMode: "Panel",
        colors: {
          crit: "rgba(245, 54, 54, 0.9)",
          disable: "rgba(128, 128, 128, 0.9)",
          ok: "rgba(50, 128, 45, 0.9)",
          warn: "rgba(237, 129, 40, 0.9)"
        },
        cornerRadius: 0,
        //datasource: "Prometheus",
        displayName: "Agent_Name",
        flipCard: true,
        flipTime: "2",
        fontFormat: "Bold",
        gridPos: {
          h: 3,
          w: 2,
          x: x,
          y: y
        },
        id: id,
        isGrayOnNoData: true,
        isHideAlertsOnDisable: false,
        isIgnoreOKColors: false,
        links: [],
        targets: [
          {
            aggregation: "Last",
            alias: value.metric.instance,
            crit: 0,
            decimals: 2,
            displayAliasType: "Warning / Critical",
            displayType: "Regular",
            displayValueWithAlias: "Never",
            expr: 'expression_to_query_service',
            format: "time_series",
            instant: true,
            intervalFactor: 1,
            legendFormat: value.metric.instance,
            refId: "A",
            units: "short",
            valueHandler: "Number Threshold"
          }
        ],
        title: value.metric.instance,
        transparent: true,
        type: "vonage-status-panel"
      }
      dashboard.panels.push(panel);
      x += 2;
    });
    callback(dashboard);
  });
}

所有面板重叠的最奇怪的部分是,如果我查看每个面板的 JSON,每个面板都有不同的坐标。

对此的任何帮助将不胜感激!

标签: grafana

解决方案


推荐阅读