一,prometheus安装
这里使用2.25版本
[root@\ prometheus~]# wget https://github.com/prometheus/prometheus/releases/download/v2.25.0/prometheus-2.25.0.linux-amd64.tar.gz
# 解压
[root@\ prometheus~]# tar xf prometheus-2.25.0.linux-amd64.tar.gz -C /usr/local/
# 配置文件有端口信息
[root@\ prometheus/usr/local/prometheus-2.25.0.linux-amd64]# vim prometheus.yml
... ...
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ['localhost:9090']
# 启动
[root@\ prometheus~]# /usr/local/prometheus-2.25.0.linux-amd64/prometheus --config.file="/usr/local/prometheus-2.25.0.linux-amd64/prometheus.yml"
level=info ts=2021-04-22T11:35:05.406Z caller=main.go:366 msg="No time or size retention was set so using the default time retention" duration=15d
level=info ts=2021-04-22T11:35:05.407Z caller=main.go:404 msg="Starting Prometheus" version="(version=2.25.0, branch=HEAD, revision=a6be548dbc17780d562a39c0e4bd0bd4c00ad6e2)"
level=info ts=2021-04-22T11:35:05.407Z caller=main.go:409 build_context="(go=go1.15.8, user=root@615f028225c9, date=20210217-14:17:24)"
level=info ts=2021-04-22T11:35:05.407Z caller=main.go:410 host_details="(Linux 3.10.0-1160.21.1.el7.x86_64 #1 SMP Tue Mar 16 18:28:22 UTC 2021 x86_64 prometheus (none))"
level=info ts=2021-04-22T11:35:05.407Z caller=main.go:411 fd_limits="(soft=65535, hard=65535)"
level=info ts=2021-04-22T11:35:05.407Z caller=main.go:412 vm_limits="(soft=unlimited, hard=unlimited)"
level=info ts=2021-04-22T11:35:05.424Z caller=web.go:532 component=web msg="Start listening for connections" address=0.0.0.0:9090
level=info ts=2021-04-22T11:35:05.425Z caller=main.go:779 msg="Starting TSDB ..."
level=info ts=2021-04-22T11:35:05.451Z caller=tls_config.go:191 component=web msg="TLS is disabled." http2=false
level=info ts=2021-04-22T11:35:05.455Z caller=head.go:668 component=tsdb msg="Replaying on-disk memory mappable chunks if any"
level=info ts=2021-04-22T11:35:05.455Z caller=head.go:682 component=tsdb msg="On-disk memory mappable chunks replay completed" duration=10.553µs
level=info ts=2021-04-22T11:35:05.455Z caller=head.go:688 component=tsdb msg="Replaying WAL, this may take a while"
level=info ts=2021-04-22T11:35:05.455Z caller=head.go:740 component=tsdb msg="WAL segment loaded" segment=0 maxSegment=0
level=info ts=2021-04-22T11:35:05.455Z caller=head.go:745 component=tsdb msg="WAL replay completed" checkpoint_replay_duration=38.238µs wal_replay_duration=236.584µs total_replay_duration=302.298µs
level=info ts=2021-04-22T11:35:05.456Z caller=main.go:799 fs_type=XFS_SUPER_MAGIC
level=info ts=2021-04-22T11:35:05.456Z caller=main.go:802 msg="TSDB started"
level=info ts=2021-04-22T11:35:05.456Z caller=main.go:928 msg="Loading configuration file" filename=/usr/local/prometheus-2.25.0.linux-amd64/prometheus.yml
level=info ts=2021-04-22T11:35:05.493Z caller=main.go:959 msg="Completed loading of configuration file" filename=/usr/local/prometheus-2.25.0.linux-amd64/prometheus.yml totalDuration=37.275538ms remote_storage=3.413µs web_handler=734ns query_engine=18.269µs scrape=36.424431ms scrape_sd=175.563µs notify=152.162µs notify_sd=43.368µs rules=55.178µs
level=info ts=2021-04-22T11:35:05.493Z caller=main.go:751 msg="Server is ready to receive web requests."
页面如下
访问页面,记得加上端口
image.png
1,监控主机安装node_exporter
# 增加监控主机,需要安装以下
https://github.com/prometheus/node_exporter/releases/download/v1.1.1/node_exporter-1.1.1.linux-amd64.tar.gz
[root@\ mysql/opt]# wget https://github.com/prometheus/node_exporter/releases/download/v1.1.1/node_exporter-1.1.1.linux-amd64.tar.gz
[root@\ mysql/opt]# tar xf node_exporter-1.1.1.linux-amd64.tar.gz -C /usr/local/
[root@\ mysql/opt]# cd /usr/local/node_exporter-1.1.1.linux-amd64/
[root@\ mysql/usr/local/node_exporter-1.1.1.linux-amd64]# ll
-rw-r--r--. 1 3434 3434 11357 Feb 13 21:42 LICENSE
-rwxr-xr-x. 1 3434 3434 19170241 Feb 13 21:31 node_exporter
-rw-r--r--. 1 3434 3434 463 Feb 13 21:42 NOTICE
# 启动
[root@\ mysql/usr/local/node_exporter-1.1.1.linux-amd64]# ./node_exporter
# 查看运行端口
[root@\ mysql~]# netstat -lntp | grep 9100
tcp6 0 0 :::9100 :::* LISTEN 26761/./node_export
# 配置文件增加主机,注意是prometheus主机
[root@\ prometheus/usr/local/prometheus-2.25.0.linux-amd64]# vim prometheus.yml
- job_name: 'mtsql'
static_configs:
- targets: ['192.168.15.151:9100']
prometheus重启,再次访问,如下图
image.png
image.png
2,数据库监控安装插件mysqld_exporter
监控mysql,需要安装插件
[root@\ mysql/opt]# wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.12.1/mysqld_exporter-0.12.1.linux-amd64.tar.gz
[root@\ mysql/opt]# tar xf mysqld_exporter-0.12.1.linux-amd64.tar.gz -C /usr/local/
# 授权
mysql> grant select,replication client,process ON *.* to 'mysql_monitor'@'localhost' identified by '123';
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
# (注意:授权ip为localhost,因为不是prometheus服务器来直接找mariadb 获取数据,而是prometheus服务器找mysql_exporter,mysql_exporter 再找mariadb。所以这个localhost是指的mysql_exporter的IP)
# 创建一个mariadb配置文件,写上连接的用户名与密码(和上面的授权的用户名 和密码要对应)
[root@\ mysql/opt]# cd /usr/local/mysqld_exporter-0.12.1.linux-amd64/
[root@\ mysql/usr/local/mysqld_exporter-0.12.1.linux-amd64]# vim .my.conf
[client]
user=mysql_monitor
password=123
# 启动mysqld_exporter
[root@\ mysql/usr/local/mysqld_exporter-0.12.1.linux-amd64]# ./mysqld_exporter --config.my-cnf=/usr/local/mysqld_exporter-0.12.1.linux-amd64/.my.conf &
# 启动端口
[root@\ mysql/usr/local/mysqld_exporter-0.12.1.linux-amd64]# netstat -lntp | grep 9104
tcp6 0 0 :::9104 :::* LISTEN 26875/./mysqld_expo
PS:
需要把配置文件的端口更改为9104,重新访问
二,grafana
1,下载
Grafana是⼀个开源的度量分析和可视化⼯具,可以通过将采集的数据分析,查询,然后进⾏可视化的展示,并能实现报警。
使⽤Grafana连接Prometheus
在grafana服务器上安装grafana。
https://grafana.com/
https://repo.huaweicloud.com/grafana/7.4.3/
[root@\ nfs/opt]# wget https://repo.huaweicloud.com/grafana/7.4.3/grafana-7.4.3.linux-amd64.tar.gz
[root@\ nfs/opt]# tar -xf grafana-7.4.3.linux-amd64.tar.gz -C /usr/local/
#增加环境变量
[root@\ nfs/opt]# vim /etc/profile
GAFANA_HOME=/usr/local/grafana-7.4.3
PATH=$PATH:$GAFANA_HOME/bin
export PATH
[root@\ nfs/opt]# source /etc/profile
# 启动
[root@\ nfs/usr/local/grafana-7.4.3/bin]# ./grafana-server -config ../conf/defaults.ini
访问如下
# 默认账号和密码为admin
image.png
访问首页
`
添加数据源 image.png image.png image.png image.png2,Grafana图形显示MySQL监控数据
Grafana图形显示MySQL监控数据
在grafana上修改配置⽂件,并下载安装mysql监控的dashboard
(包含相关json⽂件,这些json⽂件可以看作是开发⼈员开发的⼀个监控模板)
[root@\ nfs~]# mkdir -p /var/lib/grafana/
[root@\ nfs/var/lib/grafana]# git clone https://github.com/percona/grafana-dashboards.git
[root@\ nfs/var/lib/grafana]# ll
total 0
drwxr-xr-x. 8 root root 272 Apr 23 15:29 grafana-dashboards
[root@\ nfs/var/lib/grafana]# cd grafana-dashboards/
[root@\ nfs/var/lib/grafana/grafana-dashboards]# ll
total 92
-rw-r--r--. 1 root root 269 Apr 23 15:29 codecov.yml
-rw-r--r--. 1 root root 2867 Apr 23 15:29 CONTRIBUTING.md
drwxr-xr-x. 2 root root 4096 Apr 23 15:29 dashboards
-rw-r--r--. 1 root root 399 Apr 23 15:29 docker-compose.yml
-rw-r--r--. 1 root root 34520 Apr 23 15:29 LICENSE
-rw-r--r--. 1 root root 828 Apr 23 15:29 Makefile
drwxr-xr-x. 2 root root 221 Apr 23 15:29 misc
-rw-r--r--. 1 root root 28649 Apr 23 15:29 package-lock.json
drwxr-xr-x. 2 root root 4096 Apr 23 15:29 panels
drwxr-xr-x. 4 root root 254 Apr 23 15:29 pmm-app
-rw-r--r--. 1 root root 2571 Apr 23 15:29 README.md
[root@\ nfs/var/lib/grafana/grafana-dashboards]# cd dashboards/
[root@\ nfs/var/lib/grafana/grafana-dashboards/dashboards]# ll
total 9932
-rw-r--r--. 1 root root 24679 Apr 23 15:29 Advanced_Data_Exploration.json
-rw-r--r--. 1 root root 76839 Apr 23 15:29 CPU_Utilization_Details.json
-rw-r--r--. 1 root root 100017 Apr 23 15:29 Disk_Details.json
-rw-r--r--. 1 root root 380198 Apr 23 15:29 HAProxy_Instance_Summary.json
-rw-r--r--. 1 root root 100445 Apr 23 15:29 Home_Dashboard.json
-rw-r--r--. 1 root root 251597 Apr 23 15:29 Memory_Details.json
... ...
[root@\ nfs/var/lib/grafana]# cp grafana-dashboards/dashboards/* .
# 最后一行添加如下
[root@\ nfs/opt/grafana-dashboards/dashboards]# vim /usr/local/grafana-7.4.3/conf/defaults.ini
[dashboards.json]
enabled=true
path=/var/lib/grafana/dashboards
# 重新启动
[root@\ nfs/usr/local/grafana-7.4.3/bin]# grafana-server -config ../conf/defaults.ini
image.png
用json文件查看监控内容
网友评论