dongyi7513
2019-07-22 01:33
采纳率: 100%
浏览 3.3k
已采纳

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

我正在尝试使用自定义查询在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
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dt2015 2019-07-23 06:50
    已采纳

    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.

    点赞 评论
  • doucou1892 2019-07-22 03:25

    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.

    点赞 评论
  • dqfkd82886 2019-07-22 04:12

    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

    点赞 评论

相关推荐 更多相似问题