postgres_exporter redis_exporter pika_exporter+prometheus+grafana监控

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_43190337/article/details/100923204

安装postgres_exporter

1) 通过二进制文件安装

$ wget https://github.com/wrouesnel/postgres_exporter/releases/download/v0.5.1/postgres_exporter_v0.5.1_linux-amd64.tar.gz
$ tar -xvf postgres_exporter_v0.5.1_linux-amd64.tar.gz
$ mv postgres_exporter_v0.5.1_linux-amd64 /usr/local/postgres_exporter
  • 验证是否安装成功
$ cd /usr/local/postgres_exporter
$ ./postgres_exporter --version
postgres_exporter v0.5.1 (built with go1.11)
  • 设定DATA_SOURCE_NAME 环境参数
$ export DATA_SOURCE_NAME="postgresql://DB_USER_NAME:PASSWORD@DB_HOST:DB_PORT/DB_NAME?sslmode=disable"

将上面的DB_USER_NAME:PASSWORD,DB_HOST:DB_PORT改成自己的用户、密码、主机ip及端口,DB_NAME可以用postgresql,也可以写到/etc/profile中,永久生效。
例:

$ export DATA_SOURCE_NAME="postgresql://test:[email protected]:5432/test?sslmode=disable"
  • 创建systemd服务
$ vim /lib/systemd/system/postgres_exporter.service

[Unit]
Description=postgres_exporter
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/postgres_exporter/postgres_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target
  • 启动postgres_exporter
$ systemctl daemon-reload
$ systemctl start postgres_exporter
  • 验证是否启动成功
    默认监听端口为9187
$ systemctl status postgres_exporter
$ netstat -lnpt|grep 9187

2) 通过docker安装

用二进制文件安装时需要在每个被监控主机上声明变量,会暴露数据库的用户名及密码,不是很安全,所以还可以通过docker在监控端进行安装。

  • docker运行
$ docker run -itd --name postgres_exporter -p 9187:9187 -e DATA_SOURCE_NAME="postgresql://test:test@localhost:5432/test?sslmode=disable" wrouesnel/postgres_exporter
  • 验证是否启动成功
$ docker ps
CONTAINER ID        IMAGE                         COMMAND                CREATED                       STATUS              PORTS                    NAMES
cca60b0eb684        wrouesnel/postgres_exporter   "/postgres_exporter"   6 seconds ago                 Up 5 seconds        0.0.0.0:9187->9187/tcp   postgres_exporter
或
$ netstat -lnpt|grep 9187
  • prometheus.yml中加入postgres_exporter的配置
$ vim /usr/local/prometheus/prometheus.yml

  - job_name: postgres:5432
    static_configs:
    - targets: ['127.0.0.1:9187']
      labels:
        instance: 'postgres:5432'
        group: 'test'
  • 检查配置文件是否书写正确
$ cd /usr/local/prometheus
$ ./promtool check config prometheus.yml
  • 重新加载prometheus的配置
$ systemctl reload prometheus
或
$ curl -X POST http://127.0.0.1:9090/-/reload (启用了--web.enable-lifecycle选项)
  • 访问web界面
    访问 http://127.0.0.1:9090/targets 查看加入的监控信息。

grafana中加入postgres_exporter监控数据

  • 登录grafana
    访问 http://127.0.0.1:3000, 用户名密码都为admin。
  • 导入postgres_exporter模板
    此模板为455号模板,数据源选择Prometheus 模板下载地址 https://grafana.com/grafana/dashboards/455
  • 查看数据
    在这里插入图片描述

安装redis_exporter

$ wget https://github.com/oliver006/redis_exporter/releases/download/v1.0.4/redis_exporter-v1.0.4.linux-amd64.tar.gz
$ tar -xvf redis_exporter-v1.0.4.linux-amd64.tar.gz
$ mv redis_exporter-v1.0.4.linux-amd64 /usr/local/redis_exporter
  • 验证是否安装成功
$ cd /usr/local/redis_exporter
$ ./redis_exporter --version
INFO[0000] Redis Metrics Exporter v1.0.4    build date: 2019-08-08-00:05:43    sha1: 863ddc928ba         c938d3da5bbe32a59b479ca53837b    Go: go1.12.7
  • 创建systemd服务
$ vim /lib/systemd/system/redis_exporter.service

[Unit]
Description=redis_exporter
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/redis_exporter/redis_exporter -redis.addr redis://127.0.0.1:6379
Restart=on-failure
[Install]
WantedBy=multi-user.target
  • 启动redis_exporter
$ systemctl daemon-reload
$ systemctl start redis_exporter
  • 验证是否启动成功
    默认监听端口为9121
$ systemctl status redis_exporter
$ netstat -lnpt|grep 9121
  • prometheus.yml中加入redis_exporter
 $ vim /usr/local/prometheus/prometheus.yml

  - job_name: redis:6379
    static_configs:
    - targets: ['127.0.0.1:9121']
      labels:
        instance: 'redis:6379'
        group: 'test'
  • 检查配置文件是否书写正确
$ cd /usr/local/prometheus
$ ./promtool check config prometheus.yml
  • 重新加载prometheus的配置
$ systemctl reload prometheus
或
$ curl -X POST http://127.0.0.1:9090/-/reload (启用了--web.enable-lifecycle选项)
  • 访问web界面
    访问 http://127.0.0.1:9090/targets 查看加入的监控信息。

grafana中加入redis_exporter监控数据

  • 导入redis_exporter模板
    上传json文件,导入模板。
  • 查看数据

安装pika_exporter

  • pika_exporter安装
    用go编译安装,以ubuntu系统为例,apt安装:
$ apt-get install golang

设置go环境变量

$ export GOROOT=/usr/lib/go
$ export GOBIN=$GOROOT/bin
$ export PATH=$PATH:$GOBIN
$ export GOPATH=$HOME/gopath

安装pika_exporter

$ go get github.com/pourer/pika_expoter
$ cd $GOPATH/src/github.com/pourer/pika_expoter
$ make

下载失败可以用git clone

$ git clone https://github.com/pourer/pika_exporter.git

编译完安装包在/root/gopath/src/github.com/pourer/pika_exporter/bin下

$ mkdir /usr/local/pika_exporter
$ mv /root/gopath/src/github.com/pourer/pika_exporter/bin/pika_exporter  /usr/local/pika_exporter
  • 验证是否安装成功
$ cd /usr/local/pika_exporter
$ ./pika_exporter --version
INFO[0000] Pika Metrics Exporter  master build date: 2019-08-21 10:32:55 UTC sha: f14ea39 go version: go1.10.4 linux/amd64
  • 创建pika主机列表文件
$ vim pika_hosts_file
127.0.0.1:9221
$ vim metrics_file.ini 将要监控的指标写在文件中
  • 创建systemd服务
$ vim /lib/systemd/system/pika_exporter.service

[Unit]
Description=pika_exporter
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/pika_exporter/pika_exporter -pika.host-file /usr/local/pika_exporter/pika_hosts_file -web.listen-address 0.0.0.0:9121 -metrics-file /usr/local/pika_exporter/metrics_file.ini
Restart=on-failure
[Install]
WantedBy=multi-user.target
  • 启动pika_exporter
$ systemctl daemon-reload
$ systemctl start pika_exporter
  • 验证是否启动成功
    默认监听端口为9121
$ systemctl status pika_exporter
$ netstat -lnpt|grep 9121
  • prometheus.yml中加入pika_exporter
$ vim /usr/local/prometheus/prometheus.yml

  - job_name: pika:9221
    static_configs:
    - targets: ['127.0.0.1:9121']
      labels:
        instance: 'pika:9221'
        group: 'test'
  • 检查配置文件是否书写正确
$ cd /usr/local/prometheus
$ ./promtool check config prometheus.yml
  • 重新加载prometheus的配置
$ systemctl reload prometheus
或
$ curl -X POST http://127.0.0.1:9090/-/reload (启用了--web.enable-lifecycle选项)
  • 访问web界面
    访问 http://127.0.0.1:9090/targets 查看加入的监控信息。

grafana中加入pika_exporter监控数据

  • 导入pika_exporter模板
    上传json文件,导入模板。
  • 查看数据

参考文档

https://tpu.thinkpower.com.tw/tpu/articleDetails/1201
https://github.com/oliver006/redis_exporter
https://github.com/pourer/pika_exporter

猜你喜欢

转载自blog.csdn.net/qq_43190337/article/details/100923204