號稱下一代監(jiān)控系統(tǒng),來看看它有多強!
點擊上方“碼農(nóng)突圍”,馬上關注
這里是碼農(nóng)充電第一站,回復“666”,獲取一份專屬大禮包 真愛,請設置“星標”或點個“在看
一、Prometheus 概述
A multi-dimensional data model, so that data can be sliced and diced at will, along dimensions like instance, service, endpoint, and method. Operational simplicity, so that you can spin up a monitoring server where and when you want, even on your local workstation, without setting up a distributed storage backend or reconfiguring the world. Scalable data collection and decentralized architecture, so that you can reliably monitor the many instances of your services, and independent teams can set up independent monitoring servers. Finally, a powerful query language that leverages the data model for meaningful alerting (including easy silencing) and graphing (for dashboards and for ad-hoc exploration).

二、安裝 Prometheus server
2.1 開箱即用
$?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,?version?2.4.3?(branch:?HEAD,?revision:?167a4b4e73a8eca8df648d2d2043e21bdb9a7449)??
??build?user:???????root@1e42b46043e9??
??build?date:???????20181004-08:42:02??
??go?version:???????go1.11.1??
$?./prometheus?--config.file=prometheus.yml??
2.2 使用 Docker 鏡像
$?sudo?docker?run?-d?-p?9090:9090?prom/prometheus??
$?sudo?docker?run?-d?-p?9090:9090?\??
????-v?~/docker/prometheus/:/etc/prometheus/?\??
????prom/prometheus??
~/docker/prometheus/prometheus.yml,這樣可以方便編輯和查看,通過?-v?參數(shù)將本地的配置文件掛載到?/etc/prometheus/?位置,這是 prometheus 在容器中默認加載的配置文件位置。-v?參數(shù)的命令,然后通過?docker inspect?命名看看容器在運行時默認的參數(shù)有哪些(下面的 Args 參數(shù)):$?sudo?docker?inspect?0c??
[...]??
????????"Id":?"0c4c2d0eed938395bcecf1e8bb4b6b87091fc4e6385ce5b404b6bb7419010f46",??
????????"Created":?"2018-10-15T22:27:34.56050369Z",??
????????"Path":?"/bin/prometheus",??
????????"Args":?[??
????????????"--config.file=/etc/prometheus/prometheus.yml",??
????????????"--storage.tsdb.path=/prometheus",??
????????????"--web.console.libraries=/usr/share/prometheus/console_libraries",??
????????????"--web.console.templates=/usr/share/prometheus/consoles"??
????????],??
???
[...]??
2.3 配置 Prometheus
--config.file?來指定,配置文件格式為 YAML。我們可以打開默認的配置文件?prometheus.yml?看下里面的內(nèi)容:/etc/prometheus?$?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.??
??#?scrape_timeout?is?set?to?the?global?default?(10s).??
???
#?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=`?to?any?timeseries?scraped?from?this?config.??
??-?job_name:?'prometheus'??
???
????#?metrics_path?defaults?to?'/metrics'??
????#?scheme?defaults?to?'http'.??
???
????static_configs:??
????-?targets:?['localhost:9090']??
global 塊:Prometheus 的全局配置,比如? scrape_interval?表示 Prometheus 多久抓取一次數(shù)據(jù),evaluation_interval?表示多久檢測一次告警規(guī)則;alerting 塊:關于 Alertmanager 的配置,這個我們后面再看; rule_files 塊:告警規(guī)則,這個我們后面再看; scrape_config 塊:這里定義了 Prometheus 要抓取的目標,我們可以看到默認已經(jīng)配置了一個名稱為?
prometheus?的 job,這是因為 Prometheus 在啟動的時候也會通過 HTTP 接口暴露自身的指標數(shù)據(jù),這就相當于 Prometheus 自己監(jiān)控自己,雖然這在真正使用 Prometheus 時沒啥用處,但是我們可以通過這個例子來學習如何使用 Prometheus;可以訪問?http://localhost:9090/metrics?查看 Prometheus 暴露了哪些指標;
三、學習 PromQL
http://localhost:9090/?即可,它默認會跳轉到 Graph 頁面:promhttp_metric_handler_requests_total,這個指標表示?/metrics?頁面的訪問次數(shù),Prometheus 就是通過這個頁面來抓取自身的監(jiān)控數(shù)據(jù)的。在 Console 標簽中查詢結果如下:scrape_interval?參數(shù)是 15s,也就是說 Prometheus 每 15s 訪問一次?/metrics?頁面,所以我們過 15s 刷新下頁面,可以看到指標值會自增。在 Graph 標簽中可以看得更明顯:
promhttp_metric_handler_requests_total{code="200",instance="192.168.0.107:9090",job="prometheus"}?106
promhttp_metric_handler_requests_total,并且包含三個標簽?code、instance?和?job,這條記錄的值為 106。上面說過,Prometheus 是一個時序數(shù)據(jù)庫,相同指標相同標簽的數(shù)據(jù)構成一條時間序列。如果以傳統(tǒng)數(shù)據(jù)庫的概念來理解時序數(shù)據(jù)庫,可以把指標名當作表名,標簽是字段,timestamp 是主鍵,還有一個 float64 類型的字段表示值(Prometheus 里面所有值都是按 float64 存儲)。Counter Gauge Histogram Summary
3.2 PromQL 入門
#?表示?Prometheus?能否抓取?target?的指標,用于?target?的健康檢查??
up??
up{instance="192.168.0.107:9090",job="prometheus"}????1??
up{instance="192.168.0.108:9090",job="prometheus"}????1??
up{instance="192.168.0.107:9100",job="server"}????1??
up{instance="192.168.0.108:9104",job="mysql"}????0??
up{job="prometheus"}??
=?號,還可以使用?!=、=~、!~,比如下面這樣:up{job!="prometheus"}??
up{job=~"server|mysql"}??
up{job=~"192\.168\.0\.107.+"}??
=~?是根據(jù)正則表達式來匹配,必須符合 RE2 的語法。http_requests_total[5m]??
Range vector,沒辦法在 Graph 上顯示成曲線圖,一般情況下,會用在 Counter 類型的指標上,并和?rate()?或?irate()?函數(shù)一起使用(注意 rate 和 irate 的區(qū)別)。#?計算的是每秒的平均值,適用于變化很慢的?counter??
#?per-second?average?rate?of?increase,?for?slow-moving?counters??
rate(http_requests_total[5m])??
???
#?計算的是每秒瞬時增加速率,適用于變化很快的?counter??
#?per-second?instant?rate?of?increase,?for?volatile?and?fast-moving?counters??
irate(http_requests_total[5m])??
count、sum、min、max、topk?等 聚合操作,還支持?rate、abs、ceil、floor?等一堆的 內(nèi)置函數(shù),更多的例子,還是上官網(wǎng)學習吧。如果感興趣,我們還可以把 PromQL 和 SQL 做一個對比,會發(fā)現(xiàn) PromQL 語法更簡潔,查詢性能也更高。3.3 HTTP API
GET /api/v1/query GET /api/v1/query_range GET /api/v1/series GET /api/v1/label/ /values GET /api/v1/targets GET /api/v1/rules GET /api/v1/alerts GET /api/v1/targets/metadata GET /api/v1/alertmanagers GET /api/v1/status/config GET /api/v1/status/flags
POST /api/v1/admin/tsdb/snapshot POST /api/v1/admin/tsdb/delete_series POST /api/v1/admin/tsdb/clean_tombstones
四、安裝 Grafana
$?docker?run?-d?-p?3000:3000?grafana/grafana??
http://localhost:3000/?進入 Grafana 的登陸頁面,輸入默認的用戶名和密碼(admin/admin)即可。Name: prometheus Type: Prometheus URL: http://localhost:9090 Access: Browser
/api/datasources/proxy/),由 Grafana 的服務端來訪問數(shù)據(jù)源的 URL,如果數(shù)據(jù)源是部署在內(nèi)網(wǎng),用戶通過瀏覽器無法直接訪問時,這種方式非常有用。
五、使用 Exporter 收集指標
5.1 收集服務器指標
$?wget?https://github.com/prometheus/node_exporter/releases/download/v0.16.0/node_exporter-0.16.0.linux-amd64.tar.gz??
$?tar?xvfz?node_exporter-0.16.0.linux-amd64.tar.gz??
$?cd?node_exporter-0.16.0.linux-amd64??
$?./node_exporter??
/metrics?接口看看是否能正常獲取服務器指標:$?curl?http://localhost:9100/metrics??
scrape_configs?中:scrape_configs:??
??-?job_name:?'prometheus'??
????static_configs:??
??????-?targets:?['192.168.0.107:9090']??
??-?job_name:?'server'??
????static_configs:??
??????-?targets:?['192.168.0.107:9100']??
HUP?信號也可以讓 Prometheus 重新加載配置:$?killall?-HUP?prometheus??
node_load1?觀察服務器負載:node exporter,有很多的面板模板可以直接使用,譬如:Node Exporter Server Metrics 或者 Node Exporter Full 等。我們打開 Grafana 的 Import dashboard 頁面,輸入面板的 URL(https://grafana.com/dashboards/405)或者 ID(405)即可。注意事項
docker?run?-d?\??
??--net="host"?\??
??--pid="host"?\??
??-v?"/:/host:ro,rslave"?\??
??quay.io/prometheus/node-exporter?\??
??--path.rootfs?/host??
5.2 收集 MySQL 指標
$?wget?https://github.com/prometheus/mysqld_exporter/releases/download/v0.11.0/mysqld_exporter-0.11.0.linux-amd64.tar.gz??
$?tar?xvfz?mysqld_exporter-0.11.0.linux-amd64.tar.gz??
$?cd?mysqld_exporter-0.11.0.linux-amd64/??
DATA_SOURCE_NAME,這被稱為 DSN(數(shù)據(jù)源名稱),它必須符合 DSN 的格式,一個典型的 DSN 格式像這樣:user:password@(host:port)/。$?export?DATA_SOURCE_NAME='root:123456@(192.168.0.107:3306)/'??
$?./mysqld_exporter??
~/.my.cnf,或者通過?--config.my-cnf?參數(shù)指定:$?./mysqld_exporter?--config.my-cnf=".my.cnf"??
$?cat?.my.cnf??
[client]??
host=localhost??
port=3306??
user=root??
password=123456??
注意事項
CREATE?USER?'exporter'@'localhost'?IDENTIFIED?BY?'password'?WITH?MAX_USER_CONNECTIONS?3;??
GRANT?PROCESS,?REPLICATION?CLIENT,?SELECT?ON?*.*?TO?'exporter'@'localhost';??
5.3 收集 Nginx 指標
/status/format/json?接口來將 vts 的數(shù)據(jù)格式轉換為 Prometheus 的格式。/status/format/prometheus,這個接口可以直接返回 Prometheus 的格式,從這點這也能看出 Prometheus 的影響力,估計 Nginx VTS exporter 很快就要退役了(TODO:待驗證)。nginx_exporter?通過抓取 Nginx 自帶的統(tǒng)計頁面?/nginx_status?可以獲取一些比較簡單的指標(需要開啟?ngx_http_stub_status_module?模塊);nginx_request_exporter?通過 syslog 協(xié)議 收集并分析 Nginx 的 access log 來統(tǒng)計 HTTP 請求相關的一些指標;nginx-prometheus-shiny-exporter?和?nginx_request_exporter?類似,也是使用 syslog 協(xié)議來收集 access log,不過它是使用 Crystal 語言 寫的。還有?vovolie/lua-nginx-prometheus?基于 Openresty、Prometheus、Consul、Grafana 實現(xiàn)了針對域名和 Endpoint 級別的流量統(tǒng)計。5.4 收集 JMX 指標
$?wget?https://repo1.maven.org/maven2/io/prometheus/jmx/jmx_prometheus_javaagent/0.3.1/jmx_prometheus_javaagent-0.3.1.jar??
-javaagent?參數(shù)來加載:$?java?-javaagent:jmx_prometheus_javaagent-0.3.1.jar=9404:config.yml?-jar?spring-boot-sample-1.0-SNAPSHOT.jar??
config.yml?是 JMX Exporter 的配置文件,它的內(nèi)容可以 參考 JMX Exporter 的配置說明 。$?curl?http://localhost:9404/metrics??
六、告警和通知
6.1 配置告警規(guī)則
global、alerting、rule_files、scrape_config,其中?rule_files?塊就是告警規(guī)則的配置項,alerting 塊用于配置 Alertmanager,這個我們下一節(jié)再看?,F(xiàn)在,先讓我們在?rule_files?塊中添加一個告警規(guī)則文件:rule_files:??
??-?"alert.rules"??
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)"??
InstanceDown?和?APIHighRequestLatency。顧名思義,InstanceDown 表示當實例宕機時(up === 0)觸發(fā)告警,APIHighRequestLatency 表示有一半的 API 請求延遲大于 1s 時(api_http_request_latencies_second{quantile="0.5"} > 1)觸發(fā)告警。http://localhost:9090/rules?可以看到剛剛配置的規(guī)則:
http://localhost:9090/alerts?可以看到根據(jù)配置的規(guī)則生成的告警:
PENDING,這表示已經(jīng)觸發(fā)了告警規(guī)則,但還沒有達到告警條件。這是因為這里配置的?for?參數(shù)是 5m,也就是 5 分鐘后才會觸發(fā)告警,我們等 5 分鐘,可以看到這條 alert 的狀態(tài)變成了?FIRING。6.2 使用 Alertmanager 發(fā)送告警通知
/alerts?頁面可以看到所有的告警,但是還差最后一步:觸發(fā)告警時自動發(fā)送通知。這是由 Alertmanager 來完成的,我們首先 下載并安裝 Alertmanager,和其他 Prometheus 的組件一樣,Alertmanager 也是開箱即用的:$?wget?https://github.com/prometheus/alertmanager/releases/download/v0.15.2/alertmanager-0.15.2.linux-amd64.tar.gz??
$?tar?xvfz?alertmanager-0.15.2.linux-amd64.tar.gz??
$?cd?alertmanager-0.15.2.linux-amd64??
$?./alertmanager??
http://localhost:9093/?來訪問,但是現(xiàn)在還看不到告警,因為我們還沒有把 Alertmanager 配置到 Prometheus 中,我們回到 Prometheus 的配置文件?prometheus.yml,添加下面幾行:alerting:??
??alertmanagers:??
??-?scheme:?http??
????static_configs:??
????-?targets:??
??????-?"192.168.0.107:9093"??
http://192.168.0.107:9093。也可以使用命名行的方式指定 Alertmanager:$?./prometheus?-alertmanager.url=http://192.168.0.107:9093??

alertmanager.ym: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']??
routes:??
-?receiver:?'database-pager'??
??group_wait:?10s??
??match_re:??
????service:?mysql|cassandra??
???
-?receiver:?'frontend-pager'??
??group_by:?[product,?environment]??
??match:??
????team:?frontend??
email_config hipchat_config pagerduty_config pushover_config slack_config opsgenie_config victorops_config wechat_configs webhook_config
email_config?和?webhook_config,另外?wechat_configs?可以支持使用微信來告警,也是相當符合國情的了。七、學習更多
7.1 服務發(fā)現(xiàn)
7.2 告警配置管理
7.3 使用 Pushgateway
總結
附錄:什么是時序數(shù)據(jù)庫?
增:需要頻繁的進行寫操作,而且是按時間排序順序寫入 刪:不需要隨機刪除,一般情況下會直接刪除一個時間區(qū)塊的所有數(shù)據(jù) 改:不需要對寫入的數(shù)據(jù)進行更新 查:需要支持高并發(fā)的讀操作,讀操作是按時間順序升序或降序讀,數(shù)據(jù)量非常大,緩存不起作用
InfluxDB:https://influxdata.com/ Kdb+:http://kx.com/ Graphite:http://graphiteapp.org/ RRDtool:http://oss.oetiker.ch/rrdtool/ OpenTSDB:http://opentsdb.net/ Prometheus:https://prometheus.io/ Druid:http://druid.io/
參考
-?END - 最近熱文
? ?院士拿布袋領獎歸來,朋友圈刷屏了 ? ?32歲清華女教授獲獎百萬走紅后回應:人生第一次因顏值受到關注 ? ?杭州程序員從互聯(lián)網(wǎng)跳央企,曬一天工作和收入,網(wǎng)友:待一年就廢 ? ?字節(jié)跳動P0級事故:實習生刪除GB以下所有模型,差點沒上頭條......
評論
圖片
表情

