prometheus+grafana 对于现在这个时间点来说,相信很多同行都应该已经开始玩起来了,当仍然可能有一部分人可能还不知道prometheus+grafana 的存在,也可能还有一部分人虽然知道它的存在,但却懒于动手,如果是这样,那后面的内容你可得打起精神来了,因为可能你会被grafana炫酷的视觉效果惊艳到。

让我们一起了解一下吧。

首先,简单介绍下prometheus+grafana 这对鸳鸯到底是什么:

prometheus 是由 SoundCloud 开发的开源监控报警系统和时序列数据库(TSDB),prometheus是一个监控采集与数据存储框架(监控server端),具体采集什么数据依赖于具体的exporter(监控client端),例如:采集MySQL的数据需要使用到mysql_exporter,prometheus调用mysql_expoter采集到mysql的监控指标之后,把mysql_exporter采集到的数据存放到prometheus所在服务器的磁盘数据文件中保存。它的各个组件基本都是用 golang 编写,对编译和部署十分友好.并且没有特殊依赖,基本都是独立工作。以下是prometheus架构图(图片来源:https://prometheus.io/docs/introduction/overview/)

grafana是一个高颜值的监控绘图程序,也是一个可视化面板(Dashboard),grafana的厉害之处除了高颜值,还支持多种数据源(支持Graphite、zabbix、InfluxDB、Prometheus和OpenTSDB作为数据源)、支持灵活丰富的dashboard配置选项(例如:可以把多个实例的相同采集项配置在一个展示框里),使得相较于其他开源监控系统来说更易用性,学习成本更低。从视觉上来说,比以往的任何开源的监控系统都看起来要养眼很多,下面先看两张监控效果图:

相信审美还算正常的人都不会说上面两张图很丑吧,那么问题来了,我们该如何玩起来呢?下面就简单为大家介绍如何快速搭建起来!

这里方便演示过程,我们准备了如下两台测试服务器

  • prometheus+grafana server端主机:10.10.30.165

  • MySQL 客户端主机:10.10.20.14

1、安装prometheus

1.1. 下载安装包

对于prometheus,假设我们需要监控MySQL,那么我们需要下载至少3个组件,如下:

  • prometheus程序包

  • node_exporter:监控主机磁盘、内存、CPU等硬件性能指标的采集程序包

  • mysql_exporter: 监控mysql各种性能指标的采集程序包

下载链接(该页面始终只有一个最新版本):https://prometheus.io/download/

  • 下载prometheus

  • 下载node_exporter

  • 下载mysqld_exporter

  • PS:如果你还需要配置监控告警,需要下载alertmanager程序包

1.2. 解压程序包

解压prometheus

[root@localhost ~]# mkdir /data
[root@localhost ~]# tar xvf prometheus-2.1.0.linux-amd64.tar.gz -C /data/

解压exporter:由于prometheus主机自身也需要监控,所以也至少需要解压node_exporter包

[root@localhost ~]# tar xf node_exporter-0.15.2.linux-amd64.tar -C /root/
# 如果需要监控mysql,则继续解压mysql_exporter
[root@localhost ~]# tar xf mysqld_exporter-0.10.0.linux-amd64.tar  -C /root/

1.3. 启动prometheus

进入prometheus的工作目录

[root@localhost ~]# cd /data/
[root@localhost data]# mv prometheus-2.1.0.linux-amd64/ prometheus
[root@localhost ~]# cd /data/prometheus

配置 prometheus.yml配置文件

[root@localhost data]# 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).
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
- file_sd_configs:
- files:
- host.yml
job_name: Host
metrics_path: /metrics
relabel_configs:
- source_labels: [__address__]
regex: (.*)
target_label: instance
replacement: $1
- source_labels: [__address__]
regex: (.*)
target_label: __address__
replacement: $1:9100
- file_sd_configs:
files:
- mysql.yml
job_name: MySQL
metrics_path: /metrics
relabel_configs:
- source_labels: [__address__]
regex: (.*)
target_label: instance
replacement: $1
- source_labels: [__address__]
regex: (.*)
target_label: __address__
replacement: $1:9104- job_name: prometheus
static_configs:
- targets:
- localhost:9090

启动prometheus进程,30d表示prometheus只保留30天以内的数据

[root@localhost prometheus]# /data/prometheus/prometheus  --storage.tsdb.retention=30d &

如果是7.x系统,可以按照如下方式配置service启动脚本

# 修改WorkingDirectory参数为你的prometheus的工作目录
[root@localhost prometheus]# cat /usr/lib/systemd/system/prometheus.service
[Unit]
Description=Prometheus instance
Wants=network-online.target
After=network-online.target
After=postgresql.service mariadb.service mysql.service[Service]
User=root
Group=root
Type=simple
Restart=on-failure
WorkingDirectory=/data/prometheus/
RuntimeDirectory=prometheus
RuntimeDirectoryMode=0750
ExecStart=/data/prometheus/prometheus  --storage.tsdb.retention=30d --config.file=/data/prometheus/prometheus.yml
LimitNOFILE=10000
TimeoutStopSec=20[Install]
WantedBy=multi-user.target

PS:prometheus默认的web访问端口为9090,可以使用如下地址访问http://10.10.30.165:9090

2、安装grafana

前面说过,grafana是一个出图展示框架,grafana根据grafana-dashboards来进行展示,grafana-dashboards就类似于grafana的出图配置文件,根据在grafana-dashboards中的定义来确定在页面中需要展示什么指标,需要如何展示等,需要分别对这两个组件进行下载与安装。

2.1. 下载安装包

对于grafana来说,需要下载一个程序包,一个grafana-dashboards包

下载链接

grafana程序包:https://grafana.com/grafana/download

grafana-dashboards包:https://github.com/percona/grafana-dashboards/releases

2.2. 解压程序包

解压grafana

[root@localhost ~]# tar xf grafana-4.6.3.linux-x64.tar.gz -C /data/prometheus/
[root@localhost ~]# cd /data/prometheus
[root@localhost prometheus]# mv grafana-4.6.3/ grafana

2.3. 启动grafana

进入grafana工作目录,并启动

[root@localhost ]# cd /data/prometheus/grafana
[root@localhost ]# ./bin/grafana-server

如果是7.x系统,可以按照如下方式配置service启动脚本

[root@localhost service]# cat /usr/lib/systemd/system/grafana-server.service
[Unit]
Description=Grafana instance
Documentation=http://docs.grafana.org
Wants=network-online.target
After=network-online.target
After=postgresql.service mariadb.service mysql.service
[Service]
User=root
Group=root
Type=simple
Restart=on-failure
WorkingDirectory=/data/prometheus/grafana
RuntimeDirectory=grafana
RuntimeDirectoryMode=0750
ExecStart=/data/prometheus/grafana/bin/grafana-server
LimitNOFILE=10000
TimeoutStopSec=20
[Install]
WantedBy=multi-user.target

打开grafana页面(默认帐号和密码:admin/admin,默认的端口为3000,通过地址:http://10.10.30.165:3000 访问),配置数据来源。

指定prometheus地址,这里我们把grafana装在了同一台机器,直接使用127.0.0.1的地址配置即可,如下图

2.4. 在grafana中导入grafana-dashboards

解压grafana-dashboards包,该包中提供了大量的json格式文件的grafana dashboards,根据需要自行选择,我们这里需要监控主机和MySQL,就选择如下一些json文件。

[root@localhost ~]# tar xvf grafana-dashboards-1.6.1.tar.gz
[root@localhost ~]# cd grafana-dashboards-1.6.1
[root@localhost grafana-dashboards-1.6.1]# updatedb
[root@localhost grafana-dashboards-1.6.1]# locate json |grep dashboards/
............
/root/grafana-dashboards-1.6.1/dashboards/CPU_Utilization_Details_Cores.json
/root/grafana-dashboards-1.6.1/dashboards/Disk_Performance.json
/root/grafana-dashboards-1.6.1/dashboards/Disk_Space.json
............
/root/grafana-dashboards-1.6.1/dashboards/MySQL_InnoDB_Metrics.json
/root/grafana-dashboards-1.6.1/dashboards/MySQL_InnoDB_Metrics_Advanced.json
............
/root/grafana-dashboards-1.6.1/dashboards/MySQL_Overview.json
/root/grafana-dashboards-1.6.1/dashboards/MySQL_Performance_Schema.json
............
/root/grafana-dashboards-1.6.1/dashboards/MySQL_Replication.json
/root/grafana-dashboards-1.6.1/dashboards/MySQL_Table_Statistics.json
............
/root/grafana-dashboards-1.6.1/dashboards/Summary_Dashboard.json
/root/grafana-dashboards-1.6.1/dashboards/System_Overview.json
............

在grafana页面中,导入需要的json文件

在弹出的窗口中选择你需要导入的json文件

然后,如果你的grafana中已经添加过主机,此时,就可以看到相应的json dashboard监控数据

至此,prometheus+grafana的基础架构(server端)已经搭建好了,现在,你可以去给他们添加监控节点了(client端)

3、监控节点部署

3.1. 添加主机监控

以添加prometheus主机(10.10.30.165)为例进行说明

解压exporter压缩包

[root@localhost ~]# tar xf node_exporter-0.15.2.linux-amd64.tar
[root@localhost ~]# mv node_exporter-0.15.2.linux-amd64 node_exporter

启动node_exporter程序

[root@localhost ~]# cd node_exporter
[root@localhost node_exporter]# nohup ./node_exporter &

配置prometheus主机监控配置列表文件,由于之前主配置文件prometheus.yml 中已经定义了监控主机的配置文件host.yml,这里只需要把主机IP信息填入即可动态生效

[root@localhost node_exporter]# cat /data/prometheus/host.yml
- labels:
service: test
targets:
- 10.10.30.165

然后,在grafana页面中就可以看到你配置的主机

PS:如果该文件中已经配置过lables且不需要使用独立的service标签进行标记,则新添加的实例的IP可以直接放在同一个targets下,如下:

[root@localhost mysqld_exporter]# cat /data/prometheus/host.yml
- labels:
service: test
targets:
- 10.10.30.165
- 10.10.20.14

3.2. 添加MySQL监控

添加MySQL监控主机,这里以添加10.10.20.14为例进行说明

解压exporter压缩包

[root@localhost ~]# tar xf mysqld_exporter-0.10.0.linux-amd64.tar
[root@localhost ~]# mv mysqld_exporter-0.10.0.linux-amd64 mysqld_exporter

配置监控数据库需要的主机IP、数据库端口、数据库账号和密码的环境变量(注意:该账号需要单独创建,需要对所有库所有表至少具有PROCESS, REPLICATION CLIENT, SELECT权限)

[root@luoxiaobo-01 ~]# export DATA_SOURCE_NAME='admin:password@(10.10.20.14:3306)/'
[root@luoxiaobo-01 ~]# echo "export DATA_SOURCE_NAME='admin:password@(10.10.20.14:3306)/'" >> /etc/profile

启动exporter

# 由于目前最新的版本默认关闭了大量的mysql采集项,需要显式使用相应的选项开启(截止到写稿时间,最新的开发版本可以通过prometheus端的配置项让exporter端生效,而无需再exporter中使用大量的启动选项开启)
[root@localhost ~]# cd mysqld_exporter
[root@localhost mysqld_exporter]# nohup ./mysqld_exporter --collect.info_schema.processlist --collect.info_schema.innodb_tablespaces --collect.info_schema.innodb_metrics  --collect.perf_schema.tableiowaits --collect.perf_schema.indexiowaits --collect.perf_schema.tablelocks --collect.engine_innodb_status --collect.perf_schema.file_events --collect.info_schema.processlist --collect.binlog_size --collect.info_schema.clientstats --collect.perf_schema.eventswaits &

配置prometheus MySQL监控配置列表文件,由于之前主配置文件prometheus.yml 中已经定义了监控主机的配置文件mysql.yml,这里只需要把主机IP信息填入即可动态生效

[root@localhost mysqld_exporter]# cat /data/prometheus/host.yml
- labels:
service: test
targets:
- 10.10.30.165
- 10.10.20.14

然后,在grafana页面中就可以看到你配置的MySQL实例

PS:如果该文件中已经配置过lables且不需要使用独立的service标签进行标记,则新添加的实例的IP可以直接放在同一个targets下,如下:

[root@localhost mysqld_exporter]# cat /data/prometheus/mysql.yml
- labels:
service: test
targets:
- 10.10.30.165
- 10.10.20.14

3.3. grafana页面dashboard切换

根据需要切换监控模板

然后,就能看到你想要的数据

到这里,本文也接近尾声了,相信大家按照本文介绍的步骤操作一翻,已经一睹了grafana炫酷界面的芳容了,谢谢大家阅读!

| 作者简介

罗小波·沃趣科技高级数据库技术专家

IT从业多年,历任运维工程师,高级运维工程师,运维经理,数据库工程师,曾参与版本发布系统,轻量级监控系统,运维管理平台,数据库管理平台的设计与编写,熟悉MySQL的体系结构时,InnoDB存储引擎,喜好专研开源技术,追求完美。

构建狂拽炫酷屌的 MySQL 监控平台相关推荐

  1. (升级版)构建狂拽炫酷屌的MySQL监控平台

    作者:罗小波,<千金良方--MySQL性能优化金字塔法则>作者之一. 熟悉MySQL体系结构,擅长数据库的整体调优,喜好专研开源技术,并热衷于开源技术的推广,在线上线下做过多次公开的数据库 ...

  2. 口碑好的mysql数据监控平台_构建狂拽炫酷屌的 MySQL 监控平台

    prometheus+grafana 对于现在这个时间点来说,相信很多同行都应该已经开始玩起来了,当仍然可能有一部分人可能还不知道prometheus+grafana 的存在,也可能还有一部分人虽然知 ...

  3. 大数据不是你想有就能有,如何才能“狂拽炫酷吊炸天”

    如今,大数据正成为了各大企业都争相推拥的热词,其发展的如火如荼,迅疾如风,猛烈似火.从互联网领域的BAT到各类智能硬件,几乎所有的企业都正在谈大数据. 在这样一股大数据的洪流之下,专门做大数据分析的公 ...

  4. 京东电器“220V带电新人类”:找寻狂拽炫酷吊炸天的科技感

    忽如一夜春风来,进入2018年,各大电商公司开的实体店已蔚然成风,遍地开花之下,究竟这些实体店有何魅力吸引到用户?各家到底做得如何? 没有体验就没有发言权. 于是乎,就在昨日(10月27日),北京市朝 ...

  5. C语言小项目之“究极无敌螺旋丸极爆炸狂拽炫酷五彩棒的”通讯录之*派小猩*作品

    C语言小项目之"究极无敌螺旋丸极爆炸狂拽炫酷五彩棒的"通讯录之派小猩作品 项目概述 基于C语言开发一个多功能的通讯录 功能目录 1.增加 2.删除 3.查找 4.修改 5.显示 6 ...

  6. 程序员必备狂拽炫酷吊炸天的动效神器

    安利一波CSS.JS炫酷动效在线下载网站 # bttn.css 专注分享网页按钮的样式库 # Hover-Buttons 一个可以生成代码的网站 # granim.js 基于canvas的背景颜色渐变 ...

  7. 狂拽炫酷校园拽少与阳光善良少女的校园事

    男主:李·莱昂拿多·龖霎弼·卡布其·诺·杰克·火惹熬·南宫叶晨·富奎 女1:李·安吉丽娜·樱雪宇韩灵·熙梦月·欣·魅·冰雪殇璃陌梦·丽馨·雨 女2:宋·凤·颜鸢·可薇·千梦然丝伤·幽幻紫银·萝莉新梦妖 ...

  8. 用pygame_menu的十行代码给你的pygame添加一个狂拽炫酷炸的游戏菜单

    简介 通常进入游戏前都会有一个游戏菜单,选择游戏难度.查看帮助.选择模式等等,但是pygame并没有提供这个功能,于是只能凭借互联网的智慧和EasyX的经验:用pygame"绘制" ...

  9. Prometheus + Granafa 构建高大上的MySQL监控平台

    作者 | 小罗ge11 来源 | http://r6d.cn/UdS6 概述 对于MySQL的监控平台,相信大家实现起来有很多了:基于天兔的监控,还有基于zabbix相关的二次开发.相信很多同行都应该 ...

最新文章

  1. LeetCode中等题之煎饼排序
  2. mysql decimal(10,2)对应java类型
  3. 成功解决Exception “unhandled AttributeError“ module ‘cv2.cv2‘ has no attribute ‘estimateRigidTransform‘
  4. 竞赛图 计算机网络 应用题,我校学子获2020年“中国高校计算机大赛-网络技术挑战赛”全国总决赛一等奖(图)...
  5. Spring4.x(9)--Spring的Hibernate事务-XML
  6. Zeppelin介绍
  7. php写cms,浅谈thinkPHP,国内写CMS利用率最高的框架
  8. db2v9/9.5高级应用开发_使用 Vue.js 2.0 开发单页应用
  9. 懒惰是人类进步的动力,勤奋是实现偷懒的途径
  10. java+io+scanner_Java知识点总结(JavaIO- Scanner类 )
  11. Decorator模式设计模式
  12. android日志统计管理,时间日志app下载-时间日志(时间统计管理)下载v1.1.1 安卓版-西西软件下载...
  13. SpringBoot轻量级博客/论坛(包含 SpringBoot、SSM、Dubbo多个版本实现代码) 初云博客
  14. rtlab matlab版本,电力电子技术教学中电力仿真软件选择与应用.doc
  15. c语言stl大全,C++ STL库应用汇总
  16. Java练习题2-基础(含解析)
  17. 读书笔记 - 自控力
  18. Otsu算法原理与python实现
  19. vue form表单验证清除
  20. matlab画入射系数和透射系数,反射系数和透射系数.ppt

热门文章

  1. 2019年攻克要塞软考培训服务项目全新升级
  2. 七牛云徐晶:基于 WebRTC 架构的直播课堂实践
  3. 中小型网站站长需要怎样的广告联盟
  4. 学习新语言的练手项目
  5. 存储控制器wwn号_浪潮存储系统AS5600用户手册V1.1.pdf
  6. nginx php 404 not found,Laravel Nginx出现404 Not Found错误
  7. 中望3D 2021 线框设计——轮廓曲线
  8. nginx负载均衡策略
  9. 月亮网摘(2006.08.09)
  10. OpenGL(十八)——Qt OpenGL绘制一个3D世界