本站以分享各种运维经验和运维所需要的技能为主
《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 指标
#!/usr/bin/python3
import socket
import subprocess
from prometheus_client import start_http_server, Gauge
import time
hostname = socket.gethostname()
# 定义 Prometheus 指标
wearout_indicator = Gauge('media_wearout_indicator', 'Media Wearout Indicator', ['device', 'hostname'])
wear_leveling_count = Gauge('wear_leveling_count', 'Wear Leveling Count', ['device', 'hostname'])
udma_crc_error_count = Gauge('udma_crc_error_count', 'UDMA CRC Error Count', ['device', 'hostname'])
def get_disk_list():
# 执行 lsblk 命令来获取磁盘列表
cmd = "sudo lsblk | grep disk | grep -v '9.1T' | grep -v 'sda' | awk '{print $1}'"
result = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
disks = result.stdout.strip().split()
return disks
def get_smartctl_metrics(device):
print(device)
# 执行 smartctl 命令
result = subprocess.run(['smartctl', '-a', '/dev/{}'.format(device)], stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
output = result.stdout.splitlines()
print(output)
# 初始化指标值
mwi = None
wlc = None
udma_crc = None
# 解析输出
for line in output:
if 'Media_Wearout_Indicator' in line:
mwi = line.split()[3] # 第四列
if 'Wear_Leveling_Count' in line:
wlc = line.split()[3] # 第四列
if 'UDMA_CRC_Error_Count' in line:
try:
udma_crc = line.split()[9] # 第十列
except IndexError:
udma_crc = 'none'
# 更新 Prometheus 指标
if mwi is not None:
wearout_indicator.labels(device=device, hostname=hostname).set(float(mwi))
if wlc is not None:
wear_leveling_count.labels(device=device, hostname=hostname).set(float(wlc))
if udma_crc is not None:
udma_crc_error_count.labels(device=device, hostname=hostname).set(udma_crc if udma_crc != 'none' else 0)
def collect_metrics():
disks = get_disk_list()
for device in disks:
print(device)
get_smartctl_metrics(device)
if __name__ == '__main__':
# 启动 Prometheus metrics 服务器
start_http_server(8008)
while True:
collect_metrics()
time.sleep(10) # 每分钟更新一次数据
步骤 2: 配置 Prometheus
确保 Prometheus 配置文件 (prometheus.yml
) 包含对你的自定义 Exporter 的抓取配置。
# prometheus.yml
scrape_configs:
- job_name: 'smartctl_exporter'
scrape_interval: 60s
static_configs:
- targets: ['localhost:8000']
步骤 3: 在 Grafana 中设置监控
-
添加 Prometheus 数据源:
- 进入 Grafana 的设置,添加一个新的数据源,选择 Prometheus。
- 输入 Prometheus 服务器的 URL,并保存。
-
创建 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` 会被替换为用户从下拉菜单选择的具体设备值。
### 可视化和查看
- 当你在查询中使用了变量后,用户可以通过仪表板上的下拉菜单选择不同的设备,查询和面板视图会根据所选设备自动更新。
- 这使得用户能够轻松比较不同设备的性能或查看特定设备的详细信息。