Combine two prometheus query

3,112 views
Skip to first unread message

s.saurab...@gmail.com

unread,
Mar 15, 2021, 7:38:41 AM3/15/21
to Prometheus Developers

Hello,

I have a certain requirement to implement in Grafana where we need to show interface utilization for switch.

I got combination of two queries in order to achieve it

1) ifOutOctets{hostname="XX",ifIndex="103",instance="XX",job="SNMP"}

2) ifName{hostname="XX",ifIndex="103",ifName="Te1/0/1",instance="XX",job="SNMP"}

First query -- It gives interface utilization on specific index

Second query --- It gives interface name mapped to specific index

Now How can I combine both these query in a way that I get interface utilization on interface name not on index.

Pls advice.

Thanks,
Saurabh
Message has been deleted

Harry Perks

unread,
Mar 15, 2021, 8:30:29 AM3/15/21
to Prometheus Developers
Does ifOutOctets have a label ifName? If not, you'll need to perform a vector match operation - it looks like ifIndex can be used:

sum by(ifName) (ifOutOctets * on(ifIndex) group_left(ifName) ifName)

Harry Perks

unread,
Mar 15, 2021, 8:31:26 AM3/15/21
to Prometheus Developers
Note - the Prometheus users group is a better place for this type of question. https://groups.google.com/forum/#!forum/prometheus-users

Saurabh Jain

unread,
Mar 15, 2021, 12:17:06 PM3/15/21
to Harry Perks, Prometheus Developers
Thanks Harry for the update.

ifOutOctets metric  dont have the label ifName.

However I am getting below response for the same.

image.png
pls advice.

--
You received this message because you are subscribed to a topic in the Google Groups "Prometheus Developers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/prometheus-developers/PyK8s75e1vE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to prometheus-devel...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/prometheus-developers/db5825ef-58ba-44f2-b049-ec3050024f9cn%40googlegroups.com.

Robert Collier

unread,
Mar 15, 2021, 2:48:57 PM3/15/21
to Saurabh Jain, Harry Perks, Prometheus Developers
Something like this maybe?:

ifOutOctets + on(ifIndex,hostname,instance,job) group_right (ifName * 0)

You received this message because you are subscribed to the Google Groups "Prometheus Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to prometheus-devel...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/prometheus-developers/CA%2BTEA9DHhD2%3DKx7%3DnTTzfzepcZQ%2BfXEC06QXywazKYK2oZnzaQ%40mail.gmail.com.


--
Robert Collier
r...@ossifrage.net

Saurabh Jain

unread,
Mar 15, 2021, 11:53:06 PM3/15/21
to Robert Collier, Harry Perks, Prometheus Developers
Not working  . Pls chk below screenshot.

image.png

Robert Collier

unread,
Mar 16, 2021, 4:39:57 AM3/16/21
to Saurabh Jain, Harry Perks, Prometheus Developers
Ah, reverse it to:

(ifName * 0) + on(ifIndex,hostname,instance,job) group_left ifOutOctets

Screen Shot 2021-03-16 at 8.32.10 AM.png

However, if you are using the regular prometheus snmp_expporter, you should be aware that configured correctly it will do this step for you, eg, copying directly from my snmp exporters metrics:

# HELP ifOutOctets The total number of octets transmitted out of the interface, including framing characters - 1.3.6.1.2.1.2.2.1.16
# TYPE ifOutOctets counter
ifOutOctets{ifAlias="",ifDescr="apcli0",ifIndex="16",ifName="apcli0"} 0
ifOutOctets{ifAlias="",ifDescr="apclii0",ifIndex="28",ifName="apclii0"} 0
ifOutOctets{ifAlias="",ifDescr="br0",ifIndex="29",ifName="br0"} 2.7732281e+07
ifOutOctets{ifAlias="",ifDescr="eth0",ifIndex="2",ifName="eth0"} 2.76292118e+08
ifOutOctets{ifAlias="",ifDescr="lo",ifIndex="1",ifName="lo"} 1.930652e+06
ifOutOctets{ifAlias="",ifDescr="ra0",ifIndex="4",ifName="ra0"} 5.37888286e+08
ifOutOctets{ifAlias="",ifDescr="ra1",ifIndex="5",ifName="ra1"} 0
You may have to regenerate the configuration for it to do that though: https://github.com/prometheus/snmp_exporter/blob/main/README.md#generating-configuration

It's been a couple of years since I did the setup now and I don't remember the details.
 
 - Regards, Rob.



--
Robert Collier
r...@ossifrage.net

Saurabh Jain

unread,
Mar 16, 2021, 5:55:25 AM3/16/21
to Robert Collier, Harry Perks, Prometheus Developers
It is still not working.


image.png

We used the latest configuration of snmp exporter and generator and below is the snapshot of the snmp.yml file which was generated by generator.

image.png
Pls suggest if we have to tweak the snmp file to achieve the desired result.

Robert Collier

unread,
Mar 16, 2021, 10:00:22 AM3/16/21
to Saurabh Jain
[Moving the list to bcc]

You need to reverse the order - the bracketed expression first:

(ifName * 0) + on(ifIndex,hostname,instance,job) group_left ifOutOctets

I'll follow up separately re the snmp config.




--
Robert Collier
r...@ossifrage.net

Harkishen Singh

unread,
Mar 16, 2021, 1:41:53 PM3/16/21
to Prometheus Developers
Sounds to me like you want to use a result from one query as a label value to another query's label key. 
It seems pretty easy if you use SQL (by using Promscale) for this, like

select * from prom_metric.go_goroutines where labels ? ('ifIndex' == (select value from prom_metric.ifName limit 1)::text);

But not sure how to do it in PromQL.

(My previous reply from mail didn't come here, maybe due to some reply option selection, so repasting the answer. Sorry for that.)

Saurabh Jain

unread,
Mar 17, 2021, 3:00:03 AM3/17/21
to Harkishen Singh, Robert Collier, Prometheus Developers
Thanks Harkishen for your input.

Issue is resolved and I am able to get data in the query.

Thanks a lot @Robert Collier  for your support.


Reply all
Reply to author
Forward
0 new messages