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:
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