Grafana Query类型模板变量的使用

一、背景

假设我有2种类型的服务器,一种是本地电脑(每个指标名称都存在{nodename=‘mac-local’}),一种是阿里云服务器(每个指标名称都存在{nodename=‘aliyun’}),同时每个指标下都存在一个 {instance=‘具体的服务器的ip地址’}标签。

即我们采集的时间序列大致上都有如下标签:
eg:
node_uname_info{nodename='xxxx',instance='yyyy'}

二、需求

我们想监控一下每台主机的cpu的使用情况。同时不能在一个图形上显示出所有的机器的cpu使用率,需要根据用户的选择而显示。即要出现下图所示的结果,这个情况我们可以通过grafana的模板变量来实现。

 从上图可以,instance的出现是根据 服务器类型 的选择而不同的。

三、实现步骤

1、创建一个Dashboard

2、设置模板变量

1、在Dashboard页面点击小齿轮⚙️图标。

2、点击创建一个变量

3、添加 nodename 变量,获取服务器的类型

4、添加 instance 变量

注意:
instance变量的值是根据上一步 nodename 变量的值而来的,及选择不同的 nodename,instance 的值应该是不同的。及在 instance 变量的查询语句中需要引用 nodename 变的值,通过 $nodename 即可引用。

完成上方出现的3,4两步,即可出现需求中出现的画面

3、创建一个图表,监控一下 cpu 的使用率

4、上方例子的完成json字符串

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
{
  "annotations": {
    "list": [
      {
        "builtIn": 1,
        "datasource": "-- Grafana --",
        "enable": true,
        "hide": true,
        "iconColor": "rgba(0, 211, 255, 1)",
        "name": "Annotations & Alerts",
        "type": "dashboard"
      }
    ]
  },
  "editable": true,
  "gnetId": null,
  "graphTooltip": 0,
  "id": 3,
  "iteration": 1616217012533,
  "links": [],
  "panels": [
    {
      "aliasColors": {},
      "bars": false,
      "dashLength": 10,
      "dashes": false,
      "datasource": null,
      "decimals": 2,
      "description": "",
      "fieldConfig": {
        "defaults": {
          "custom": {},
          "unit": "percent"
        },
        "overrides": []
      },
      "fill": 1,
      "fillGradient": 0,
      "gridPos": {
        "h": 8,
        "w": 24,
        "x": 0,
        "y": 0
      },
      "hiddenSeries": false,
      "id": 2,
      "legend": {
        "alignAsTable": true,
        "avg": true,
        "current": true,
        "max": true,
        "min": true,
        "rightSide": true,
        "show": true,
        "total": false,
        "values": true
      },
      "lines": true,
      "linewidth": 1,
      "nullPointMode": "null",
      "options": {
        "alertThreshold": true
      },
      "percentage": false,
      "pluginVersion": "7.4.3",
      "pointradius": 2,
      "points": true,
      "renderer": "flot",
      "seriesOverrides": [],
      "spaceLength": 10,
      "stack": false,
      "steppedLine": false,
      "targets": [
        {
          "expr": "(1 - avg(irate(node_cpu_seconds_total{mode='idle',nodename=~\"$nodename\",instance=~\"$instance\"}[5m])) by (instance)) * 100",
          "interval": "",
          "legendFormat": "{{instance}} CPU使用率",
          "refId": "A"
        }
      ],
      "thresholds": [],
      "timeFrom": null,
      "timeRegions": [],
      "timeShift": null,
      "title": "$instance CPU使用率",
      "tooltip": {
        "shared": true,
        "sort": 0,
        "value_type": "individual"
      },
      "type": "graph",
      "xaxis": {
        "buckets": null,
        "mode": "time",
        "name": null,
        "show": true,
        "values": []
      },
      "yaxes": [
        {
          "$$hashKey": "object:231",
          "format": "percent",
          "label": null,
          "logBase": 1,
          "max": null,
          "min": null,
          "show": true
        },
        {
          "$$hashKey": "object:232",
          "format": "short",
          "label": null,
          "logBase": 1,
          "max": null,
          "min": null,
          "show": true
        }
      ],
      "yaxis": {
        "align": false,
        "alignLevel": null
      }
    }
  ],
  "schemaVersion": 27,
  "style": "dark",
  "tags": [],
  "templating": {
    "list": [
      {
        "allValue": null,
        "current": {
          "selected": true,
          "tags": [],
          "text": [
            "mac-local"
          ],
          "value": [
            "mac-local"
          ]
        },
        "datasource": null,
        "definition": "label_values(node_uname_info{nodename=~\".*\"},nodename)",
        "description": null,
        "error": null,
        "hide": 0,
        "includeAll": false,
        "label": "服务器类型",
        "multi": true,
        "name": "nodename",
        "options": [
          {
            "selected": false,
            "text": "aliyun",
            "value": "aliyun"
          },
          {
            "selected": true,
            "text": "mac-local",
            "value": "mac-local"
          }
        ],
        "query": {
          "query": "label_values(node_uname_info{nodename=~\".*\"},nodename)",
          "refId": "StandardVariableQuery"
        },
        "refresh": 0,
        "regex": "",
        "skipUrlSync": false,
        "sort": 0,
        "tagValuesQuery": "",
        "tags": [],
        "tagsQuery": "",
        "type": "query",
        "useTags": false
      },
      {
        "allValue": null,
        "current": {
          "selected": true,
          "text": [
            "localhost:9081"
          ],
          "value": [
            "localhost:9081"
          ]
        },
        "datasource": null,
        "definition": "label_values(node_uname_info{nodename=~\"$nodename\",instance=~\".*\"},instance)",
        "description": null,
        "error": null,
        "hide": 0,
        "includeAll": false,
        "label": null,
        "multi": true,
        "name": "instance",
        "options": [
          {
            "selected": true,
            "text": "localhost:9081",
            "value": "localhost:9081"
          }
        ],
        "query": {
          "query": "label_values(node_uname_info{nodename=~\"$nodename\",instance=~\".*\"},instance)",
          "refId": "StandardVariableQuery"
        },
        "refresh": 0,
        "regex": "",
        "skipUrlSync": false,
        "sort": 0,
        "tagValuesQuery": "",
        "tags": [],
        "tagsQuery": "",
        "type": "query",
        "useTags": false
      }
    ]
  },
  "time": {
    "from": "now-5m",
    "to": "now"
  },
  "timepicker": {},
  "timezone": "",
  "title": "测试模板变量",
  "uid": "Umb3YDQMz",
  "version": 6
}

链接:https://blog.csdn.net/fu_huo_1993/article/details/115026228



感谢您的阅读,如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮。本文欢迎各位转载,但是转载文章之后必须在文章页面中给出作者和原文连接
posted @   南昌拌粉的成长  阅读(924)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示