[Grafana] grafana监控redis、mongodb、mysql数据库


本文总阅读量

0、参考链接

https://mp.weixin.qq.com/s/Qz87UE1aXFZbAdFlx7Zeow

1、grafana下载安装

1
2
wget https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana-5.2.2-1.x86_64.rpm
sudo yum localinstall grafana-5.2.2-1.x86_64.rpm

启动grafana

1
2
service grafana-server start
service grafana-server status

加入开机启动

1
chkconfig --add grafana-server

查看grafana端口

1
2
netstat -plntu | grep grafana-serv
tcp6 0 0 :::3000 :::* LISTEN 2539/grafana-server

登录web界面,localhost:3000
用户:admin
密码:admin(默认)
登录之后提示修改密码,登录界面如下图所示

2、安装Prometheus

下载Prometheus

1
wget https://github.com/prometheus/prometheus/releases/download/v2.3.2/prometheus-2.3.2.linux-amd64.tar.gz

解压安装

1
tar -zxvf prometheus-2.3.2.linux-amd64.tar.gz

配置 Prometheus

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
cd prometheus-2.3.2.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: ['0.0.0.0:9090']

启动prometheus

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
./prometheus --config.file=prometheus.yml
----
level=info ts=2018-08-28T03:42:08.966212621Z caller=main.go:222 msg="Starting Prometheus" version="(version=2.3.2, branch=HEAD, revision=71af5e29e815795e9dd14742ee7725682fa14b7b)"
level=info ts=2018-08-28T03:42:08.966876421Z caller=main.go:223 build_context="(go=go1.10.3, user=root@5258e0bd9cc1, date=20180712-14:02:52)"
level=info ts=2018-08-28T03:42:08.966953021Z caller=main.go:224 host_details="(Linux 3.10.0-693.21.1.el7.x86_64 #1 SMP Wed Mar 7 19:03:37 UTC 2018 x86_64 dax-mysql-mha (none))"
level=info ts=2018-08-28T03:42:08.966990021Z caller=main.go:225 fd_limits="(soft=1024, hard=4096)"
level=info ts=2018-08-28T03:42:08.968101421Z caller=main.go:533 msg="Starting TSDB ..."
level=info ts=2018-08-28T03:42:08.970314221Z caller=web.go:415 component=web msg="Start listening for connections" address=0.0.0.0:9090
level=info ts=2018-08-28T03:42:08.977918922Z caller=main.go:543 msg="TSDB started"
level=info ts=2018-08-28T03:42:08.977966022Z caller=main.go:603 msg="Loading configuration file" filename=prometheus.yml
level=info ts=2018-08-28T03:42:08.979126022Z caller=main.go:629 msg="Completed loading of configuration file" filename=prometheus.yml
level=info ts=2018-08-28T03:42:08.979171222Z caller=main.go:502 msg="Server is ready to receive web requests."
level=info ts=2018-08-28T05:00:04.558075427Z caller=compact.go:398 component=tsdb msg="write block" mint=1535421600000 maxt=1535428800000 ulid=01CNZEEBGJ237RVH965D3VWT57
level=info ts=2018-08-28T05:00:04.566214242Z caller=head.go:348 component=tsdb msg="head GC completed" duration=1.539902ms
level=info ts=2018-08-28T05:00:04.575381958Z caller=head.go:357 component=tsdb msg="WAL truncation completed" duration=9.095816ms
-----

登录控制台 localhost:9090,登录界面如下:

3、mysql_exporter部署

1
2
3
wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.11.0/mysqld_exporter-0.11.0.linux-amd64.tar.gz
tar -zxvf mysqld_exporter-0.11.0.linux-amd64.tar.gz
cd mysqld_exporter-0.11.0.linux-amd64/

配置连接数据库文件

1
2
3
4
5
cat .my.cnf
[client]
host = 127.0.0.1
user=root
password=12345678

启动mysqld_exporter

1
2
3
4
5
6
7
8
9
10
11
./mysqld_exporter --config.my-cnf="/data/soft/mysqld_exporter-0.11.0.linux-amd64/.my.cnf"  
----
INFO[0000] Starting mysqld_exporter (version=0.11.0, branch=HEAD, revision=5d7179615695a61ecc3b5bf90a2a7c76a9592cdd) source="mysqld_exporter.go:206"
INFO[0000] Build context (go=go1.10.3, user=root@3d3ff666b0e4, date=20180629-15:00:35) source="mysqld_exporter.go:207"
INFO[0000] Enabled scrapers: source="mysqld_exporter.go:218"
INFO[0000] --collect.global_status source="mysqld_exporter.go:222"
INFO[0000] --collect.global_variables source="mysqld_exporter.go:222"
INFO[0000] --collect.slave_status source="mysqld_exporter.go:222"
INFO[0000] --collect.info_schema.tables source="mysqld_exporter.go:222"
INFO[0000] Listening on :9104 source="mysqld_exporter.go:232"
----

查看进程是否启动正常:

1
netstat -tunlp|grep 9104

登录web界面,http://localhost:9104/

修改vim prometheus.yml配置文件

1
2
3
4
5
- job_name: mysql
static_configs:
- targets: ['127.0.0.1:9104']
labels:
instance: test

备注:
job_name,当前执行job的名字
targets,连接的主机
instance,数据库实例的标签明

重新启动prometheus,进入prometheus控制台,点击status->target

4、添加数据源



保存测试

5、下载仪表盘

https://github.com/percona/grafana-dashboards/tree/master/dashboards
选择仪表盘类型,导入到grafana



点击仪表盘查看

6、mongodb_exporter部署用于监控mongodb

prometheus监控mongodb数据库,安装之前需要配置go环境下载配置go环境

1
2
wget https://dl.google.com/go/go1.11.linux-amd64.tar.gz
tar -C /usr/local -xzf go1.11.linux-amd64.tar.gz

添加环境变量

1
2
3
echo "
GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin" >> ~/.bash_profile

使配置的环境变量生效

1
source ~/.bash_profile

创建配置mognodb所需要的目录

1
mkdir -p $HOME/go/{dir,src,pkg}

安装glide

1
curl https://glide.sh/get | sh

安装mongodb-exporter

1
2
3
git clone git@github.com:dcu/mongodb_exporter.git $GOPATH/src/github.com/dcu/mongodb_exporter
cd $GOPATH/src/github.com/dcu/mongodb_exporter
make build

查看mongdob_exporter使用的参数

1
./mongodb_exporter -h

启动mongodb_exporter,获取数据库的数据

1
./mongodb_exporter -mongodb.uri mongodb://localhost:30000

启动端口默认为9001,登录http://localhost:9001,查看是否正常

修改prometheus.yml,添加mongodb参数

1
2
3
- job_name: mongodb
static_configs:
- targets: ['127.0.0.1:9001']

重启premetheus,登录premetheus控制台查看获取mognodb是否正常

登录grafana添加模板,可以添加模板id或者导入代码

点击链接,跳转到mongodb_exporter模板
添加完成模板如下图所示:

7、redis_exporter部署用于监控redis,安装redis-exporter模板

1
2
3
go get github.com/oliver006/redis_exporter
cd $GOPATH/src/github.com/oliver006/redis_exporter
go build

启动redis_exporter进程获取redis数据(默认进程端口号位9121)

1
./redis_exporter -redis.addr redis://127.0.0.1:6379 -redis.password test

启动端口默认为9001,登录http://localhost:9121,查看是否正常

修改prometheus.yml,添加redis参数

1
2
3
- job_name: redis
static_configs:
- targets: ['127.0.0.1:9121']

重启premetheus,登录premetheus控制台查看获取redis是否正常

登录grafana添加模板,可以添加模板id或者导入代码,点击此处跳转到redis_exporter链接
,导入完成之后界面如下图所示:

目录
  1. 1. 0、参考链接
  2. 2. 1、grafana下载安装
  3. 3. 2、安装Prometheus
  4. 4. 3、mysql_exporter部署
  5. 5. 4、添加数据源
  6. 6. 5、下载仪表盘
  7. 7. 6、mongodb_exporter部署用于监控mongodb
  8. 8. 7、redis_exporter部署用于监控redis,安装redis-exporter模板

Proudly powered by Hexo and Theme by Lap
本站访客数人次
© 2020 zeven0707's blog