Prometheus 和 Alertmanager實戰配置


Prometheus時序數據庫

 

一、Prometheus

1、Prometheus安裝

1)源碼安裝

prometheus安裝包最新版本下載地址:https://prometheus.io/download/

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

注:通過執行

./prometheus -h

可以查看具體得執行參數,參數后面可以查看默認得參數。如下圖所示。

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

創建目錄和prometheus配置文件

mkdir /prometheus
vim /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 

參考鏈接:https://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.gz
tar zxvf grafana-5.2.3.linux-amd64.tar.gz
cd grafana-5.2.3.linux-amd64.tar.gz
bin/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/prometheus
cd $GOPATH/src/github.com/prometheus
git clone https://github.com/prometheus/alertmanager.git
cd alertmanager
make 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: 'https://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)企業微信注冊地址:https://work.weixin.qq.com

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

email_config
hipchat_config
pagerduty_config
pushover_config
slack_config
opsgenie_config
victorops_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、企業微信告警展示

 

 參考鏈接:

https://www.kancloud.cn/cdh0805010118/prometheus/719379

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

https://www.kancloud.cn/cdh0805010118/prometheus/719380

https://blog.csdn.net/qq_21398167/article/details/76008594?locationNum=10&fps=1

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM