其他教程

其他教程

Products

当前位置:首页 > 其他教程 >

正则表达式:Grafana变量将连字符分隔的IP转换为点分隔的IP

GG网络技术分享 2025-03-18 16:15 6


问题描述:

我正在尝试使用自定义查询在Grafana中创建变量来扫描EKS群集中的节点。 当前,节点变量存储为“连字符”分隔的字符串(例如10-109-22-102); 我打算将其报告为“点”分隔的字符串(例如10.109.22.102)。 正则表达式是否有可能?

Grafana变量支持通过抓取给定指标然后使用正则表达式提取报告的指标中的值来创建活动变量。 我目前正在抓取一个指标:

kube_node_info{container_runtime_version=\"docker://18.6.1\",endpoint=\"http\",instance=\"10.11.111.111:8080\",job=\"kube-state-metrics\",kernel_version=\"xxxxx\",kubelet_version=\"xxxx\",kubeproxy_version=\"xxxx\",namespace=\"infra\",node=\"ip-10-11-111-111.us-sdlls-as.compute.internal\",os_image=\"Amazon

Linux

2\",pod=\"prometheus-operator-kube-state-metrics-sdfgsjdkgl-saldjl\",service=\"prometheus-operator-kube-state-metrics\"}

我目前正在使用正则表达式: /.*node=\"ip-([^\"]*).us-*/ 来提取“连字符”分隔的IP。 我也尝试过使用其他比赛组,例如:([\\d]{2,3})-([\\d]{2,3})-([\\d]{2,3})-([\\d]{2,3})然而,这只返回第一匹配组在grafana(在这种情况下)10) 作为唯一的选择。

预期输出应为:

10.11.111.111

网友观点:

The actual intent was to capture the IP of the instance and list all of them as a dynamic variable via a query and then enable all the node metrics via the IPs. This was a bad approach since lot of prometheus node-metrics are exported with label node=ip-XX-XX-XXX-XXX-<region>-.compute.internal (where XX are digits). Also grafana variables reports match of only the 1st sub-group - ideally it should be all the subgroups.

However, even if i were to capture the subgroup say XX.XX.XXX.XXX this would mean for every metric that I need to capture and graph I would need to address for matches for all the IPs in the EKS nodes - this is not possible especially if we want to enable \\\"All\\\" variable in which grafana replaces the variable call with regex of all the variables matches. Ex. if my k8s node had following IPs:

* 10.10.0.1

* 10.10.0.2

* 10.10.0.3

and I call this variable as Node

Grafana will list following variables:

* All

* 10.10.0.1

* 10.10.0.2

* 10.10.0.3

On selecting All grafana will do replace of $Node to ./*(10.10.0.1|10.10.0.2|10.10.0.3)*/ However if i had a Query for getting the node info : kube_pod_info{node=~\\\"$Node\\\"} -> this would fail because node name is in a different format we may try and solve it by using label_replace but again we cannot address using $Node in case All variable is used.

Conclusion : use the variable $Node (i.e.

* ip-10-10-0-1.us-sdlls-as.compute.internal

* ip-10-10-0-2.us-sdlls-as.compute.internal

* ip-10-10-0-3.us-sdlls-as.compute.internal

) - as-is this would make all other panels much more convenient and easy to configure.

It seems to me the one you have designed should be working fine:

Test

package main

import (

\\\"regexp\\\"

\\\"fmt\\\"

)

func main() {

var re = regexp.MustCompile(`(?m)([0-9]{2,3})-([0-9]{2,3})-([0-9]{2,3})-([0-9]{2,3})`)

var str = `kube_node_info{container_runtime_version=\\\"docker://18.6.1\\\",endpoint=\\\"http\\\",instance=\\\"10.11.111.111:8080\\\",job=\\\"kube-state-metrics\\\",kernel_version=\\\"xxxxx\\\",kubelet_version=\\\"xxxx\\\",kubeproxy_version=\\\"xxxx\\\",namespace=\\\"infra\\\",node=\\\"ip-10-11-111-111.us-sdlls-as.compute.internal\\\",os_image=\\\"Amazon Linux 2\\\",pod=\\\"prometheus-operator-kube-state-metrics-sdfgsjdkgl-saldjl\\\",service=\\\"prometheus-operator-kube-state-metrics\\\"

kube_node_info{container_runtime_version=\\\"docker://18.6.1\\\",endpoint=\\\"http\\\",instance=\\\"10.11.111.111:8080\\\",job=\\\"kube-state-metrics\\\",kernel_version=\\\"xxxxx\\\",kubelet_version=\\\"xxxx\\\",kubeproxy_version=\\\"xxxx\\\",namespace=\\\"infra\\\",node=\\\"ip-10-11-111-111.us-sdlls-as.compute.internal\\\",os_image=\\\"Amazon Linux 2\\\",pod=\\\"prometheus-operator-kube-state-metrics-sdfgsjdkgl-saldjl\\\",service=\\\"prometheus-operator-kube-state-metrics\\\"

`

var substitution = \\\"$1.$2.$3.$4\\\"

fmt.Println(re.ReplaceAllString(str, substitution))

}

The expression is explained on the top right panel of regex101.com, if you wish to explore/simplify/modify it, and in this link, you can watch how it would match against some sample inputs, if you like.

Your regex must work fine. However, the character sets are redundant since you are only using digits per set. Hence, they can be removed:

(\\d{2,3})-(\\d{2,3})-(\\d{2,3})-(\\d{2,3})

The captured data can then be substituted as:

$1.$2.$3.$4

Demo

python自测100题

如果你在寻找python工作,那你的面试可能会涉及Python相关的问题。

通过对网络资料的收集整理,本文列出了100道python的面试题以及答案,你可以根据需求阅读测试。

python基础

Q1.什么是Python?

Python是一种面向对象的,交互式的,解释型的计算机程序设计语言。Python的设计具有高可读性,它使用英语关键词而非标点符号,语法结构也比其他语言简单。


Q2.Python的主要功能是什么?

1)python是一种解释型语言,因此在使用python时不需要进行编译

2)声明变量和类似变量时,不需要重复声明变量的类型;

3)Python非常适合面向对象的编程,因为它允许类的定义以及组合和继承;

4)函数是第一类对象,这意味着它们可以分配给变量,从其他函数返回并传递给函数,类也是第一类对象;

5)用于许多领域,包括Web应用程序,自动化,科学建模,大数据应用程序等等。

Q3.Python中支持的数据类型有哪些?

Python支持5种数据类型:

1)Numbers(数字)——用于保存数值;

标签:

提交需求或反馈

Demand feedback