本文主要介绍如何使用grafana中可视化配置图表Table,与传统的表格展示方式不同,Grafana的Table支持丰富的可视化配置,可以将数据以行列形式展示出来。同时,还可以将Table与其他图表(如折线图、柱状图等)进行组合,实现多维度数据的展示。本文最终的显示效果

自定义grafana_table(数据源Prometheus)_promethetus的blackblx

前面相关文章:1、自定义grafana-time-serial(数据源Prometheus) https://blog.51cto.com/chier11/10003772  2、Prometheus部署及其使用(blackbox+grafana) https://blog.51cto.com/chier11/9736763

准备工作

1、服务和数据来源均正常运行:grafana、prometheus等;grafana的数据源已接入prometheus;对于公网地址的ping已经采集到数据;

2、监控指标项已经确定:probe_success、probe_dns_lookup_time_seconds、probe_duration_seconds,以及使用的函数(如sum_over_time)已经确定;

3、变量:配置获取ip地址、以及分组(此处使用vsr_icmp组名)等进行筛选;

前期工作

配置自定义变量

1、配置变量获取ip地址,前面文章已经介绍,此处省略:

2、配置筛选的分组(因为后面很多和分组有关,所以写成变量,也可以直接写固定的名称,比如group=“vsr_icmp”),该变量如下:

自定义grafana_table(数据源Prometheus)_blackbox的grafana展示_02

目前自定义的变量有:ip_addr获取ip地址、group获取icmp的分组信息

自定义grafana_table(数据源Prometheus)_blackbox的grafana展示_03

查看效果:

自定义grafana_table(数据源Prometheus)_promethetus的blackblx_04

确定使用的指标项和涉及的函数

大致涉及的指标项和函数如下(组名:vsr_icmp和自定义变量ip_addr):

##以下4个指标项,涉及的自定义变量和组信息等根据需求自行就行,此处只提供例子
1、连通性:probe_success{instance=~"$ip_addr"} - 0
2、DNS解析:probe_dns_lookup_time_seconds{group="vsr_icmp"}
3、耗时:probe_duration_seconds{group="vsr_icmp"} - 0
4、1H可用率:sum_over_time(probe_success{group="vsr_icmp"}[60m])/count_over_time(probe_success{group="vsr_icmp"}[60m])

配置table对应的列值--指标项和函数

此处包含组名、公网地址、连通性、DNS解析、耗时(延迟)、1H可用率等六项。

自定义grafana_table(数据源Prometheus)_blackbox的grafana展示_05

选择【table】可视化类型。然后在“指标编辑区”内填写table值的指标项。

添加”连通性“,如图

自定义grafana_table(数据源Prometheus)_blackbox的grafana展示_06

添加”DNS解析“,如图

自定义grafana_table(数据源Prometheus)_blackbox的grafana展示_07

添加“延时”,如图

自定义grafana_table(数据源Prometheus)_blackbox的grafana展示_08

添加“1H可用率”,如图

自定义grafana_table(数据源Prometheus)_promethetus的blackblx_09

汇总的  指标编辑区。

自定义grafana_table(数据源Prometheus)_blackbox的grafana展示_10

配置table的显示效果

以下所有操作均在【table】视图编辑的【override】中添加和编辑

添加实例显示名

--intaence 的 override属性。选择“Fields with name

自定义grafana_table(数据源Prometheus)_blackbox的grafana展示_11

自定义grafana_table(数据源Prometheus)_blackbox的grafana展示_12

添加显示名称 "+Add override property" 中的 “Standard options>Display name” 选项,填写“公网地址

自定义grafana_table(数据源Prometheus)_blackbox的grafana展示_13

添加显示单位 "+Add override property" 中的 “Standard options>Unit” 选项,选择:Misc中的short

自定义grafana_table(数据源Prometheus)_promethetus的blackblx_14

自定义grafana_table(数据源Prometheus)_promethetus的blackblx_15

添加 "+Add override property" 中的 “Standard options>Decimals” 选项,填写“2

自定义grafana_table(数据源Prometheus)_blackbox的grafana展示_16

整个公网地址显示属性如下:

自定义grafana_table(数据源Prometheus)_promethetus的blackblx_17

添加连通性图形显示

其他略过,只说下:添加 "+Add override property" 中的 “Value mappings > Value mappings” 选项,然后对应填写“相应的值对应的颜色”

自定义grafana_table(数据源Prometheus)_promethetus的blackblx_18

自定义grafana_table(数据源Prometheus)_blackbox的grafana展示_19

该部分最终效果

自定义grafana_table(数据源Prometheus)_promethetus的blackblx_20

添加dns解析图形显示

其他略过,只说下:添加 "+Add override property" 中的 “Thresholds > Thresholds ” 选项,然后对应填写“相应的值对应的颜色”

自定义grafana_table(数据源Prometheus)_promethetus的blackblx_21

自定义grafana_table(数据源Prometheus)_blackbox的grafana展示_22

该部分最终效果

自定义grafana_table(数据源Prometheus)_promethetus的blackblx_23

添加耗时图形显示

最终效果

自定义grafana_table(数据源Prometheus)_promethetus的blackblx_24

添加1H可用率图形显示

自定义grafana_table(数据源Prometheus)_promethetus的blackblx_25

这里增加一个超过0.99开始变色,0.9变为黄色,低于0.9显示红色

自定义grafana_table(数据源Prometheus)_promethetus的blackblx_26

继续配置table编辑的图层

自定义grafana_table(数据源Prometheus)_blackbox的grafana展示_27

添加“Transform data”

自定义grafana_table(数据源Prometheus)_promethetus的blackblx_28

选择“Merge series/tables 类型

自定义grafana_table(数据源Prometheus)_promethetus的blackblx_29

继续添加 “+ Add another transformation”

自定义grafana_table(数据源Prometheus)_promethetus的blackblx_30

选择“Organize fields by name”

自定义grafana_table(数据源Prometheus)_blackbox的grafana展示_31

选择显示的字段和需要隐藏的字段

自定义grafana_table(数据源Prometheus)_blackbox的grafana展示_32

自定义grafana_table(数据源Prometheus)_promethetus的blackblx_33

自定义grafana_table(数据源Prometheus)_blackbox的grafana展示_34


最终整体效果显示

组里全部ip地址

自定义grafana_table(数据源Prometheus)_blackbox的grafana展示_35

自定义grafana_table(数据源Prometheus)_blackbox的grafana展示_36

单个ip地址显示

自定义grafana_table(数据源Prometheus)_blackbox的grafana展示_37


整体json--grafana版

以下json显示如上图,相应参数修改需根据实际情况进行调整。

{
  "annotations": {
    "list": [
      {
        "builtIn": 1,
        "datasource": {
          "type": "grafana",
          "uid": "-- Grafana --"
        },
        "enable": true,
        "hide": true,
        "iconColor": "rgba(0, 211, 255, 1)",
        "name": "Annotations & Alerts",
        "type": "dashboard"
      }
    ]
  },
  "editable": true,
  "fiscalYearStartMonth": 0,
  "graphTooltip": 0,
  "id": 8,
  "links": [],
  "liveNow": false,
  "panels": [
    {
      "datasource": {
        "type": "prometheus",
        "uid": "d1bfba55-f3f4-4b11-8530-b1e0e0ca6db0"
      },
      "fieldConfig": {
        "defaults": {
          "color": {
            "mode": "thresholds"
          },
          "custom": {
            "align": "center",
            "cellOptions": {
              "type": "auto"
            },
            "inspect": false
          },
          "mappings": [],
          "thresholds": {
            "mode": "absolute",
            "steps": [
              {
                "color": "green",
                "value": null
              }
            ]
          },
          "unitScale": true
        },
        "overrides": [
          {
            "matcher": {
              "id": "byName",
              "options": "instance"
            },
            "properties": [
              {
                "id": "displayName",
                "value": "公网地址"
              },
              {
                "id": "unit",
                "value": "short"
              },
              {
                "id": "decimals",
                "value": 2
              }
            ]
          },
          {
            "matcher": {
              "id": "byName",
              "options": "Value #A"
            },
            "properties": [
              {
                "id": "displayName",
                "value": "连通状态"
              },
              {
                "id": "custom.cellOptions",
                "value": {
                  "type": "color-background"
                }
              },
              {
                "id": "custom.align",
                "value": "center"
              },
              {
                "id": "mappings",
                "value": [
                  {
                    "options": {
                      "0": {
                        "color": "semi-dark-red",
                        "index": 1,
                        "text": "离线"
                      },
                      "1": {
                        "color": "semi-dark-green",
                        "index": 0,
                        "text": "在线"
                      }
                    },
                    "type": "value"
                  }
                ]
              }
            ]
          },
          {
            "matcher": {
              "id": "byName",
              "options": "Value #B"
            },
            "properties": [
              {
                "id": "displayName",
                "value": "DNS解析"
              },
              {
                "id": "custom.cellOptions",
                "value": {
                  "type": "color-background"
                }
              },
              {
                "id": "custom.align",
                "value": "center"
              },
              {
                "id": "decimals",
                "value": 2
              },
              {
                "id": "unit",
                "value": "s"
              },
              {
                "id": "thresholds",
                "value": {
                  "mode": "absolute",
                  "steps": [
                    {
                      "color": "green",
                      "value": null
                    },
                    {
                      "color": "yellow",
                      "value": 0.01
                    },
                    {
                      "color": "red",
                      "value": 0.02
                    }
                  ]
                }
              }
            ]
          },
          {
            "matcher": {
              "id": "byName",
              "options": "Value #C"
            },
            "properties": [
              {
                "id": "displayName",
                "value": "耗时"
              },
              {
                "id": "unit",
                "value": "s"
              },
              {
                "id": "custom.cellOptions",
                "value": {
                  "mode": "lcd",
                  "type": "gauge",
                  "valueDisplayMode": "text"
                }
              },
              {
                "id": "decimals",
                "value": 2
              },
              {
                "id": "color",
                "value": {
                  "mode": "continuous-GrYlRd"
                }
              },
              {
                "id": "max",
                "value": 0.5
              },
              {
                "id": "custom.align",
                "value": "center"
              }
            ]
          },
          {
            "matcher": {
              "id": "byName",
              "options": "Value #D"
            },
            "properties": [
              {
                "id": "displayName",
                "value": "1H可用率"
              },
              {
                "id": "unit",
                "value": "percentunit"
              },
              {
                "id": "custom.cellOptions",
                "value": {
                  "mode": "basic",
                  "type": "gauge",
                  "valueDisplayMode": "text"
                }
              },
              {
                "id": "color",
                "value": {
                  "mode": "thresholds"
                }
              },
              {
                "id": "max",
                "value": 1
              },
              {
                "id": "thresholds",
                "value": {
                  "mode": "absolute",
                  "steps": [
                    {
                      "color": "red",
                      "value": null
                    },
                    {
                      "color": "yellow",
                      "value": 0.9
                    },
                    {
                      "color": "light-green",
                      "value": 0.99
                    }
                  ]
                }
              }
            ]
          },
          {
            "matcher": {
              "id": "byName",
              "options": "DNS解析"
            },
            "properties": [
              {
                "id": "custom.width",
                "value": 162
              }
            ]
          },
          {
            "matcher": {
              "id": "byName",
              "options": "连通状态"
            },
            "properties": [
              {
                "id": "custom.width",
                "value": 112
              }
            ]
          },
          {
            "matcher": {
              "id": "byName",
              "options": "耗时"
            },
            "properties": [
              {
                "id": "custom.width",
                "value": 625
              }
            ]
          },
          {
            "matcher": {
              "id": "byName",
              "options": "公网地址"
            },
            "properties": [
              {
                "id": "custom.width",
                "value": 173
              }
            ]
          },
          {
            "matcher": {
              "id": "byName",
              "options": "组名"
            },
            "properties": [
              {
                "id": "custom.width",
                "value": 104
              }
            ]
          },
          {
            "matcher": {
              "id": "byName",
              "options": "1H可用率"
            },
            "properties": [
              {
                "id": "custom.width",
                "value": 218
              }
            ]
          }
        ]
      },
      "gridPos": {
        "h": 7,
        "w": 24,
        "x": 0,
        "y": 0
      },
      "id": 2,
      "options": {
        "cellHeight": "md",
        "footer": {
          "countRows": false,
          "fields": "",
          "reducer": [
            "sum"
          ],
          "show": false
        },
        "frameIndex": 3,
        "showHeader": true,
        "sortBy": [
          {
            "desc": false,
            "displayName": "1H可用率"
          }
        ]
      },
      "pluginVersion": "10.3.3",
      "targets": [
        {
          "datasource": {
            "type": "prometheus",
            "uid": "d1bfba55-f3f4-4b11-8530-b1e0e0ca6db0"
          },
          "editorMode": "code",
          "exemplar": false,
          "expr": "probe_success{group=~\"vsr_icmp\",job=~\"icmp_status\",instance=~\"$ip_addr\"} - 0",
          "format": "table",
          "instant": true,
          "legendFormat": "",
          "range": false,
          "refId": "A"
        },
        {
          "datasource": {
            "type": "prometheus",
            "uid": "d1bfba55-f3f4-4b11-8530-b1e0e0ca6db0"
          },
          "editorMode": "code",
          "exemplar": false,
          "expr": "probe_dns_lookup_time_seconds{group=~\"vsr_icmp\",job=~\"icmp_status\",instance=~\"$ip_addr\"}",
          "format": "table",
          "hide": false,
          "instant": true,
          "legendFormat": "",
          "range": false,
          "refId": "B"
        },
        {
          "datasource": {
            "type": "prometheus",
            "uid": "d1bfba55-f3f4-4b11-8530-b1e0e0ca6db0"
          },
          "editorMode": "code",
          "exemplar": false,
          "expr": "probe_duration_seconds{group=~\"vsr_icmp\",job=~\"icmp_status\",instance=~\"$ip_addr\"} - 0",
          "format": "table",
          "hide": false,
          "instant": true,
          "legendFormat": "",
          "range": false,
          "refId": "C"
        },
        {
          "datasource": {
            "type": "prometheus",
            "uid": "d1bfba55-f3f4-4b11-8530-b1e0e0ca6db0"
          },
          "editorMode": "code",
          "exemplar": false,
          "expr": "sum_over_time(probe_success{group=~\"vsr_icmp\",job=~\"icmp_status\",instance=~\"$ip_addr\"}[60m])/count_over_time(probe_success{group=~\"vsr_icmp\",job=~\"icmp_status\",instance=~\"$ip_addr\"}[60m])",
          "format": "table",
          "hide": false,
          "instant": true,
          "legendFormat": "",
          "range": false,
          "refId": "D"
        }
      ],
      "title": "检测明细 $ip_addr",
      "transformations": [
        {
          "id": "merge",
          "options": {}
        },
        {
          "id": "organize",
          "options": {
            "excludeByName": {
              "Time": true,
              "__name__": true,
              "job": true
            },
            "includeByName": {},
            "indexByName": {},
            "renameByName": {
              "group": "组名",
              "job": ""
            }
          }
        }
      ],
      "type": "table"
    },
    {
      "datasource": {
        "type": "prometheus",
        "uid": "d1bfba55-f3f4-4b11-8530-b1e0e0ca6db0"
      },
      "description": "",
      "fieldConfig": {
        "defaults": {
          "color": {
            "mode": "palette-classic"
          },
          "custom": {
            "axisBorderShow": false,
            "axisCenteredZero": false,
            "axisColorMode": "text",
            "axisLabel": "ICMP时延",
            "axisPlacement": "left",
            "axisSoftMax": 10,
            "axisSoftMin": 0,
            "barAlignment": 0,
            "drawStyle": "line",
            "fillOpacity": 0,
            "gradientMode": "none",
            "hideFrom": {
              "legend": false,
              "tooltip": false,
              "viz": false
            },
            "insertNulls": false,
            "lineInterpolation": "smooth",
            "lineStyle": {
              "fill": "solid"
            },
            "lineWidth": 1,
            "pointSize": 4,
            "scaleDistribution": {
              "type": "linear"
            },
            "showPoints": "auto",
            "spanNulls": false,
            "stacking": {
              "group": "A",
              "mode": "none"
            },
            "thresholdsStyle": {
              "mode": "off"
            }
          },
          "fieldMinMax": false,
          "mappings": [],
          "thresholds": {
            "mode": "absolute",
            "steps": [
              {
                "color": "green",
                "value": null
              }
            ]
          },
          "unit": "s",
          "unitScale": true
        },
        "overrides": []
      },
      "gridPos": {
        "h": 10,
        "w": 24,
        "x": 0,
        "y": 7
      },
      "id": 1,
      "options": {
        "legend": {
          "calcs": [
            "min",
            "max",
            "mean",
            "last"
          ],
          "displayMode": "table",
          "placement": "bottom",
          "showLegend": true,
          "sortBy": "Name",
          "sortDesc": false
        },
        "tooltip": {
          "mode": "multi",
          "sort": "desc"
        }
      },
      "targets": [
        {
          "datasource": {
            "type": "prometheus",
            "uid": "d1bfba55-f3f4-4b11-8530-b1e0e0ca6db0"
          },
          "editorMode": "code",
          "expr": "probe_duration_seconds{group=\"vsr_icmp\",job=\"icmp_status\",instance=~\"$ip_addr\"}",
          "instant": false,
          "legendFormat": "{{ instance }}",
          "range": true,
          "refId": "A"
        }
      ],
      "title": "拨测延时 $ip_addr",
      "type": "timeseries"
    }
  ],
  "refresh": "auto",
  "schemaVersion": 39,
  "tags": [],
  "templating": {
    "list": [
      {
        "allValue": "",
        "current": {
          "selected": true,
          "text": [
            "All"
          ],
          "value": [
            "$__all"
          ]
        },
        "datasource": {
          "type": "prometheus",
          "uid": "d1bfba55-f3f4-4b11-8530-b1e0e0ca6db0"
        },
        "definition": "label_values(probe_icmp_duration_seconds,instance)",
        "hide": 0,
        "includeAll": true,
        "label": "",
        "multi": true,
        "name": "ip_addr",
        "options": [],
        "query": {
          "qryType": 1,
          "query": "label_values(probe_icmp_duration_seconds,instance)",
          "refId": "PrometheusVariableQueryEditor-VariableQuery"
        },
        "refresh": 1,
        "regex": "",
        "skipUrlSync": false,
        "sort": 0,
        "type": "query"
      },
      {
        "current": {
          "selected": true,
          "text": [
            "vsr_icmp"
          ],
          "value": [
            "vsr_icmp"
          ]
        },
        "datasource": {
          "type": "prometheus",
          "uid": "d1bfba55-f3f4-4b11-8530-b1e0e0ca6db0"
        },
        "definition": "label_values(probe_icmp_duration_seconds,group)",
        "hide": 0,
        "includeAll": true,
        "label": "group",
        "multi": true,
        "name": "group",
        "options": [],
        "query": {
          "qryType": 1,
          "query": "label_values(probe_icmp_duration_seconds,group)",
          "refId": "PrometheusVariableQueryEditor-VariableQuery"
        },
        "refresh": 1,
        "regex": "",
        "skipUrlSync": false,
        "sort": 0,
        "type": "query"
      }
    ]
  },
  "time": {
    "from": "now-30m",
    "to": "now"
  },
  "timepicker": {},
  "timezone": "browser",
  "title": "my_icmp 拨测",
  "uid": "df7083ca-79e2-4a81-8efc-d020a07ba6fc",
  "version": 25,
  "weekStart": ""
}

相关prometheus的配置脚本

此处只展示prometheus中icmp配置和icmp有关的配置

prometheus.yml配置行:/usr/local/prometheus/conf.d/icmp_get/*.yml

- job_name: icmp_status
    metrics_path: /probe
    params:
      module: [icmp]
    file_sd_configs:
      - files: ['/usr/local/prometheus/conf.d/icmp_get/*.yml']
        refresh_interval: 10s
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: 10.0.185.22:9115  ##IP地址

自定义grafana_table(数据源Prometheus)_promethetus的blackblx_38

ip地址配置文件 /usr/local/prometheus/conf.d/icmp_get/*.yml

- targets:
  - 3.1.3.4
  - 3.1.3.5
  - 3.1.3.6
  labels:
    group: 'vsr_icmp'   ##根据实际添加参数和值

自定义grafana_table(数据源Prometheus)_promethetus的blackblx_39


以上是prometheus的blackbox中icmp的grafana配置,如有问题可留言。