Help with custom variable


I have two network instances which consists of different virtual machines, example:

up{instance="home-lab-1-vm-0:8001", job="node"}
up{instance="home-lab-1-vm-1:8001", job="node"}
up{instance="home-lab-1-vm-2:8001", job="node"}

up{instance="home-lab-2-vm-0:8001", job="node"}
up{instance="home-lab-2-vm-1:8001", job="node"}
up{instance="home-lab-2-vm-2:8001", job="node"}

I want to create a custom variable with names
home-lab-1 and home-lab-2

How can I use such variable in the query?

It would be nice to know, what you’re trying to achieve.

I guess, you try to group your VMs by home-lab-1 and home-lab-2 - kind of like grouping them by facility or location.

I’d start by refactoring your job config and set a custom label for each target-vm. In your example, you got 3 VMs each, so we can group them as list of targets - setting our custom label for each list.


  - job_name: "vm"


      # Home Lab 1
      - targets:
        - "home-lab-1-vm-0:8001"
        - "home-lab-1-vm-1:8001" 
        - "home-lab-1-vm-0:8001"
          facility: "home-lab-1"
      # Home Lab 2
      - targets:
        - "home-lab-2-vm-0:8001"
        - "home-lab-2-vm-1:8001"
        - "home-lab-2-vm-2:8001"
          facility: "home-lab-2"

Then you can define your variable like that:

… and use it in queries like that:

up{job="vm", facility="$facility"}

1 Like