prometheus基本概念用法記錄

Prometheus

  1. 基本概念

    promethues是一套開源的系統監控報警框架。

    ​ Prometheus 所有采集的監控數據均以指標(metric)的形式保存在內置的時間序列數據庫當中(TSDB):屬於同一指標名稱,同一標籤集合的、有時間戳標記的數據流。除了存儲的時間序列,Prometheus 還可以根據查詢請求產生臨時的、衍生的時間序列作爲返回結果。

    ​ 特點:

    • 強大的多爲數據模型
    • 靈活的查詢語言
    • 易於管理
    • 高效
    • 使用pull模式採集時間序列數據
    • 多種可視化圖形界面
    • 易於伸縮
  2. prometheus組成和架構:

    image001.png

    • prometheus server: 主要負責數據採集和存儲,提供promQL查詢語言支持。prometheus是一個時序數據庫,將採集到的監控數據按照時間序列的方式存儲到本地磁盤。
    • Push Gateway: 支持臨時性job主動推送指標的中間網關。
    • PromDash: 使用rails開發的dashboard,用於可視化指標數據。
    • Exporters: 負責監控機器運行狀態,提供被監控組件信息的 HTTP 接口被叫做 exporter。
      • 直接採集: exporter內置了prometheus支持,直接向prometheus暴露數據端點。
      • 間接採集:原不支持prometheus。通過prometheus提供的clien library編寫的目標監控採集程序。
    • Altermanager: 從 Prometheus server 端接收到 alerts 後,會進行去除重複數據,分組,並路由到對收的接受方式,發出報警。常見的接收方式有:電子郵件,pagerduty,OpsGenie, webhook 等。
    • WebUI:9090提供圖形化界面功能。
  3. 基本工作原理

    • Prometheus server 定期從配置好的 jobs 或者 exporters 中拉 metrics,或者接收來自 Pushgateway 發過來的 metrics,或者從其他的 Prometheus server 中拉 metrics。
    • Prometheus server 在本地存儲收集到的 metrics,並運行已定義好的 alert.rules,記錄新的時間序列或者向 Alertmanager 推送警報。
    • Alertmanager 根據配置文件,對接收到的警報進行處理,發出告警。
    • 在圖形界面中,可視化採集數據。
  4. 基本概念

    數據模型:prometheus中存儲的數據爲時間序列,是由Metric的名字和一系列的標籤(鍵值對)唯一標識的,不同的標籤代表不同的時間序列。

    ​ 樣本:實際時間序列,每個序列包括一個float64的值和一個毫秒級的時間戳。(指標+時間戳+樣本值)

    ​ metric名字: 具有語義,表示功能:例如:http_requeststotal, 表示 http 請求的總數。其中,metric 名字由 ASCII 字符,數字,下劃線,以及冒號組成,且必須滿足正則表達式 [a-zA-Z:][a-zA-Z0-9_:]*。

    ​ 標籤:使一個時間序列有不同未讀的識別。例如 http_requeststotal{method="Get"} 表示所有 http 請求中的 Get 請求。當 method="post" 時,則爲新的一個 metric。標籤中的鍵由 ASCII 字符,數字,以及下劃線組成,且必須滿足正則表達式 [a-zA-Z:][a-zA-Z0-9_:]*。

    ​ 格式:<metric name>{<label name>=<label value>, …},例如:http_requests_total{method="POST",endpoint="/api/tracks"}。

    Metric類型

    counter: 累加性metirc。

    Gauge:可增減性metric

    Histogram:樹狀圖

    summary: 彙總

  5. PromQL查詢
    1. 數據類型

      瞬時向量(instant vector):一組時間序列,每個時間序列包含單個樣本。
      區間向量(range vector):一組時間序列,每個時間序列包含一段時間範圍內的樣本數據。
      標量(scalar): 一個浮點型數據值。
      字符串(string): 一個簡單的字符串值。
    2. 時間序列過濾器

      瞬時向量過濾器:
      eg: http_requests_total ,通過{}裏附件一組標籤過濾時間序列。
      標籤匹配雲算符:
        = : 選擇與提供的字符串完全相同的標籤。
        != : 選擇與提供的字符串不相同的標籤。
        =~ : 選擇正則表達式與提供的字符串(或子字符串)相匹配的標籤。
        !~ : 選擇正則表達式與提供的字符串(或子字符串)不匹配的標籤。
      區間向量過濾器:
      eg:http_requests_total{job="prometheus"}[5m],通過[]指定區間提取數值。
      時間單位:
      s - 秒
        m - 分鐘
        h - 小時
        d - 天
        w - 周
        y - 年
      時間位移操作:
      在瞬時向量表達式或者區間向量表達式中,都是以當前時間爲基準.
      eg:http_requests_total offset 5m "offset 關鍵字需要緊跟在選擇器({})後面"
    3. 操作符

      算數二次元運算符
      eg:加減乘除
      布爾運算符:
      eg:= ,!= ,< , > ,<= ,>= 
      集合運算符:
      and,or,unless
      匹配模式
      
    4. 聚合操作

      語法:<aggr-op>([parameter,] <vector expression>) [without|by (<label list>)]  只有count_values, quantile, topk, bottomk支持參數(parameter)
      sum (求和);min (最小值);max (最大值);avg (平均值);stddev (標準差);stdvar (標準差異);count (計數);count_values (對 value 進行計數);bottomk (樣本值最小的 k 個元素);topk (樣本值最大的k個元素);quantile (分佈統計)
      eg:<aggr-op>([parameter,] <vector expression>) [without|by (<label list>)]
      without 用於從計算結果中移除列舉的標籤,而保留其它標籤。by 則正好相反,結果向量中只保留列出的標籤,其餘標籤則移除。通過 without 和 by 可以按照樣本的問題對數據進行聚合。  
    5. 任務和實例

      ​ 採集不同的監控指標,我們需要運行相應的監控採集程序,並且讓prometheus server知道這些export實例的訪問地址。每一個監控樣本的http服務稱之爲一個實例。node exporter可以稱之爲一個實例。

      ​ 一組用於相同採集目的的實例,或者一個採集進程的多個副本則通過一個一個任務管理。

      * job: node
        * instance 2: 1.2.3.4:9100
        * instance 4: 5.6.7.8:9100
    6. HTTP API中響應格式

      瞬時數據查詢:
      url請求參數:
      eg:'http://localhost:9090/api/v1/query?query=up&time=2015-07-01T20:10:51.781Z'
            query=:PromQL表達式。
            time=<rfc3339 | unix_timestamp>:用於指定用於計算PromQL的時間戳。可選參數,默認情況下使用當前系統時間。
            timeout=:超時設置。可選參數,默認情況下使用-query,timeout的全局設置
      區間數據查詢:
      url請求參數:
      eg:'http://localhost:9090/api/v1/query_range?query=up&start=2015-07-01T20:10:30.781Z&end=2015-07-01T20:11:00.781Z&step=15s'
      query=: PromQL表達式。
        start=<rfc3339 | unix_timestamp>: 起始時間。
        end=<rfc3339 | unix_timestamp>: 結束時間。
        step=: 查詢步長。
        timeout=: 超時設置。可選參數,默認情況下使用-query,timeout的全局設置。
      
  6. Prometheus告警

    1. 告警規則定義(Alertrule difinition)

      告警名稱: 自定義名稱.

      告警規則:基於PromQL表達式定義告警觸發條件.定義在配置文件中

       groups:
       - name: example
         rules:
         - alert: HighErrorRate
           expr: job:request_latency_seconds:mean5m{job="myjob"} > 0.5
           for: 10m
           labels:
             severity: page
           annotations:
             summary: High request latency
             description: description info
        #group:定義一組相關規則
        #alert:告警規則名稱
        #expr:基於PromQL的觸發條件
        #for 等待評估時間
        #label 自定義標籤
        #annotation: 指定一組附加信息Alertmanger特性
    2. Altermanager特性

      分組:可以將詳細的告警機制合併成一個通知
       抑制:當發出一個警告時,可以停止重複發送此告警的引發的其他告警機制
       靜默:將告警進行靜默處理
    3. 安裝啓動Altermanger

      wget https://github.com/prometheus/alertmanager/releases/download/v0.15.3/alertmanager-0.15.3.linux-amd64.tar.gz
       cd alertmanager-0.15.3.linux-amd64/
       ./alertmanager
    4. altermanager.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']
       路由(route)以及接收器(receivers)。所有的告警信息都會從配置中的頂級路由(route)進入路由樹,根據路由規則將告警信息發送給相應的接收器。
       全局配置(global):用於定義一些全局的公共參數,如全局的SMTP配置,Slack配置等內容;
       模板(templates):用於定義告警通知時的模板,如HTML模板,郵件模板等;
       告警路由(route):根據標籤匹配,確定當前告警應該如何處理;
       接收人(receivers):接收人是一個抽象的概念,它可以是一個郵箱也可以是微信,Slack或者Webhook等,接收人一般配合告警路由使用;
       抑制規則(inhibit_rules):合理設置抑制規則可以減少垃圾告警的產生
    5. 重啓prometheus

      killall -9 prometheus
      nohup prometheus &
  7. prometheus 安裝
    1. 安裝prometheus server

      wget https://github.com/prometheus/prometheus/releases/download/v2.6.0/prometheus-2.6.0.linux-amd64.tar.gz
      tar -zxvf prometheus-2.6.0.linux-amd64.tar.gz
      cd prometheus-2.6.0.linux-amd64
      ./prometheus &
      ln -s /root/prometheus/prometheus-2.6.0.linux-amd64/prometheus /usr/local/bin/prometheus
      nohup prometheus &
      
    2. 安裝Node Exporter 採集主機運行數據(採集主機運行指標比如cpu,內存和磁盤等信息)

      wget https://github.com/prometheus/node_exporter/releases/download/v0.17.0/node_exporter-0.17.0.linux-amd64.tar.gz
      tar -zxvf node_exporter-0.17.0.linux-amd64.tar.gz 
      cd node_exporter-0.17.0.linux-amd64/
      mv node_exporter  /usr/local/bin/
      nohup  node_exporter &
      
      curl -l -H "Content-type: application/json" -X POST -d '{"msgtype": "markdown","markdown": {"title":"Prometheus告警信息","text": "#### 監控指標\n> 監控描述信息\n\n> ###### 告警時間 \n"},"at": {"isAtAll": false}}' https://oapi.dingtalk.com/robot/send?access_token=51345145d106753486bd71614bf881283f91e2124535276b257f99327e41dc87
      {"errcode":0,"errmsg":"ok"}
    3. Prometheus中添加收集的監控數據,修改prometheus.yml文件,並在scrape_configs添加一下內容。

      scrape_configs:
      - job_name: 'prometheus'
        static_configs:
          - targets: ['localhost:9090']
      # 採集node exporter監控數據
      - job_name: 'node'
        static_configs:
          - targets: ['localhost:9100']
    4. 使用Grafana創建可視化Dashboard

      docker run -d -p 3000:3000 grafana/grafana
      #訪問http://localhost:3000 默認用戶名admin 密碼admin

先記錄,後補充。

參考文章如下:

prometheus非官方手冊

prometheus-book

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章