Prometheus 和 Alertmanager實戰配置

來源:互聯網
上載者:User
Prometheus時序資料庫

 

一、Prometheus1、Prometheus安裝

1)源碼安裝

prometheus安裝包最新版本:prometheus.io/download/

wget https://github.com/prometheus/prometheus/releases/download/v2.3.2/prometheus-2.3.2.linux-amd64.tar.gztar txvf prometheus-2.3.2.linux-amd64.tar.gzcd prometheus-2.3.2.linux-amd64.tar.gz./prometheus --config.file=prometheus.yml

註:通過執行

./prometheus -h

可以查看具體得執行參數,參數後面可以查看預設得參數。如所示。

2)docker 方式安裝(前提docker已經安裝完畢)

建立目錄和prometheus設定檔

mkdir /prometheusvim /prometheus/prometheus.yml

註:對於prometheus.yml檔案的配置,稍後詳細介紹。

拉取prometheus鏡像

docker pull prom/prometheus

啟動prometheus

docker run -d -p 9090:9090 --name prometheus -v /home/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus

註:參數的簡要說明

a、-d選項啟動獨立模式下的prometheus容器,這意味著容器將在後台啟動,這種情況下只有stop docker才可以關閉prometheus,而不能執行ctrl+c

b、-p選擇指定連接埠號碼映射,通過訪問原生9090連接埠,即可訪問prometheus容器的9090連接埠

c、--name指定容器的名稱

d、-v選項建立本機檔案和docker內檔案的映射

e、--config.file指定運行docker內prometheus的設定檔

2、prometheus設定檔的設定

prometheus的設定檔採用的是yaml檔案,yaml檔案書寫的要求如下:

大小寫敏感使用縮排展示層級關係縮排時不允許使用Tab鍵,只允許使用空格。縮排的空格數目不重要,只要相同層級的元素左側對齊即可

prometheus.yml的範例

# Prometheus全域配置項global:  scrape_interval:     15s # 設定抓取資料的周期,預設為1min  evaluation_interval: 15s # 設定更新rules檔案的周期,預設為1min  scrape_timeout: 15s # 設定抓取資料的逾時時間,預設為10s
external_labels: # 額外的屬性,會添加到拉取得資料並存到資料庫中
monitor: 'codelab_monitor'
# Alertmanager配置alerting: alertmanagers: - static_configs: - targets: ["localhost:9093"] # 設定alertmanager和prometheus互動的介面,即alertmanager監聽的ip地址和連接埠 # rule配置,首次讀取預設載入,之後根據evaluation_interval設定的周期載入rule_files: - "alertmanager_rules.yml" - "prometheus_rules.yml"# scape配置scrape_configs:- job_name: 'prometheus' # job_name預設寫入timeseries的labels中,可以用於查詢使用 scrape_interval: 15s # 抓取周期,預設採用global配置 static_configs: # 靜態配置 - targets: ['localdns:9090'] # prometheus所要抓取資料的地址,即instance執行個體項- job_name: 'example-random' static_configs: - targets: ['localhost:8080']
3、動態更新prometheus的配置項

 動態更新Prometheus的配置,即熱更新載入,一共有兩種方式:

1)向prometheus進程發送SIGHUP訊號

2)curl -X POST http://localdns:9090/-/reload 

參考連結:songjiayang.gitbooks.io/prometheus/content/qa/hotreload.html

4、prometheus資料展示

此處介紹兩種Prometheus資料介面化顯示的方式。

1)運算式瀏覽器

在瀏覽器中,輸入部署prometheus資料庫的機器ip地址以及連接埠號碼

http://localdns:9090/graph

介面展示如下,就可以通過瀏覽器查看Prometheus中的資料。

 

2)Grafana圖形介面

安裝啟動

wget https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana-5.2.3.linux-amd64.tar.gztar zxvf grafana-5.2.3.linux-amd64.tar.gzcd grafana-5.2.3.linux-amd64.tar.gzbin/grafana-server web

Grafana預設服務連接埠號碼為3000,通過瀏覽器對Grafana進行訪問。

http://localdns:3000

預設登入名稱密碼為"admin/admin"。

建立一個Prometheus資料來源Data source:

1 在左側工具列中,點擊"Configuration"菜單。2 點擊"Data Sources"。3 點擊"Add data source"。4 資料來源Type選擇“Prometheus”。5 設定Prometheus服務訪問地址(例如:http://localhost:9090)。6 調整其他想要的設定(例如:關閉代理訪問)。7 點擊“Add”按鈕,儲存這個新資料來源。

之後,通過添加儀錶盤(dashboards)進行資料的展示。

二、Alertmanager(email警示)1、Alertmanager安裝

源碼安裝

mkdir -p $GOPATH/src/github.com/prometheuscd $GOPATH/src/github.com/prometheusgit clone https://github.com/prometheus/alertmanager.gitcd alertmanagermake build

啟動

./alertmanager-config.file= alertmanager.yml #預設配置項為alertmanager.yml

註:alertmanager.yml設定檔,預設是不存在的,需要建立。

2、alertmanager.yml的配置
# 全域配置項global:   resolve_timeout: 5m #處理逾時時間,預設為5min  smtp_smarthost: 'smtp.sina.com:25' # 郵箱smtp伺服器代理  smtp_from: '******@sina.com' # 發送郵箱名稱  smtp_auth_username: '******@sina.com' # 郵箱名稱  smtp_auth_password: '******' # 郵箱密碼或授權碼
wechat_api_url: 'qyapi.weixin.qq.com/cgi-bin/' # 企業地址

# 定義模板信心
templates:
- 'template/*.tmpl'
# 定義路由樹資訊route: group_by: ['alertname'] # 警示分組依據 group_wait: 10s # 最初即第一次等待多久時間發送一組警報的通知 group_interval: 10s # 在發送新警報前的等待時間 repeat_interval: 1m # 發送重複警報的周期 對於email配置中,此項不可以設定過低,否則將會由於郵件發送太多頻繁,被smtp伺服器拒絕 receiver: 'email' # 發送警報的接收者的名稱,以下receivers name的名稱# 定義警報接收者資訊receivers: - name: 'email' # 警報 email_configs: # 郵箱配置 - to: '******@163.com' # 接收警報的email配置
html: '{{ template "test.html" . }}' # 設定郵箱的內容範本
headers: { Subject: "[WARN] 警示郵件"} # 內送郵件的標題
webhook_configs: # webhook配置
- url: 'http://127.0.0.1:5001'
send_resolved: true
wechat_configs: # 企業警示配置
- send_resolved: true
to_party: '1' # 接收組的id
agent_id: '1000002' # (企業-->自定應用-->AgentId)
corp_id: '******' # 公司資訊(我的企業-->CorpId[在底部])
api_secret: '******' # 企業(企業-->自定應用-->Secret)
message: '{{ template "test_wechat.html" . }}' # 發送訊息模板的設定
# 一個inhibition規則是在與另一組匹配器匹配的警報存在的條件下,使匹配一組匹配器的警報失效的規則。兩個警報必須具有一組相同的標籤。 
inhibit_rules:
- source_match:
severity: 'critical'
target_match:
severity: 'warning'
equal: ['alertname', 'dev', 'instance']

註:

1)repeat_interval配置項,對於email來說,此項不可以設定過低,否則將會由於郵件發送太多頻繁,被smtp伺服器拒絕

2)企業登入位址:work.weixin.qq.com

上述配置的email、webhook和wechat三種警示方式。目前Alertmanager所有的警示方式有以下幾個方面:

email_confighipchat_configpagerduty_configpushover_configslack_configopsgenie_configvictorops_config
3、.tmpl模板的配置

1)test.tmpl

{{ define "test.html" }}<table border="1">        <tr>                <td>警示項</td>                <td>執行個體</td>                <td>警示閥值</td>                <td>開始時間</td>        </tr>        {{ range $i, $alert := .Alerts }}                <tr>                        <td>{{ index $alert.Labels "alertname" }}</td>                        <td>{{ index $alert.Labels "instance" }}</td>                        <td>{{ index $alert.Annotations "value" }}</td>                        <td>{{ $alert.StartsAt }}</td>                </tr>        {{ end }}</table>{{ end }}

註:上述Labels項,表示prometheus裡面的可選label項。annotation項表示警示規則中定義的annotation項的內容。

2)test_wechat.tmpl

{{ define "cdn_live_wechat.html" }}  {{ range $i, $alert := .Alerts.Firing }}    [警示項]:{{ index $alert.Labels "alertname" }}    [執行個體]:{{ index $alert.Labels "instance" }}    [警示閥值]:{{ index $alert.Annotations "value" }}    [開始時間]:{{ $alert.StartsAt }}  {{ end }}{{ end }}

註:此處range遍曆項與email模板中略有不同,只遍曆當前沒有處理的警示(Firing)。此項如果不設定,則在Alert中已經Resolved的警示項,也會被發送到企業。

4、在Prometheus模組定義警示規則

alertmanager_rules.yml範例設定檔(與prometheus同目錄下)

groups: - name: test-rules   rules:   - alert: InstanceDown # 警示名稱     expr: up == 0 # 警示的判定條件,參考Prometheus進階查詢來設定     for: 2m # 滿足警示條件期間多久後,才會發送警示     labels: #標籤項      team: node     annotations: # 解析項,詳細解釋警示資訊      summary: "{{$labels.instance}}: has been down"      description: "{{$labels.instance}}: job {{$labels.job}} has been down "
value: {{$value}}
5、警示資訊生命週期的3中狀態

1)inactive:表示當前警示資訊即不是firing狀態也不是pending狀態

2)pending:表示在設定的閾值時間範圍內被啟用的

3)firing:表示超過設定的閾值時間被啟用的

三、結果展示

啟動prometheus和alertmanager,滿足警示條件後,就可以收到警示郵件了。

1、瀏覽器介面化警示展示

在瀏覽器輸入alertmanager的配置地址,即可查看所監控到的警示資訊

http://localdns:9093/#/alerts

2、郵箱警示展示

1)原始郵箱警示展示

 2)模板郵箱警示展示

 3、企業警示展示

 

 參考連結:

www.kancloud.cn/cdh0805010118/prometheus/719379

songjiayang.gitbooks.io/prometheus/content/alertmanager/what.html

www.kancloud.cn/cdh0805010118/prometheus/719380

76008594?locationNum=10&fps=1

 blog.qikqiak.com/post/alertmanager-of-prometheus-in-practice/

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.