The background,

Suppose I have two types of servers, one is a local computer ({nodename=’mac-local’} for each indicator name) and one is an Ari cloud server ({nodename=’aliyun’} for each indicator name). There is also a {instance=’ specific server IP address ‘} label under each indicator.

Eg: node_uname_info{nodename=’ XXXX ‘,instance=’ YYYY ‘}

Second, the demand for

We want to monitor the CPU usage of each host. At the same time, the CPU usage of all machines cannot be displayed in one graph, but must be displayed according to the user’s choice. That is, to produce the result shown below, we can do this through the grafana template variable.

As you can see from the figure above, the occurrence of instance varies depending on the type of server selected.

Three, implementation steps

Create a Dashboard

2. Set template variables

1. Click the pinion ⚙️ icon on the Dashboard page.

2. Click Create variable

1. Add the nodename variable to get the server type

2. Add the instance variable.

Note: The value of instance is derived from the value of the nodename variable in the previous step, and the value of instance should be different for different nodenames. $nodename = $nodename = $nodename = $nodename

Finish the 1,2 steps above, you can appear in the demand of the screen.

3. Create a chart to monitor CPU usage

Complete json string for the example above

By introducing this JSON string into grafana, you can achieve the effect shown above. But make sure that each time series has nodename and instance tags.

{
  "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 usage."refId": "A"}]."thresholds": []."timeFrom": null."timeRegions": []."timeShift": null."title": $instance CPU usage."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": "Server Type"."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": "Test template variables"."uid": "Umb3YDQMz"."version": 6
}
Copy the code

5. Reference links

Use of grafana template variables