当前位置:   article > 正文

【监控】用python3自研exporter收集数据,并用prometheus收集,在grafana使用变量可视化展示_prometheus python exporter gauge 当无法获取数据时 怎么修改值

prometheus python exporter gauge 当无法获取数据时 怎么修改值

 本站以分享各种运维经验和运维所需要的技能为主

《python零基础入门》:python零基础入门学习

《python运维脚本》: python运维脚本实践

shell》:shell学习

《terraform》持续更新中:terraform_Aws学习零基础入门到最佳实战

《k8》暂未更新

docker学习》暂未更新

《ceph学习》ceph日常问题解决分享

《日志收集》ELK+各种中间件

《运维日常》运维日常

《linux》运维面试100问

topic方向:python3写exporter收集数据作为 promethus的监控指标

具体问题:如何自定义制作exporter去收集数据作为 promethus的监控指标,之后在grafana制作成自己要的监控指标

步骤 1:  写exporter收集数据脚本,使用start_http_server开放监听端口,被prometheus采集

例如笔者这里需要把机器的磁盘指标如何收集看下面脚本:

主要睇:---其他代码是你如何收集数据

# 定义 Prometheus 指标

# 更新 Prometheus 指标

  1. #!/usr/bin/python3
  2. import socket
  3. import subprocess
  4. from prometheus_client import start_http_server, Gauge
  5. import time
  6. hostname = socket.gethostname()
  7. # 定义 Prometheus 指标
  8. wearout_indicator = Gauge('media_wearout_indicator', 'Media Wearout Indicator', ['device', 'hostname'])
  9. wear_leveling_count = Gauge('wear_leveling_count', 'Wear Leveling Count', ['device', 'hostname'])
  10. udma_crc_error_count = Gauge('udma_crc_error_count', 'UDMA CRC Error Count', ['device', 'hostname'])
  11. def get_disk_list():
  12. # 执行 lsblk 命令来获取磁盘列表
  13. cmd = "sudo lsblk | grep disk | grep -v '9.1T' | grep -v 'sda' | awk '{print $1}'"
  14. result = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
  15. disks = result.stdout.strip().split()
  16. return disks
  17. def get_smartctl_metrics(device):
  18. print(device)
  19. # 执行 smartctl 命令
  20. result = subprocess.run(['smartctl', '-a', '/dev/{}'.format(device)], stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
  21. output = result.stdout.splitlines()
  22. print(output)
  23. # 初始化指标值
  24. mwi = None
  25. wlc = None
  26. udma_crc = None
  27. # 解析输出
  28. for line in output:
  29. if 'Media_Wearout_Indicator' in line:
  30. mwi = line.split()[3] # 第四列
  31. if 'Wear_Leveling_Count' in line:
  32. wlc = line.split()[3] # 第四列
  33. if 'UDMA_CRC_Error_Count' in line:
  34. try:
  35. udma_crc = line.split()[9] # 第十列
  36. except IndexError:
  37. udma_crc = 'none'
  38. # 更新 Prometheus 指标
  39. if mwi is not None:
  40. wearout_indicator.labels(device=device, hostname=hostname).set(float(mwi))
  41. if wlc is not None:
  42. wear_leveling_count.labels(device=device, hostname=hostname).set(float(wlc))
  43. if udma_crc is not None:
  44. udma_crc_error_count.labels(device=device, hostname=hostname).set(udma_crc if udma_crc != 'none' else 0)
  45. def collect_metrics():
  46. disks = get_disk_list()
  47. for device in disks:
  48. print(device)
  49. get_smartctl_metrics(device)
  50. if __name__ == '__main__':
  51. # 启动 Prometheus metrics 服务器
  52. start_http_server(8008)
  53. while True:
  54. collect_metrics()
  55. time.sleep(10) # 每分钟更新一次数据

步骤 2: 配置 Prometheus

确保 Prometheus 配置文件 (prometheus.yml) 包含对你的自定义 Exporter 的抓取配置。

  1. # prometheus.yml
  2. scrape_configs:
  3. - job_name: 'smartctl_exporter'
  4. scrape_interval: 60s
  5. static_configs:
  6. - targets: ['localhost:8000']

步骤 3: 在 Grafana 中设置监控

  1. 添加 Prometheus 数据源

    • 进入 Grafana 的设置,添加一个新的数据源,选择 Prometheus。
    • 输入 Prometheus 服务器的 URL,并保存。
  2. 创建 Dashboard

    • 创建一个新的 Dashboard 并添加一个 Panel。
    • 在 Panel 的设置中,选择你的 Prometheus 数据源。
    • 使用 PromQL 查询你的数据,例如:
      • media_wearout_indicator{device="sda"}
      • wear_leveling_count{device="sda"}
      • udma_crc_error_count{device="sda"}
    • 根据你的需求调整图表的其它设置。

变量学习

也可以添加变量的方式更好可视化查询:

在 Grafana 中使用变量可以极大地提升仪表板的灵活性和交互性,特别是当你需要根据不同的条件(如设备、时间范围等)查看数据时。以下是如何在 Grafana 中创建和使用设备变量(`device`)的步骤,以便你可以轻松切换并查看不同设备的指标数据:

### 创建变量

1. **打开仪表板设置**:
   - 打开你的仪表板,然后点击右上角的齿轮图标进入“Dashboard settings”。

2. **进入变量配置**:
   - 在设置菜单中,选择“Variables”选项。

3. **添加新变量**:
   - 点击“Add variable”按钮开始创建新变量。

4. **配置变量属性**:
   - **Name**: 给变量一个名称,如 `device`。
   - **Type**: 选择“Query”。
   - **Label**: 设置变量的显示标签,如 `Select Device`。
   - **Data source**: 选择你的 Prometheus 数据源。
   - **Query**: 输入用于获取设备列表的 PromQL 查询。例如,如果你的指标中包含设备标签,可以使用类似以下的查询:
     ```plaintext
     label_values(media_wearout_indicator, device)
     ```
     这个查询会从 `media_wearout_indicator` 指标中提取所有不同的 `device` 标签值。

5. **其他设置**:
   - **Sort**: 根据需要选择排序方式,通常选择“Alphabetical”以便查找。
   - **Selection Options**: 可以设置是否允许多选、包含“全部”选项等。

6. **保存并测试**:
   - 保存变量设置并返回仪表板,检查变量是否按预期工作。

### 使用变量

在创建了变量后,你可以在任何查询中使用这个变量。例如,如果你创建了名为 `device` 的变量,可以在 PromQL 查询中这样使用它:

```plaintext
media_wearout_indicator{device="$device"}
wear_leveling_count{device="$device"}
udma_crc_error_count{device="$device"}
```

在这些查询中,`$device` 会被替换为用户从下拉菜单选择的具体设备值。

### 可视化和查看

- 当你在查询中使用了变量后,用户可以通过仪表板上的下拉菜单选择不同的设备,查询和面板视图会根据所选设备自动更新。
- 这使得用户能够轻松比较不同设备的性能或查看特定设备的详细信息。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小舞很执着/article/detail/996566
推荐阅读
相关标签
  

闽ICP备14008679号