配置 prometheus
先去這個網站下載 對應版本 的 prometheus prometheus 下載
然后
tar xvfz prometheus-*.tar.gz
cd prometheus-*
啟動之前 先配置 vim prometheus.yml
prometheus.yml
# my global config
global:
scrape_interval: 15s # 將抓取間隔設置為每 15 秒。默認為每 1 分鍾。
evaluation_interval: #15s 每 15 秒評估一次規則。默認值為每 1 分鍾。
# 抓取超時 設置為全局默認值(10 秒)。
# 警報管理器配置
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# 加載規則一次並根據全局“evaluation_interval”定期評估它們。
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# 只包含一個要抓取的端點的抓取配置:
# 這里是普羅米修斯本身。
scrape_configs:
# 作業名稱作為標簽 `job=<job_name>` 添加到從此配置中抓取的任何時間序列中。
- job_name: "prometheus"
# 指標路徑 默認 為 '/metrics'
# scheme defaults to 'http'. 方案默認為“http”。
static_configs:
- targets: [ "localhost:9090" ] # 這個監聽的就是 prometheus 服務本身的指標 輸入 http:://localhost:9090/metrics 就能看到
- job_name: 'node'
static_configs:
# 這個監聽的就是 localhost 本身的 硬件指標(cpu 內存 硬盤 等信息 ,需要在 本機上面 安裝 node_exporter 軟件(具體百度)
- targets: [ 'localhost:9554' ]
labels:
group: 'x99-exporter'
- targets: [ 'localhost:8554' ] # 這個是 要自己寫個go 程序 定義 port=8554 的服務 ,下面 會講到
labels:
group: 'x99-metrics-client'
具體參數 可以 看官網 的文檔 配置文檔
然后執行 ./prometheus --config.file prometheus.yml
啟動 prometheus 服務
這里最好 開 tmux 或者干脆 用 sh 腳本 做成個服務 ,不然占 你一個 命令行 窗口 ,不 ctrl+c 退出 就 干不了其他的事情了 ……
訪問 http:😕/localhost:9090 (我這里是 http:😕/172.168.10.99:9090 我在172.168.10.99上安裝 的prometheus 服務 )
可以 點擊 這個 地球 查看 已經收集的指標
然后點擊這個graph 就能看到 對應指標的 數據 變化曲線了
http:😕/localhost:9090/metrics 就是 prometheus 這個服務自身提供的指標
編寫 go client 指標上報客戶端
上面 的 http:😕/localhost:9090/metrics 是自帶的指標,我們要是想自定義 指標上報 ,就 得自己寫個 client ,然后給 prometheus server 上報就可以 了
寫個 指標上報的 go 程序 注意prometheus pull metrics 指標的時候 有兩種方式 一種是 下面代碼的 定時 調用數據 然后 prometheus 收集,還有一種是 prometheus 每隔n 秒call metrics-client ,然后 metrics-client 調用命令 采集指標 這個在具體文檔里面有 prometheus-go 代碼示例
main.go
package main
import (
"fmt"
"github.com/fatih/color"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"net/http"
"os/exec"
"strconv"
"strings"
"time"
)
func main() {
// metrics 有四種類型 [metrics 類型](https://prometheus.io/docs/concepts/metric_types/)
// Counter Gauge Histogram Summary 我這里用的是 gauge 類型
var gaugeVec = promauto.NewGaugeVec( // 定義一個自動注冊的服務(這樣不用手動寫 prometheus.Register())
prometheus.GaugeOpts{
Namespace: "命名空間(英語)",
Name: "metrics1 ",
Help: "隨便寫寫幫助",
}, []string{ // 定義 label
"ip",
"n_start",
"type",
},
) // 這樣定義出來的格式就是 命名空間(英語)_metrics1{ip="xxx",n_start="xxx",type="xxx"} 0(float64類型) 這樣的
//每2秒,就 給 prometheus 傳 指標數據
go func() {
for {
// WithLabelValues 確定 label的值 ,set 確定這條數據 的值(這里寫固定了,實際業務是要把 value 填進去的)
gaugeVec.WithLabelValues("localhost", "02", "duduType").Set(233)
time.Sleep(2 * time.Second)
}
}()
http.Handle("/metrics", promhttp.Handler()) // 訪問 /metrics 觸發
sprint := fmt.Sprintf("程序啟動,正在堅挺 8554 端口 ")
color.Blue(sprint)
err := http.ListenAndServe(":8554", nil)
if err != nil {
sprint1 := fmt.Sprintf("程序啟動失敗, %+v", err)
color.Blue(sprint1)
}
}
執行 go run main.go 就把 指標客戶 端 跑起來了
然后 在 prometheus 后台 搜索 就會有對應的數據
配置 grafana
grafana 是 用來 顯示圖表的 這里為 prometheus 數據 顯示圖標
下載安裝 grafana
使用
默認情況下,Grafana 將監聽 http://localhost:3000。默認登錄名是“admin”/“admin”。
創建 Prometheus 數據源
點擊 dashboard -> home -> 創建 dashboard
可以看到 對應指標的 圖表 創建成功了
配置報警
安裝 下載 alertManager
在github 上面 下載對應版本的 alertManager 下載地址
解壓
tar -zxvf alertmanager-0.23.0.linux-amd64.tar.g
進入目錄
cd alertmanager-0.23.0.linux-amd64
編輯 alertmanager.yml
# alertmanager 報警 配置
global:
resolve_timeout: 5m
smtp_smarthost: 'smtp.mxhichina.com:465' # 阿里郵箱
smtp_from: 'ifnk@aliyouxiang.com' # 賬戶 名
smtp_auth_username: 'ifnk@aliyouxiang.com' # 賬戶 名
smtp_auth_password: 'dudu-dudu-dudu' # 密碼
#smtp_auth_secret: false
smtp_require_tls: false
smtp_hello: 'qq.com'
route:
group_by: ['alertname']
group_wait: 1s
group_interval: 5s
repeat_interval: 60s
receiver: 'mail'
receivers:
- name: 'mail'
email_configs:
- to: 'ifnk@aliyouxiang.com@hlytec.com' # 要發送的郵箱地址 ,多個地址 用 逗號 隔開
運行
./alertmanager --config.file=alertmanager.yml
訪問 http://172.168.10.99(你機器自己的ip):9093 即可看到 alertmanager 后台管理 頁面
為Prometheus配置Alertmanager
在 prometheus.yml
配置 alertmanager
監聽 alertmanager 指標
打開網址 http://localhost:9090/status
看看有沒有成功
添加警報規則
現在Alertmanager已經配置完成,讓我們添加第一條警報規則。
與記錄規則一樣,警報規則在Prometheus服務器配置中加載的規則文件內也使用YAML語句定義。
alert-rules.yml
# 報警規則
groups:
- name: generals.rules
rules:
- alert: "程序可能崩了"
expr: (errCoreCount - errCoreCount offset 5s) > 0 # errCoreCount 當前數值減去 5秒前數值 大於0
for: 1s # errCoreCount 當前數值減去 5秒前數值 大於0 的時間超過 1秒 就 給 alertmanager server 發送報警
labels:
severity: error
annotations:
summary: " {{ $labels.instance }} 的core 文件 增加了 "
description: "{{ $labels.instance }} 的core 文件 增加了 ,很可能程序 崩了 "
然后 重啟 prometheus 服務 打開 http://172.168.10.99:9090/alerts
就能看到 對應的規則了
警報可能有以下三種狀態:
·Inactive:警報未激活。
·Pending:警報已滿足測試表達式條件,但仍在等待for子句中指定的持續時間。
·Firing:警報已滿足測試表達式條件,並且Pending的時間已超過for子句的持續時間。
並且郵件 也收到了
監控機器本身 的 指標 (cpu 內存 硬盤 網絡 等 )
這個比較簡單了 ,node-exporter 有人給做好了
下載 后
wget https://github.com/prometheus/node_exporter/releases/download/v*/node_exporter-*.*-amd64.tar.gz
tar xvfz node_exporter-*.*-amd64.tar.gz
cd node_exporter-*.*-amd64
./node_exporter --web.listen-address 0.0.0.0:9554 # 這里監 聽端口 9554
然后 在 prometheus 里面 配置
常見的參數 常見主機監控指標
然后 配合 grafana 畫出 圖來
常用統計查詢命令
-
場景: 你每5秒上報 一個平均值 ,比如你 每5秒 吃 10個饅頭,然后 下五秒 吃 12 個饅頭 , 我想統計 你 每 五分鍾 要 吃多少個饅頭 用
sum_over_time
示例
sum_over_time(g20ck172_counter_kafaka_xdr_in[5m])
,你想看 11:10 ~ 11:15 分鍾 吃了多少饅頭,鼠標應該懸浮到 11:15 就能看到 總數了