使用Prometheus+Grafana 监控MySQL/MONGODB
之前就久仰 Prometheus 大名,因为有用zabbix 进行监控,就没去安装它。现在正好用上监控MONGO+MYSQL。
服务器端组件:
prometheus #服务端
alertmanager #预警
Grafana #前端展示,
客户端组件:
node_exporter
mongodb_exporter
mysqld_exporter
mongodb01 192.168.0.11
mongodb02 192.168.0.12
mysql01 192.168.0.13
mysql02 192.168.0.14
prometheus Grafana 安装在mongodb02
mongodb01,mongodb02 安装了 node_exporter ,mongodb_exporter
mysql01,mysql02 安装了 node_exporter ,mysql_exporter
+
1.安装 GO
golang-bin.x86_64 0:1.8.3-1.el7
1.下载安装 Prometheus (https://prometheus.io/download/)
1.1 下载安装
[root@mongodb01 download]# tar xvf prometheus-2.0.0.linux-amd64.tar.gz
-C /usr/local/
mv /usr/local/prometheus-2.0.0.linux-amd64 /usr/local/prometheus2.0
1.2 配置文件
[root@mongodb01 download]#vi /usr/local/prometheus2.0/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=` 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: linux_mongo1
static_configs:
- targets: ['192.168.11.190:9100']
labels:
instance: linux_mongo1
- job_name: mongodb1
static_configs:
- targets: ['192.168.11.190:9104']
labels:
instance: mongodb1
- job_name: linux_mongo2
static_configs:
- targets: ['192.168.11.191:9100']
labels:
instance: linux_mongo2
- job_name: mongodb2
static_configs:
- targets: ['192.168.11.191:9104']
labels:
instance: mongodb2
- job_name: linux_mysql1
static_configs:
- targets: ['192.168.11.192:9100']
labels:
instance: linux_mysql1
- job_name: mysqldb1
static_configs:
- targets: ['192.168.11.192:9104']
labels:
instance: mysqldb1
- job_name: linux_mysql2
static_configs:
- targets: ['192.168.11.193:9100']
labels:
instance: linux_mysql2
- job_name: mysqldb2
static_configs:
- targets: ['192.168.11.193:9104']
labels:
instance: mysqldb2
1.3 启动 prometheus
[root@mongodb01 prometheus2.0]# ./prometheus --config.file=prometheus.yml &
[root@mongodb01 prometheus2.0]# level=info ts=2017-12-07T05:56:43.220942048Z caller=main.go:215 msg="Starting Prometheus" version="(version=2.0.0, branch=HEAD, revision=0a74f98628a0463dddc90528220c94de5032d1a0)"
level=info ts=2017-12-07T05:56:43.221007739Z caller=main.go:216 build_context="(go=go1.9.2, user=root@615b82cb36b6, date=20171108-07:11:59)"
level=info ts=2017-12-07T05:56:43.221024785Z caller=main.go:217 host_details="(Linux 3.10.0-514.26.2.el7.x86_64 #1 SMP Tue Jul 4 15:04:05 UTC 2017 x86_64 mongodb01 (none))"
level=info ts=2017-12-07T05:56:43.223677457Z caller=web.go:380 component=web msg="Start listening for connections" address=0.0.0.0:9090
level=info ts=2017-12-07T05:56:43.223707498Z caller=main.go:314 msg="Starting TSDB"
level=info ts=2017-12-07T05:56:43.223717423Z caller=targetmanager.go:71 component="target manager" msg="Starting target manager..."
level=info ts=2017-12-07T05:56:43.229958353Z caller=main.go:326 msg="TSDB started"
level=info ts=2017-12-07T05:56:43.230020283Z caller=main.go:394 msg="Loading configuration file" filename=prometheus.yml
level=info ts=2017-12-07T05:56:43.231843003Z caller=main.go:371 msg="Server is ready to receive requests."
2.下载 EXPORTER: MONGODB/MYSQL/NODE
https://github.com/percona/mongodb_exporter
https://github.com/prometheus/mysqld_exporter/releases/download/v0.10.0/mysqld_exporter-0.10.0.linux-amd64.tar.gz
https://github.com/prometheus/node_exporter/releases/download/v0.15.2/node_exporter-0.15.2.linux-amd64.tar.gz
3.安装 node_exporter
[root@mysql01 app]# tar -xzvf
node_exporter-0.15.2.linux-amd64.tar.gz -C /usr/local/
node_exporter-0.15.2.linux-amd64/
node_exporter-0.15.2.linux-amd64/LICENSE
node_exporter-0.15.2.linux-amd64/NOTICE
node_exporter-0.15.2.linux-amd64/node_exporter
[root@mysql01 app]# nohup /usr/local/node_exporter-0.15.2.linux-amd64/node_exporter &
[1] 6795
[root@mysql01 app]# nohup: ignoring input and appending output to ‘nohup.out’
4.安装MYSQLD_EXPORTER
4.1 mysqld_exporter
[root@mysql01 app]# tar xvf mysqld_exporter-0.10.0.linux-amd64.tar.gz -C /usr/local
mysqld_exporter-0.10.0.linux-amd64/
mysqld_exporter-0.10.0.linux-amd64/LICENSE
mysqld_exporter-0.10.0.linux-amd64/NOTICE
mysqld_exporter-0.10.0.linux-amd64/mysqld_exporter
4.2 添加监控用户及设置权限
root@master:mysql>GRANT REPLICATION CLIENT,PROCESS ON *.* TO 'mysql_monitor'@'localhost' identified by 'mysql_monitor'; Query OK, 0 rows affected (0.00 sec) root@master:mysql>GRANT SELECT ON *.* TO 'mysql_monitor'@'localhost'; Query OK, 0 rows affected (0.00 sec)
4.3配置文件添加MYSQL访问信息
$ vi /usr/local/mysqld_exporter-0.10.0.linux-amd64/.my.cnf [client] port=3306 user=mysql_monitor password=mysql_monitor
4.4 启动
[root@mysql01 app]#
/usr/local/mysqld_exporter-0.10.0.linux-amd64/mysqld_exporter
-config.my-cnf=”/usr/local/mysqld_exporter-0.10.0.linux-amd64/.my.cnf” &
4.5 Mongodb_exporter 安装
download:
软件包: prometheus-mongodb-exporter 1.0.0-1
https://packages.debian.org/stretch/prometheus-mongodb-exporter
1.yum install -y golang glide
2.定义一个gopath的环境变量
export GOPATH=/opt/go/
3.把exporter包拷到$GOPATH/src/github.com/dcu/mongodb_exporter
4.cd $GOPATH/src/github.com/dcu/mongodb_exporter && make build
5.启动 登录到mongodb server.
./mongodb_exporter -mongodb.uri mongodb://root:psd@localhost:2001/admin
5.安装 grafana
[root@mongodb02 download]# yum install initscripts [root@mongodb02 download]# yum install fontconfig [root@mongodb02 grafana]# yum install freetype* [root@mongodb02 grafana]# yum install urw-fonts [root@mongodb02 download]# rpm -Uvh grafana-4.6.2-1.x86_64.rpm warning: grafana-4.6.2-1.x86_64.rpm: Header V4 RSA/SHA1 Signature, key ID 24098cb6: NOKEY Preparing... ################################# [100%] Updating / installing... 1:grafana-4.6.2-1 ################################# [100%] ### NOT starting on installation, please execute the following statements to configure grafana to start automatically using systemd sudo /bin/systemctl daemon-reload sudo /bin/systemctl enable grafana-server.service ### You can start grafana-server by executing sudo /bin/systemctl start grafana-server.service POSTTRANS: Running script [database] # You can configure the database connection by specifying type, host, name, user and password # as seperate properties or as on string using the url propertie. # Either "mysql", "postgres" or "sqlite3", it's your choice type = mysql host = 127.0.0.1:3306 name = grafana user = grafana # If the password contains # or ; you have to wrap it with trippel quotes. Ex """#password;""" password = # Use either URL or the previous fields to configure the database # Example: mysql://user:secret@host:port/database url =mysql://grafana:5SCti04n0wXunsj2@localhost:3106/grafana # For "postgres" only, either "disable", "require" or "verify-full" ;ssl_mode = disable # For "sqlite3" only, path relative to data_path setting ;path = grafana.db # Max idle conn setting default is 2 ;max_idle_conn = 2 # Max conn setting default is 0 (mean not set) ;max_open_conn =
5.1 编辑配置文件/etc/grafana/grafana.ini,修改dashboards.json段落下两个参数的值:
[dashboards.json]
enabled = true
path = /var/lib/grafana/dashboards
5.2 下载安装仪表盘 grafana-dashboards
gwet
https://codeload.github.com/percona/grafana-dashboards/zip/master
unzip grafana-dashboards-master.zip
cp -r grafana-dashboards-master/dashboards /var/lib/grafana
5.3运行Grafana服务
$ systemctl daemon-reload $ systemctl start grafana-server $ systemctl status grafana-server
5.4 可以通过端口3000打开 页面 http://47.95.47.18:3000/ (默认用户 admin/admin)
5.5 mysql 数据库中添加库名及用户账号
grant all privileges on grafana.* to 'grafana'@'192.168.%' identified by 'grafana' ; CREATE DATABASE IF NOT EXISTS grafana DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
5.6 安装plugins
[root@mongodb02 mysql]# grafana-cli plugins list-remote id: abhisant-druid-datasource version: 0.0.4 id: adremsoft-netcrunch-app version: 1.0.0 id: alexanderzobnin-zabbix-app version: 3.7.0 id: ayoungprogrammer-finance-datasource version: 1.0.0
#下载安装 zabbix
# git clone https://github.com/linglong0820/grafana-zabbix # mkdir /usr/share/grafana/public/app/plugins/datasource/ -pv # cp -r grafana-zabbix/zabbix/ /usr/share/grafana/public/app/plugins/datasource/
使用grafana-cli 安装插件
# grafana-cli plugins install alexanderzobnin-zabbix-app 安装zabbix插件
#grafana-cli plugins install grafana-worldmap-panel 安装世界地图插件 #grafana-cli plugins install grafana-clock-panel 安装时间插件 #grafana-cli plugins install grafana-piechart-panel 安装圆饼插件 # /etc/init.d/grafana-server restart
5.7 导入 dashboard
unzip grafana-dashboards-master.zip cp grafana-dashboards-master/dashboards /var/lib/grafana/
prometheus
grafana
http://39.106.152.0:3000/login
参考URL
客户端下载
https://github.com/percona/mongodb_exporter
官方文档
https://prometheus.io/docs/operating/configuration/
https://prometheus.io/blog/2015/06/01/advanced-service-discovery/
http://docs.grafana.org/features/datasources/prometheus/
软件包: prometheus-mongodb-exporter (1.0.0-1 以及其他的) download
https://packages.debian.org/stretch/prometheus-mongodb-exporter
“mongodb://root:123456@localhost:24001/admin”
PMM INSTALL
http://www.ywnds.com/?p=9713
http://www.jianshu.com/p/97233bac2964
https://www.linuxba.com/archives/7915#36_pmm_client
grafana + cloudwatch
http://www.mamicode.com/info-detail-1750659.html