linux之prometheus+grafana

news2024/9/20 13:17:09

Prometheus介绍

Prometheus(普罗米修斯)是一套开源的监控&报警&时间序列数据库的组合, 由go语言开发。 适合监控容器平台, 因为kubernetes(俗称k8s)的流行带动了prometheus的发展。 PS:由于目前还未学习容器,所以在今天的课程里使用prometheus监控仍然监控物理服务器。

官方网站: https://prometheus.io/

时序数据库介绍

  • 关系型 mysql,oracle,sql server,sybase,db2,access等
  • 非关系型(nosql) key-value memcache redis etcd
  1. 文档型 mongodb elasticsearch
  2. 列式 hbase
  3. 时序 prometheus
  4. 图形数据库 Neo4j

时间序列数据(TimeSeries Data) 

按照时间顺序记录系统、设备状态变化的数据被称为时序数据.

时序数据主要的特点
  • 数据带有时间属性,且数据量随着时间递增
  • 大都为插入操作较多且无更新的需求,插入数据多,每秒钟插入数据可到达千万甚至是上亿条
  • 分析过去时序数据可以做成多纬度报表,揭示其趋势性、规律性、异常性 分析时序数据趋势可以做大数据分析,机器学习,实现预测和预警
  • 能够按照条件筛选数据, 也可以按照时间范围统计,聚合,展示数据

常见应用场景

  • 无人驾驶车辆运行中要记录的经度,纬度,速度,方向,旁边物体的距离等等。每时每刻都要将数 据记录下来做分析。
  • 某一个地区的各车辆的行驶轨迹数据 传
  • 统证券行业实时交易数据
  • 实时运维监控数据等

prometheus的主要特性

  •  多维度数据模型
  •  灵活的查询语言
  •  不依赖分布式存储,单个服务器节点是自主的
  •  以HTTP方式,通过pull模型拉去时间序列数据
  •  也可以通过中间网关支持push模型
  •  通过服务发现或者静态配置, 来发现目标服务对象
  •  支持多种多样的图表和界面展

prometheus结构架构图

prometheus监控

需要准备三台主机,修改其主机名,互绑IP

server,agent1,grafana

[root@server ~]# cat /etc/hosts 
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.100.50	server.example.com	server
192.168.100.10	agent.example.com	agent
192.168.100.50	grafana.example.com	grafana

[root@server ~]# scp /etc/hosts root@192.168.100.10:/etc/hosts
[root@server ~]# scp /etc/hosts root@192.168.100.30:/etc/hosts

 安装prometheus

下载地址: https://prometheus.io/download/ (请使用共享的软件版本,以免出现不兼容问题)

安装 

二进制版解压就能用,不需要编译

[root@server ~]# ls
anaconda-ks.cfg  prometheus-2.54.0.linux-amd64.tar.gz
[root@server ~]# tar -zxvf prometheus-2.54.0.linux-amd64.tar.gz -C /usr/local



[root@server local]# mv prometheus-2.54.0.linux-amd64/ prometheus
[root@server local]# ls
bin  etc  games  include  lib  lib64  libexec  prometheus  sbin  share  src

查看配置文件

[root@server ~]# egrep -n : /usr/local/prometheus/prometheus.yml | awk -F'#' '{print $1}'
2:global:                                 全局配置段
3: scrape_interval: 15s                      每15s抓取(采集)数据一次
4: evaluation_interval: 15s                 每15秒计算一次规则
8:alerting: Alertmanager                    报警相关
9: alertmanagers:
10: - static_configs:
11: - targets:
12:
15:rule_files:                             规则文件列表
19:
21:scrape_configs:                         抓取的配置文件(也就是监控的实例)
23: - job_name: 'prometheus'             监控的实例名称
28: static_configs:
29: - targets: ['localhost:9090']         监控的实例IP与端口,在这里为监控服务器本身

使用默认配置文件启动,加 & 后台符号

[root@server ~]# /usr/local/prometheus/prometheus --config.file="/usr/local/prometheus/prometheus.yml" &

/ 后台启动/


查看9090端口是否启动
[root@server ~]# ss -anlt
State           Recv-Q           Send-Q                     Local Address:Port                     Peer Address:Port          Process          
LISTEN          0                128                              0.0.0.0:22                            0.0.0.0:*                              
LISTEN          0                4096                                   *:9090                                *:*                              
LISTEN          0                128                                 [::]:22                               [::]:*                              

prometheus界面

 通过浏览器访问http://服务器IP:9090就可以访问到prometheus的主界面

 点击status后点击targets查看呗监控主机

 通过http://服务器IP:9090/metrics可以查看到监控的数据(将localhost换成IP)

这里的metrics你可以类比成zabbix里的监控项

 在web主界面可以通过关键字查询metrics, 并显示图形

虽然prometheus服务器通过9090端口能监控一些metrics,但像cpu负载等这些linux常见的监控项却没 有,需要node_exporter组件。

node_exporter组件可以安装在本机或远程linux主机上。 

监控远程linux主机 

在远程linux主机(被监控端agent1)上安装node_exporter组件

下载地址: https://prometheus.io/download/ (请使用共享的软件版本,以免出现不兼容问题)

安装node_exporter组件

[root@agent ~]# ls
anaconda-ks.cfg  node_exporter-1.8.2.linux-amd64.tar.gz
[root@agent ~]# tar -zxvf node_exporter-1.8.2.linux-amd64.tar.gz -C /usr/local/



[root@agent ~]# cd /usr/local/
[root@agent local]# ls
bin  etc  games  include  lib  lib64  libexec  node_exporter-1.8.2.linux-amd64  sbin  share  src
[root@agent local]# mv node_exporter-1.8.2.linux-amd64/ node_exporter


 

启动node_exporter, 并验证端口

root@agent local]# nohup /usr/local/node_exporter/node_exporter &


说明: 如果把启动node_exporter的终端给关闭,那么进程也可能会随之关闭。nohup命令可以挂起在后
台,除非杀掉相关进程,否则不会随终端关闭而关闭进程。

nohup命令: 如果把启动node_exporter的终端给关闭,那么进程也可能会随之关闭。nohup命令可以挂
起在后台,除非杀掉相关进程,否则不会随终端关闭而关闭进程。


查看端口9100是否启动

[root@agent local]# ss -anlt
State           Recv-Q           Send-Q                     Local Address:Port                     Peer Address:Port          Process          
LISTEN          0                128                              0.0.0.0:22                            0.0.0.0:*                              
LISTEN          0                4096                                   *:9100                                *:*                              
LISTEN          0                128                                 [::]:22                               [::]:*                              
[root@agent local]# 

浏览器访问http://被监控端IP:9100/metrics就可以查看到node_exporter在被监控端收集的 metrics

 回到prometheus服务器的配置文件里添加被监控机器的配置段

[root@server ~]# cd /usr/local/prometheus/
[root@server prometheus]# ls
console_libraries  consoles  LICENSE  NOTICE  prometheus  prometheus.yml  promtool
[root@server prometheus]# vim prometheus.yml 





  - job_name: "agent"
    static_configs:
      - targets: ["192.168.100.10:9100"]



[root@server prometheus]# egrep -n : /usr/local/prometheus/prometheus.yml | awk -F'#' '{print $1}'
2:global:
3:  scrape_interval: 15s 
4:  evaluation_interval: 15s 
8:alerting:
9:  alertmanagers:
10:    - static_configs:
11:        - targets:
12:          
15:rule_files:
19:
21:scrape_configs:
23:  - job_name: "prometheus"
28:    static_configs:
29:      - targets: ["localhost:9090"]
30:  - job_name: "agent"                 最后加上这三行,取一个job名称来代表被监控的机器
31:    static_configs:
32:      - targets: ["192.168.100.10:9100"]        ///这里改成被监控机器的IP,后面端口接9100

 改完配置文件后,重启服务

[root@server ~]# pkill prometheus


root@server ~]# ss -anlt
State           Recv-Q           Send-Q                     Local Address:Port                     Peer Address:Port          Process          
LISTEN          0                128                              0.0.0.0:22                            0.0.0.0:*                              
LISTEN          0                128                                 [::]:22                               [::]:*           
确认端口没有进程占用

启动

[root@server ~]# /usr//local/prometheus/prometheus --config.file="/usr/local/prometheus/prometheus.yml" &

查看端口是否启动
[root@server ~]# ss -anlt
State           Recv-Q           Send-Q                     Local Address:Port                     Peer Address:Port          Process          
LISTEN          0                128                              0.0.0.0:22                            0.0.0.0:*                              
LISTEN          0                4096                                   *:9090                                *:*                              
LISTEN          0                128                                 [::]:22                               [::]:*    
启动成功

回到web管理界面 --》点Status --》点Targets --》可以看到多了一台监控目标

前面实现了prometheus监控本机9090, 但是还有很多metrics无法监控,比如cpu负载信息等。这个时 候我们在prometheus服务器上也安装node_exporter,并监控

安装方法与前面一样

[root@server ~]# egrep -n : /usr/local/prometheus/prometheus.yml | awk -F'#' '{print $1}'
2:global:
3:  scrape_interval: 15s 
4:  evaluation_interval: 15s 
8:alerting:
9:  alertmanagers:
10:    - static_configs:
11:        - targets:
12:          
15:rule_files:
19:
21:scrape_configs:
23:  - job_name: "prometheus"
28:    static_configs:
29:      - targets: ["localhost:9090"]
30:  - job_name: "agent"
31:    static_configs:
32:      - targets: ["192.168.100.10:9100"]
33:  - job_name: "server"                       // 其他不变,添加这三行
34:    static_configs:
35:      - targets: ["192.168.100.50:9100"]

 启动后查看端口
[root@server ~]# ss -anlt
State           Recv-Q           Send-Q                     Local Address:Port                     Peer Address:Port          Process          
LISTEN          0                128                              0.0.0.0:22                            0.0.0.0:*                              
LISTEN          0                4096                                   *:9090                                *:*                              
LISTEN          0                4096                                   *:9100                                *:*                              
LISTEN          0                128                                 [::]:22   

启动成功

启动prometheus,先要杀死进程

[root@server ~]# pkill prometheus 
[root@server ~]# /usr/local/prometheus/prometheus --
config.file="/usr/local/prometheus/prometheus.yml" &

启动后查看端口
[root@server ~]# ss -anlt
State           Recv-Q           Send-Q                     Local Address:Port                     Peer Address:Port          Process          
LISTEN          0                128                              0.0.0.0:22                            0.0.0.0:*                              
LISTEN          0                4096                                   *:9090                                *:*                              
LISTEN          0                4096                                   *:9100                                *:*                              
LISTEN          0                128                                 [::]:22                               [::]:*                              
[root@server ~]# 
启动成功

查看web端,成功添加

监控远程mysql

在被管理机agent上安装mysqld_exporter组件

下载地址: https://prometheus.io/download/ (请使用共享的软件版本,以免出现不兼容问题)

[root@agent ~]# ls
anaconda-ks.cfg  mysqld_exporter-0.15.1.linux-amd64.tar.gz  node_exporter-1.8.2.linux-amd64.tar.gz  nohup.out
[root@agent ~]# 
[root@agent ~]# tar -zxvf mysqld_exporter-0.15.1.linux-amd64.tar.gz -C /usr/local/  //解压
mysqld_exporter-0.15.1.linux-amd64/
mysqld_exporter-0.15.1.linux-amd64/LICENSE
mysqld_exporter-0.15.1.linux-amd64/mysqld_exporter
mysqld_exporter-0.15.1.linux-amd64/NOTICE
[root@agent ~]# mv /usr/local/mysqld_exporter-0.15.1.linux-amd64/ /usr/local/mysqld_exporter
[root@agent ~]# 

在agent上安装mariadb并启动,用于被监控

[root@agent ~]# yum install mariadb-server -y
[root@agent ~]# systemctl restart mariadb
[root@agent ~]# systemctl enable mariadb

授权

说明: 授权ip为localhost,因为不是prometheus服务器来直接找mariadb获取数据,而是prometheus 服务器找mysqld_exporter,mysqld_exporter再找mariadb。所以这个localhost是指的mysql_exporter 的IP

MariaDB [(none)]> grant all ON *.* to 'mysql_monitor'@'localhost' identified by 'linux';
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.000 sec)

MariaDB [(none)]> 

创建连接mariadb配置文件

[root@agent ~]# vim /usr/local/mysqld_exporter/.my.cnf
[root@agent ~]# cat /usr/local/mysqld_exporter/.my.cnf
[client]
user=mysql_monitor
password=linux

启动mysqld_exporter并验证9104端口

[root@agent ~]# nohup /usr/local/mysqld_exporter/mysqld_exporter --config.my-cnf=/usr/local/mysqld_exporter/.my.cnf &
[2] 2758
[root@agent ~]# nohup: ignoring input and appending output to 'nohup.out'

[root@agent ~]# ss -anlt
State           Recv-Q           Send-Q                     Local Address:Port                     Peer Address:Port          Process          
LISTEN          0                128                              0.0.0.0:22                            0.0.0.0:*                              
LISTEN          0                4096                                   *:9100                                *:*                              
LISTEN          0                4096                                   *:9104                                *:*                              
LISTEN          0                128                                 [::]:22                               [::]:*                              
LISTEN          0                80                                     *:3306                                *:*                              

 回到prometheus服务器的配置文件里添加被监控的mariadb的配置段

[root@server ~]# vim /usr/local/prometheus/prometheus.yml 
/// 最后一行加上这个内容
  - job_name: "agent-mariadb"
    static_configs:
      - targets: ["192.168.100.10:9104"]   /被监控的主机的IP,后面接端口9104

重启服务

[root@server ~]# pkill prometheus
[root@server ~]# ss -anlt | grep 9090
[root@server ~]# /usr/local/prometheus/prometheus --
config.file="/usr/local/prometheus/prometheus.yml" &
[root@server ~]# ss -anlt |grep 9090
tcp6 0 0 :::9090 :::* LISTEN 76661/prometheus

回到web管理界面 --》点Status --》点Targets --》可以看到监控mariadb了

可以查看mariadb连接数

grafana安装与登录 

在grafana服务器上安装grafana

下载地址:https://grafana.com/grafana/download (请使用共享的软件版本,以免出现不兼容问题)

拷贝软件包到grafana服务器上安装

[root@grafana ~]# rpm -ivh grafana-6.4.2-1.x86_64.rpm 
warning: grafana-6.4.2-1.x86_64.rpm: Header V4 RSA/SHA1 Signature, key ID 24098cb6: NOKEY
error: Failed dependencies:
	fontconfig is needed by grafana-6.4.2-1.x86_64     //此时提醒需要安装三个依赖包
	freetype is needed by grafana-6.4.2-1.x86_64
	urw-fonts is needed by grafana-6.4.2-1.x86_64


// 依次安装

[root@grafana ~]# yum -y install fontconfig freetype urw-fonts 

 再次进行安装

[root@grafana ~]# rpm -ivh grafana-6.4.2-1.x86_64.rpm 
warning: grafana-6.4.2-1.x86_64.rpm: Header V4 RSA/SHA1 Signature, key ID 24098cb6: NOKEY
Verifying...                          ################################# [100%]
Preparing...                          ################################# [100%]
Updating / installing...
   1:grafana-6.4.2-1                  ################################# [100%]
### NOT starting on installation, please execute the following statements to configure grafana to start automatically using systemd
 sudo /bin/systemctl daemon-reload
 sudo /bin/systemctl enable grafana-server.service
### You can start grafana-server by executing
 sudo /bin/systemctl start grafana-server.service
POSTTRANS: Running scrip

启动grafana,并验证端口

[root@grafana ~]# systemctl start grafana-server
[root@grafana ~]# systemctl enable grafana-server
Created symlink /etc/systemd/system/multi-user.target.wants/grafana-server.service → /usr/lib/systemd/system/grafana-server.service.
[root@grafana ~]# ss -anlt
State           Recv-Q           Send-Q                     Local Address:Port                     Peer Address:Port          Process          
LISTEN          0                128                              0.0.0.0:22                            0.0.0.0:*                              
LISTEN          0                128                                 [::]:22                               [::]:*                              
LISTEN          0                4096                                   *:3000                                *:*                              


//3000 已启动

通过浏览器访问 http:// grafana服务器IP :3000登录,使用默认的admin用户,admin密码就可以登陆 了

默认用户和密码都是admin 

登录进去以后会要求设置新的密码

 设置prometheus为grafana数据源

把prometheus服务器收集的数据做为数据源添加到grafana,让grafana可以得到prometheus的数据。

 

 

grafana实现自定义监控cpu负载

 

 

 保存

最后在dashboard可以查看到

 导入json模板实现mysql监控

 

 

 

 grafana+onealert报警

prometheus报警需要使用alertmanager这个组件,而且报警规则需要手动编写(对运维来说不友好)。 所以我这里选用grafana+onealert报警。 注意: 实现报警前把所有机器时间同步再检查一遍

grafana对接onealert

在onealert里添加grafana应用

官方网站: http://www.onealert.com

增加grafana应用

 此时需要在grafana中配置Webhook URL。

按照睿象云的操作步骤去完成

 

 操作如下

 

测试的时候邮箱会收到报警信息

 

此时就对接成功

配置通知策略

这一个报警通知方式设置为短信 

 这一个选择邮件

测试

mysql连接数超过3

创建alert

 自定义报警规则

这里平均值写3就行了,因为要测试mysql连接数

 

保存后,进行验证,在终端多连接几台mysql

 

此时查看手机信息和邮件

 

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

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

相关文章

嵌入式Linux:proc文件系统

目录 1、使用 cat 命令读取 /proc 文件系统 2、在应用程序中使用 open() 和 read() 函数读取 /proc 文件系统 proc 文件系统是一个虚拟文件系统,它以文件系统的形式为应用层提供访问系统内核数据的接口。用户和应用程序可以通过 proc 文件系统获取系统信息和进程相…

腾讯一面算法题:最长重复子串 1044,讲个比较好理解的思路

文章目录 1044. 最长重复子串前言思路Version 1:暴力Version 2:引入二分,优化 O ( n 2 ) O(n^2) O(n2)Version 3:引入自定义哈希,优化字符串比较Version 4:计算所有字符串的哈希值Version 5:引…

前后端项目交互异步请求JSON数据类型后端标准响应数据格式

java同步请求 当网页与后端交互时,前端不能再进行其他操作 服务器响应回来的内容,会把整个浏览器中的内容覆盖 这种请求方式在前后端交互时不太友好 现在的前后端交互请求都使用异步请求 异步请求(不同步) 通过在前端中使用js中提供的XMLHttpRequest对象实现发送异步请求…

人工智能与机器学习在医学领域的应用

作者主页: 知孤云出岫 人工智能与机器学习在医学中的应用 目录 作者主页:人工智能与机器学习在医学中的应用1. 引言2. 医学中的AI和ML技术概述2.1 人工智能和机器学习基础2.2 数据在医学AI中的重要性 3. 医学AI和ML的具体应用领域3.1 影像诊断3.2 基因组学与个性化医疗3.3 疾…

JavaEE篇:多线程(1)

一 认识线程(Thread) 1.1 概念 1.1.1 线程是什么? 线程被创建出来是为了完成分配给它的任务。线程又称轻量级进程,是操作系统的基本调度单位。一个线程就是一个执行流。线程的创建销毁和切换都比进程更加的方便。进程是操作系统分配资源的基本单位&am…

C++ //练习 17.16 如果前一题程序中的regex对象用“[^c]ei“进行初始化,将会发生什么?用此模式测试你的程序,检查你的答案是否正确。

C Primer(第5版) 练习 17.16 练习 17.16 如果前一题程序中的regex对象用"[^c]ei"进行初始化,将会发生什么?用此模式测试你的程序,检查你的答案是否正确。 环境:Linux Ubuntu(云服务…

「C++系列」数据结构

文章目录 一、数据结构1. 线性数据结构2. 非线性数据结构3. 其他重要数据结构 二、定义数据结构1. 数组(Array)2. 链表(LinkedList)3. 栈(Stack) 三、指针、关键字1. 指针链表树 2. 关键字 四、相关链接 一…

【TCP/IP】UDP协议数据格式和报文格式

学习一个网络协议,主要就是学习“数据格式”/“报文格式” 源端口/目的端口 端口号是属于传输层的概念UDP 报头使用两个自己的长度来表示端口号之所以端口号的范围是 0~65535,是因为底层网络协议做出了强制要求如果使用一个 10 w 这样的端口&#xff0…

机器学习:多元线性回归模型

目录 前言 一、讲在前面 1.多元_血压.csv: 2.完整代码: 3.运行结果: 二、实现步骤 1.导入库 2.导入数据 3.绘制散点图(这步可以省略) ​编辑 4.求特征和标签的相关系数 5.建立并训练线性回归模型 6.检验模…

NtripShare全站仪自动化监测之气象改正

最近有幸和自动化监测领域权威专家进行交流,讨论到全站仪气象改正的问题,因为有些观点与专家不太一致,所以再次温习了一下全站仪气象改正的技术细节。 气象改正的概念 全站仪一般利用光波进行测距,首先仪器会处理测距光波的相位漂…

C++| QT图片调整透明度叠加

QT图片调整透明度叠加 实际效果界面UI放置控件设置布局界面自适应 代码项目工程的文件初始化按钮功能滑动条功能图片调整透明度叠加 实际效果 三个图片(QLabel)显示,两个按钮(QPushButton)加载图片,一个&a…

【Java学习】反射和枚举详解

所属专栏:Java学习 🍁1. 反射 在程序运行时,可以动态地创建对象、调用方法、访问和修改字段,以及获取类的各种属性信息(如成员变量、方法、构造函数等),这种机制就称为反射 反射相关的类 类名用…

【算法】马踏棋盘(骑士周游)问题回溯算法实现以及使用贪心算法优化

目录 1.游戏规则 2.算法分析 3.解决步骤和思路 4.马踏棋盘算法的代码实现 4.1计算马儿还能走哪些位置 4.2马踏棋盘的核心代码 4.3马踏棋盘算法完整代码 4.4使用贪心算法进行优化 4.4.1思路 4.4.2代码实现 1.游戏规则 将马儿随机放在国际象棋的 8*8 棋盘的某个方格中…

阶段练习——minishell

目录 (一)文件复制(my_cp函数) (二)文件内容查看(my_cat函数) (三)切换目录(my_cd函数) (四)列出目录内容…

一款专为IntelliJ IDEA用户设计的插件,极大简化Spring项目中的API调试过程,功能强大(附源码)

前言 在软件开发过程中,尤其是Spring MVC(Boot)项目中,API调试调用是一项常见但繁琐的任务。现有的开发工具虽然提供了一些支持,但往往存在效率不高、操作复杂等问题。为了处理这些痛点,提升开发效率,一款新的工具应运…

python 捕获异常

捕获指定异常 e 是保存的异常信息 捕获多个异常

快速体验fastllm安装部署并支持AMD ROCm推理加速

序言 fastllm是纯c实现,无第三方依赖的高性能大模型推理库。 本文以国产海光DCU为例,在AMD ROCm平台下编译部署fastllm以实现LLMs模型推理加速。 测试平台:曙光超算互联网平台SCNet GPU/DCU:异构加速卡AI 显存64GB PCIE&#…

Selenium + Python 自动化测试18(数据驱动实现测试)

我们的目标是:按照这一套资料学习下来,大家可以独立完成自动化测试的任务。 上一篇我们讨论了数据驱动测试中如何读取Excel文件,今天我们试着进一步深入学习数据驱动。 本篇文章我们讨论一下如何使用数据驱动思想实现测试。 1、数据驱动框架…

从零开始学cv-5: 图像的仿射变换

文章目录 一,简介:二,图像仿射变换详解2.1,图像平移:2.2 ,图像旋转:2.3,仿射变换: 一,简介: 仿射变换(Affine Transformation 或 Aff…

Lumina学术引擎免费问世,性能超谷歌学术5倍

Lumina介绍 Lumina是一款完全免费的AI学术搜索引擎,借助强大的数据库和高效的匹配速度。利用超过 15 种模型从超过 100 万篇期刊文章中找出最相关的来源,从而构建答案。搜索结果相关性平均比谷歌学术高出5倍,支持超1亿研究对象搜索&#xff…