Prometheus是一个用 Golang 编写的开源的系统监控和警报工具包,能够收集和处理来自各种目标的指标,还可以查询、查看、分析指标并根据阈值收到警报。
一、基础介绍
- Prometheus是一个用 Golang 编写的开源的系统监控和警报工具包,能够收集和处理来自各种目标的指标,还可以查询、查看、分析指标并根据阈值收到警报。自2012年启动以来,许多公司和组织都采用了Prometheus,该项目拥有非常活跃的开发人员和用户社区。它现在是一个独立的开源项目,独立于任何公司进行维护。Prometheus于2016年加入云原生计算基金会,成为继Kubernetes之后的第二个托管项目。
- 在Prometheus的架构设计中,Prometheus Server并不直接服务监控特定的目标,其主要任务负责数据的收集,存储并且对外提供数据查询支持。因此为了能够监控到某些东西,如主机的CPU使用率,我们需要使用到Exporter。Prometheus周期性的从Exporter暴露的HTTP服务地址(通常是/metrics)拉取监控样本数据。
- Exporter是Prometheus的指标数据收集组件。它负责从目标Jobs收集数据,并把收集到的数据转换为Prometheus支持的时序数据格式。和传统的指标数据收集组件不同的是,他只负责收集,并不向Server端发送数据,而是等待Prometheus Server 主动抓取,node-exporter 默认的抓取url地址:http://ip:9100/metrics。
- Prometheus可以使用Pushgateway这个组件推送node-exporter的指标数据到远端Prometheus,node-exporter用于采集node的运行指标,包括node的cpu、load、filesystem、meminfo、network等基础监控指标,类似于zabbix监控系统的的zabbix-agent。node-exporter由Prometheus官方提供、维护,属于监控指标收集类UNIX内核操作系统的必备的exporter。
- Grafana是一个开源的数据可视化工具,它提供了强大的数据可视化功能、灵活的告警系统、用户和角色管理、插件系统等核心功能。
- Grafana的架构由前端、后端和数据源组成。前端是基于React的单页应用,后端是基于Go语言的服务器,数据源通过插件与Grafana集成。这种架构设计使得Grafana能够灵活地适应不同的数据源和用户需求。
- Grafana支持与多种数据源(如Elasticsearch、InfluxDB、Prometheus等)集成,使得它能够展示来自这些数据源的数据。
二、安装使用
- 安装prometheus
- cd /usr/local/src
- wget https://github.com/prometheus/prometheus/releases/download/v2.54.1/prometheus-2.54.1.linux-amd64.tar.gz
- tar -zxvf prometheus-2.54.1.linux-amd64.tar.gz
- mv prometheus-2.54.1.linux-amd64 /usr/local/
- mv prometheus-2.54.1.linux-amd64 prometheus-2.54.1
- cd prometheus-2.54.1
- ./prometheus –config.file=prometheus.yml
- 添加到系统服务:vim /usr/lib/systemd/system/prometheus.service
1 | [Unit] |
- systemctl start prometheus
- systemctl status prometheus
- systemctl restart prometheus
- systemctl stop prometheus
安装go环境
- 源码安装
- cd /usr/local/src
- wget https://go.dev/dl/go1.23.0.linux-386.tar.gz
- tar -zxvf go1.23.0.linux-386.tar.gz
- mv go /usr/local
- export GOROOT=/usr/local/go
- export GOPATH=/home/gopath
- export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
- GOROOT 是 go 的安装目录
- GOPATH 是 go 的工作目录
- PATH 是环境变量
- go version
- 命令安装
- snap install go
- apt install golang-go
- snap install go
- 源码安装
安装node_exporter
- 源码安装
- git clone https://github.com/prometheus/node_exporter.git
- cd node_exporter
- go build
- 下载慢设置代理:export GOPROXY=https://goproxy.cn
- ./node_exporter –version
- 使用编译好的包
- wget https://github.com/prometheus/node_exporter/releases/download/v1.8.2/node_exporter-1.8.2.linux-amd64.tar.gz
- tar -zxvf node_exporter-1.8.2.linux-amd64.tar.gz
- mv node_exporter-1.8.2.linux-amd64 /usr/local/
- cd /usr/local
- mv node_exporter-1.8.2.linux-amd64 node_exporter-1.8.2
- cd /usr/local/node_exporter-1.8.2/
- ./node_exporter –log.level=debug
- ./node_exporter –help
- curl -s http://localhost:9100/metrics
- 设置安全组:9100
- 浏览器访问:http://101.200.15.11:9100/metrics
- curl -s http://8.141.94.234:9100/metrics
- 修改prometheus配置文件prometheus.yml
- 源码安装
1 | # my global config |
- systemctl restart prometheus
- 浏览器访问:http://ip:9090/targets
- 添加到系统服务:vim /usr/lib/systemd/system/node_exporter.service
1 | [Unit] |
- systemctl start node_exporter
- systemctl stop node_exporter
- systemctl restart node_exporter
- systemctl status node_exporter
安装grafana,传送门
源码安装
- wget https://dl.grafana.com/grafana-enterprise/release/12.1.1/grafana-enterprise_12.1.1_16903967602_linux_amd64.tar.gz
- tar -zxvf grafana-enterprise_12.1.1_16903967602_linux_amd64.tar.gz
- mv grafana-12.1.1/ /usr/local/
- cd grafana-12.1.1/
- ./bin/grafane-server
- http://101.200.15.11:3000
- sudo useradd -r -s /bin/false grafana
- sudo chown -R grafana:users /usr/local/grafana-12.1.1
- 添加到系统启动服务:vim /usr/lib/systemd/system/grafana.service
1
2
3
4
5
6
7
8
9
10
11
12
13[Unit]
Description=Grafana Server
After=network.target
[Service]
Type=simple
User=grafana
Group=users
ExecStart=/usr/local/grafana-12.1.1/bin/grafana server --config=/usr/local/grafana-12.1.1/conf/sample.ini --homepath=/usr/local/grafana-12.1.1
Restart=on-failure
[Install]
WantedBy=multi-user.target- systemctl start grafana
- systemctl status grafana
- systemctl stop grafana
- systemctl daemon-reload
使用命令
- sudo yum install -y https://dl.grafana.com/enterprise/release/grafana-enterprise-11.2.0-1.x86_64.rpm
- systemctl enable grafana-server
- systemctl start grafana-server
- systemctl status grafana-server
- 未启动成功,修改/usr/lib/systemd/system/grafana.service:User=root Group=root
- systemctl restart grafana-server
- systemctl stop grafana-server
- 访问:http://101.200.15.11:3000
- 默认:admin/admin,登录成功后会提示修改密码
- 修改:admin/xxx
- 配置prometheus数据源
- Connections
- Data sources
- Add data source
- prometheus
- 输入链接
- save & test
- 配置显示面板
- Dashboards
- Create dashboard
- Import a dashboard
- 输入:8919,详见
- https://grafana.com/grafana/dashboards/1860-node-exporter-full/
监听其他服务器
三、参考
- https://www.prometheus.io/
- https://prometheus.io/download/
- https://github.com/prometheus/prometheus
- https://grafana.com/
- https://grafana.com/docs/grafana/latest/setup-grafana/installation/
- https://grafana.com/docs/grafana/latest/setup-grafana/installation/debian/
- https://grafana.com/grafana/dashboards/