prometheus 監控學習

行行出bug發表於2018-09-01

什麼是 prometheus

1. 簡單介紹

Prometheus is an open-source systems monitoring and alerting toolkit originally built at SoundCloud. Since its inception in 2012, many companies and organizations have adopted Prometheus, and the project has a very active developer and user community. It is now a standalone open source project and maintained independently of any company. To emphasize this, and to clarify the project's governance structure, Prometheus joined the Cloud Native Computing Foundation in 2016 as the second hosted project, after Kubernetes.

Prometheus是一個開源監控系統,它前身是SoundCloud的警告工具包。從2012年開始,許多公司和組織開始使用Prometheus。該專案的開發人員和使用者社群非常活躍,越來越多的開發人員和使用者參與到該專案中。目前它是一個獨立的開源專案,且不依賴與任何公司。 為了強調這點和明確該專案治理結構,Prometheus在2016年繼Kurberntes之後,加入了Cloud Native Computing Foundation。

結構圖

2. 特點

  • 多維度資料模型
  • 靈活的查詢語言
  • 不依賴分散式儲存,單個伺服器節點是自主的
  • 以HTTP方式,通過pull模型拉去時間序列資料
  • 也通過中間閘道器支援push模型
  • 通過服務發現或者靜態配置,來發現目標服務物件
  • 支援多種多樣的圖表和介面展示,grafana也支援它

和zabbix的對比


prometheus 的安裝

1. 預編譯後的二進位制壓縮檔案安裝

下載原始碼 解壓安裝 tar -xzvf prohetheus-xxx.tar.gz 進入解壓後的目錄執行修改配置檔案: prometheus.yml 命令列執行: ./prometheus --config.file=prometheus.yml

執行時控制檯可能會報: transport: http2Client.notifyError got notified that the client transport was broken unexpected EOF。 官方issue給出的解決 修改bash配置檔案後重啟就能生效。

2. Docker映象安裝

3. 原始碼安裝(需要go環境)

4. 三方配置管理系統


prometheus 的使用

1. 元件介紹

prometheus的監控服務和報警服務都是以元件和配置的形式進行的。要使客戶端被監控,需要安裝相應的元件來獲取監控資料或是去自己去實現prometheus的資料介面,並將監控資料傳遞給監控伺服器。 在沒有特殊需求的情況下選擇官方提供的官方元件或是官方推薦的三方元件庫,使用起來更便捷穩定性也有保障。

  • alertmanager 報警元件,接收prometheus傳送的報警資料,並依據配置的報警方式(email,hipchat,webhook,wechat等)傳送報警資訊。
  • node_exporter 節點監控元件(官方提供的用於監控硬體的核心元件),執行在機器上後,能收集機器的硬體資訊,網路資訊等
  • pushgateway 資料傳送的代理元件,官方建議用在監控短期任務的監控資料收發:任務將資料傳送給pushgateway,prometheus從pushgateway獲取監控資料
  • blackbox_exporter 黑盒監控元件,主要用於監控網路是否通暢,服務是否可用:示例
  • mysqld_exporter mysql官方提供的監控元件,用於收集mysql的監控資料,供prometheus伺服器獲取。很多軟體都提供了用於獲取自己資料的監控元件來供prometheus獲取資料,我們要做的只是將元件配置好然後執行起來收集資料就行。
  • grafana 不屬於prometueus的元件,主要使用者展示監控資料。原生的prometheus介面展示效果不太好,目前官方已經棄用,而改用展示效果更好的grafana,*2.5.0 (2015-10-28)*以及之後版本的ganfana能很好的支援prometheus。

2. prometheus的配置

因 prmetheus 是進行元件化管理,元件組合全靠配置檔案來完成,這裡不羅列如何進行具體配置了,詳情請見 premetheus配置 配置被監控物件 配置報警元件 安裝配置grafana 安裝grafana: 連結地址


3. 監控資料管理

資料儲存位置管理官方文件 本地儲存的,在啟動時指定儲存位置: ./prometheus --storage.tsdb.path=somePath 遠端儲存的,在配置檔案中指定 資料儲存時間管理(預設15天),需注意伺服器的磁碟是否能儲存下監控資料檔案,遠端訪問的,需要注意prometheus是否有對遠端檔案的讀寫許可權 啟動時指定: ./prometheus --storage.tsdb.retention=15d


4. 環境搭建示例

prometheus + grafana + node + alert

下載壓縮軟體包:

在prometheus官網下載最新版的 prometheus, node_exporter, alertmanager 在grafana官網上下載最新版本的grafana的壓縮檔案

prometheus
alertmanager
node_exporter
grafana
將各個檔案解壓.


將node_exporter配置到監控伺服器中

進入 prometheus 解壓目錄,vim prometheus.yml,加入如下設定:

scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: 'prometheus'

    # Override the global default and scrape targets from this job every 5 seconds.
    scrape_interval: 5s

    static_configs:
      - targets: ['127.0.0.1:9090']

  - job_name: 'node' # 將node新增到監控中
    static_configs:
      - targets: ['127.0.0.1:9100']
複製程式碼

啟動 node_exporter: 到 node_exporter 解壓目錄下,執行: ./node_exporter 啟動成功後開啟瀏覽器,輸入: http://127.0.0.1:9100/metrics 成功後會有如下顯示:

顯示


啟動prometheus: 到 prometheus 解壓目錄下,執行: ./prometheus --config.file=prometheus.yml 啟動成功後在瀏覽器輸入: http://127.0.0.1:9090/graph 成功後會顯示:

prometheus介面
點選 Status -> Targets, 如果能看到當前有兩個正在被監控的程式,且都處於UP狀態,則配置成功
prothemeus-targets
pro_targets


為 prometheus 新增報警

配置檔案處理

  • 到 alertmanager 解壓目錄下, vim alertmanager.yml 中加入:
global:
  # The smarthost and SMTP sender used for mail notifications.
  smtp_smarthost: 'you email host:587'
  smtp_from: 'email_name@qq.com'
  smtp_auth_username: 'email_name@qq.com'
  smtp_auth_password: 'email_password'
  resolve_timeout: 5m

route:
  group_by: ['alertname']
  group_wait: 10s
  group_interval: 10s
  repeat_interval: 1h
  receiver: 'web.hook'
receivers:
- name: 'web.hook'
  email_configs:
  - to: 'receive alert email account'
inhibit_rules:
  - source_match:
      severity: 'critical'
    target_match:
      severity: 'warning'
    equal: ['alertname', 'dev', 'instance']
複製程式碼
  • 到 prometheus 解壓目錄下, 新建檔案 alert.rules vim alert.rules 在檔案中加入:
groups:
- name: example
  rules:

  # Alert for any instance that is unreachable for >5 minutes.
  - alert: InstanceDown
    expr: up == 0
    for: 5m
    labels:
      severity: page
    annotations:
      summary: "Instance {{ $labels.instance }} down"
      description: "{{ $labels.instance }} of job {{ $labels.job }} has been down for more than 5 minutes."

  # Alert for any instance that has a median request latency >1s.
  - alert: APIHighRequestLatency
    expr: api_http_request_latencies_second{quantile="0.5"} > 1
    for: 10m
    annotations:
      summary: "High request latency on {{ $labels.instance }}"
      description: "{{ $labels.instance }} has a median request latency above 1s (current value: {{ $value }}s)"
複製程式碼
  • 將報警規則和報警元件配置到 prometheus 中: vim prometheus.yml,加入如下設定:
rule_files:
  - "test_alert.rules"

# Alerting specifies settings related to the Alertmanager.
alerting:
  alertmanagers:
    - static_configs:
      - targets: ['127.0.0.1:9093']
複製程式碼

啟動 alertmanager 到 alertmanager 解壓目錄下,輸入 ./alertmanager --config.file=alertmanager.yml 啟動成功後在瀏覽器輸入: http://127.0.0.1:9093/#/alerts 看到如下介面,表明啟動成功:

image

重啟 premotheus(ctrl + c關掉後按上面的命令重啟就行)


加入 grafana 美化輸出介面

  • 啟動grafana 到 grafana 解壓目錄下輸入: ./bin/grafana-server web 啟動程式 瀏覽器中輸入: http://localhost:3000 使用者名稱: admin 密碼: admin 直接跳過修改密碼進入介面
  • 將 prometheus 配置到 DataSource 中:
    grafana_data_source
  • 新增模板: 點選 + -> import
    image

將模板id新增到下面的框中,點選load就可以看到效果了,找模板點這裡:grafana官網模板庫

grafana_dashborad

load 成功後需要指定模板用在哪個資料來源上(勾選剛才配置的資料來源)

image

勾選完成後可以看到 import 能點了,點選後正式進入監控頁面:

image
至此簡單使用就完成了

[其他的配置案例]www.cnblogs.com/iiiiher/p/8…

相關文章