Prometheus 标签使用示例整合

Prometheus 监控实例


一、Prometheus 根据标签聚合总CPU使用率

1、主机添加标签(可在多个主机内添加相同标签实现聚合):vim prometheus.conf

static_configs:
- targets: ['localhost:9090']
  # 添加标签选项
  labels:
  # 标签key:标签value 
  idc: bj

2、检查配置文件

./promtool check config prometheus.yml

3、配置文件重新生效

kill -hup PID

4、监控平台:使用promSQL查询指定标签内主机的所有CPU总和

sum(process_cpu_seconds_total{idc="bj"})

二、Prometheus 重命名标签 根据标签聚合总CPU使用率

1、修改配置文件:vim prometheus.conf

scrape_configs:
  # 作业改为bj
  - job_name: 'bj'
    static_configs:
    - targets: ['localhost:9090']
# 添加重命名标签
    relabel_configs:
# 基于正则表达式匹配操作
    - action: replace
  # 指定源标签 
      source_labels: ['job']
  # 写入正则,捕获值
      regex: (.*)
  # 替换正则表达式匹配到的分组,分组引用 $1
      replacement: $1
  # 重新标记标签 为 idc
      target_label: idc

2、检查配置文件

./promtool check config prometheus.yml

3、配置文件重新生效

kill -hup PID

4、使用promSQL查询指定标签内主机的所有CPU总和

sum(process_cpu_seconds_total{job="bj"})

三、Prometheus 根据标签过滤目标

1、指定标签下的主机停止数据采集

scrape_configs:
  - job_name: 'bj'
    static_configs:
    - targets: ['localhost:9090']
    relabel_configs:
    # 启动drop标签过滤,被指定到的标签停止数据采集
    - action: drop
      # 指定 job 标签
      source_labels: ['job']

2、指定标签下的主机保留数据采集

scrape_configs:
  - job_name: 'bj'
    static_configs:
    - targets: ['localhost:9090']
    relabel_configs:
    # 启动keep标签过滤,被指定到的标签保留数据采集
    - action: keep
      # 指定 job 标签
      source_labels: ['job']

四、Prometheus 删除标签

1、删除标签动作

scrape_configs:
  - job_name: 'bj'
    static_configs:
    - targets: ['localhost:9090']
    relabel_configs:
    # 删除指定标签
    - action: labeldrop
      # 指定 job 标签
      regex: job

 

posted @ 2019-08-02 15:57  kevin.Xiang  阅读(12576)  评论(0编辑  收藏  举报