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