数据抓取解析"/>
Prometheus metrics数据抓取解析
Prometheus node的监控数据如链接展示,我们希望能更加方便的看到监控数据,shodan对Prometheus metrics 的数据做了格式化处理。172.96.3.215:9100/metricshttp://172.96.3.215:9100/metrics
本文我自己实现了一个命令行工具,可以输出类shodan数据格式监控数据。以下是代码示例
// ExtractMsg 提取信息
/*1.node_dmi_info2.node_exporter_build_info3.node_network_info4.node_os_info5.node_uname_info按照顺序从前到后寻找
*/
func ExtractMsg(resp string) {//(1)提取node_dmi_info信息的子串tmpindex := 0dmiResult, dmiEndindex := common(resp, "node_dmi_info{")tmpindex += dmiEndindex//(2)提取node_exporter_build_info信息的子串buildResult, buildEndindex := common(resp[tmpindex:], "node_exporter_build_info{")tmpindex += buildEndindexnetworkStartIndex := tmpindex//(3)提取node_os_info信息的子串osResult, osEndindex := common(resp[tmpindex:], "node_os_info{")tmpindex += osEndindex// 提取node_network_info信息的子串,特殊模块network(resp[networkStartIndex:], "node_network_info{")//(4)提取node_uname_info信息的子串unameResult, _ := common(resp[tmpindex:], "node_uname_info{")// 逐个序列化json.Unmarshal([]byte(dmiResult), &prometheus.NodeDmiInfo)json.Unmarshal([]byte(buildResult), &prometheus.NodeExporterBuildInfo)json.Unmarshal([]byte(osResult), &prometheus.NodeOsInfo)json.Unmarshal([]byte(unameResult), &prometheus.NodeUnameInfo)
}// common 公共模块
func common(resp, findstr string) (result string, endIndex int) {startIndex := strings.Index(resp, findstr)// 找不到的情况if startIndex == -1 {return "", 0}endIndex = strings.Index(resp[startIndex:], "} 1")endIndex = endIndex + startIndex + 1// 提取子串的内容result = strings.ReplaceAll(resp[startIndex+len(findstr)-1:endIndex], "=", ":")re := regexp.MustCompile(`(\w+):([^,]+)`)result = re.ReplaceAllString(result, `"$1":$2`)return
}// network 单独的网络模块
func network(resp, findstr string) {count := strings.Count(resp, findstr)prometheus.NodeNetworkInfo = make([]Response.NodeNetworkInfo, count)//找到第一个开始位置startIndex := strings.Index(resp, findstr)for i := 0; i < count; i++ {//找到结束位置endIndex := strings.Index(resp[startIndex:], "} 1")//算出结束位置endIndex = endIndex + startIndex + 1// 提取子串的内容result := strings.ReplaceAll(resp[startIndex+len(findstr)-1:endIndex], "=", ":")// 把多余的部分截掉,使其可以被反序列化为对象result = strings.TrimLeft(result, "nfo")// 正则并且加引号,使其称为JSON格式re := regexp.MustCompile(`(\w+):([^,]+)`)result = re.ReplaceAllString(result, `"$1":$2`)// 反序列化err := json.Unmarshal([]byte(result), &prometheus.NodeNetworkInfo[i])if err != nil {panic(err)}startIndex = endIndex}
}
效果如下:
完整代码详见GitHub
FrankZhang63/Promethues: Promethues metrics 类shodan数据格式 (github)
更多推荐
Prometheus metrics数据抓取解析
发布评论