亚洲国产精品无码久久大片,亚洲AV无码乱码麻豆精品国产,亚洲品质自拍网站,少妇伦子伦精品无码STYLES,国产精久久久久久久

號稱(chēng)下一代監控系統!來(lái)看看它有多牛逼

優(yōu)采云 發(fā)布時(shí)間: 2022-04-30 15:24

  號稱(chēng)下一代監控系統!來(lái)看看它有多牛逼

  我們不僅僅可以在 Prometheus 的 Graph 頁(yè)面查詢(xún) PromQL,Prometheus 還提供了一種 HTTP API 的方式,可以更靈活的將 PromQL 整合到其他系統中使用,譬如下面要介紹的 Grafana,就是通過(guò) Prometheus 的 HTTP API 來(lái)查詢(xún)指標數據的。實(shí)際上,我們在 Prometheus 的 Graph 頁(yè)面查詢(xún)也是使用了 HTTP API。

  我們看下 Prometheus 的 HTTP API 官方文檔,它提供了下面這些接口:

  GET?/api/v1/query<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />GET?/api/v1/query_range<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />GET?/api/v1/series<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />GET?/api/v1/label//values<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />GET?/api/v1/targets<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />GET?/api/v1/rules<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />GET?/api/v1/alerts<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />GET?/api/v1/targets/metadata<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />GET?/api/v1/alertmanagers<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />GET?/api/v1/status/config<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />GET?/api/v1/status/flags<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />

  從 Prometheus v2.1 開(kāi)始,又新增了幾個(gè)用于管理 TSDB 的接口:

  POST?/api/v1/admin/tsdb/snapshot<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />POST?/api/v1/admin/tsdb/delete_series<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />POST?/api/v1/admin/tsdb/clean_tombstones<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />

  四、安裝 Grafana

  雖然 Prometheus 提供的 Web UI 也可以很好的查看不同指標的視圖,但是這個(gè)功能非常簡(jiǎn)單,只適合用來(lái)調試。要實(shí)現一個(gè)強大的監控系統,還需要一個(gè)能定制展示不同指標的面板,能支持不同類(lèi)型的展現方式(曲線(xiàn)圖、餅狀圖、熱點(diǎn)圖、TopN 等),這就是儀表盤(pán)(Dashboard)功能。

  因此 Prometheus 開(kāi)發(fā)了一套儀表盤(pán)系統 PromDash,不過(guò)很快這套系統就被廢棄了,官方開(kāi)始推薦使用 Grafana 來(lái)對 Prometheus 的指標數據進(jìn)行可視化,這不僅是因為 Grafana 的功能非常強大,而且它和 Prometheus 可以完美的無(wú)縫融合。

  Grafana 是一個(gè)用于可視化大型測量數據的開(kāi)源系統,它的功能非常強大,界面也非常漂亮,使用它可以創(chuàng )建自定義的控制面板,你可以在面板中配置要顯示的數據和顯示方式,它 支持很多不同的數據源,比如:Graphite、InfluxDB、OpenTSDB、Elasticsearch、Prometheus 等,而且它也 支持眾多的插件。

  下面我們就體驗下使用 Grafana 來(lái)展示 Prometheus 的指標數據。首先我們來(lái)安裝 Grafana,我們使用最簡(jiǎn)單的 Docker 安裝方式:

  $?docker?run?-d?-p?3000:3000?grafana/grafana??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />

  運行上面的 docker 命令,Grafana 就安裝好了!你也可以采用其他的安裝方式,參考 官方的安裝文檔。安裝完成之后,我們訪(fǎng)問(wèn) :3000/ 進(jìn)入 Grafana 的登陸頁(yè)面,輸入默認的用戶(hù)名和密碼(admin/admin)即可。

  要使用 Grafana,第一步當然是要配置數據源,告訴 Grafana 從哪里取數據,我們點(diǎn)擊 Add data source 進(jìn)入數據源的配置頁(yè)面:

  

  我們在這里依次填上:

  要注意的是,這里的 Access 指的是 Grafana 訪(fǎng)問(wèn)數據源的方式,有 Browser 和 Proxy 兩種方式。Browser 方式表示當用戶(hù)訪(fǎng)問(wèn) Grafana 面板時(shí),瀏覽器直接通過(guò) URL 訪(fǎng)問(wèn)數據源的;而 Proxy 方式表示瀏覽器先訪(fǎng)問(wèn) Grafana 的某個(gè)代理接口(接口地址是 /api/datasources/proxy/),由 Grafana 的服務(wù)端來(lái)訪(fǎng)問(wèn)數據源的 URL,如果數據源是部署在內網(wǎng),用戶(hù)通過(guò)瀏覽器無(wú)法直接訪(fǎng)問(wèn)時(shí),這種方式非常有用。

  配置好數據源,Grafana 會(huì )默認提供幾個(gè)已經(jīng)配置好的面板供你使用,如下圖所示,默認提供了三個(gè)面板:Prometheus Stats、Prometheus 2.0 Stats 和 Grafana metrics。點(diǎn)擊 Import 就可以導入并使用該面板。

  我們導入 Prometheus 2.0 Stats 這個(gè)面板,可以看到下面這樣的監控面板。如果你的公司有條件,可以申請個(gè)大顯示器掛在墻上,將這個(gè)面板投影在大屏上,實(shí)時(shí)觀(guān)察線(xiàn)上系統的狀態(tài),可以說(shuō)是非常 cool 的。

  五、使用 Exporter 收集指標

  目前為止,我們看到的都還只是一些沒(méi)有實(shí)際用途的指標,如果我們要在我們的生產(chǎn)環(huán)境真正使用 Prometheus,往往需要關(guān)注各種各樣的指標,譬如服務(wù)器的 CPU負載、內存占用量、IO開(kāi)銷(xiāo)、入網(wǎng)和出網(wǎng)流量等等。

  正如上面所說(shuō),Prometheus 是使用 Pull 的方式來(lái)獲取指標數據的,要讓 Prometheus 從目標處獲得數據,首先必須在目標上安裝指標收集的程序,并暴露出 HTTP 接口供 Prometheus 查詢(xún),這個(gè)指標收集程序被稱(chēng)為 Exporter,不同的指標需要不同的 Exporter 來(lái)收集,目前已經(jīng)有大量的 Exporter 可供使用,幾乎囊括了我們常用的各種系統和軟件。

  官網(wǎng)列出了一份 常用 Exporter 的清單,各個(gè) Exporter 都遵循一份端口約定,避免端口沖突,即從 9100 開(kāi)始依次遞增,這里是 完整的 Exporter 端口列表。另外值得注意的是,有些軟件和系統無(wú)需安裝 Exporter,這是因為他們本身就提供了暴露 Prometheus 格式的指標數據的功能,比如 Kubernetes、Grafana、Etcd、Ceph 等。

  這一節就讓我們來(lái)收集一些有用的數據。

  5.1 收集服務(wù)器指標

  首先我們來(lái)收集服務(wù)器的指標,這需要安裝 node_exporter,這個(gè) exporter 用于收集 *NIX 內核的系統,如果你的服務(wù)器是 Windows,可以使用 WMI exporter。

  和 Prometheus server 一樣,node_exporter 也是開(kāi)箱即用的:

  $?wget?https://github.com/prometheus/node_exporter/releases/download/v0.16.0/node_exporter-0.16.0.linux-amd64.tar.gz??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />$?tar?xvfz?node_exporter-0.16.0.linux-amd64.tar.gz??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />$?cd?node_exporter-0.16.0.linux-amd64??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />$?./node_exporter??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />

  node_exporter 啟動(dòng)之后,我們訪(fǎng)問(wèn)下 /metrics 接口看看是否能正常獲取服務(wù)器指標:

  $?curl?http://localhost:9100/metrics??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />

  如果一切 OK,我們可以修改 Prometheus 的配置文件,將服務(wù)器加到 scrape_configs 中:

  scrape_configs:??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />??-?job_name:?'prometheus'??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />????static_configs:??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />??????-?targets:?['192.168.0.107:9090']??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />??-?job_name:?'server'??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />????static_configs:??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />??????-?targets:?['192.168.0.107:9100']??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />

  修改配置后,需要重啟 Prometheus 服務(wù),或者發(fā)送 HUP 信號也可以讓 Prometheus 重新加載配置:

  $?killall?-HUP?prometheus??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />

  在 Prometheus Web UI 的 Status -&gt; Targets 中,可以看到新加的服務(wù)器:

  在 Graph 頁(yè)面的指標下拉框可以看到很多名稱(chēng)以 node 開(kāi)頭的指標,譬如我們輸入node_load1觀(guān)察服務(wù)器負載:

  

  如果想在 Grafana 中查看服務(wù)器的指標,可以在 Grafana 的 Dashboards 頁(yè)面 搜索 node exporter,有很多的面板模板可以直接使用,譬如:Node Exporter Server Metrics 或者 Node Exporter Full 等。我們打開(kāi) Grafana 的 Import dashboard 頁(yè)面,輸入面板的 URL()或者 ID(405)即可。

  

  注意事項

  一般情況下,node_exporter 都是直接運行在要收集指標的服務(wù)器上的,官方不推薦用 Docker 來(lái)運行 node_exporter。如果逼不得已一定要運行在 Docker 里,要特別注意,這是因為 Docker 的文件系統和網(wǎng)絡(luò )都有自己的 namespace,收集的數據并不是宿主機真實(shí)的指標??梢允褂靡恍┳兺ǖ姆椒?,比如運行 Docker 時(shí)加上下面這樣的參數:

  docker?run?-d?\??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />??--net="host"?\??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />??--pid="host"?\??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />??-v?"/:/host:ro,rslave"?\??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />??quay.io/prometheus/node-exporter?\??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />??--path.rootfs?/host??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />

  關(guān)于 node_exporter 的更多信息,可以參考 node_exporter 的文檔 和 Prometheus 的官方指南 Monitoring Linux host metrics with the Node Exporter。

  5.2 收集 MySQL 指標

  mysqld_exporter 是 Prometheus 官方提供的一個(gè) exporter,我們首先 下載最新版本 并解壓(開(kāi)箱即用):

  $?wget?https://github.com/prometheus/mysqld_exporter/releases/download/v0.11.0/mysqld_exporter-0.11.0.linux-amd64.tar.gz??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />$?tar?xvfz?mysqld_exporter-0.11.0.linux-amd64.tar.gz??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />$?cd?mysqld_exporter-0.11.0.linux-amd64/??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />

  mysqld_exporter 需要連接到 mysqld 才能收集它的指標,可以通過(guò)兩種方式來(lái)設置 mysqld 數據源。第一種是通過(guò)環(huán)境變量 DATA_SOURCE_NAME,這被稱(chēng)為 DSN(數據源名稱(chēng)),它必須符合 DSN 的格式,一個(gè)典型的 DSN 格式像這樣:user:password@(host:port)/。

  $?export?DATA_SOURCE_NAME='root:123456@(192.168.0.107:3306)/'??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />$?./mysqld_exporter??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />

  另一種方式是通過(guò)配置文件,默認的配置文件是 ~/.f,或者通過(guò) --config.my-cnf 參數指定:

  $?./mysqld_exporter?--config.my-cnf=".my.cnf"??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />

  配置文件的格式如下:

  $?cat?.my.cnf??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />[client]??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />host=localhost??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />port=3306??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />user=root??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />password=123456??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />

  如果要把 MySQL 的指標導入 Grafana,可以參考 這些 Dashboard JSON。

  注意事項

  這里為簡(jiǎn)單起見(jiàn),在 mysqld_exporter 中直接使用了 root 連接數據庫,在真實(shí)環(huán)境中,可以為 mysqld_exporter 創(chuàng )建一個(gè)單獨的用戶(hù),并賦予它受限的權限(PROCESS、REPLICATION CLIENT、SELECT),最好還限制它的最大連接數(MAX_USER_CONNECTIONS)。

  CREATE?USER?'exporter'@'localhost'?IDENTIFIED?BY?'password'?WITH?MAX_USER_CONNECTIONS?3;??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />GRANT?PROCESS,?REPLICATION?CLIENT,?SELECT?ON?*.*?TO?'exporter'@'localhost';??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />

  5.3 收集 Nginx 指標

  官方提供了兩種收集 Nginx 指標的方式。

  不過(guò),在 nginx-module-vts 最新的版本中增加了一個(gè)新接口:/status/format/prometheus,這個(gè)接口可以直接返回 Prometheus 的格式,從這點(diǎn)這也能看出 Prometheus 的影響力,估計 Nginx VTS exporter 很快就要退役了(TODO:待驗證)。

  除此之外,還有很多其他的方式來(lái)收集 Nginx 的指標,比如:nginx_exporter 通過(guò)抓取 Nginx 自帶的統計頁(yè)面 /nginx_status 可以獲取一些比較簡(jiǎn)單的指標(需要開(kāi)啟 ngx_http_stub_status_module 模塊);nginx_request_exporter 通過(guò) syslog 協(xié)議 收集并分析 Nginx 的 access log 來(lái)統計 HTTP 請求相關(guān)的一些指標;nginx-prometheus-shiny-exporter 和 nginx_request_exporter 類(lèi)似,也是使用 syslog 協(xié)議來(lái)收集 access log,不過(guò)它是使用 Crystal 語(yǔ)言 寫(xiě)的。還有 vovolie/lua-nginx-prometheus 基于 Openresty、Prometheus、Consul、Grafana 實(shí)現了針對域名和 Endpoint 級別的流量統計。

  有需要或感興趣的同學(xué)可以對照說(shuō)明文檔自己安裝體驗下,這里就不一一嘗試了。

  5.4 收集 JMX 指標

  最后讓我們來(lái)看下如何收集 Java 應用的指標,Java 應用的指標一般是通過(guò) JMX(Java Management Extensions) 來(lái)獲取的,顧名思義,JMX 是管理 Java 的一種擴展,它可以方便的管理和監控正在運行的 Java 程序。

  JMX Exporter 用于收集 JMX 指標,很多使用 Java 的系統,都可以使用它來(lái)收集指標,比如:Kafaka、Cassandra 等。首先我們下載 JMX Exporter:

  $?wget?https://repo1.maven.org/maven2/io/prometheus/jmx/jmx_prometheus_javaagent/0.3.1/jmx_prometheus_javaagent-0.3.1.jar??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />

  JMX Exporter 是一個(gè) Java Agent 程序,在運行 Java 程序時(shí)通過(guò) -javaagent 參數來(lái)加載:

  $?java?-javaagent:jmx_prometheus_javaagent-0.3.1.jar=9404:config.yml?-jar?spring-boot-sample-1.0-SNAPSHOT.jar??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />

  其中,9404 是 JMX Exporter 暴露指標的端口,config.yml 是 JMX Exporter 的配置文件,它的內容可以 參考 JMX Exporter 的配置說(shuō)明 。然后檢查下指標數據是否正確獲?。?/p>

  $?curl?http://localhost:9404/metrics??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />

  六、告警和通知

  至此,我們能收集大量的指標數據,也能通過(guò)強大而美觀(guān)的面板展示出來(lái)。不過(guò)作為一個(gè)監控系統,最重要的功能,還是應該能及時(shí)發(fā)現系統問(wèn)題,并及時(shí)通知給系統負責人,這就是 Alerting(告警)。

  Prometheus 的告警功能被分成兩部分:一個(gè)是告警規則的配置和檢測,并將告警發(fā)送給 Alertmanager,另一個(gè)是 Alertmanager,它負責管理這些告警,去除重復數據,分組,并路由到對應的接收方式,發(fā)出報警。常見(jiàn)的接收方式有:Email、PagerDuty、HipChat、Slack、OpsGenie、WebHook 等。

  6.1 配置告警規則

  我們在上面介紹 Prometheus 的配置文件時(shí)了解到,它的默認配置文件 prometheus.yml 有四大塊:global、alerting、rule_files、scrape_config,其中 rule_files 塊就是告警規則的配置項,alerting 塊用于配置 Alertmanager,這個(gè)我們下一節再看?,F在,先讓我們在 rule_files 塊中添加一個(gè)告警規則文件:

  rule_files:??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />??-?"alert.rules"??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />

  然后參考 官方文檔,創(chuàng )建一個(gè)告警規則文件 alert.rules:

  groups:??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />-?name:?example??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />??rules:??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />???<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />??#?Alert?for?any?instance?that?is?unreachable?for?>5?minutes.??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />??-?alert:?InstanceDown??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />????expr:?up?==?0??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />????for:?5m??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />????labels:??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />??????severity:?page??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />????annotations:??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />??????summary:?"Instance?{{?$labels.instance?}}?down"??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />??????description:?"{{?$labels.instance?}}?of?job?{{?$labels.job?}}?has?been?down?for?more?than?5?minutes."??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />???<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />??#?Alert?for?any?instance?that?has?a?median?request?latency?>1s.??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />??-?alert:?APIHighRequestLatency??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />????expr:?api_http_request_latencies_second{quantile="0.5"}?>?1??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />????for:?10m??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />????annotations:??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />??????summary:?"High?request?latency?on?{{?$labels.instance?}}"??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />??????description:?"{{?$labels.instance?}}?has?a?median?request?latency?above?1s?(current?value:?{{?$value?}}s)"??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />

  這個(gè)規則文件里,包含了兩條告警規則:InstanceDown 和 APIHighRequestLatency。顧名思義,InstanceDown 表示當實(shí)例宕機時(shí)(up === 0)觸發(fā)告警,APIHighRequestLatency 表示有一半的 API 請求延遲大于 1s 時(shí)(api_http_request_latencies_second{quantile="0.5"} &gt; 1)觸發(fā)告警。

  配置好后,需要重啟下 Prometheus server,然后訪(fǎng)問(wèn) :9090/rules 可以看到剛剛配置的規則:

  訪(fǎng)問(wèn) :9090/alerts 可以看到根據配置的規則生成的告警:

  

  這里我們將一個(gè)實(shí)例停掉,可以看到有一條 alert 的狀態(tài)是 PENDING,這表示已經(jīng)觸發(fā)了告警規則,但還沒(méi)有達到告警條件。這是因為這里配置的 for 參數是 5m,也就是 5 分鐘后才會(huì )觸發(fā)告警,我們等 5 分鐘,可以看到這條 alert 的狀態(tài)變成了 FIRING。

  6.2 使用 Alertmanager 發(fā)送告警通知

  雖然 Prometheus 的 /alerts 頁(yè)面可以看到所有的告警,但是還差最后一步:觸發(fā)告警時(shí)自動(dòng)發(fā)送通知。這是由 Alertmanager 來(lái)完成的,我們首先 下載并安裝 Alertmanager,和其他 Prometheus 的組件一樣,Alertmanager 也是開(kāi)箱即用的:

  $?wget?https://github.com/prometheus/alertmanager/releases/download/v0.15.2/alertmanager-0.15.2.linux-amd64.tar.gz??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />$?tar?xvfz?alertmanager-0.15.2.linux-amd64.tar.gz??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />$?cd?alertmanager-0.15.2.linux-amd64??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />$?./alertmanager??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />

  Alertmanager 啟動(dòng)后默認可以通過(guò) :9093/ 來(lái)訪(fǎng)問(wèn),但是現在還看不到告警,因為我們還沒(méi)有把 Alertmanager 配置到 Prometheus 中,我們回到 Prometheus 的配置文件 prometheus.yml,添加下面幾行:

  alerting:??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />??alertmanagers:??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />??-?scheme:?http??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />????static_configs:??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />????-?targets:??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />??????-?"192.168.0.107:9093"??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />

  這個(gè)配置告訴 Prometheus,當發(fā)生告警時(shí),將告警信息發(fā)送到 Alertmanager,Alertmanager 的地址為 :9093。也可以使用命名行的方式指定 Alertmanager:

  $?./prometheus?-alertmanager.url=http://192.168.0.107:9093??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />

  這個(gè)時(shí)候再訪(fǎng)問(wèn) Alertmanager,可以看到 Alertmanager 已經(jīng)接收到告警了:

  

  下面的問(wèn)題就是如何讓 Alertmanager 將告警信息發(fā)送給我們了,我們打開(kāi)默認的配置文件 alertmanager.ym:

  global:??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />??resolve_timeout:?5m??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />???<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />route:??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />??group_by:?['alertname']??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />??group_wait:?10s??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />??group_interval:?10s??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />??repeat_interval:?1h??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />??receiver:?'web.hook'??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />receivers:??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />-?name:?'web.hook'??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />??webhook_configs:??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />??-?url:?'http://127.0.0.1:5001/'??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />inhibit_rules:??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />??-?source_match:??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />??????severity:?'critical'??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />????target_match:??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />??????severity:?'warning'??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />????equal:?['alertname',?'dev',?'instance']??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />

  其中 global 塊表示一些全局配置;route 塊表示通知路由,可以根據不同的標簽將告警通知發(fā)送給不同的 receiver,這里沒(méi)有配置 routes 項,表示所有的告警都發(fā)送給下面定義的 web.hook 這個(gè) receiver;如果要配置多個(gè)路由,可以參考 這個(gè)例子:

  routes:??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />-?receiver:?'database-pager'??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />??group_wait:?10s??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />??match_re:??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />????service:?mysql|cassandra??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />???<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />-?receiver:?'frontend-pager'??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />??group_by:?[product,?environment]??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />??match:??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />????team:?frontend??<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />

  緊接著(zhù),receivers 塊表示告警通知的接收方式,每個(gè) receiver 包含一個(gè) name 和一個(gè) xxx_configs,不同的配置代表了不同的接收方式,Alertmanager 內置了下面這些接收方式:

  email_config<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />hipchat_config<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />pagerduty_config<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />pushover_config<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />slack_config<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />opsgenie_config<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />victorops_config<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />wechat_configs<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />webhook_config<br style="outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;" />

  雖然接收方式很豐富,但是在國內,其中大多數接收方式都很少使用。最常用到的,莫屬 email_config 和 webhook_config,另外 wechat_configs 可以支持使用微信來(lái)告警,也是相當符合國情的了。

  其實(shí)告警的通知方式是很難做到面面俱到的,因為消息軟件各種各樣,每個(gè)國家還可能不同,不可能完全覆蓋到,所以 Alertmanager 已經(jīng)決定不再添加新的 receiver 了,而是推薦使用 webhook 來(lái)集成自定義的接收方式??梢詤⒖?這些集成的例子,譬如 將釘釘接入 Prometheus AlertManager WebHook。

  七、學(xué)習更多

  到這里,我們已經(jīng)學(xué)習了 Prometheus 的大多數功能,結合 Prometheus + Grafana + Alertmanager 完全可以搭建一套非常完整的監控系統。不過(guò)在真正使用時(shí),我們會(huì )發(fā)現更多的問(wèn)題。

  7.1 服務(wù)發(fā)現

  由于 Prometheus 是通過(guò) Pull 的方式主動(dòng)獲取監控數據,所以需要手工指定監控節點(diǎn)的列表,當監控的節點(diǎn)增多之后,每次增加節點(diǎn)都需要更改配置文件,非常麻煩,這個(gè)時(shí)候就需要通過(guò)服務(wù)發(fā)現(service discovery,SD)機制去解決。

  Prometheus 支持多種服務(wù)發(fā)現機制,可以自動(dòng)獲取要收集的 targets,可以參考 這里,包含的服務(wù)發(fā)現機制包括:azure、consul、dns、ec2、openstack、file、gce、kubernetes、marathon、triton、zookeeper(nerve、serverset),配置方法可以參考手冊的 Configuration 頁(yè)面??梢哉f(shuō) SD 機制是非常豐富的,但目前由于開(kāi)發(fā)資源有限,已經(jīng)不再開(kāi)發(fā)新的 SD 機制,只對基于文件的 SD 機制進(jìn)行維護。

  關(guān)于服務(wù)發(fā)現網(wǎng)上有很多教程,譬如 Prometheus 官方博客中這篇文章 Advanced Service Discovery in Prometheus 0.14.0 對此有一個(gè)比較系統的介紹,全面的講解了 relabeling 配置,以及如何使用 DNS-SRV、Consul 和文件來(lái)做服務(wù)發(fā)現。

  另外,官網(wǎng)還提供了 一個(gè)基于文件的服務(wù)發(fā)現的入門(mén)例子,Julius Volz 寫(xiě)的 Prometheus workshop 入門(mén)教程中也 使用了 DNS-SRV 來(lái)當服務(wù)發(fā)現。

  7.2 告警配置管理

  無(wú)論是 Prometheus 的配置還是 Alertmanager 的配置,都沒(méi)有提供 API 供我們動(dòng)態(tài)的修改。一個(gè)很常見(jiàn)的場(chǎng)景是,我們需要基于 Prometheus 做一套可自定義規則的告警系統,用戶(hù)可根據自己的需要在頁(yè)面上創(chuàng )建修改或刪除告警規則,或者是修改告警通知方式和聯(lián)系人,正如在 Prometheus Google Groups 里的 這個(gè)用戶(hù)的問(wèn)題:How to dynamically add alerts rules in rules.conf and prometheus yml file via API or something?

  不過(guò)遺憾的是,Simon Pasquier 在下面說(shuō)到,目前并沒(méi)有這樣的 API,而且以后也沒(méi)有這樣的計劃來(lái)開(kāi)發(fā)這樣的 API,因為這樣的功能更應該交給譬如 Puppet、Chef、Ansible、Salt 這樣的配置管理系統。

  7.3 使用 Pushgateway

  Pushgateway 主要用于收集一些短期的 jobs,由于這類(lèi) jobs 存在時(shí)間較短,可能在 Prometheus 來(lái) Pull 之前就消失了。官方對 什么時(shí)候該使用 Pushgateway 有一個(gè)很好的說(shuō)明。

  總結

  最近兩年 Prometheus 的發(fā)展非常迅速,社區也非?;钴S,國內研究 Prometheus 的人也越來(lái)越多。隨著(zhù)微服務(wù),DevOps,云計算,云原生等概念的普及,越來(lái)越多的企業(yè)開(kāi)始使用 Docker 和 Kubernetes 來(lái)構建自己的系統和應用,像 Nagios 和 Cacti 這樣的老牌監控系統會(huì )變得越來(lái)越不適用,相信 Prometheus 最終會(huì )發(fā)展成一個(gè)最適合云環(huán)境的監控系統。

  干貨分享<p style="margin: 10px;outline: 0px;max-width: 100%;color: rgb(63, 63, 63);line-height: 1.6;box-sizing: border-box !important;overflow-wrap: break-word !important;">最近將個(gè)人學(xué)習筆記整理成冊,使用PDF分享。關(guān)注我,回復如下代碼,即可獲得百度盤(pán)地址,無(wú)套路領(lǐng)??!

  ?001:《Java并發(fā)與高并發(fā)解決方案》學(xué)習筆記;?002:《深入JVM內核——原理、診斷與優(yōu)化》學(xué)習筆記;?003:《Java面試寶典》?004:《Docker開(kāi)源書(shū)》?005:《Kubernetes開(kāi)源書(shū)》?006:《DDD速成(領(lǐng)域驅動(dòng)設計速成)》?007:全部?008:加技術(shù)群討論加個(gè)關(guān)注不迷路

  喜歡就點(diǎn)個(gè)"在看"唄^_^</p>

0 個(gè)評論

要回復文章請先登錄注冊


官方客服QQ群

微信人工客服

QQ人工客服


線(xiàn)

亚洲国产精品无码久久大片,亚洲AV无码乱码麻豆精品国产,亚洲品质自拍网站,少妇伦子伦精品无码STYLES,国产精久久久久久久