利用prometheus, Grafana, postgres

分類:IT技術 時間:2017-01-23
prometheus是一個優秀的服務樹監控組件,
Grafana 是一個圖形化展示界面
postgres_exporter 是一個postgres服務器狀態收集組件,服務於prometheus,
prometheus 官方提供了mysql的服務器狀態的手機組件,postgres的卻沒有提供,好在擁有萬能的github
 
[ https://github.com/wrouesnel/postgres_exporter](https://github.com/wrouesnel/postgres_exporter)

安裝流程:

## 安裝prometheus
下載prometheus, 可以到[官網](https://prometheus.io/download/)下載編譯好的二進制文件,也可以到github 下載源碼自己編譯:

```
wget  https://github.com/prometheus/prometheus/releases/download/v1.4.1/prometheus-1.4.1.linux-amd64.tar.gz

#編輯配置文件
nvim prometheus.yml

```
下面的文件為我的配置文件,第二個job 為postgres的狀態收集任務.
```
#cat  prometheus.yml

# cd ../prometheus-1.4.1.linux-amd64 

 16:56:17  ?  /opt/pkg/prometheus-1.4.1.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).

  # 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:
  # - "first.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'

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

    static_configs:
      - targets: ['localhost:9090']

  - job_name: 'postgres'
    static_configs:
      - targets: ['127.0.0.1:9187']
        labels:
          instance: db1

```

啟動prometheus
```
./prometheus
```

## 安裝postgres_exporter

github 上有幾個組件,自己可以任選幾個


[包括慢查詢,表,數據庫,buffers 的統計信息](https://github.com/mc2soft/postgresql_exporter)
個人選用的為:

https://github.com/wrouesnel/postgres_exporter

包括buffers,rows 統計等的信息,同樣可以自行編譯或者使用github release的二進制

```
wget  https://github.com/wrouesnel/postgres_exporter/releases/download/v0.1.1/postgres_exporter
```



運行時命令行,最後一個參數log.level 可以不配置, queries.yaml 使用源碼裏面的
[queries.yaml](https://raw.githubusercontent.com/wrouesnel/postgres_exporter/master/queries.yaml)

需在監控的數據庫上執行一下以下命令(不使用數據庫超級管理員的權限的時候,註意更改密碼),

```
CREATE USER postgres_exporter PASSWORD 'password';
ALTER USER postgres_exporter SET SEARCH_PATH TO postgres_exporter,pg_catalog;

CREATE SCHEMA postgres_exporter AUTHORIZATION postgres_exporter;

CREATE FUNCTION postgres_exporter.f_select_pg_stat_activity()
RETURNS setof pg_catalog.pg_stat_activity
LANGUAGE sql
SECURITY DEFINER
AS $$
  SELECT * from pg_catalog.pg_stat_activity;
$$;

CREATE FUNCTION postgres_exporter.f_select_pg_stat_replication()
RETURNS setof pg_catalog.pg_stat_replication
LANGUAGE sql
SECURITY DEFINER
AS $$
  SELECT * from pg_catalog.pg_stat_replication;
$$;

CREATE VIEW postgres_exporter.pg_stat_replication
AS
  SELECT * FROM postgres_exporter.f_select_pg_stat_replication();

CREATE VIEW postgres_exporter.pg_stat_activity
AS
  SELECT * FROM postgres_exporter.f_select_pg_stat_activity();

GRANT SELECT ON postgres_exporter.pg_stat_replication TO postgres_exporter;
GRANT SELECT ON postgres_exporter.pg_stat_activity TO postgres_exporter;
```

監控
```
#配置 數據庫數據源信息

export  DATA_SOURCE_NAME=postgresql://postgres:[email protected]:5433/postgres?sslmode=disable
#啟動監控命令
./postgres_exporter -extend.query-path queries.yaml -log.level debug
```
該程序會開啟服務器的 `:9187` 端口


##  安裝grafana

下載grafana


 下載postgres_expoter的 的dasboard [](https://grafana.net/dashboards/455)

```
wget  https://grafana.net/api/dashboards/455/revisions/1/download
```
但是該模板存在小問題,
需要編輯   原先的 __inputs  ->  name -> 是不對的

![輸入圖片說明](https://static.oschina.net/uploads/img/201701/23163453_pwYp.png "在這裏輸入圖片標題")


啟動grafana
```
https://grafana.net/api/dashboards/455/revisions/1/download
```


進入grafana 的頁面    為http://ip:3000/

默認用戶名,密碼分別為  admin admin

點擊左側 data sources, ->   add source 

![輸入圖片說明](https://static.oschina.net/uploads/img/201701/23164242_Gopq.png "在這裏輸入圖片標題")

填寫  prometheus 的主機端口

postgres_local

http://127.0.0.1:9090

選擇dashboard -> import -> 導入剛才下載的 postgres_exporter


在host 選項中填寫第一步 prometheus 中job  instance 中的主機參數,就可以擁有基本的圖形話界面了。

![輸入圖片說明](https://static.oschina.net/uploads/img/201701/23165003_fxFN.png "在這裏輸入圖片標題")

![輸入圖片說明](https://static.oschina.net/uploads/img/201701/23165542_xcxF.png "在這裏輸入圖片標題")

Tags:

文章來源:


ads
ads

相關文章
ads

相關文章

ad