使用Prometheus进行系统监控,包括Mysql、Redis,并使用Grafana图形化表示

news2024/9/25 19:01:13

Prometheus是一个开源的的监控工具,而且还免费。这一次我们用Prometheus来对之前安装的所有服务,包括Mysql、Redis、系统状况等进行监控,并结合Grafana进行图形化展示

Prometheus下载和安装

下载地址(以下所有插件的官方下载地址都是这个):https://prometheus.io/download/

选择合适版本用wget进行下载:

wget -P "/opt" https://github.com/prometheus/prometheus/releases/download/v2.54.1/prometheus-2.54.1.linux-amd64.tar.gz

可以看到下载默认是从gayhub上下载的,如果没有代理的话,下载速度十分感人。所以我这边使用国内镜像https://mirrors.tuna.tsinghua.edu.cn/github-release/prometheus/prometheus/

wget -P "/opt" https://mirrors.tuna.tsinghua.edu.cn/github-release/prometheus/prometheus/2.54.1%20_%202024-08-27/prometheus-2.54.1.linux-amd64.tar.gz

接下来进行解压和安装,顺便把文件夹改个名字:

tar -xvzf /opt/prometheus-2.54.1.linux-amd64.tar.gz
mv prometheus-2.54.1.linux-amd64 prometheus-2.54.1

安装脚本

将以上过程,用shell脚本实现。并配置开机启动。

#!/bin/bash
#Prometheus安装和配置脚本

#下载
wget -P "/opt" https://mirrors.tuna.tsinghua.edu.cn/github-release/prometheus/prometheus/2.54.1%20_%202024-08-27/prometheus-2.54.1.linux-amd64.tar.gz

# 解压
tar -zxf /opt/prometheus-2.54.1.linux-amd64.tar.gz -C /opt/
mv /opt/prometheus-2.54.1.linux-amd64 /opt/prometheus-2.54.1
# 创建用户和组
# groupadd prometheus
# useradd -g prometheus -s /sbin/nologin prometheus

# 创建目录
mkdir -p /opt/prometheus-2.54.1/data

# 修改权限
# chown -R prometheus:prometheus /opt/prometheus-2.54.1/

# 配置开机启动
cat > /etc/systemd/system/prometheus.service << EOF
[Unit]
Description=Prometheus
Documentation=https://prometheus.io
Wants=network-online.target
After=network-online.target

[Service]
Type=simple
User=root
Group=root
ExecStart=/opt/prometheus-2.54.1/prometheus \
--config.file=/opt/prometheus-2.54.1/prometheus.yml \
--storage.tsdb.path=/opt/prometheus-2.54.1/data

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl start prometheus.service
systemctl status prometheus.service
systemctl enable prometheus.service

接下来,我们就可以从浏览器访问了,http://192.168.32.21:9090/注意打开防火墙9090端口。

我们点击status-target,就可以默认看到本机。如下图所示:

在这里插入图片描述

配置文件

各配置文件的意义可以参考这个链接,

https://www.cnblogs.com/liwenchao1995/p/16895710.html

配置文件默认就在安装目录下,

[root@node21 prometheus-2.54.1]# cat /opt/prometheus-2.54.1/prometheus.yml 
#全局配置 (如果有内部单独设定,会覆盖这个参数)
global:

#告警插件定义。这里会设定alertmanager这个报警插件。
alerting:

#告警规则。 按照设定参数进行扫描加载,用于自定义报警规则,其报警媒介和route路由由alertmanager插件实现。
rule_files:

#采集配置。配置数据源,包含分组job_name以及具体target。又分为静态配置和服务发现
scrape_configs:

#用于远程存储写配置
remote_write:

#用于远程读配置
remote_read:

Grafana安装

Grafana是一个可视化的平台,它可以采用Prometheus的数据源,来对服务器的监控实现图形化展示。所以经常搭配使用。我们下载rpm文件,然后使用yum安装。

#!/bin/bash
# Grafana安装配置

# 下载
wget -P "/opt" https://mirrors.tuna.tsinghua.edu.cn/grafana/yum/rpm/Package/grafana-enterprise-10.1.0-1.x86_64.rpm
yum localinstall /opt/grafana-enterprise-10.1.0-1.x86_64.rpm -y

# 设置开机启动
systemctl enable grafana-server
# 启动
systemctl start grafana-server

# 开放端口
firewall-cmd --permanent --add-port=3000/tcp
firewall-cmd --reload

安装后可以使用ip+端口进行访问,默认账号密码都是admin。注意更改密码。

Prometheus+Grafana监控MySQL

参考了如下链接:https://www.cnblogs.com/easydb/p/14151866.html

整个服务的架构如下:

在这里插入图片描述

安装node_exporter

node_exporter是用来收集服务器信息的一个组件,该组件需要安装在被监控的机器上。我们来简单写一个安装脚本

#!/bin/bash
# mysql监控组件node_exporter和mysqld_exporter

# 下载,没找到国内下载源,下载速度很慢。建议用scp发送到其他机器。
wget -P "/opt" https://github.com/prometheus/node_exporter/releases/download/v1.8.2/node_exporter-1.8.2.linux-amd64.tar.gz
#scp "/opt/node_exporter-1.8.2.linux-amd64.tar.gz" root@"$target_ip":/opt/
# 解压
tar -zxvf /opt/node_exporter-1.8.2.linux-amd64.tar.gz -C /opt/
# 改名
mv /opt/node_exporter-1.8.2.linux-amd64 /opt/node_exporter
# 设置开机启动
cat > /etc/systemd/system/node_exporter.service << EOF
[Unit]
Description=node_exporter
After=network.target
[Service]
Type=simple
ExecStart=/opt/node_exporter/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
# 启动
systemctl daemon-reload
systemctl start node_exporter
systemctl enable node_exporter

在目标主机上进行安装:

#!/bin/bash
#在指定范围主机上,批量安装node_exporter。需要准备好配置文件和安装文件

start_ip=22
end_ip=24
network="192.168.32"

for ip in $(seq ${start_ip} ${end_ip})
do
    target_ip=${network}.${ip}
    scp "/opt/node_exporter-1.8.2.linux-amd64.tar.gz" root@"$target_ip":/opt/
    scp "/etc/systemd/system/node_exporter.service" root@"$target_ip":/etc/systemd/system/
    echo "正在 ${target_ip} 安装node_exporter..."
    ssh root@"$target_ip" << EOF
        tar -zxf /opt/node_exporter-1.8.2.linux-amd64.tar.gz -C /opt/
        mv /opt/node_exporter-1.8.2.linux-amd64 /opt/node_exporter
        systemctl daemon-reload
        systemctl start node_exporter
        systemctl enable node_exporter
EOF
    echo "${target_ip} 上的node_exporter配置完成..."
done

访问如下页面,如果能看到说明成功了(注意防火墙需要开放9100端口)

在这里插入图片描述

修改Prometheus的配置文件

在被监控主机端安装了node_exporter后,就可以在Prometheus的配置中,添加目标主机来进行监控了,修改配置文件如下所示:

#!/bin/bash
#修改配置文件,添加被监控主机

# 目标主机 IP 范围
network="192.168.32"
start_ip=22
end_ip=24

for ip in $(seq ${start_ip} ${end_ip})
do
    target_ip=${network}.${ip}
    echo "正在写入$target_ip的配置"
    cat >> /opt/prometheus-2.54.1/prometheus.yml << EOF
  - job_name: '$target_ip'      # 给被监控主机取个名字,我这里直接填的IP
    static_configs:
    - targets: ['$target_ip:9100']      # 被监控主机的IP和端口
EOF

done

# 重启服务
systemctl stop prometheus.service
systemctl start prometheus.service
systemctl status prometheus.service

再次访问,已经可以看到我们添加的这几台机器了,如下所示

在这里插入图片描述

安装mysqld_exporter

监控mysql服务需要安装mysqld_exporter组件,我们使用如下脚本在目标主机上安装。顺便添加开机启动和配置文件:

简单说明下,我们需要准备两个配置文件

  1. mysqld_exporter.cnf用来指定连接mysql的用户名和密码
  2. 另一个mysqld_exporter.service是用来做开机自动启动的
#!/bin/bash
#在指定范围主机上,批量安装node_exporter。需要准备好配置文件和安装文件
# wget -P "/opt/" https://github.com/prometheus/mysqld_exporter/releases/download/v0.15.1/mysqld_exporter-0.15.1.linux-amd64.tar.gz

# 准备配置文件,用来指定mysql的用户,建议是建一个专门的监控用户
cat > /opt/mysqld_exporter.cnf << EOF
[client]
user=root
password=Abc@1234
# host=127.0.0.1:3306
EOF

# 准备开机启动的配置文件
cat > /opt/mysqld_exporter.service << EOF
[Unit]
Description=mysqld_exporter
After=network.target
[Service]
Type=simple
ExecStart=/opt/mysqld_exporter/mysqld_exporter \
  --config.my-cnf /etc/mysqld_exporter.cnf \
  --collect.slave_status \
  --collect.slave_hosts \
  --log.level=error \
  --collect.info_schema.processlist \
  --collect.info_schema.innodb_metrics \
  --collect.info_schema.innodb_tablespaces \
  --collect.info_schema.innodb_cmp \
  --collect.info_schema.innodb_cmpmem
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF

# 指定ip范围
start_ip=22
end_ip=23
network="192.168.32"

# 在指定主机上分别配置
for ip in $(seq ${start_ip} ${end_ip})
do
    target_ip=${network}.${ip}
    scp "/opt/mysqld_exporter-0.15.1.linux-amd64.tar.gz" root@"$target_ip":/opt/
    scp "/opt/mysqld_exporter.cnf" root@"$target_ip":/etc/
    scp "/opt/mysqld_exporter.service" root@"$target_ip":/etc/systemd/system/
    echo "正在 ${target_ip} 安装mysqld_exporter..."
    ssh root@"$target_ip" << EOF
    tar -zxf /opt/mysqld_exporter-0.15.1.linux-amd64.tar.gz -C /opt/
    mv /opt/mysqld_exporter-0.15.1.linux-amd64 /opt/mysqld_exporter
    systemctl daemon-reload
    systemctl start mysqld_exporter
    systemctl enable mysqld_exporter
    # 开放端口
    firewall-cmd --permanent --add-port=9104/tcp
    firewall-cmd --reload
EOF
    echo "正在写入$target_ip的配置"
    cat >> /opt/prometheus-2.54.1/prometheus.yml << EOF
  - job_name: 'mysql$ip'
    static_configs:
    - targets: ['$target_ip:9104']
EOF
done

# 重启服务
systemctl stop prometheus.service
systemctl start prometheus.service
systemctl status prometheus.service

然后启动后,访问ip+9104端口,如果看到如下界面,说明安装成功了

在这里插入图片描述

回到Prometheus界面,刷新也可以看到监控的mysql了

在这里插入图片描述

设置Grafana可视化

这个挺简单的,请参考原链接。我就不截图了,简单来说就是下面这个步骤

  1. 在添加Prometheus数据源
  2. 在dashboard导入模版11074
  3. 导入后即可查看详细

最终效果如下所示:

在这里插入图片描述

Mysql数据可视化

mysql的可视化和上面类似,导入对应的模版即可。比如7362。

还有一种方式是直接去Github上下载json文件,然后从Grafana的web界面导入。下载地址是:https://github.com/percona/grafana-dashboards 然后找到dashboard的mysql那个文件夹,下载kson文件,后面就不多介绍了。

效果如下:

在这里插入图片描述

监控Redis

redis的监控和上面的流程差不多,简单来说就是如下步骤

  1. 安装redis_exporter
  2. 在Prometheus中添加配置
  3. 在Grafana中添加模版,比如12776,https://grafana.com/grafana/dashboards/11835-redis-dashboard-for-prometheus-redis-exporter-helm-stable-redis-ha/。当然也可以用别的dashboard模版

用到的脚步如下:

首先是在本机上安装redis_expoeter:

#redis_exporter的安装配置

wget -P "/opt" https://github.com/oliver006/redis_exporter/releases/download/v1.63.0/redis_exporter-v1.63.0.linux-amd64.tar.gz

redis_pwd=Abc@1234

# 解压和安装
tar -zxf /opt/redis_exporter-v1.63.0.linux-amd64.tar.gz -C /opt/
mv /opt/redis_exporter-v1.63.0.linux-amd64 /opt/redis_exporter

# 开放端口,redis_exporter默认使用9121端口
firewall-cmd --permanent --add-port=9121/tcp
firewall-cmd --reload

# 准备配置文件
cat > /etc/systemd/system/redis_exporter.service << EOF
[Unit]
Description=redis_exporter
After=network.target
[Service]
Type=simple
ExecStart=/opt/redis_exporter/redis_exporter -redis.addr 127.0.0.1:6379  -redis.password $redis_pwd
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF

# 启动
systemctl daemon-reload
systemctl start redis_exporter
systemctl enable redis_exporter

# 写入Prometheus配置
    cat >> /opt/prometheus-2.54.1/prometheus.yml << EOF
  - job_name: 'redis_192.168.32.21'
    static_configs:
    - targets: ['192.168.32.21:9121']
EOF
# 重启Prometheus
systemctl restart prometheus

在指定范围的Ip内主机上安装:

#!/bin/bash
#在指定主机范围安装redis_exporter,默认本机上已经有安装文件和service文件

# 指定ip范围
start_ip=23
end_ip=24
network="192.168.32"

# 在指定主机上分别配置
for ip in $(seq ${start_ip} ${end_ip})
do
    target_ip=${network}.${ip}
    scp "/opt/redis_exporter-v1.63.0.linux-amd64.tar.gz" root@"$target_ip":/opt/
    scp "/etc/systemd/system/redis_exporter.service" root@"$target_ip":/etc/systemd/system/
    echo "正在 ${target_ip} 安装redis_exporter..."
    ssh root@"$target_ip" << EOF
    tar -zxf /opt/redis_exporter-v1.63.0.linux-amd64.tar.gz -C /opt/
    mv /opt/redis_exporter-v1.63.0.linux-amd64 /opt/redis_exporter
    systemctl daemon-reload
    systemctl start redis_exporter
    systemctl enable redis_exporter
    # 开放端口
    firewall-cmd --permanent --add-port=9121/tcp
    firewall-cmd --reload
EOF
    echo "正在写入$target_ip的redis配置"
    cat >> /opt/prometheus-2.54.1/prometheus.yml << EOF
  - job_name: 'redis_$ip'
    static_configs:
    - targets: ['$target_ip:9121']
EOF
done

# 重启服务
systemctl stop prometheus.service
systemctl start prometheus.service
systemctl status prometheus.service

最终效果如下

在这里插入图片描述

总结

至此,我们实现了用Prometheus监控系统信息、Mysql、Redis。本次实践只是单纯的获取了信息源,可以在Grafana面板中进行图形化表示而已。至于详细的监控指标和报警设置,那又是另外一个话题了。我们下次再聊。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2164507.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

二叉搜索树(来学包会) C++经验+1

目录 什么是二叉搜索树 解二叉搜索树 二叉搜索树的操作 二叉搜索树的插入&#xff08;三步走&#xff09; 二叉搜索树的搜索 二叉搜索树的删除 1.删除的节点是叶子节点 2.删除的节点只有一边的子树 3.删除的节点左子树和右子树都有 详细完整代码 什么是二叉搜索树 二…

MT76X8、MT7621、MT7981和QCA9531的GPIO列表

一、 MT76X8 GPIO列表; 二、 MT7621 GPIO列表; 三、MTK7981 GPIO列表; 四、QCA9531 GPIO列表;

CentOS 7 aarch64制作openssh 9.9p1 rpm包 —— 筑梦之路

本篇文章还是基于开源项目openssh-rpms制作。 https://github.com/boypt/openssh-rpms.git 官方发行说明&#xff1a; OpenSSH: Release Notes 1. 修改version.env 2. 下载源码包 openssl网站改版&#xff0c;下载地址和之前不一样了 # 下载openssl1.1.1w源码包cd downlo…

nacos 快速入门

目录 什么是 Nacos Nacos 的主要特点&#xff1a; Dockerfiledocker-compose.yml 快速搭建 nacos 单机 什么是 Nacos Nacos/nɑ:kəʊs/ 是“动态命名和配置服务”的缩写&#xff0c;是一个用于构建云原生应用的易于使用的动态服务发现、配置和服务管理平台。 Nacos 致力于…

【JAVA开源】基于Vue和SpringBoot的图书馆管理系统

本文项目编号 T 044 &#xff0c;文末自助获取源码 \color{red}{T044&#xff0c;文末自助获取源码} T044&#xff0c;文末自助获取源码 目录 一、系统介绍二、演示录屏三、启动教程四、功能截图五、文案资料5.1 选题背景5.2 国内外研究现状5.3 可行性分析5.4 用例设计 六、核…

Linux·进程概念(上)

1.操作系统 任何计算机系统都包含一个基本的程序合集&#xff0c;称为操作系统(Operator System)。笼统的理解&#xff0c;操作系统包括&#xff1a; 内核(进程管理&#xff0c;内存管理&#xff0c;文件管理&#xff0c;驱动管理) 其他程序(函数库&#xff0c;shell程序) OS的…

知乎知+推广怎么做?投放费用是多少?

知乎以其独特的问答形式不仅吸引了大量高质量的用户群体&#xff0c;也成为了一个不可多得的品牌营销阵地。为了帮助企业更好地利用这一平台进行品牌推广&#xff0c;知乎推出了“知”推广服务&#xff0c;而作为专业的数字营销解决方案提供商&#xff0c;云衔科技更是全面支持…

Linux开发环境配置(上)

✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅ ✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨ &#x1f33f;&#x1f33f;&#x1f33f;&#x1f33f;&#x1f33f;&#x1f33f;&#x1f33f;&#x1f33f;&#x1f33f;&#x1f33f;&#x1f33f;&#x1f33f;&#x1f33f;&#x1f33f;&#x1…

前端文件上传全过程

特别说明&#xff1a;ui框架使用的是蚂蚁的antd 这里主要是学习前端上传接口的传递参数包括前端上传之前对于代码的整理 一、第一步将前端页面画出来 源代码&#xff1a; /** 费用管理 - IT费用管理 - 费用数据上传 */ import { useState } from "react"; import {…

NTLM Relay攻击原理 + 工具使用

前言 仅仅是记录自己看《域内攻防指南》的体会&&理解&#xff0c;具体的知识学习建议看windows protocol &#xff08;✨&#xff09; ✅&#xff1a;NTLM是不依赖于上层协议的&#xff01;&#xff01;&#xff01;NTLM起到的就是认证&#xff0c;只认证Client的身份…

并查集 (Union-Find) :从基础到优化

并查集 (Union-Find) 并查集是一种树形数据结构&#xff0c;主要用于处理不相交集合&#xff08;Disjoint Set&#xff09;的合并和查询问题。它特别适用于解决有关连通性的问题&#xff0c;比如在图论中判断两点是否在同一个连通分量中。并查集可以高效地支持以下两种操作&am…

个人博客系统测试(selenium)

P. S.&#xff1a;以下代码均在VS2019环境下测试&#xff0c;不代表所有编译器均可通过。 P. S.&#xff1a;测试代码均未展示头文件stdio.h的声明&#xff0c;使用时请自行添加。 博主主页&#xff1a;Yan. yan.                        …

OceanBase 3.X 高可用 (一)

OceanBase 3.X 高可用&#xff08;一&#xff09; 一、分布式核心 OceanBase 3.x 采用的是paxos 协议&#xff0c;与raft协议相比。其复杂程度高&#xff0c;实现技术难度大。 Paxos 协议允许事务日志乱序发送&#xff0c;顺序提交。raft允许事务顺序发送&#xff0c;顺序提…

深度学习:常见损失函数简介--名称、作用和用法

目录 1. L1 Loss 2. NLL Loss (Negative Log Likelihood Loss) 3. NLLLoss2d 4. Gaussian NLL Loss 5. MSE Loss (Mean Squared Error Loss) 6. BCE Loss (Binary Cross-Entropy Loss) 7. Smooth L1 Loss 8. Cross Entropy Loss 1. L1 Loss 作用&#xff1a;计算预测值…

了解通用 SQL 语法

上世纪 90 年代中期&#xff0c;Sun Microsystems 公司推出了一种“一次编写&#xff0c;[随处]运行”的编程语言。这种语言就是 Java。尽管时至今日它仍然是最受欢迎的编程语言之一&#xff0c;但其口号却显得有些过于乐观。Java 语言的发展历程与 SQL 有着诸多相似之处。Java…

C语言常见字符串函数模拟实现一:(strlen,strcpy,strcat,strcmp,strstr )

strlen模拟实现 重点&#xff1a;1.字符串已经\0作为结束标志&#xff0c;strlen返回的是字符串\0前面出现的字符个数&#xff08;不包含\0&#xff09; 2.参数指向的字符串必须要以\0结束。 3.注意函数的返回值是size_t&#xff0c;是无符号的&#xff0c;加减是无法对比的。…

实用的云手机软件有哪些?高性价比云手机推荐

云手机不仅能模拟传统手机的功能&#xff0c;还能实现跨设备操作、数据同步等&#xff0c;极大地提升了用户的便利性。在众多云手机软件中&#xff0c;哪些软件表现出色呢&#xff1f;下面整理了一些功能强大、操作便捷且性能稳定的云手机APP&#xff0c;供大家参考选择。 1. O…

编程练习2 数据单元的变量替换

示例1: 1,2<A>00 示例2: 1,2<A>00,3<A>00 示例3: <B>12,1,2<B>1 示例4: <B<12,1 输出依次如下&#xff1a; #include<iostream> #include<vector> #include<string>using namespace std;/* 字符分割函数 将传入…

IIS中配置HTTPS证书的详细步骤

在IIS&#xff08;Internet Information Services&#xff09;中导入HTTPS证书的步骤主要包括下载证书、导入证书和为网站绑定证书几个环节。以下是详细的步骤说明&#xff1a; 一、下载SSL证书 首先&#xff0c;确保你已经从证书颁发机构&#xff08;CA&#xff09;下载了适…

三.python入门语法2

目录​​​​​​​ 1.控制结构 1.1.顺序结构 1.2.选择结构 习题 1.3.循环结构 1.3.1. while语句 1.3.2.for语句 1.3.3.循环嵌套 1.4.break语句 1.5.continue语句 1.6.pass语句 习题 1.控制结构 在学习控制结构之前我们通过一个故事来简单的描述一下控制结构&…