Hostname instead of IPs for panel legends in Grafana with Prometheus metrics

cn flag

I want to monitor different servers with Prometheus fed by exporters like node-exporter or postgres-exporter. The metrices should be presented in Grafana as dashboards and alerts.

The exporters will be imported by following targets in the prometheus.yml:

  scrape_interval:     15s

    monitor: 'codelab-monitor'

  - job_name: 'node-exporter'

    scrape_interval: 5s

      - targets: ['', '', '']

  - job_name: 'postgres-exporter'
      - targets : ['']

It is not possible to bind the servers by an user friendly URL/hostname.

I want to build up dashboard panels and alerts for these targets in Grafana, but with the goal that in the dahboards and alerts were not displayed the IPs of the hosts but their hostnames.

The dashboard will have two different sections:

1. Graphs for metrices of specific servers, selected by variable filter, e.g.

enter image description here

enter image description here

2. Graphs for metrices of all servers, with instance label, e.g.

enter image description here

For case 1 I've found a solution for displaying hostnames instead of the IPs in the dropdown filter. For that I created two variables:

Name Query Label Hide
hostname label_values(nodename) Host:
instance label_values(node_uname_info{nodename="$hostname"}, instance) Variable

The metric of the panel is filtered by the variable $instance:

100 - rate(node_cpu_seconds_total{mode="idle", instance="$instance"}[1m]) * 100

In the case 2, I'm not successful. I want to set the hostnames underneath the panel graphs. But in the Legend-field I only can access the instance (the IP/port combination) of the metric and not the hostname:

enter image description here

I'm not able to access the $hostname-variable in the Legend-field.

So how can I put the hostname underneath the panel graphs? Is there a Grafana internal way, or do I have to manipulate the prometheus.yml?


Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.