• Prometheus metrics数据抓取解析


    Prometheus node的监控数据如链接展示,我们希望能更加方便的看到监控数据,shodan对Prometheus metrics 的数据做了格式化处理。172.96.3.215:9100/metricsicon-default.png?t=N7T8http://172.96.3.215:9100/metrics

     本文我自己实现了一个命令行工具,可以输出类shodan数据格式监控数据。以下是代码示例

    1. // ExtractMsg 提取信息
    2. /*
    3. 1.node_dmi_info
    4. 2.node_exporter_build_info
    5. 3.node_network_info
    6. 4.node_os_info
    7. 5.node_uname_info
    8. 按照顺序从前到后寻找
    9. */
    10. func ExtractMsg(resp string) {
    11. //(1)提取node_dmi_info信息的子串
    12. tmpindex := 0
    13. dmiResult, dmiEndindex := common(resp, "node_dmi_info{")
    14. tmpindex += dmiEndindex
    15. //(2)提取node_exporter_build_info信息的子串
    16. buildResult, buildEndindex := common(resp[tmpindex:], "node_exporter_build_info{")
    17. tmpindex += buildEndindex
    18. networkStartIndex := tmpindex
    19. //(3)提取node_os_info信息的子串
    20. osResult, osEndindex := common(resp[tmpindex:], "node_os_info{")
    21. tmpindex += osEndindex
    22. // 提取node_network_info信息的子串,特殊模块
    23. network(resp[networkStartIndex:], "node_network_info{")
    24. //(4)提取node_uname_info信息的子串
    25. unameResult, _ := common(resp[tmpindex:], "node_uname_info{")
    26. // 逐个序列化
    27. json.Unmarshal([]byte(dmiResult), &prometheus.NodeDmiInfo)
    28. json.Unmarshal([]byte(buildResult), &prometheus.NodeExporterBuildInfo)
    29. json.Unmarshal([]byte(osResult), &prometheus.NodeOsInfo)
    30. json.Unmarshal([]byte(unameResult), &prometheus.NodeUnameInfo)
    31. }
    32. // common 公共模块
    33. func common(resp, findstr string) (result string, endIndex int) {
    34. startIndex := strings.Index(resp, findstr)
    35. // 找不到的情况
    36. if startIndex == -1 {
    37. return "", 0
    38. }
    39. endIndex = strings.Index(resp[startIndex:], "} 1")
    40. endIndex = endIndex + startIndex + 1
    41. // 提取子串的内容
    42. result = strings.ReplaceAll(resp[startIndex+len(findstr)-1:endIndex], "=", ":")
    43. re := regexp.MustCompile(`(\w+):([^,]+)`)
    44. result = re.ReplaceAllString(result, `"$1":$2`)
    45. return
    46. }
    47. // network 单独的网络模块
    48. func network(resp, findstr string) {
    49. count := strings.Count(resp, findstr)
    50. prometheus.NodeNetworkInfo = make([]Response.NodeNetworkInfo, count)
    51. //找到第一个开始位置
    52. startIndex := strings.Index(resp, findstr)
    53. for i := 0; i < count; i++ {
    54. //找到结束位置
    55. endIndex := strings.Index(resp[startIndex:], "} 1")
    56. //算出结束位置
    57. endIndex = endIndex + startIndex + 1
    58. // 提取子串的内容
    59. result := strings.ReplaceAll(resp[startIndex+len(findstr)-1:endIndex], "=", ":")
    60. // 把多余的部分截掉,使其可以被反序列化为对象
    61. result = strings.TrimLeft(result, "nfo")
    62. // 正则并且加引号,使其称为JSON格式
    63. re := regexp.MustCompile(`(\w+):([^,]+)`)
    64. result = re.ReplaceAllString(result, `"$1":$2`)
    65. // 反序列化
    66. err := json.Unmarshal([]byte(result), &prometheus.NodeNetworkInfo[i])
    67. if err != nil {
    68. panic(err)
    69. }
    70. startIndex = endIndex
    71. }
    72. }

    效果如下:

     

     完整代码详见GitHub

    FrankZhang63/Promethues: Promethues metrics 类shodan数据格式 (github.com)

  • 相关阅读:
    指纹浏览器开发指南-EasyBR
    工作摸鱼秘籍
    电脑设备管理器在哪里可以找到
    React中的Hooks--useReducer()
    一文搞清各种来源的wmts服务加载,告别ctrl+c,v
    为什么应该在开发环境中使用 Kubernetes
    【算法|动态规划No.27】leetcode516. 最长回文子序列
    fire-voc 火光 烟火 火灾 目标检测数据集
    Docker 常用命令使用
    stm32单片机之串口通信例程
  • 原文地址:https://blog.csdn.net/qq_67503717/article/details/133912191