1.Prometheus介绍
prometheus是由谷歌研发的一款开源的监控软件,它通过安装在远程机器上的exporter,通过HTTP协议从远程的机器收集数据并存储在本地的时序数据库上
同时Prometheus后端用 golang语言开发,前端是 Grafana
2.Prometheus源码安装与启动配置
实验环境
192.168.0.223 Prometheus 服务端 centos 7.6
192.168.0.222 node_exporter 客户端 centos 7.6
下载Prometheus版本
[root@localhost ~]# wget https://mirrors.tuna.tsinghua.edu.cn/github-release/prometheus/prometheus/2.39.1%20_%202022-10-07/prometheus-2.39.1.linux-amd64.tar.gz
解压文件prometheus-2.39.1.linux-amd64.tar.gz 到/usr/local目录下
[root@localhost ~]# tar xf prometheus-2.39.1.linux-amd64.tar.gz -C /usr/local
prometheus-2.39.1.linux-amd64改名为prometheus
[root@localhost local]# mv prometheus-2.39.1.linux-amd64 prometheus
查看版本
[root@localhost prometheus]# ./prometheus --version
prometheus.yml 配置
# my global config
global:
# 默认情况下,每15s拉取一次目标采样点数据。
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
# 每15秒评估一次规则。默认值为每1分钟。
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# job名称会增加到拉取到的所有采样点上,同时还有一个instance目标服务的host:port标签也会增加到采样点上
- job_name: 'prometheus'
# 覆盖global的采样点,拉取时间间隔5s
scrape_interval: 5s
static_configs:
- targets: ['localhost:9090']
服务启动
指定配置文件启动
[root@localhost prometheus]# ./prometheus --config.file=prometheus.yml
访问:http://192.168.0.223:9090/
客户端安装node_exporter
下载node_exporter 安装文件
https://github.com/prometheus/node_exporter/releases/download/v1.1.2/node_exporter-1.1.2.linux-amd64.tar.gz
解压node_exporter-1.1.2.linux-amd64.tar.gz到 /usr/local/目录
[root@localhost ~]# tar xf node_exporter-1.1.2.linux-amd64.tar.gz -C /usr/local/
更改名字
[root@localhost ~]# mv node_exporter-1.1.2.linux-amd64/ node_exporter
启动node_exporter
# 直接启动
[root@VM_2-44 /usr/local/node_exporter]# ./node_exporter &
# 启动后会监听9100端口
# 添加为服务方式启动
[root@VM_2-44 ~]# vim /usr/lib/systemd/system/node_exporter.service
[Unit]
Description=node_exporter
After=network.target
[Service]
ExecStart=/usr/local/node_exporter/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target
# 启动node_exporter
systemctl daemon-reload
systemctl start node_exporter
普罗米修斯服务端配置文件添加监控项
切换到服务端服务器
[root@localhost /usr/local/prometheus]# vim prometheus.yml
# my global config
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['192.168.0.223:9090']
- job_name: 'linux'
static_configs:
- targets: ['192.168.0.222:9100'] # 多个用,分开
更改完配置重启prometheus服务
systemctl restart prometheus.service

Grafana 展示 Prometheus 数据展示
下载Grafana rpm包并上传服务器
https://mirrors.tuna.tsinghua.edu.cn/grafana/yum/rpm/grafana-7.4.3-1.x86_64.rpm
[root@localhost ~]# yum install initscripts fontconfig
[root@localhost ~]# yum install -y grafana-7.4.3-1.x86_64.rpm
[root@localhost ~]# systemctl start grafana-server.service
打开grafana网站
http://192.168.0.223:3000/login
admin admin
添加数据源
Configuration -> Data Sources ->add data source -> Prometheus
新增Dashboard Linux基础数据展示
更多grafana模板: https://grafana.com/grafana/dashboards 搜索 相应 dashboards的id如8919,1222
也可以选择别的模版进行展示
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?