#(1)prometheus介紹
prometheus是一款 強大的監控系統和時序系統
采集數據: 在目標主機上安裝exporter, exporter組件會在目標處收集監控數據, 并暴露一個http接口供prometheus查詢, prometheus通過pull的方式來采集數據;
目前exporter已經采集絕大多數的第三方數據, 比如nginx, mysql等; 支持的exporter列表https://prometheus.io/docs/instrumenting/exporters/prometheus組件: prometheus server : 負責收集和存儲指標數據,支持表達式查詢,和告警的生成prometheus exporter altermanager PushgatewayWeb UI
#(2)安裝 Prometheus server
prometheus支持多種安裝方式, 例如docker, ansible;
1)方式一:二進制安裝
test -d /tools || mkdir /tools; cd /tools
wget https://github.com/prometheus/prometheus/releases/download/v2.4.3/prometheus-2.4.3.linux-amd64.tar.gz
tar xvfz prometheus-2.4.3.linux-amd64.tar.gz
cd prometheus-2.4.3.linux-amd64
./prometheus --version
./prometheus --config.file=prometheus.yml #啟動
2)方式二安裝 docker安裝
docker run -d -p 9090:9090 prom/prometheus
3)配置文件
# cat prometheus.yml
# my global config
global:scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# Alertmanager configuration
alerting:alertmanagers:- static_configs:- targets:# - alertmanager:9093# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:# - "first_rules.yml"# - "second_rules.yml"# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.- job_name: Prometheusstatic_configs:- targets: ['localhost:9090']labels:instance: Prometheus- job_name: mysqlstatic_configs:- targets: ['192.168.1.42:9104']labels:instance: db1- job_name: linux01static_configs:- targets: ['192.168.1.42:9100']labels:instance: db1global 塊:Prometheus 的全局配置,比如 scrape_interval 表示 Prometheus 多久抓取一次數據,evaluation_interval 表示多久檢測一次告警規則;
alerting 塊:關于 Alertmanager 的配置,這個我們后面再看;
rule_files 塊:告警規則,這個我們后面再看;
scrape_config 塊:這里定義了 Prometheus 要抓取的目標,我們可以看到默認已經配置了一個名稱為 prometheus 的 job,這是因為 Prometheus 在啟動的時候也會通過 HTTP 接口暴露自身的指標數據,這就相當于 Prometheus 自己監控自己,雖然這在真正使用 Prometheus 時沒啥用處,但是我們可以通過這個例子來學習如何使用 Prometheus;可以訪問 http://localhost:9090/metrics 查看 Prometheus 暴露了哪些指標;
#(3)學習 PromQL
1)進入prometheus的http頁面, http://localhost:9090
Alerts 展示了定義的所有告警規則Status 可以查看各種 Prometheus 的狀態信息Graph頁面 提供的一種特殊表達式來查詢監控數據,這個表達式被稱為 PromQL(Prometheus Query Language)通過 PromQL 不僅可以在 Graph 頁面查詢數據,而且還可以通過 Prometheus 提供的 HTTP API 來查詢
2) 數據模型
一條 Prometheus 數據由一個指標名稱(metric)和 N 個標簽(label,N >= 0)組成的promhttp_metric_handler_requests_total{code="200",instance="Prometheus",job="Prometheus"} 14數據的指標名稱為 promhttp_metric_handler_requests_total 并且包含三個標簽 code、instance 和 job ,這條記錄的值為 14Prometheus 是一個時序數據庫,相同指標相同標簽的數據構成一條時間序列
3)Prometheus 的數據分類
Counter 用于計數 例如:請求次數、任務完成數、錯誤發生次數,這個值會一直增加,不會減少
Gauge 就是一般的數值,可大可小,例如:溫度變化、內存使用變化
Histogram 是直方圖,或稱為柱狀圖,常用于跟蹤事件發生的規模,例如:請求耗時、響應大小 ,它特別之處是可以對記錄的內容進行分組,提供 count 和 sum 的功能
Summary: 和 Histogram 十分相似,也用于跟蹤事件發生的規模,不同之處是,它提供了一個 quantiles 的功能,可以按百分比劃分跟蹤的結果。例如:quantile 取值 0.95,表示取采樣值里面的 95% 數據
4)PromQL 入門
up : 表示 Prometheus 能否抓取 target 的指標,用于 target 的健康檢查
up{job="prometheus"} 指定某個 label 來查詢 不僅可以使用 = 號,還可以使用 !=、=~、!~
up{job!="prometheus"}
up{job=~"192\.168\.0\.107.+"}
up{job=~"server|mysql"} =~ 是根據正則表達式來匹配
http_requests_total[5m] 查出 5 分鐘內所有抓取的 HTTP 請求數 注意它返回的數據類型是 Range vector,沒辦法在 Graph 上顯示成曲線圖, 會用在 Counter 類型的指標上,并和 rate() 或 irate() 函數一起使用rate(http_requests_total[5m]) 計算的是每秒的平均值,適用于變化很慢的 counter
irate(http_requests_total[5m]) 計算的是每秒瞬時增加速率,適用于變化很快的 counter
PromQL 還支持 count、sum、min、max、topk 等 聚合操作
#(4)安裝 Grafana
雖然 Prometheus 提供的 Web UI 也可以很好的查看不同指標的視圖,但是這個功能非常簡單,只適合用來調試。要實現一個強大的監控系統,還需要一個能定制展示不同指標的面板,能支持不同類型的展現方式(曲線圖、餅狀圖、熱點圖、TopN 等),這就是儀表盤(Dashboard)功能。因此 Prometheus 開發了一套儀表盤系統 PromDash,不過很快這套系統就被廢棄了,官方開始推薦使用 Grafana 來對 Prometheus 的指標數據進行可視化,這不僅是因為 Grafana 的功能非常強大,而且它和 Prometheus 可以完美的無縫融合。
1)docker安裝
docker run -d -p 3000:3000 grafana/grafana
2)訪問: http://localhost:3000/ , 默認用戶名和密碼是admin和admin
3)配置數據源
要注意的是,這里的 Access 指的是 Grafana 訪問數據源的方式,有 Browser 和 Proxy 兩種方式。
Browser 方式表示當用戶訪問 Grafana 面板時,瀏覽器直接通過 URL 訪問數據源的;
而 Proxy 方式表示瀏覽器先訪問 Grafana 的某個代理接口(接口地址是 /api/datasources/proxy/),由 Grafana 的服務端來訪問數據源的 URL,如果數據源是部署在內網,用戶通過瀏覽器無法直接訪問時,這種方式非常有用。
參考文檔: https://www.aneasystone.com/archives/2018/11/prometheus-in-action.html
轉載于:https://blog.51cto.com/1000682/2361984