Grafana+prometheus监控体系实践

一、前言

1.1、grafana介绍

Grafana是一个开源指标分析和可视化套件,常用于可视化基础设施的性能数据和应用程序分析的时间序列数据。也可以应用于其他领域,包括工业传感器,家庭自动化,天气和过程控制。但请注意,我们使用Grafana最关心的是如何把数据进行聚合后进行展示。
Grafana支持多种不同的时序数据库数据源,Grafana对每种数据源提供不同的查询方法,而且能很好的支持每种数据源的特性。它支持下面几种数据源:Graphite、Elasticsearch、CloudWatch、InfluxDB、OpenTSDB、Prometheus、MySQL、Postgres、Microsoft SQL Server (MSSQL)。每种数据源都有相应的文档,可以将多个数据源的数据合并到一个单独的仪表板上。

1.2、Prometheus介绍

Prometheus是一个开源监控系统,集数据采集、存储与展示为一体,功能十分强大,官网架构图如下
Grafana+prometheus监控体系实践
架构图中各模块功能解析:
Prometheus Server:Prometheus Sever是Prometheus组件中的核心部分,负责实现对监控数据的获取,存储及查询
Exporters:Exporter将监控数据采集的端点通过HTTP服务的形式暴露给Prometheus Server,Prometheus Server通过访问该Exporter提供的Endpoint端点,即可以获取到需要采集的监控数据
Alertmanager:实现prometheus的告警功能
webui:通过grafana来实现webui展示
PushGateway:Prometheus数据采集基于Prometheus Server从Exporter pull数据,因此当网络环境不允许Prometheus Server和Exporter进行通信时,可以使用PushGateway来进行中转。通过PushGateway将内部网络的监控数据主动Push到Gateway中,Prometheus Server采用针对Exporter同样的方式,将监控数据从PushGateway pull到Prometheus Server。

1.3、监控流程

Prometheus从exporters中收取数据并入库,grafana从prometheus中提取数据图表展示

1.4、准备工作

节点:
Node1:192.168.143.130 Prometheus server & grafana
Node2:192.168.143.131 node_exporter

软件版本:
Granafa 5.3.2
Prometheus 2.5.0

二、部署Grafana+prometheus

2.1、安装Grafana+prometheus(server)

Node1:
rpm -ivh grafana-5.3.2-1.x86_64.rpm 
tar xzf  prometheus-2.5.0.tar.gz -C /usr/local/ 

2.2、编辑prometheus配置文件,加入被监控主机node2

vim prometheus.yml

scrape_configs:
  - job_name: 'node2_sys'
    static_configs:
    - targets: ['192.168.143.131:9100'] 

2.3、添加用户

groupadd prometheus
useradd -g prometheus -s /sbin/nologin prometheus
chown -R prometheus:prometheus /usr/local/prometheus-2.5.0/

2.4、启动脚本

vim start.sh 
#!/bin/bash
#
./prometheus --config.file=prometheus.yml >>Prometheus.log &>/dev/null &

2.5、启动Grafana+prometheus

prometheus:./start.sh
Grafana:systemctl start grafana-server.service

三、部署node_exporter

3.1、安装node_exporter(client)

Node2:
tar xzf node_exporter.tar.gz -C /usr/local/

3.2、添加用户

groupadd prometheus
useradd -g prometheus -s /sbin/nologin prometheus
chown -R prometheus:prometheus /usr/local/node_exporter/

3.3、添加开机启动

vim /usr/lib/systemd/system/node_exporter.service
[Unit]
Description=Prometheus
Documentation=https://prometheus.io/
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/node_exporter/node_exporter --web.listen-address=:9100 --collector.textfile.directory=/usr/local/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target

3.4、启动node_exporter

systemctl start node_exporter.service
systemctl enable node_exporter.service

四、grafana界面设置

4.1、prometheus-ui查看监控数据

url:http://192.168.143.130:9090
Grafana+prometheus监控体系实践
根据输入的key找到你要监控的数据,Element下面的是向grafana上添加的公式

4.2、grafana添加数据源

url: http://192.168.143.130:3000
默认用户名密码admin
Grafana+prometheus监控体系实践
添加DataSource,type中选择prometheus,url中输入prometheus地址,name中输入该DataSource名称,保存
Grafana+prometheus监控体系实践

4.3、添加dashboard

Grafana+prometheus监控体系实践

4.4、添加graph

Grafana+prometheus监控体系实践

4.5、编辑graph

Grafana+prometheus监控体系实践

扫描二维码关注公众号,回复: 5923950 查看本文章

在DataSource选择添加好的数据源,在空白处填入监控公式
Grafana+prometheus监控体系实践

4.6、使用网络中现有的表盘模版

import导入
Grafana+prometheus监控体系实践
输入表盘模版url: https://grafana.com/dashboards/8919
Grafana+prometheus监控体系实践
选择数据源
Grafana+prometheus监控体系实践

Grafana+prometheus监控体系实践

五、自定义监控脚本

5.1、启动时加参数定义监控脚本采集信息输出目录

启动时加参数--collector.textfile.directory

[root@node2 node_exporter]# cat /usr/lib/systemd/system/node_exporter.service
[Unit]
Description=Prometheus
Documentation=https://prometheus.io/
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/node_exporter/node_exporter --web.listen-address=:9100 --collector.textfile.directory=/usr/local/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target

5.2、自定义监控脚本

[root@node2 node_exporter]# cat mysql_status.sh 
#!/bin/bash
systemctl status mariadb.service |grep running &>/dev/null
if [ $? -eq 0 ];then
    echo "mysql_status 0"
else 
    echo "mysql_status 1"
fi

5.3、脚本输出格式

后缀为.prom,文件中存自定义的key value值
./mysql_status.sh >mysql_status.prom

5.4、prometheus以采集到mysql_status信息

Grafana+prometheus监控体系实践

5.5、grafana添加图表

Grafana+prometheus监控体系实践

参考文章
• Prometheus github:https://github.com/prometheus
• grafana github:https://github.com/grafana/grafana
• Prometheus node_exporter:https://github.com/prometheus/node_exporter
• Prometheus配置文件详解:https://prometheus.io/docs/prometheus/latest/configuration/configuration/
• 公司大牛写的部署文档

猜你喜欢

转载自blog.51cto.com/jiayimeng/2380214