Grafana监控主机性能以及数据可视化

Grafana 是大规模指标数据可视化展现的开源工具,广泛应用于监控报警、数据分析。它可以支持多种数据源,包括 Graphite,Elasticsearch,CloudWatch,InfluxDB,OpenTSDB,Prometheus,MySQL,Postgres,Microsoft SQL Server (MSSQL)。

本次实验所用到的服务:

使用服务及端口
服务 端口 说明
MySQL 3306 数据库
MySQL-exporter 9104 用于监控MySQL的服务,作用为取MySQL的状态
Grafana可视化监控 3000 基于zabbix的监控工具,用于监控报警、数据分析,本次实验用于监控MySQL和主机状态作为前端显示
Prometheus监控 9090 通过普罗米修斯来监控主机状态,CPU、swap空间、内存、线程数等
node_exporter 9100 用于监控linux主机的状态,通过此服务推送状态给普罗米修斯

主机基础监控效果图:

 

 

 

MySQL状态效果图:

 安装配置:

由于使用的是云主机,基本带宽比较小,所以使用docker搭建相关服务

docker安装zabbix-mysql

docker pull zabbix/zabbix-server-mysql
docker run --name zabbix-server-mysql -e DB_SERVER_HOST="localhost" -e MYSQL_USER="root" -e MYSQL_PASSWORD="123456" -d zabbix/zabbix-server-mysql

查看zabbix启动日志

docker logs -f zabbix-server-service

安装zabbix自己的数据库,由于需要监控主机的MySQL所有的业务,所以选用开其他端口

docker run -dit -p 3307:3306 --name zabbix-mysql --restart always -e MYSQL_DATABASE="zabbix" -e MYSQL_USER="zabbix" -e MYSQL_PASSWORD="123" -e MYSQL_ROOT_PASSWORD="123" mysql

安装zabbix相关插件以及服务

docker run -dit -p 10051:10051 --name=zabbix-server-mysql --restart=always -e DB_SERVER_HOST="zabbix-mysql" -e MYSQL_DATABASE="zabbix" -e MYSQL_USER="zabbix" -e MYSQL_PASSWORD="123" -e MYSQL_ROOT_PASSWORD="123456" -e  zabbix/zabbix-server-mysql

docker run -p 80:80 --name zabbix-web-nginx-mysql --restart=always -e DB_SERVER_HOST="zabbix-mysql" -e MYSQL_DATABASE="zabbix" -e MYSQL_USER="zabbix" -e MYSQL_PASSWORD="123" -e MYSQL_ROOT_PASSWORD="123456" -e ZBX_SERVER_HOST="zabbix-server-mysql" zabbix/zabbix-web-nginx-mysql

docker下拉其他镜像,ps 命令:docker pull grafana/grafana

REPOSITORY                                TAG                 CREATED           SIZE
docker.io/grafana/grafana                 latest              5 days ago        202 MB
docker.io/prom/prometheus                 latest              6 days ago        185 MB
docker.io/zabbix/zabbix-web-nginx-mysql   latest              6 days ago        165 MB
docker.io/zabbix/zabbix-server-mysql      centos-latest       6 days ago        356 MB
docker.io/zabbix/zabbix-server-mysql      latest              6 days ago        68.1 MB
docker.io/zabbix/zabbix-java-gateway      latest              6 days ago        83.8 MB
docker.io/mariadb                         latest              11 days ago       405 MB
docker.io/mysql                           5.7                 12 days ago       447 MB
docker.io/mysql                           latest              12 days ago       556 MB
docker.io/prom/node-exporter              latest              2 months ago      26 MB
docker.io/prom/mysqld-exporter            latest              22 months ago     17.5 MB

启动Grafana

docker run  -d --name grafana -p 3000:3000   -v /data/grafana:/var/lib/grafana  grafana/grafana

启动Prome / Prometheus 

docker run  -d   -p 9090:9090   -v /opt/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml    prom/prometheus

其中两个配置文件内容如下

[root@Huawei ~/ansible]# cat /opt/prometheus/prometheus.yml 
global:
  scrape_interval:     60s
  evaluation_interval: 60s
 
scrape_configs:
  - job_name: prometheus
    static_configs:
      - targets: ['localhost:9090']
        labels:
          instance: prometheus
 
  - job_name: linux
    static_configs:
      - targets: ['localhost:9100']
        labels:
          instance: localhost
          
  - job_name: mysql_local
    static_configs:
      - targets: ['localhost:9104']
        labels:
          instance: localhost      
[root@Huawei ~/ansible]# cat /etc/prometheus/prometheus.yml 
global:
  scrape_interval:     15s 
  evaluation_interval: 15s

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
    - targets: ['localhost:9090']
  - job_name: 'node_exporter'
    static_configs:
    - targets: ['localhost:9100']
  - job_name: 'mysql_localhost'
    static_configs:
    - targets: ['localhost:9104']

启动监控linux的node节点,用以监控linux状态

  docker run -d -p 9100:9100 \
  -v "/proc:/host/proc:ro" \
  -v "/sys:/host/sys:ro" \
  -v "/:/rootfs:ro" \
  prom/node-exporter

查看Linux状态是否可以通

[root@Huawei ~]# curl http://localhost:9100/metrics
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 53542    0 53542    0     0  3541k      0 --:--:-- --:--:-- --:--:-- 3734k
# HELP go_gc_duration_seconds A summary of the GC invocation durations.
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 6.083e-06
go_gc_duration_seconds{quantile="0.25"} 7.459e-06
···········
promhttp_metric_handler_requests_total{code="200"} 1411
promhttp_metric_handler_requests_total{code="500"} 0
promhttp_metric_handler_requests_total{code="503"} 0

启动linux-mysql的node节点,用于监控主机mysql状态

docker run -d --name mysql-exporter -p 9104:9104 -e DATA_SOURCE_NAME="grafana:123@(localhost:3306)/mysql" prom/mysqld-exporter

防火墙开放端口

 firewall-cmd --add-port=9090/tcp --permanent
 firewall-cmd --add-port=9100/tcp --permanent
 firewall-cmd --add-port=9104/tcp --permanent

查看当前的端口状态,这里实验没有开启zabbix

[root@Huawei ~]# netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      10748/mysqld        
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      954/sshd            
tcp6       0      0 :::9090                 :::*                    LISTEN      2370/docker-proxy-c 
tcp6       0      0 :::9100                 :::*                    LISTEN      24049/node_exporter 
tcp6       0      0 :::9104                 :::*                    LISTEN      32604/docker-proxy- 
tcp6       0      0 :::3000                 :::*                    LISTEN      10085/docker-proxy- 

检查当前MySQL接口是否可以通

curl http://localhost:9104/metrics
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0# HELP go_gc_duration_seconds A summary of the GC invocation durations.
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 9.832e-06
go_gc_duration_seconds{quantile="0.25"} 1.092e-05
go_gc_duration_seconds{quantile="0.5"} 1.1677e-05
go_gc_duration_seconds{quantile="0.75"} 1.227e-05

·············
promhttp_metric_handler_requests_total{code="200"} 1047
promhttp_metric_handler_requests_total{code="500"} 0
promhttp_metric_handler_requests_total{code="503"} 0

由此可见,当前linux状态和MySQL状态都是可以通的

当前查看Prometheus状态

http://localhost:9090/targets

 

 随机测试主机状态,查看Prometheus是否可以绘图

 

 由此可见,我们的服务均已部署成功,接下来登录Grafana进行配置

 

 

 

 测试是否已完成,数据库连接成功

 

 这里开始添加监控

 

 上传做好的视图json文件

 

 MySQL状态成功显示

 

 这里推荐Prometheus的UID  8919

 

posted @ 2021-05-24 18:08  Security  阅读(2937)  评论(0编辑  收藏  举报