PostgreSQL监控之pgwatch2

前言:

  • 虽然做者已经推出了docker版本,只需一键(一条命令)便可完成搭建。
  • 可是本着学习的心理仍是要本身折腾一遍,一键虽好但不明了啊。
  • 官方已经把安装写的很详细了,但毕竟是英文......
  • pgwatch2官方地址:https://github.com/cybertec-postgresql/pgwatch2

系统环境:

CentOS 7.5
IP:192.168.1.2

grafana-5.1.4
PostgreSQL-十、pg_stat_statements模块
InfluxDB-1.5.3
Python3

#关闭selinux以及firewalld。
##自行安装以上应用!!!本文不提供安装过程!!!

1、配置PostgreSQL

vi /var/lib/pgsql/10/data/postgresql.conf

shared_preload_libraries = 'pg_stat_statements'
track_io_timing = on

#编辑postgresql.conf配置文件,添加以下两行(前提:装好pg_stat_statements模块

vi  /var/lib/pgsql/10/data/pg_hba.conf

local   all             all                                     peer
host    all             all             127.0.0.1/32            trust
host    all             all             ::1/128                 trust

#修改认证模式,经过127.0.0.1访问的用户不须要密码认证
#为了安全请自行配置,pg_hba.conf配置文件详解请自行百度~~

PostgreSQL监控之pgwatch2
PostgreSQL监控之pgwatch2

systemctl restart postgresql-10.service

#重启数据库

mkdir /app && cd /app
git clone https://github.com/cybertec-postgresql/pgwatch2.git

#克隆源码仓库,有一些东西须要用到。

PostgreSQL监控之pgwatch2

su - postgres
psql -c "create user pgwatch2_grafana password 'xyz'"
psql -c "create database pgwatch2_grafana owner pgwatch2_grafana"

psql -c "create user pgwatch2 password 'xyz'"
psql -c "create database pgwatch2 owner pgwatch2"

psql -f /app/pgwatch2/pgwatch2/sql/datastore_setup/config_store.sql pgwatch2
psql -f /app/pgwatch2/pgwatch2/sql/datastore_setup/metric_definitions.sql pgwatch2

#切换至postgre用户以便操做数据库。
#建立pgwatch2_grafana、pgwatch2用户并设置密码。
#建立pgwatch2_grafana、pgwatch2数据库并设置所属主。
#导入pgwatch2的数据。

PostgreSQL监控之pgwatch2

2、配置InfluxDB

vi /etc/influxdb/influxdb.conf

[http]
enabled = true
bind-address = "127.0.0.1:8086"

#修改配置文件,将http端的bind-address修改为127.0.0.1
#由于是作监控用的,因此不作认证,但仅容许本地地址访问。

systemctl start influxdb

#启动InfluxDB

PostgreSQL监控之pgwatch2

influx

CREATE USER "pgwatch2" WITH PASSWORD 'xyz'
CREATE DATABASE pgwatch2
use pgwatch2
GRANT ALL ON pgwatch2 to "pgwatch2"

#建立pgwatch2用户以及同名的数据库,而且受权。

PostgreSQL监控之pgwatch2


3、配置Grafana

vi /etc/grafana/grafana.ini

[database]
type = postgres
host = 127.0.0.1:5432
name = pgwatch2_grafana
user = pgwatch2_grafana
password = xyz

#将Grafana的数据存入postgresql

http://192.168.1.2:3000
#访问Grafana的web页面进行配置
#默认用户名与密码:admin

PostgreSQL监控之pgwatch2
PostgreSQL监控之pgwatch2
PostgreSQL监控之pgwatch2
PostgreSQL监控之pgwatch2
PostgreSQL监控之pgwatch2

#开始配置仪表盘
#源码仓库中:pgwatch2/grafana_dashboards/v5/路径下就是全部的仪表盘内容,每一个文件夹为一个仪表盘
#每一个文件夹内都含有dashboard.json文件,把这个文件导入便可

#这里为了展现效果我就只导入一个:db-overview/dashboard.json

#github网页上的会比较好复制,复制网页中的全部内容:
#https://raw.githubusercontent.com/cybertec-postgresql/pgwatch2/master/grafana_dashboards/v5/db-overview/dashboard.json

PostgreSQL监控之pgwatch2
PostgreSQL监控之pgwatch2
PostgreSQL监控之pgwatch2
PostgreSQL监控之pgwatch2
PostgreSQL监控之pgwatch2
PostgreSQL监控之pgwatch2


4、配置pgwatch2

cd /app/pgwatch2/
pip3 install -U -r webpy/requirements.txt
cd webpy
python3 web.py

#进入pgwatch目录,pip安装一些依赖库
#执行web.py

PostgreSQL监控之pgwatch2
PostgreSQL监控之pgwatch2

#访问web页面进行下一步配置
#配置被监控数据库的信息,注意,这里填的是被监控数据库的信息!!!
#也就是你要在对应的数据库建立用户而且受权,而后启用pg_stat_statements模块
#我这里仅仅只监控本机上的pgwatch2数据库。

PostgreSQL监控之pgwatch2
PostgreSQL监控之pgwatch2

#配置被监控的数据库
#这里我就只监控pgwatch2数据库

su - postgres
psql -d pgwatch2 -c "CREATE EXTENSION pg_stat_statements;"
psql -d pgwatch2 -c "CREATE EXTENSION plpythonu;"
psql -f /app/pgwatch2/pgwatch2/sql/metric_fetching_helpers/stat_activity_wrapper.sql pgwatch2
psql -f /app/pgwatch2/pgwatch2/sql/metric_fetching_helpers/stat_statements_wrapper.sql pgwatch2
psql -f /app/pgwatch2/pgwatch2/sql/metric_fetching_helpers/cpu_load_plpythonu.sql pgwatch2
psql -f /app/pgwatch2/pgwatch2/sql/metric_fetching_helpers/table_bloat_approx.sql pgwatch2

#官方说明:https://github.com/cybertec-postgresql/pgwatch2#steps-to-configure-your-database-for-monitoring

PostgreSQL监控之pgwatch2

#编译pgwatch2程序

yum install go -y
cd /app/pgwatch2/pgwatch2
./build_gatherer.sh

#安装go语言环境,进入pgwatch2目录,执行build_gatherer.sh开始编译
#这个过程会比较慢,由于会到github上下载东西,必定要保证电脑能够ping通github
#编译完成后会生成一个pgwatch2的可执行文件

PostgreSQL监控之pgwatch2

./pgwatch2

#运行pgwatch2程序
#由于刚才设置的全部帐户密码跟pgwatch2程序默认值是一致的,全部直接运行便可
#若不一致,请自行使用./pgwatch2 --help查看帮助

#执行不报错,接下来去Grafana看数据就好了!!!

PostgreSQL监控之pgwatch2
PostgreSQL监控之pgwatch2
PostgreSQL监控之pgwatch2

#最后,把pgwatch2配置成能够用systemctl方式启动
#把下面的内容粘贴到/etc/systemd/system/pgwatch2.service
#而后就能够用systemctl start 启动啦
#注意,经过这个方式启动,全部的输出信息都会在/var/log/messages

[Unit]
Description=pgwatch2
After=syslog.target
After=network.target

[Service]
User=root
Restart=on-failure
PIDFile=/tmp/pgwatch2.pid
KillMode=control-group
ExecStart=/app/pgwatch2/pgwatch2/pgwatch2
ExecStop=/bin/kill -SIGTERM $MAINPID
RestartSec=10s
TimeoutSec=0

[Install]
WantedBy=multi-user.target

PostgreSQL监控之pgwatch2

相关文章
相关标签/搜索