開源的系統監控和報警工具,監控項目的流量、內存量、負載量等實時數據。mysql
它經過直接或短時jobs中介收集監控數據,在本地存儲全部收集到的數據,而且經過定義好的rules產生新的時間序列數據,或發送警報。經過其它api能夠將採集到的數據可視化。git
簡單的說,主要就是如下幾個步驟:github
本地
收集並存儲數據。若是採用第三方系統收集數據metrics,且數據不是prometheus時序對,則須要定義exporter將那些metrics export爲prometheus時序對。如今有不少已經定義好的官方或第三方的exporters。有些軟件抓取的數據直接就是prometheus格式的。以一個例子來講明部署流程。sql
有不少種安裝方法,這裏我使用預編譯的二進制文件
。到這裏下載。以後解壓,terminal中輸入./prometheus
,回車啓動prometheus服務。docker
打開解壓後的prometheus目錄,發現其中有個prometheus.yml文件。prometheus.yml是設置監控對象等的配置文件。打開prometheus.yml,默認的prometheus.yml的初始配置以下:數據庫
global: scrape_interval: 15s # By default, scrape targets every 15 seconds. # Attach these labels to any time series or alerts when communicating with # external systems (federation, remote storage, Alertmanager). external_labels: monitor: 'codelab-monitor' # 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: 'prometheus' # Override the global default and scrape targets from this job every 5 seconds. scrape_interval: 5s static_configs: - targets: ['localhost:9090']
這裏設定監控目標爲localhost:9090,即prometheus本身。瀏覽器打開localhost:9090
,就能訪問prometheus提供的可視化界面。localhost:9090/metrics
提供了全部的監控數據信息。其中有一條prometheus_target_interval_length_seconds
,表示真實的數據獲取間隔,在prometheus首頁輸入它並回車,就能夠看到一系列的數據,它們有不一樣quantile,從0.01至0.99不等。quantitle表示有多少比例的數據在這個值之內。若是隻關注0.99的數據,能夠輸入prometheus_target_interval_length_seconds{quantile="0.99"}
查詢。查詢還支持函數,好比count(prometheus_target_interval_length_seconds)
可 以查詢數量。
若是想要查詢結果直接包含數量那個數據,建立一個prometheus.rules文件,在文件中定義這條規則,而後在prometheus.yml中配置rules文件。express
//prometheus.rules test:prometheus_target_interval_length_seconds:count = count(prometheus_target_interval_length_seconds) //prometheus.yml # my global config global: scrape_interval: 15s # By default, scrape targets every 15 seconds. evaluation_interval: 15s # By default, scrape targets every 15 seconds. # scrape_timeout is set to the global default (10s). # Attach these labels to any time series or alerts when communicating with # external systems (federation, remote storage, Alertmanager). external_labels: monitor: 'codelab-monitor' # Load rules once and periodically evaluate them according to the global 'evaluation_interval'. rule_files: - "prometheus.rules" # - "second.rules" # 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: 'prometheus' # Override the global default and scrape targets from this job every 5 seconds. scrape_interval: 5s # metrics_path defaults to '/metrics' # scheme defaults to 'http'. static_configs: - targets: ['localhost:9090']
以後,就能夠直接輸入test:prometheus_target_interval_length_seconds:count
查詢數據了。這裏rule比較簡單,若是有一些經常使用的但比較複雜的數據,均可以用rule的方法來定義獲取。api
修改prometheus.yml,在文件最後添加:瀏覽器
- job_name: 'mysql' # Override the global default and scrape targets from this job every 5 seconds. scrape_interval: 5s # metrics_path defaults to '/metrics' # scheme defaults to 'http'. static_configs: - targets: ['localhost:9104'] labels: instance: db1
重啓prometheus服務:服務器
$ ./prometheus -config.file=prometheus.yml
再打開localhost:9090,查看Status
-> Targets
頁面下,就能夠看到配置的兩個target:一個是prometheus自己,State
爲UP
,另外一個是mysql,State
爲DOWN
,由於咱們尚未配置監控mysql的服務。
在在這裏下載並解壓mysql exporter,或者直接使用docker:
$ docker pull prom/mysqld-exporter
mysqld_exporter須要鏈接到mysql,須要mysql的權限,須要先爲他建立用戶並賦予所需的權限:
CREATE USER 'mysqlexporter'@'localhost' IDENTIFIED BY 'msyqlexporter'; GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost' WITH MAX_USER_CONNECTIONS 3;
而後在docker中運行exporter,其中DATA_SOURCE_NAME是環境變量,用於鏈接數據庫。
$ docker run -d \ -p 9104:9104 \ -e DATA_SOURCE_NAME="mysqlexporter:mysqlexporter@(localhost:3306)/data_store" prom/mysqld-exporter
此時再刷下localhost:9090/targets
,就能夠看到mysql的state
轉爲UP
,即已經成功的監測了mysql。
核心的幾個點:
侷限:
適用於監控全部時間序列的項目。