Prometheus + Grafana 監控 Linux 和 MySQL 安裝配置 原

一、介紹Prometheus
Prometheus(普羅米修斯)是一套開源的監控&報警&時間序列數據庫的組合,起始是由SoundCloud公司開發的。隨着發展,越來越多公司和組織接受採用Prometheus,社區也十分活躍,他們便將它獨立成開源項目,並且有公司來運作。Google SRE的書內也曾提到跟他們BorgMon監控系統相似的實現是Prometheus。現在最常見的Kubernetes容器管理系統中,通常會搭配Prometheus進行監控。
Prometheus基本原理是通過HTTP協議週期性抓取被監控組件的狀態,這樣做的好處是任意組件只要提供HTTP接口就可以接入監控系統,不需要任何SDK或者其他的集成過程。這樣做非常適合虛擬化環境比如VM或者Docker 。
Prometheus應該是爲數不多的適合Docker、Mesos、Kubernetes環境的監控系統之一。
輸出被監控組件信息的HTTP接口被叫做exporter 。目前互聯網公司常用的組件大部分都有exporter可以直接使用,比如Varnish、Haproxy、Nginx、MySQL、Linux 系統信息 (包括磁盤、內存、CPU、網絡等等),具體支持的源看:https://github.com/prometheus。
與其他監控系統相比,Prometheus的主要特點是:
一個多維數據模型(時間序列由指標名稱定義和設置鍵/值尺寸)。
非常高效的存儲,平均一個採樣數據佔~3.5bytes左右,320萬的時間序列,每30秒採樣,保持60天,消耗磁盤大概228G。
一種靈活的查詢語言。
不依賴分佈式存儲,單個服務器節點。
時間集合通過HTTP上的PULL模型進行。
通過中間網關支持推送時間。
通過服務發現或靜態配置發現目標。
多種模式的圖形和儀表板支持。
二、Prometheus架構概覽
該圖說明了普羅米修斯(Prometheus)及其一些生態系統組件的整體架構:

它的服務過程是這樣的Prometheus daemon負責定時去目標上抓取metrics(指標) 數據,每個抓取目標需要暴露一個http服務的接口給它定時抓取。
Prometheus:支持通過配置文件、文本文件、zookeeper、Consul、DNS SRV lookup等方式指定抓取目標。支持很多方式的圖表可視化,例如十分精美的Grafana,自帶的Promdash,以及自身提供的模版引擎等等,還提供HTTP API的查詢方式,自定義所需要的輸出。
Alertmanager:是獨立於Prometheus的一個組件,可以支持Prometheus的查詢語句,提供十分靈活的報警方式。
PushGateway:這個組件是支持Client主動推送metrics到PushGateway,而Prometheus只是定時去Gateway上抓取數據。
如果有使用過statsd的用戶,則會覺得這十分相似,只是statsd是直接發送給服務器端,而Prometheus主要還是靠進程主動去抓取。
大多數Prometheus組件都是用Go編寫的,它們可以輕鬆地構建和部署爲靜態二進制文件。訪問prometheus.io以獲取完整的文檔,示例和指南。

三、Prometheus的數據模型
Prometheus從根本上所有的存儲都是按時間序列去實現的,相同的metrics(指標名稱) 和label(一個或多個標籤) 組成一條時間序列,不同的label表示不同的時間序列。爲了支持一些查詢,有時還會臨時產生一些時間序列存儲。
metrics name&label指標名稱和標籤
每條時間序列是由唯一的”指標名稱”和一組”標籤(key=value)”的形式組成。
指標名稱:一般是給監測對像起一名字,例如http_requests_total這樣,它有一些命名規則,可以包字母數字_之類的的。通常是以應用名稱開頭_監測對像_數值類型_單位這樣。例如:push_total、userlogin_mysql_duration_seconds、app_memory_usage_bytes。
標籤:就是對一條時間序列不同維度的識別了,例如一個http請求用的是POST還是GET,它的endpoint是什麼,這時候就要用標籤去標記了。最終形成的標識便是這樣了:http_requests_total{method=”POST”,endpoint=”/api/tracks”}。
記住,針對http_requests_total這個metrics name無論是增加標籤還是刪除標籤都會形成一條新的時間序列。
查詢語句就可以跟據上面標籤的組合來查詢聚合結果了。
如果以傳統數據庫的理解來看這條語句,則可以考慮http_requests_total是表名,標籤是字段,而timestamp是主鍵,還有一個float64字段是值了。(Prometheus裏面所有值都是按float64存儲)。

四、Prometheus四種數據類型
Counter
Counter用於累計值,例如記錄請求次數、任務完成數、錯誤發生次數。一直增加,不會減少。重啓進程後,會被重置。
例如:http_response_total{method=”GET”,endpoint=”/api/tracks”} 100,10秒後抓取http_response_total{method=”GET”,endpoint=”/api/tracks”} 100。
Gauge:Gauge常規數值,例如 溫度變化、內存使用變化。可變大,可變小。重啓進程後,會被重置。
例如: memory_usage_bytes{host=”master-01″} 100 < 抓取值、memory_usage_bytes{host=”master-01″} 30、memory_usage_bytes{host=”master-01″} 50、memory_usage_bytes{host=”master-01″} 80 < 抓取值。
Histogram:Histogram(直方圖)可以理解爲柱狀圖的意思,常用於跟蹤事件發生的規模,例如:請求耗時、響應大小。它特別之處是可以對記錄的內容進行分組,提供count和sum全部值的功能。
例如:{小於10=5次,小於20=1次,小於30=2次},count=7次,sum=7次的求和值。
Summary:Summary和Histogram十分相似,常用於跟蹤事件發生的規模,例如:請求耗時、響應大小。同樣提供 count 和 sum 全部值的功能。
例如:count=7次,sum=7次的值求值。
它提供一個quantiles的功能,可以按%比劃分跟蹤的結果。例如:quantile取值0.95,表示取採樣值裏面的95%數據。

五、安裝運行Prometheus(二進制版)

1、首先安裝GO
在聯網環境下可以直接通過yum命令安裝,在內網下則需要下載對應的rpm安裝包再安裝。
yum install go
go version
[root@mysqlnode05 go]# ls
golang-1.8.3-1.el7.x86_64.rpm  golang-bin-1.8.3-1.el7.x86_64.rpm  golang-src-1.8.3-1.el7.noarch.rpm
[root@mysqlnode05 go]# yum localinstall golang-1.8.3-1.el7.x86_64.rpm  golang-bin-1.8.3-1.el7.x86_64.rpm  golang-src-1.8.3-1.el7.noarch.rpm
Loaded plugins: fastestmirror
Examining golang-1.8.3-1.el7.x86_64.rpm: golang-1.8.3-1.el7.x86_64
......
Installed:
  golang.x86_64 0:1.8.3-1.el7          golang-bin.x86_64 0:1.8.3-1.el7          golang-src.noarch 0:1.8.3-1.el7

Complete!
[root@mysqlnode05 go]# go version
go version go1.8.3 linux/amd64

2、解壓安裝包
[root@mysqlnode05 prometheus]# gtar -zxf prometheus-2.0.0.linux-amd64.tar.gz  -C /usr/local/
[root@mysqlnode05 prometheus]# cd /usr/local/prometheus-2.0.0.linux-amd64/
[root@mysqlnode05 prometheus-2.0.0.linux-amd64]# ls
console_libraries  consoles  LICENSE  NOTICE  prometheus  prometheus.yml  promtool

3、修改prometheus.yml文件,配置targets
其中的IP和端口則是對應的exporter的監聽端口
[root@mysqlnode05 prometheus-2.0.0.linux-amd64]# 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=<job_name>` to any timeseries scraped from this config.
  - job_name: 'prometheus'

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
      - targets: ['localhost:9090']
        labels:
          instance: prometheus
  - job_name: linux
    static_configs:
      - targets: ['192.168.0.225:9100']
        labels:
          instance: db1

  - job_name: mysql
    static_configs:
      - targets: ['192.168.0.222:9104']
        labels:
          instance: db1

4、啓動並通過web訪問
[root@mysqlnode05 prometheus-2.0.0.linux-amd64]# ./prometheus  --config.file=prometheus.yml
level=info ts=2017-11-29T10:57:31.908935485Z caller=main.go:215 msg="Starting Prometheus" version="(version=2.0.0, branch=HEAD, revision=0a74f98628a0463dddc90528220c94de5032d1a0)"
level=info ts=2017-11-29T10:57:31.909287113Z caller=main.go:216 build_context="(go=go1.9.2, user=root@615b82cb36b6, date=20171108-07:11:59)"
......
level=info ts=2017-11-29T10:57:31.914852164Z caller=web.go:380 component=web msg="Start listening for connections" address=0.0.0.0:9090
Prometheus內置了一個web界面,我們可通過http://monitor_host:9090進行訪問:

六、安裝MySQLD和Node exporter

1、安裝mysqld_exporter
    #安裝go
    yum localinstall golang-1.8.3-1.el7.x86_64.rpm  golang-bin-1.8.3-1.el7.x86_64.rpm  golang-src-1.8.3-1.el7.noarch.rpm
    #解壓
[root@mysqlnode02 ~]# gtar -zxf /rpmpackages/mysqld_exporter-0.10.0.linux-amd64.tar.gz  -C /usr/local/
[root@mysqlnode02 ~]# cd /usr/local/mysqld_exporter-0.10.0.linux-amd64/
[root@mysqlnode02 mysqld_exporter-0.10.0.linux-amd64]# ls
LICENSE  mysqld_exporter  NOTICE
#創建專用用戶
mysql> create user 'mysql_prome'@'localhost' identified by 'Aa123456789';
mysql> grant REPLICATION CLIENT,PROCESS ON *.* TO 'mysql_prome'@'localhost';
mysql> grant SELECT ON *.* TO 'mysql_prome'@'localhost';
mysql> flush privileges;
#創建exporter配置文件
[root@mysqlnode02 mysqld_exporter-0.10.0.linux-amd64]# vi .my.cnf
[root@mysqlnode02 mysqld_exporter-0.10.0.linux-amd64]# cat .my.cnf
[client]
user=mysql_prome
password=Aa123456789
#啓動mysqld_exporter
[root@mysqlnode02 mysqld_exporter-0.10.0.linux-amd64]# ./mysqld_exporter -config.my-cnf .my.cnf  >> mysqld_exporter.log 2>&1 &
[1] 60231
[root@mysqlnode02 mysqld_exporter-0.10.0.linux-amd64]# netstat -nlap | grep 9104
tcp6       0      0 :::9104                 :::*                    LISTEN      60231/./mysqld_expo

2、安裝node_exporter
    #安裝go
    yum localinstall golang-1.8.3-1.el7.x86_64.rpm  golang-bin-1.8.3-1.el7.x86_64.rpm  golang-src-1.8.3-1.el7.noarch.rpm
    #解壓
[root@mysqlnode05 prometheus-2.0.0.linux-amd64]# gtar -zxf /rpmpackages/prometheus/node_exporter-0.15.1.linux-amd64.tar.gz   -C /usr/local/
[root@mysqlnode05 prometheus-2.0.0.linux-amd64]# cd /usr/local/node_exporter-0.15.1.linux-amd64/
[root@mysqlnode05 node_exporter-0.15.1.linux-amd64]# ls
LICENSE  node_exporter  NOTICE
#啓動node_exporter
[root@mysqlnode05 node_exporter-0.15.1.linux-amd64]# ./node_exporter >>  node_exporter.log 2>&1 &
[1] 39483
[root@mysqlnode05 prometheus-2.0.0.linux-amd64]# netstat -nlap | grep 9100
tcp6       0      0 :::9100                 :::*                    LISTEN      39483/./node_export

3、通過prometheus的web界面查看任務項均處於UP狀態

4、安裝grafana
參照《grafana安裝配置》

七、配置通過grafana監控服務和MySQL數據庫

#編輯配置文件/etc/grafana/grafana.ini,修改dashboards.json段落下兩個參數的值:
[root@mysqlnode04 ~]# vi /etc/grafana/grafana.ini
[dashboards.json]
enabled = true
path = /var/lib/grafana/dashboards
#安裝儀表盤(Percona提供)可以在聯網的環境中先git clone再複製到內網環境
git clone https://github.com/percona/grafana-dashboards.git
[root@mysqlnode04 ~]# cp -r /rpmpackages/grafana-dashboards/dashboards  /var/lib/grafana/
#爲Grafana打補丁
[root@mysqlnode04 ~]# sed -i 's/expr=\(.\)\.replace(\(.\)\.expr,\(.\)\.scopedVars\(.*\)var \(.\)=\(.\)\.interval/expr=\1.replace(\2.expr,\3.scopedVars\4var \5=\1.replace(\6.interval, \3.scopedVars)/' /usr/share/grafana/public/app/plugins/datasource/prometheus/datasource.ts
[root@mysqlnode04 ~]# sed -i 's/,range_input/.replace(\/"{\/g,"\\"").replace(\/}"\/g,"\\""),range_input/; s/step_input:""/step_input:this.target.step/' /usr/share/grafana/public/app/plugins/datasource/prometheus/query_ctrl.ts
#重新加載啓動Grafana服務
[root@mysqlnode04 ~]# systemctl daemon-reload
[root@mysqlnode04 ~]# systemctl start grafana-server
[root@mysqlnode04 ~]# systemctl status grafana-server
● grafana-server.service - Grafana instance
   Loaded: loaded (/usr/lib/systemd/system/grafana-server.service; disabled; vendor preset: disabled)
   Active: active (running) since Thu 2017-11-30 09:46:03 CST; 12s ago

八、通過web界面查看監控情況

如果只是想監控MySQL或MongoDB,那麼請使用PMM(Percona Monitoring and Management)監控工具,在Prometheus+Granafa基礎之上添加了慢查詢收集等額外功能,更專業的MySQL&MongoDB監控系統。完結。。。

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