当前位置: 首页 > news >正文

Prometheus metrics数据抓取解析

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

http://www.lryc.cn/news/198300.html

相关文章:

  • 【算法训练-排序算法 三】【排序应用】合并区间
  • 【iOS】计算器仿写
  • 华为认证 | 华为HCIE认证该怎样备考?
  • 10月份stable diffusion animatediff等插件使用指南,又来更新了
  • 抓包工具charles修改请求和返回数据
  • matlab中绘制 维诺图(Voronoi Diagram)
  • Mybatis TypeHandler 介绍及使用
  • Linux SVN 命令详解
  • Maven依赖引入的优先机制
  • 全开源无加密跨境电商购物网站系统源码(无货源模式+多语言+多货币)
  • Python常用视频编辑操作——读取与保存视频、更改帧数、拼接视频、视频语音合并、视频与图像互转等
  • 从javascript到vue再到react的演变
  • 50个渗透(黑客)常用名词及解释
  • 开源游戏引擎和模拟器的项目合集 | 开源专题 No.38
  • ELK + Filebeat 分布式日志管理平台部署
  • Stable Diffusion原理
  • 2022年亚太杯APMCM数学建模大赛A题结晶器熔剂熔融结晶过程序列图像特征提取及建模分析求解全过程文档及程序
  • 金融网站如何做好安全防护措施?
  • 2023年中国恋爱社区未来发展趋势分析:多元化盈利模式实现可持续发展[图]
  • Elasticsearch:生成式人工智能带来的社会转变
  • 服务器数据恢复-RAID5中磁盘被踢导致阵列崩溃的服务器数据恢复案例
  • 负荷不均衡问题分析处理流程
  • Spring篇---第四篇
  • 算法通过村第十五关-超大规模|白银笔记|经典问题
  • Mini小主机All-in-one搭建教程6-安装苹果MacOS系统
  • Android中使用Glide加载圆形图像或给图片设置指定圆角
  • Nginx 代理
  • uniapp(uncloud) 使用生态开发接口详情4(wangeditor 富文本, 云对象, postman 网络请求)
  • Halcon 中查看算子和函数的执行时间
  • Python中的With ...as... 作用