Prometheus【普羅米修斯】+Grafana部署企業級監控(一)


一、前言

Prometheus是最初在SoundCloud上構建的開源系統監視和警報工具包。自2012年成立以來,許多公司和組織都采用了Prometheus,該項目擁有非常活躍的開發人員和用戶社區。Prometheus 於2016年加入了 Cloud Native Computing Foundation,這是繼Kubernetes之后的第二個托管項目。

Exporter是一個采集監控數據並通過Prometheus監控規范對外提供數據的組件,能為Prometheus提供監控的接口。

Exporter將監控數據采集的端點通過HTTP服務的形式暴露給Prometheus Server,Prometheus Server通過訪問該Exporter提供的Endpoint端點,即可獲取到需要采集的監控數據。不同的Exporter負責不同的業務。

Prometheus              開源的系統監控和報警框架,靈感源自Google的Borgmon監控系統

AlertManager            處理由客戶端應用程序(如Prometheus server)發送的警報。它負責將重復數據刪除,分組和路由到正確的接收者集成,還負責沉默和抑制警報

Node_Exporter           用來監控各節點的資源信息的exporter,應部署到prometheus監控的所有節點

PushGateway             推送網關,用於接收各節點推送的數據並暴露給Prometheus server

官網:https://prometheus.io

文檔:https://prometheus.io/docs/introduction/overview/

下載prometheus各組件:https://prometheus.io/download/

二、prometheus 介紹

2.1 prometheus的特點

1. 多維的數據模型(基於時間序列的Key、Value鍵值對)

2. 靈活的查詢和聚合語言PromQL

3. 提供本地存儲和分布式存儲

4. 通過基於HTTP的Pull模型采集時間序列數據

5. 可利用Pushgateway(Prometheus的可選中間件)實現Push模式

6. 可通過動態服務發現或靜態配置發現目標機器

7. 支持多種圖表和數據大盤

2.2 prometheus的組件

1. Prometheus server,負責拉取、存儲時間序列數據

2. 客戶端庫(client library),插入應用程序代碼

3. 推送網關(push gateway),支持短暫的任務

4. 特殊類型的exporter,支持如HAProxy,StatsD,Graphite等服務

5. 一個alertmanager處理告警

6. 各種支持工具

2.3 prometheus的架構

image

2.4 prometheus的使用場景

prometheus非常適合記錄任何純數字時間序列。它既適合以機器為中心的監視,也適合監視高度動態的面向服務的體系結構。在微服務世界中,它對多維數據收集和查詢的支持是一種特別的優勢。

prometheus的設計旨在提高可靠性,使其成為中斷期間要使用的系統,從而使您能夠快速診斷問題。每個prometheus服務器都是獨立的,而不依賴於網絡存儲或其他遠程服務,當基礎設施部分出現問題時仍然可以使用它。

2.5 prometheus的相關概念

  • 數據模型:

prometheus將所有數據存儲為時間序列:屬於相同 metric名稱和相同標簽組(鍵值對)的時間戳值流。

  • metric 和 標簽:

每一個時間序列都是由其 metric名稱和一組標簽(鍵值對)組成唯一標識。

metric名稱代表了被監控系統的一般特征(如 http_requests_total代表接收到的HTTP請求總數)。它可能包含ASCII字母和數字,以及下划線和冒號,它必須匹配正則表達式[a-zA-Z_:][a-zA-Z0-9_:]*

注意:冒號是為用戶定義的記錄規則保留的,不應該被exporter使用

標簽給prometheus建立了多維度數據模型:對於相同的 metric名稱,標簽的任何組合都可以標識該 metric的特定維度實例(例如:所有使用POST方法到 /api/tracks 接口的HTTP請求)。查詢語言會基於這些維度進行過濾和聚合。更改任何標簽值,包括添加或刪除標簽,都會創建一個新的時間序列。

標簽名稱可能包含ASCII字母、數字和下划線,它必須匹配正則表達式[a-zA-Z_][a-zA-Z0-9_]*。另外,以雙下划線__開頭的標簽名稱僅供內部使用。

標簽值可以包含任何Unicode字符。標簽值為空的標簽被認為是不存在的標簽。

  • 表示法:

給定 metric名稱和一組標簽,通常使用以下表示法標識時間序列:

<metric name>{<label name>=<label value>, ...}

例如,一個時間序列的 metric名稱是 api_http_requests_total,標簽是method="POST"handler="/messages"。可以這樣寫:

api_http_requests_total{method="POST", handler="/messages"}

這和OpenTSDB的表示法是一樣的。

  • metric類型:
Counter             值只能單調增加或重啟時歸零,可以用來表示處理的請求數、完成的任務數、出現的錯誤數量等

Gauge               值可以任意增加或減少,可以用來測量溫度、當前內存使用等

Histogram           取樣觀測結果,一般用來請求持續時間或響應大小,並在一個可配置的分布區間(bucket)內計算這些結果,提供所有觀測結果的總和

                        累加的 counter,代表觀測區間:<basename>_bucket{le="<upper inclusive bound>"}
                        所有觀測值的總數:<basename>_sum
                        觀測的事件數量:<basenmae>_count

Summary             取樣觀測結果,一般用來請求持續時間或響應大小,提供觀測次數及所有觀測結果的總和,還可以通過一個滑動的時間窗口計算可分配的分位數
                        觀測的事件流φ-quantiles (0 ≤ φ ≤ 1):<basename>{quantile="φ"}
                        所有觀測值的總和:<basename>_sum
                        觀測的事件數量:<basename>_count
  • 實例與任務:

在prometheus中,一個可以拉取數據的端點叫做實例(instance),一般等同於一個進程。一組有着同樣目標的實例(例如為彈性或可用性而復制的進程副本)叫做任務(job)。

當prometheus拉取目標時,它會自動添加一些標簽到時間序列中,用於標識被拉取的目標:

job:目標所屬的任務名稱

instance:目標URL中的<host>:<port>部分

如果兩個標簽在被拉取的數據中已經存在,那么就要看配置選項 honor_labels 的值來決定行為了。

每次對實例的拉取,prometheus會在以下的時間序列中保存一個樣本(樣本指的是在一個時間序列中特定時間點的一個值):

up{job="<job-name>", instance="<instance-id>"}:如果實例健康(可達),則為 1 ,否則為 0

scrape_duration_seconds{job="<job-name>", instance="<instance-id>"}:拉取的時長

scrape_samples_post_metric_relabeling{job="<job-name>", instance="<instance-id>"}:在 metric relabeling 之后,留存的樣本數量

scrape_samples_scraped{job="<job-name>", instance="<instance-id>"}:目標暴露出的樣本數量

up 時間序列對於實例的可用性監控來說非常有用。

三、prometheus 部署配置

系統 IP地址 軟件安裝 節點
Centos 7.6 10.0.0.10 prometheus master
Centos 7.6 10.0.0.11 Altermanager node1
Centos 7.6 10.0.0.12 Grafana node2

3.1 下載prometheus

# 官網下載地址:https://prometheus.io/download/
wget https://github.com/prometheus/prometheus/releases/download/v2.28.1/prometheus-2.28.1.linux-amd64.tar.gz

# 解壓縮
tar -xf prometheus-2.28.1.linux-amd64.tar.gz -C /usr/local/

# 重命名
cd /usr/local/ && mv prometheus-2.28.1.linux-amd64  prometheus

# 設置軟鏈接
ln -s /usr/local/prometheus/prometheus   /usr/bin/prometheus

3.2 配置systemd管理prometheus

# 創建prometheus用戶
useradd -M -s /sbin/nologin prometheus

# 修改權限
chown -R prometheus:prometheus /usr/local/prometheus 

#  編輯prometheus.service文件
vim /usr/lib/systemd/system/prometheus.service

示例文件1 簡潔明了

[Unit]
Description=Prometheus
Documentation=https://prometheus.io/
After=network.target

[Service]
WorkingDirectory=/usr/local/prometheus/
ExecStart=/usr/local/prometheus/prometheus
ExecReload=/bin/kill -HUP $MAINPID
ExecStop=/bin/kill -KILL $MAINPID
Type=simple
KillMode=control-group
Restart=on-failure
RestartSec=15s

[Install]
WantedBy=multi-user.target

示例文件2 更加細節

[Unit]
Description=Prometheus
Documentation=https://prometheus.io/
After=network.target

[Service]
Type=simple
Environment="GOMAXPROCS=4"
User=prometheus
Group=prometheus
ExecReload=/bin/kill -HUP $MAINPID
ExecStart=/usr/local/prometheus/prometheus \
  --config.file=/usr/local/prometheus/prometheus.yml \
  --storage.tsdb.path=/usr/local/prometheus \
  --storage.tsdb.retention=30d \
  --web.console.libraries=/usr/local/prometheus/console_libraries \
  --web.console.templates=/usr/local/prometheus/consoles \
  --web.listen-address=0.0.0.0:9090 \
  --web.read-timeout=5m \
  --web.max-connections=30 \
  --query.max-concurrency=50 \
  --query.timeout=2m \
  # 開啟熱重啟
  --web.enable-lifecycle
PrivateTmp=true
PrivateDevices=true
ProtectHome=true
NoNewPrivileges=true
LimitNOFILE=infinity
ReadWriteDirectories=/usr/local/prometheus
ProtectSystem=full

SyslogIdentifier=prometheus
Restart=always

[Install]
WantedBy=multi-user.target

3.3 啟動prometheus

3.3.1 通過二進制文件啟動prometheus

# 前台啟動
/usr/local/prometheus/prometheus 
# 后台啟動方式一
/usr/local/prometheus/prometheus &
# 后台啟動方式二
nohup /usr/local/prometheus/prometheus &>/var/log/prometheus.log  &

3.3.2 通過systemd啟動prometheus

#  重載配置文件
systemctl daemon-reload
# 設置開機啟動並啟動prometheus
systemctl enable prometheus && systemctl start prometheus
# 查看端口
netstat -lntp | grep prometheus
tcp6       0      0 :::9090                 :::*                    LISTEN      43742/prometheus
# 查看運行狀態
[root@prometheus-server opt]# systemctl status prometheus
● prometheus.service - Prometheus
   Loaded: loaded (/usr/lib/systemd/system/prometheus.service; enabled; vendor preset: disabled)
   Active: active (running) since 二 2021-07-27 09:57:58 CST; 21min ago
     Docs: https://prometheus.io/
 Main PID: 1134 (prometheus)
    Tasks: 10
   Memory: 107.0M
   CGroup: /system.slice/prometheus.service
           └─1134 /usr/local/prometheus/prometheus

訪問hostip:9090,至此,prometheus部署完成,接下來需要配置prometheus。

四、prometheus配置文件詳解

prometheus的配置文件prometheus.yml,它主要分以下幾個配置塊:

全局配置        global

告警配置        alerting

規則文件配置    rule_files

拉取配置        scrape_configs

遠程讀寫配置    remote_read、remote_write

4.1 全局配置global

global指定在所有其他配置上下文中有效的參數。還可用作其他配置部分的默認設置。

global:
  # 默認拉取頻率
  [ scrape_interval: <duration> | default = 1m ]

  # 拉取超時時間
  [ scrape_timeout: <duration> | default = 10s ]

  # 執行規則頻率
  [ evaluation_interval: <duration> | default = 1m ]

  # 通信時添加到任何時間序列或告警的標簽
  # external systems (federation, remote storage, Alertmanager).
  external_labels:
    [ <labelname>: <labelvalue> ... ]

  # 記錄PromQL查詢的日志文件
  [ query_log_file: <string> ]

4.2 告警配置alerting

alerting指定與Alertmanager相關的設置。

alerting:
  alert_relabel_configs:
    [ - <relabel_config> ... ]
  alertmanagers:
    [ - <alertmanager_config> ... ]

4.3 規則文件配置rule_files

rule_files指定prometheus加載的任何規則的位置,從所有匹配的文件中讀取規則和告警。目前沒有規則

rule_files:
  [ - <filepath_glob> ... ]

4.4 拉取配置 scrape_configs

scrape_configs指定prometheus監控哪些資源。默認會拉取prometheus本身的時間序列數據,通過http://hostIP:9090/metrics進行拉取。

一個scrape_config指定一組目標和參數,描述如何拉取它們。在一般情況下,一個拉取配置指定一個作業。在高級配置中,這可能會改變。

可以通過static_configs參數靜態配置目標,也可以使用支持的服務發現機制之一動態發現目標。

此外,relabel_configs在拉取之前,可以對任何目標及其標簽進行修改

scrape_configs:
job_name: <job_name>

# 拉取頻率
[ scrape_interval: <duration> | default = <global_config.scrape_interval> ]

# 拉取超時時間
[ scrape_timeout: <duration> | default = <global_config.scrape_timeout> ]

# 拉取的http路徑
[ metrics_path: <path> | default = /metrics ]

# honor_labels 控制prometheus處理已存在於收集數據中的標簽與prometheus將附加在服務器端的標簽("作業"和"實例"標簽、手動配置的目標標簽和由服務發現實現生成的標簽)之間的沖突
# 如果 honor_labels 設置為 "true",則通過保持從拉取數據獲得的標簽值並忽略沖突的服務器端標簽來解決標簽沖突
# 如果 honor_labels 設置為 "false",則通過將拉取數據中沖突的標簽重命名為"exported_<original-label>"來解決標簽沖突(例如"exported_instance"、"exported_job"),然后附加服務器端標簽
# 注意,任何全局配置的 "external_labels"都不受此設置的影響。在與外部系統的通信中,只有當時間序列還沒有給定的標簽時,它們才被應用,否則就會被忽略
[ honor_labels: <boolean> | default = false ]

# honor_timestamps 控制prometheus是否遵守拉取數據中的時間戳
# 如果 honor_timestamps 設置為 "true",將使用目標公開的metrics的時間戳
# 如果 honor_timestamps 設置為 "false",目標公開的metrics的時間戳將被忽略
[ honor_timestamps: <boolean> | default = true ]

# 配置用於請求的協議
[ scheme: <scheme> | default = http ]

# 可選的http url參數
params:
  [ <string>: [<string>, ...] ]

# 在每個拉取請求上配置 username 和 password 來設置 Authorization 頭部,password 和 password_file 二選一
basic_auth:
  [ username: <string> ]
  [ password: <secret> ]
  [ password_file: <string> ]

# 在每個拉取請求上配置 bearer token 來設置 Authorization 頭部,bearer_token 和 bearer_token_file 二選一
[ bearer_token: <secret> ]

# 在每個拉取請求上配置 bearer_token_file 來設置 Authorization 頭部,bearer_token_file 和 bearer_token 二選一
[ bearer_token_file: /path/to/bearer/token/file ]

# 配置拉取請求的TLS設置
tls_config:
  [ <tls_config> ]

# 可選的代理URL
[ proxy_url: <string> ]

# Azure服務發現配置列表
azure_sd_configs:
  [ - <azure_sd_config> ... ]

# Consul服務發現配置列表
consul_sd_configs:
  [ - <consul_sd_config> ... ]

# DNS服務發現配置列表
dns_sd_configs:
  [ - <dns_sd_config> ... ]

# EC2服務發現配置列表
ec2_sd_configs:
  [ - <ec2_sd_config> ... ]

# OpenStack服務發現配置列表
openstack_sd_configs:
  [ - <openstack_sd_config> ... ]

# file服務發現配置列表
file_sd_configs:
  [ - <file_sd_config> ... ]

# GCE服務發現配置列表
gce_sd_configs:
  [ - <gce_sd_config> ... ]

# Kubernetes服務發現配置列表
kubernetes_sd_configs:
  [ - <kubernetes_sd_config> ... ]

# Marathon服務發現配置列表
marathon_sd_configs:
  [ - <marathon_sd_config> ... ]

# AirBnB's Nerve服務發現配置列表
nerve_sd_configs:
  [ - <nerve_sd_config> ... ]

# Zookeeper Serverset服務發現配置列表
serverset_sd_configs:
  [ - <serverset_sd_config> ... ]

# Triton服務發現配置列表
triton_sd_configs:
  [ - <triton_sd_config> ... ]

# 靜態配置目標列表
static_configs:
  [ - <static_config> ... ]

# 目標relabel配置列表
relabel_configs:
  [ - <relabel_config> ... ]

# metric relabel配置列表
metric_relabel_configs:
  [ - <relabel_config> ... ]

# 每次拉取樣品的數量限制
# metric relabelling之后,如果有超過這個數量的樣品,整個拉取將被視為失效。0表示沒有限制
[ sample_limit: <int> | default = 0 ]

4.5遠程讀寫配置 remote_read/remote_write

remote_read/remote_write將數據源與prometheus分離,當前不做配置

# 與遠程寫功能相關的設置
remote_write:
  [ - <remote_write> ... ]

# 與遠程讀功能相關的設置
remote_read:
  [ - <remote_read> ... ]

4.6 簡單配置示例

vim  /usr/local/prometheus/prometheus.yml
global:
  scrape_interval: 15s
  evaluation_interval: 15s

alerting:
  alertmanagers:
  - static_configs:
    - targets:
      # - alertmanager:9093

rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
    - targets: ['10.0.0.10:9090']
# 對配置文件進行檢查
# /usr/local/prometheus/promtool check config /usr/local/prometheus/prometheus.yml

Checking /usr/local/prometheus/prometheus.yml
  SUCCESS: 0 rule files found # 看到SUCCESS就說明配置文件沒有問題

【擴展】配置自動發現服務 示例

global:
  scrape_interval: 15s
  evaluation_interval: 15s

alerting:
  alertmanagers:
  - static_configs:
    - targets:
      # - alertmanager:9093

rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

scrape_configs:
  - job_name: 'nodes'
    # 靜態配置換成了服務發現配置
    file_sd_configs:
    - files:
      - "nodes/*.yml"

allnodes.yml

- targets:
  - "10.0.0.10:9100"
  - "10.0.0.11:9100"
  - "10.0.0.12:9100"
  labels:
    app: nodes
    name: mynode

五、Node_expoter部署

下載node_exporter

# 下載
wget https://github.com/prometheus/node_exporter/releases/download/v1.2.0/node_exporter-1.2.0.linux-amd64.tar.gz

# 解壓
tar -xf node_exporter-1.2.0.linux-amd64.tar.gz -C /usr/local/

# 重命名
cd /usr/local/ && mv node_exporter-1.2.0.linux-amd64  node_exporter

# 設置軟鏈接/usr/local
ln -s /usr/local/node_exporter/node_exporter   /usr/bin/node_exporter

# 配置systemd管理node_exporter
# useradd -M -s /sbin/nologin prometheus              #若已創建,可省略該步
chown -R prometheus:prometheus /usr/local/node_exporter
# 編輯node_exporter.service文件
vim /usr/lib/systemd/system/node_exporter.service

示例1 簡單明了

[Unit]
Description=Node_exporter
Documentation=https://github.com/prometheus/node_exporter/
After=network.target

[Service]
WorkingDirectory=/usr/local/node_exporter/
ExecStart=/usr/local/node_exporter/node_exporter
ExecReload=/bin/kill -HUP $MAINPID
ExecStop=/bin/kill -KILL $MAINPID
Type=simple
KillMode=control-group
Restart=on-failure
RestartSec=15s

[Install]
WantedBy=multi-user.target

示例2 更加詳細

[Unit]
Description=Node_exporter
After=network.target

[Service]
User=prometheus
Group=prometheus
ExecStart=/usr/local/node_exporter/node_exporter \
  --web.listen-address=0.0.0.0:9100 \
  --web.telemetry-path=/metrics \
  --log.level=info \
  --log.format=logfmt
ExecReload=/bin/kill -HUP $MAINPID
ExecStop=/bin/kill -KILL $MAINPID
Type=simple
KillMode=control-group
Restart=always
RestartSec=15s

[Install]
WantedBy=multi-user.target

5.2 啟動node_exporter

5.2.1 使用二進制文件啟動

# 前台啟動
node_exporter 

# 后台啟動方式1
node_exporter &

# 后台啟動方式2
nohup node_exporter &> /var/log/node_exporter.log &

# 帶參數后台啟動
nohup node_exporter  --web.listen-address=0.0.0.0:9100  --web.telemetry-path=/metrics \ 
&> /var/log/node_exporter/node_exporter.log &

5.2.2 使用systemd啟動

# 重載配置文件
systemctl daemon-reload

# 設置開機啟動並啟動node_exporter
systemctl enable node_exporter && systemctl start node_exporter

# 檢查端口
netstat -lntp | grep node_exporter
tcp6       0      0 :::9100                 :::*                    LISTEN      2725/node_exporter

# 查看運行狀態
# systemctl status node_exporter
● node_exporter.service - node_exporter
   Loaded: loaded (/usr/lib/systemd/system/node_exporter.service; enabled; vendor preset: disabled)
   Active: active (running) since 二 2021-07-27 11:10:04 CST; 6s ago
     Docs: https://github.com/prometheus/node_exporter/
 Main PID: 83850 (node_exporter)
    Tasks: 6
   Memory: 28.0M
   CGroup: /system.slice/node_exporter.service
           └─83850 /usr/local/node_exporter/node_exporter

訪問hostip:9100
node exporter展示了prometheus可以拉取的指標,包括在輸出中更下方的各種系統指標(帶有前綴node_)。要查看這些指標(以及幫助和類型信息):

curl http://localhost:9100/metrics | grep 'node_'

5.3 配置scrape_configs

啟動好node_exporter后,還需要配置prometheus才能訪問node exporter指標。

vim /usr/local/prometheus/prometheus.yml                #修改 scrape_configs 內容
scrape_configs:
  - job_name: 'prometheus'
    static_configs:
    - targets: ['10.0.0.10:9090']

  - job_name: 'nodes'
    static_configs:
    - targets: ['10.0.0.10:9100','10.0.0.11:9100','10.0.0.12:9100']
# 重啟prometheus或者重載prometheus配置文件
systemctl restart prometheus  或者 systemctl reload prometheus

查看node狀態
訪問prometheus頁面,StatusTargets
可以看到,之前部署的node exporter狀態是UP,說明運行正常。

通過部署的node_exporter可以收集當前主機的系統基礎信息。如查看系統15分鍾平均負載,

至此,node_exporter部署完成。

【拓展】node_exporter配置用戶名和密碼

#  安裝httpd-tools
yum install -y httpd-tools

#  生成密碼
htpasswd -nBC 10 "" | tr -d ':\n'   # 回車后輸入密碼  例如輸入6個1
$2y$10$SpFQBSWkvNboPXm/YaxwZOUo1WDi86QGSpf1ZfXJHyZmrK9RVWXX6

# node_exporter安裝目錄下編輯web-config.yml文件
basic_auth_users:
  # 用戶名: 密碼-上面加密生產的字符串 
  mynode: $2y$10$SpFQBSWkvNboPXm/YaxwZOUo1WDi86QGSpf1ZfXJHyZmrK9RVWXX6
# 啟動node_exporter
node_exporter --web.config=/usr/local/node_exporter/web-config.yml 

# systemd啟動配置文件如下
[Unit]
Description=Node_exporter
Documentation=https://github.com/prometheus/node_exporter/
After=network.target

[Service]
WorkingDirectory=/usr/local/node_exporter/
# 啟動命令加入了web.config的配置文件位置
ExecStart=/usr/local/node_exporter/node_exporter  --web.config=/usr/local/node_exporter/web-config.yml 
ExecReload=/bin/kill -HUP $MAINPID
ExecStop=/bin/kill -KILL $MAINPID
Type=simple
KillMode=control-group
Restart=on-failure
RestartSec=15s

[Install]
WantedBy=multi-user.target

再次訪問ip:9100此時必須使用賬戶名+密碼才能訪問
此時,prometheus將無法抓取node_exporter的數據

修改prometheus配置文件如下:

vim /usr/local/prometheus/prometheus.yml                #修改 scrape_configs 內容
scrape_configs:
  - job_name: 'prometheus'
    static_configs:
    - targets: ['10.0.0.10:9090']

  - job_name: 'nodes'
    # 增加用戶認證信息
    basic_auth:
       # 這里配置的是瀏覽器訪問時需要輸入的用戶名和密碼
       username: mynode
       password: 111111
    static_configs:
    - targets: ['10.0.0.10:9100','10.0.0.11:9100','10.0.0.12:9100']
# 重啟prometheus或者重載prometheus配置文件
systemctl restart prometheus  或者 systemctl reload prometheus

六、AlertManager部署

6.1 下載alertmanager

# 下載
wget  https://github.com/prometheus/alertmanager/releases/download/v0.22.2/alertmanager-0.22.2.linux-amd64.tar.gz

# 解壓
tar -xf alertmanager-0.22.2.linux-amd64.tar.gz -C /usr/local/

# 重命名
cd /usr/local/ && mv alertmanager-0.22.2.linux-amd64  alertmanager

# 設置軟鏈接
ln -s /usr/local/alertmanager/alertmanager  /usr/bin/alertmanager
ln -s /usr/local/alertmanager/amtool /usr/bin/amtool

# 配置systemd管理alertmanager
useradd -M -s /sbin/nologin prometheus              #若已創建,可省略
chown -R prometheus:prometheus /usr/local/alertmanager
# 編輯alertmanager.service文件
vim /usr/lib/systemd/system/alertmanager.service

示例1 簡潔明了

[Unit]
Description=Alertmanager
Documentation=https://github.com/prometheus/alertmanager/releases/
After=network.target

[Service]
WorkingDirectory=/usr/local/alertmanager/
ExecStart=/usr/local/alertmanager/alertmanager
ExecReload=/bin/kill -HUP $MAINPID
ExecStop=/bin/kill -KILL $MAINPID
Type=simple
KillMode=control-group
Restart=on-failure
RestartSec=15s

[Install]
WantedBy=multi-user.target

示例2 更加詳細

[Unit]
Description=Alertmanager
Documentation=https://github.com/prometheus/alertmanager/releases/
After=network.target

[Service]
User=prometheus
Group=prometheus
ExecStart=/usr/local/alertmanager/alertmanager \
  --config.file=/usr/local/alertmanager/alertmanager.yml \
  --storage.path=/usr/local/alertmanager/data \
  --web.listen-address=0.0.0.0:9093 \
  --cluster.listen-address=0.0.0.0:9094 \
  --log.level=info \
  --log.format=logfmt
ExecReload=/bin/kill -HUP $MAINPID
ExecStop=/bin/kill -KILL $MAINPID 
Type=simple
KillMode=control-group
Restart=always
RestartSec=15s

[Install]
WantedBy=multi-user.target

檢查配置文件

[root@localhost alertmanager]# amtool check-config alertmanager.yml 
Checking 'alertmanager.yml'  SUCCESS
Found:
 - global config
 - route
 - 1 inhibit rules
 - 1 receivers
 - 0 templates

6.2 啟動altermanager

6.2.1 通過二進制文件啟動

# 前台啟動
alertmanager
# 后台啟動方式1
alertmanager &
# 后台啟動方式2
nohup alertmanager &> /var/log/alertmanager/alertmanager.log &
# 帶參數啟動
nohup alertmanager  --config.file=/usr/local/alertmanager/alertmanager.yml \
  --storage.path=/usr/local/alertmanager/data   --web.listen-address=0.0.0.0:9093 \
  --cluster.listen-address=0.0.0.0:9094  &> /var/log/alertmanager/alertmanager.log   &

6.2.2 systemd啟動

# 重載配置文件
systemctl daemon-reload
# 設置開機啟動並啟動alertmanager
systemctl enable alertmanager && systemctl start alertmanager
# 查看端口
netstat -lntp | grep alertmanager
tcp6       0      0 :::9093                 :::*                    LISTEN      89558/alertmanager  
tcp6       0      0 :::9094                 :::*                    LISTEN      89558/alertmanager

# 查看運行狀態
[root@localhost alertmanager]# systemctl status alertmanager
● alertmanager.service - Alertmanager
   Loaded: loaded (/usr/lib/systemd/system/alertmanager.service; enabled; vendor preset: disabled)
   Active: active (running) since 二 2021-07-27 14:49:47 CST; 7s ago
     Docs: https://github.com/prometheus/alertmanager/releases/
 Main PID: 89558 (alertmanager)
    Tasks: 9
   Memory: 17.4M
   CGroup: /system.slice/alertmanager.service
           └─89558 /usr/local/alertmanager/alertmanager

訪問hostIP:9093 因未配置prometheus這里顯示為空

6.3 配置altering

啟動好alertmanager后,還需要配置prometheus才能通過alertmanager告警。

vim /usr/local/prometheus/prometheus.yml                #更改 alerting 內容
alerting:
  alertmanagers:
  - static_configs:
    - targets:
      - "10.0.0.11:9093"

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
    - targets: ['10.0.0.10:9090']

  - job_name: 'nodes'
    static_configs:
    - targets: ['10.0.0.10:9100','10.0.0.11:9100','10.0.0.12:9100']

  - job_name: 'alertmanager'
    static_configs:
    - targets: ['10.0.0.11:9093']
# 重啟prometheus或者重載配置文件
systemctl restart prometheus 或者 systemctl reload prometheus

訪問prometheus頁面,StatusTargets
可以看到,之前部署的alertmanager狀態是UP,說明運行正常。

至此,alertmanager部署完成。但alertmanager還需要進一步配置通知路由和通知接收者。

6.4 altermanager配置

alertmanager通過命令行標志和配置文件進行配置。命令行標志配置不可變的系統參數時,配置文件定義禁止規則,通知路由和通知接收器。

alertmanager的配置文件alertmanager.yml,它主要分以下幾個配置塊:

全局配置        global

通知模板        templates

路由配置        route

接收器配置      receivers

抑制配置        inhibit_rules

6.4.1 全局配置 global

global指定在所有其他配置上下文中有效的參數。還用作其他配置部分的默認設置。

global:
  # 默認的SMTP頭字段
  [ smtp_from: <tmpl_string> ]

  # 默認的SMTP smarthost用於發送電子郵件,包括端口號
  # 端口號通常是25,對於TLS上的SMTP,端口號為587
  # Example: smtp.example.org:587
  [ smtp_smarthost: <string> ]

  # 要標識給SMTP服務器的默認主機名
  [ smtp_hello: <string> | default = "localhost" ]

  # SMTP認證使用CRAM-MD5,登錄和普通。如果為空,Alertmanager不會對SMTP服務器進行身份驗證
  [ smtp_auth_username: <string> ]

  # SMTP Auth using LOGIN and PLAIN.
  [ smtp_auth_password: <secret> ]

  # SMTP Auth using PLAIN.
  [ smtp_auth_identity: <string> ]

  # SMTP Auth using CRAM-MD5.
  [ smtp_auth_secret: <secret> ]

  # 默認的SMTP TLS要求
  # 注意,Go不支持到遠程SMTP端點的未加密連接
  [ smtp_require_tls: <bool> | default = true ]

  # 用於Slack通知的API URL
  [ slack_api_url: <secret> ]
  [ victorops_api_key: <secret> ]
  [ victorops_api_url: <string> | default = "https://alert.victorops.com/integrations/generic/20131114/alert/" ]
  [ pagerduty_url: <string> | default = "https://events.pagerduty.com/v2/enqueue" ]
  [ opsgenie_api_key: <secret> ]
  [ opsgenie_api_url: <string> | default = "https://api.opsgenie.com/" ]
  [ wechat_api_url: <string> | default = "https://qyapi.weixin.qq.com/cgi-bin/" ]
  [ wechat_api_secret: <secret> ]
  [ wechat_api_corp_id: <string> ]

  # 默認HTTP客戶端配置
  [ http_config: <http_config> ]

  # 如果告警不包括EndsAt,則ResolveTimeout是alertmanager使用的默認值,在此時間過后,如果告警沒有更新,則可以聲明警報已解除
  # 這對Prometheus的告警沒有影響,它們包括EndsAt
  [ resolve_timeout: <duration> | default = 5m ]

6.4.2 通知模板 templates

templates指定了從其中讀取自定義通知模板定義的文件,最后一個文件可以使用一個通配符匹配器,如templates/*.tmpl

templates:
  [ - <filepath> ... ]

6.4.3 路由配置 route

route定義了路由樹中的節點及其子節點。如果未設置,則其可選配置參數將從其父節點繼承。

每個告警都會在已配置的頂級路由處進入路由樹,該路由樹必須與所有告警匹配(即沒有任何已配置的匹配器),然后它會遍歷子節點。如果continue設置為false,它將在第一個匹配的子項之后停止;如果continue設置為true,則告警將繼續與后續的同級進行匹配。如果告警與節點的任何子節點都不匹配(不匹配的子節點或不存在子節點),則根據當前節點的配置參數來處理告警。

route:
  group_by: ['alertname']
  group_wait: 10s
  group_interval: 10s
  repeat_interval: 1h
  receiver: 'web.hook'

6.4.4 接收器配置 receivers

receivers是一個或多個通知集成的命名配置。建議通過webhook接收器實現自定義通知集成。

receivers:
- name: 'web.hook'
  webhook_configs:
  - url: 'http://127.0.0.1:5001/'

6.4.5 抑制規則配置 inhibit_rules

當存在與另一組匹配器匹配的告警(源)時,抑制規則會使與一組匹配器匹配的告警(目標)“靜音”。目標和源告警的equal列表中的標簽名稱都必須具有相同的標簽值。

在語義上,缺少標簽和帶有空值的標簽是相同的。因此,如果equal源告警和目標告警都缺少列出的所有標簽名稱,則將應用抑制規則。

inhibit_rules:
  - source_match:
      severity: 'critical'
    target_match:
      severity: 'warning'
    equal: ['alertname', 'dev', 'instance']

6.4.6 默認配置示例

vim /usr/local/alertmanager/alertmanager.yml
global:
  resolve_timeout: 5m

route:
  group_by: ['alertname']
  group_wait: 10s
  group_interval: 10s
  repeat_interval: 1h
  receiver: 'web.hook'
receivers:
- name: 'web.hook'
  webhook_configs:
  - url: 'http://127.0.0.1:5001/'
inhibit_rules:
  - source_match:
      severity: 'critical'
    target_match:
      severity: 'warning'
    equal: ['alertname', 'dev', 'instance']

七、Grafana部署

grafana 是一款采用 go 語言編寫的開源應用,主要用於大規模指標數據的可視化展現,是網絡架構和應用分析中最流行的時序數據展示工具,目前已經支持絕大部分常用的時序數據庫。

官網:https://grafana.com

7.1 安裝Grafana

7.1.1 通過yum方式安裝

vim /etc/yum.repos.d/grafana.repo
[grafana]
name=grafana
baseurl=https://packages.grafana.com/oss/rpm
repo_gpgcheck=1
enabled=1
gpgcheck=1
gpgkey=https://packages.grafana.com/gpg.key
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
# 執行命令安裝Grafana
yum makecache fast -y
yum install -y initscripts urw-fonts wget
yum install -y grafana

7.2.2 二進制包安裝

# 下載rpm包
wget https://dl.grafana.com/oss/release/grafana-8.0.6-1.x86_64.rpm
# 執行安裝,效果等同於yum安裝
yum install -y grafana-8.0.6-1.x86_64.rpm

# 下載二進制文件
wget https://dl.grafana.com/oss/release/grafana-8.0.6.linux-amd64.tar.gz
# 解壓
tar -xf grafana-8.0.6.linux-amd64.tar.gz -C /usr/local/
# 重命名
cd /usr/local/  && mv grafana-8.0.6  grafana

# 設置軟鏈接
ln -s /usr/local/grafana/bin/grafana-server /usr/bin/grafana-server
ln -s /usr/local/grafana/bin/grafana-cli /usr/bin/grafana-cli

# 配置systemd管理文件
vim /usr/lib/systemd/system/grafana-server.service
[Unit]
Description=Grafana
Documentation=https://grafana.com/
After=network.target

[Service]
WorkingDirectory=/usr/local/grafana/
ExecStart=/usr/local/grafana/bin/grafana-server
ExecReload=/bin/kill -HUP $MAINPID
ExecStop=/bin/kill -KILL $MAINPID
Type=simple
KillMode=control-group
Restart=on-failure
RestartSec=15s

[Install]
WantedBy=multi-user.target

7.1.3 docker部署grafana

mkdir /opt/grafana && chmod 777 /opt/grafana

docker pull grafana/grafana

docker run -d -p 3000:3000 --name=grafana -v /opt/grafana:/var/lib/grafana grafana/grafana

docker exec -it grafana grafana-cli plugins install alexanderzobnin-zabbix-app              #安裝zabbix插件

docker restart grafana

初始賬號密碼為admin、admin,建議后面更改密碼。

grafana容器配置文件:/etc/grafana/grafana.ini

7.2 啟動grafana

7.2.1 二進制文件啟動

# 前台啟動
grafana-server

# 后台啟動方式1
grafana-server &

# 后台啟動方式2
nohup grafana-server &>/var/log/grafana/grafana.log  & 

# 帶參數后台啟動
nohup grafana-server -config "/usr/local/grafana/conf/defaults.ini"  &>/var/log/grafana/grafana.log  &

7.2.2 systemd啟動

# 重載配置文件
systemctl daemon-reload

# 設置開機啟動並啟動Grafana
systemctl enable grafana-server && systemctl start grafana-server

# 查看端口
netstat -lntp | grep 3000
tcp6       0      0 :::3000                 :::*                    LISTEN      3303/grafana-server 

# 查看運行狀態
[root@localhost grafana]# systemctl status grafana-server
● grafana-server.service - Grafana
   Loaded: loaded (/usr/lib/systemd/system/grafana-server.service; disabled; vendor preset: disabled)
   Active: active (running) since 二 2021-07-27 15:43:06 CST; 10s ago
     Docs: https://grafana.com/
 Main PID: 3303 (grafana-server)
   CGroup: /system.slice/grafana-server.service
           └─3303 /usr/local/grafana/bin/grafana-server

訪問ip:3000,初始賬號密碼為adminadmin,建議后面更改密碼。
grafana部署完成。

二進制安裝:【grafana配置文件:/usr/local/grafana/defaults.ini

yum安裝【grafana配置文件:/etc/grafana/grafana.ini

7.3 使用grafana

7.3.1 導入prometheus數據源

ConfigurationData SourcesPrometheusSelect,填入http://ip:9090,保存即可

7.3.2 導入dashboard

官方dashboard模板:https://grafana.com/grafana/dashboards

選擇排行第一的中文模板:1 Node Exporter for Prometheus Dashboard CN v20200628,模板ID是8919。

ManageImport,填入模板ID,導入,
自定義dashboard名稱,選擇數據源Prometheus
至此,prometheus + grafana 部署完成

7.3.3 安裝插件【可選】

grafana所有插件地址:https://grafana.com/grafana/plugins?orderBy=weight&direction=asc

grafana-cli plugins install alexanderzobnin-zabbix-app              #安裝zabbix插件

grafana-cli plugins install grafana-clock-panel                     #時鍾

grafana-cli plugins install grafana-piechart-panel                  #餅圖

grafana-cli plugins install novalabs-annotations-panel              #注釋

grafana-cli plugins install farski-blendstat-panel                  #混合

grafana-cli plugins install yesoreyeram-boomtable-panel             #多表

grafana-cli plugins install yesoreyeram-boomtheme-panel             #多主題

grafana-cli plugins install jeanbaptistewatenberg-percent-panel     #百分比

grafana-cli plugins install corpglory-progresslist-panel            #進程列表

grafana-cli plugins install mxswat-separator-panel                  #分隔符 |

grafana-cli plugins install aidanmountford-html-panel               #網頁

安裝完插件需要重啟grafana:systemctl restart grafana-server

監控主機是否存活

grafana與zabbix結合時,zabbix的agent.ping無法准確反映主機是否宕機。因此,需要使用icmpping[<target>,<packets>,<interval>,<size>,<timeout>]來監控是否存活,存活返回1,反之返回0。


免責聲明!

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



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