你的位置:首页 > 信息动态 > 新闻中心
信息动态
联系我们

# Prometheus+Grafana监控PostgreSQL

2021/12/19 22:02:42

Prometheus+Grafana监控PostgreSQL

Prometheus:2.32.0
Grafana:8.3.3
PG:13.2
Linux:CentOS7.6
docker:18.06.3

1、安装Prometheus

下载地址;https://prometheus.io/download/

[root@wcbpg ~]# docker images
REPOSITORY                                           TAG                 IMAGE ID            CREATED             SIZE
mambapg13                                            1.0                 bb50c95ef78c        6 hours ago         2.56GB
wcbcentos76                                          1.0                 6877e73a2018        2 weeks ago         1.9GB
wcbpg                                                1.0                 988fc4d35414        2 weeks ago         1.9GB
registry.cn-hangzhou.aliyuncs.com/lhrbest/lhrpgall   1.0                 a726460146fc        7 months ago        3.96GB
centos                                               7.6.1810            f1cb7c7d58b7        2 years ago         202MB
[root@wcbpg ~]# 
[root@wcbpg ~]#docker run -d --name mambaprometheus -h mambaprometheus \
  -p 12222:22 -p 23389:3389 -p 29090:9090 -p 29093:9093 -p 23000:3000 \
  --privileged=true centos:7.6.1810 \
  /usr/sbin/init
108317a2ba8151870f8d38450b9b359742752de0412cf49b4ba7849f3f784f66
[root@wcbpg ~]# pwd
/root
[root@wcbpg ~]# ls -lth
total 79M
-rw-r--r--  1 root root  72M Dec 18 18:59 prometheus-2.32.0.linux-amd64.tar.gz
-rw-r--r--  1 root root   16 Dec  9 23:20 file3.txt
-rw-r--r--  1 root root   37 Dec  9 23:19 file2.txt
drwxr-xr-x  2 root root   31 Dec  8 23:32 data
-rw-r--r--  1 root root 6.7M Dec  8 10:07 postgres_exporter-0.9.0.linux-amd64.tar.gz
-rw-------. 1 root root 1.4K Nov 27 22:58 anaconda-ks.cfg
-rw-r--r--  1 root root 2.5K Dec 26  2020 Centos-7.repo
[root@wcbpg ~]# docker cp prometheus-2.32.0.linux-amd64.tar.gz mambaprometheus:/
[root@wcbpg ~]# docker exec -it mambaprometheus bash
[root@mambaprometheus /]# ls -lth
total 72M
drwxr-xr-x  18 root root  500 Dec 18 19:13 run
drwxr-xr-x  19 root root 3.2K Dec 18 19:13 dev
dr-xr-xr-x 204 root root    0 Dec 18 19:13 proc
drwxr-xr-x   1 root root   35 Dec 18 19:13 etc
-rw-r--r--   1 root root  72M Dec 18 18:59 prometheus-2.32.0.linux-amd64.tar.gz
dr-xr-xr-x  13 root root    0 Dec 16 23:03 sys
drwxrwxrwt   1 root root 4.0K Nov 28 22:44 tmp
dr-xr-x---   1 root root   27 Nov 28 22:44 root
drwxr-xr-x   1 root root   28 Nov 28 20:13 var
-rw-r--r--   1 root root  12K Dec  4  2018 anaconda-post.log
lrwxrwxrwx   1 root root    7 Dec  4  2018 bin -> usr/bin
lrwxrwxrwx   1 root root    7 Dec  4  2018 lib -> usr/lib
lrwxrwxrwx   1 root root    9 Dec  4  2018 lib64 -> usr/lib64
lrwxrwxrwx   1 root root    8 Dec  4  2018 sbin -> usr/sbin
drwxr-xr-x   1 root root   96 Dec  4  2018 usr
drwxr-xr-x   2 root root    6 Apr 11  2018 home
drwxr-xr-x   2 root root    6 Apr 11  2018 media
drwxr-xr-x   2 root root    6 Apr 11  2018 mnt
drwxr-xr-x   2 root root    6 Apr 11  2018 opt
drwxr-xr-x   2 root root    6 Apr 11  2018 srv
[root@mambaprometheus /]# tar -zxvf  prometheus-2.32.0.linux-amd64.tar.gz -C /usr/local/
prometheus-2.32.0.linux-amd64/
prometheus-2.32.0.linux-amd64/consoles/
prometheus-2.32.0.linux-amd64/consoles/index.html.example
prometheus-2.32.0.linux-amd64/consoles/node-cpu.html
prometheus-2.32.0.linux-amd64/consoles/node-disk.html
prometheus-2.32.0.linux-amd64/consoles/node-overview.html
prometheus-2.32.0.linux-amd64/consoles/node.html
prometheus-2.32.0.linux-amd64/consoles/prometheus-overview.html
prometheus-2.32.0.linux-amd64/consoles/prometheus.html
prometheus-2.32.0.linux-amd64/console_libraries/
prometheus-2.32.0.linux-amd64/console_libraries/menu.lib
prometheus-2.32.0.linux-amd64/console_libraries/prom.lib
prometheus-2.32.0.linux-amd64/prometheus.yml
prometheus-2.32.0.linux-amd64/LICENSE
prometheus-2.32.0.linux-amd64/NOTICE
prometheus-2.32.0.linux-amd64/prometheus
prometheus-2.32.0.linux-amd64/promtool
[root@mambaprometheus /]# ln -s /usr/local/prometheus-2.32.0.linux-amd64 /usr/local/prometheus
[root@mambaprometheus /]# ln -s /usr/local/prometheus/prometheus /usr/local/bin/prometheus
[root@mambaprometheus /]# prometheus --config.file=/usr/local/prometheus/prometheus.yml --storage.tsdb.path=/usr/local/prometheus/data/ --web.enable-lifecycle --storage.tsdb.retention.time=60d &
[root@mambaprometheus /]# lsof -i:9090     
COMMAND    PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
prometheu 3514 root    7u  IPv6 684351      0t0  TCP *:websm (LISTEN)
prometheu 3514 root   11u  IPv4 684373      0t0  TCP localhost:58312->localhost:websm (ESTABLISHED)
prometheu 3514 root   12u  IPv6 684374      0t0  TCP localhost:websm->localhost:58312 (ESTABLISHED)

至此,Prometheus安装完成,可以直接在电脑访问http://192.168.142.110:29090/

请添加图片描述

2、安装grafana

下载地址:https://grafana.com/grafana/download

[root@wcbpg ~]# docker cp grafana-enterprise-8.3.3-1.x86_64.rpm mambaprometheus:/
[root@wcbpg ~]# docker exec -it mambaprometheus bash
[root@mambaprometheus /]# pwd
/
[root@mambaprometheus /]# ls grafana-enterprise-8.3.3-1.x86_64.rpm
grafana-enterprise-8.3.3-1.x86_64.rpm
[root@mambaprometheus /]# yum install grafana-enterprise-8.3.3-1.x86_64.rpm
[root@mambaprometheus /]# systemctl daemon-reload
[root@mambaprometheus /]# systemctl enable grafana-server.service
Created symlink from /etc/systemd/system/multi-user.target.wants/grafana-server.service to /usr/lib/systemd/system/grafana-server.service.
[root@mambaprometheus /]# systemctl start grafana-server.service
[root@mambaprometheus /]# lsof -i:3000
COMMAND    PID    USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
grafana-s 3877 grafana    8u  IPv6 723062      0t0  TCP *:hbci (LISTEN)
[root@mambaprometheus /]# systemctl status grafana-server.service

在宿主机访问http://192.168.142.110:23000/

用户名和密码:admin/admin

请添加图片描述

3、监控PG数据库

https://github.com/wrouesnel/postgres_exporter

地址:https://github.com/prometheus-community/postgres_exporter/releases/download/v0.9.0/postgres_exporter-0.9.0.linux-amd64.tar.gz

[root@wcbpg ~]# docker cp postgres_exporter-0.9.0.linux-amd64.tar.gz mambaprometheus:/
[root@wcbpg ~]# docker exec -it mambaprometheus bash
[root@mambaprometheus /]# tar -zxvf postgres_exporter-0.9.0.linux-amd64.tar.gz
postgres_exporter-0.9.0.linux-amd64/
postgres_exporter-0.9.0.linux-amd64/LICENSE
postgres_exporter-0.9.0.linux-amd64/postgres_exporter

[root@mambaprometheus /]# 
[root@mambaprometheus /]# mv ./postgres_exporter-0.9.0.linux-amd64/postgres_exporter /usr/local/bin/

[root@mambaprometheus /]# export DATA_SOURCE_NAME="postgresql://postgres:wcb@192.168.142.110:15436/postgres?sslmode=disable"
[root@mambaprometheus /]# 
[root@mambaprometheus /]# 
[root@mambaprometheus /]# nohup postgres_exporter --web.listen-address=":9187" >/dev/null 2>&1 &
[2] 3893
[root@mambaprometheus /]# curl http://localhost:9187/metrics

在/usr/local/prometheus/prometheus.yml添加一下配置

  - job_name: 'PostgreSQL'
    static_configs:
    - targets: ['localhost:9187']
      labels:
        instance: 192.168.142.110:15436

访问http://192.168.142.110:29090/targets

请添加图片描述

添加数据源

请添加图片描述

请添加图片描述

添加模板:

https://grafana.com/grafana/dashboards?category=databases&dataSource=prometheus&direction=desc&orderBy=downloads&search=postgres

PG模板;9628

请添加图片描述
请添加图片描述
请添加图片描述