快速安装(暂不可用)
Prometheus安装可使用一键安装脚本https://gitee.com/lushanyanyv/prometheus
Grafana安装请参照下文
服务器监控只需要一个grafana即可,每台服务安装Prometheus,使用脚本更便捷!
国内
curl -sL https://gitee.com/li-chen-xing/prometheus/raw/master/prometheus-setup.sh | sed 's/\r$//' | bash
国外
curl -sL https://gitee.com/li-chen-xing/prometheus/raw/master/prometheus-setup-abroad.sh | sed 's/\r$//' | bash
准备
关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
关闭selinux
sed -i 's/enforcing/disabled/' /etc/selinux/config # 永久
setenforce 0 # 临时
sestatus #查询Selinux状态
下载软件包CentOS
yum install -y net-tools bash-completion lrzsz wget fontconfig urw-fonts
yum -y update && yum -y upgrade
#安装go
yum install -y epel-release
yum install golang
#查看go版本
go version
下载软件包Ubuntu
#安装go
sudo apt install golang-go -y
#查看go版本
go version
安装Prometheus
Github主页:https://github.com/prometheus/prometheus
下载Prometheus:Download | Prometheus
自行下载后上传
或者执行以下命令
cd /root && wget https://github.com/prometheus/prometheus/releases/download/v3.1.0/prometheus-3.1.0.linux-amd64.tar.gz
解压
tar -zxvf prometheus-3.1.0.linux-amd64.tar.gz
mv prometheus-3.1.0.linux-amd64/ prometheus
rm -f prometheus-3.1.0.linux-amd64.tar.gz
启动
cd prometheus
./prometheus --config.file=./prometheus.yml &
访问 http://ip:9090
配置开机自启
#ubuntu
cd /lib/systemd/system
#centos
cd /etc/systemd/system
vi prometheus.service
[Unit]
Description=Prometheus Monitoring System
Documentation=Prometheus Monitoring System
[Service]
ExecStart=/root/prometheus/prometheus \
--config.file=/root/prometheus/prometheus.yml \
--web.listen-address=:9090
Restart=on-failure
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable prometheus
systemctl start prometheus
systemctl status prometheus
进阶
修改 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:
# - "first_rules.yml"
# - "second_rules.yml"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: "prometheus"
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ["localhost:9090"]
# 添加 PushGateway 监控配置
- job_name: 'pushgateway'
static_configs:
- targets: ['localhost:9091']
labels:
instance: pushgateway
# 添加 Node Exporter 监控配置
- job_name: 'node exporter'
static_configs:
- targets: ['localhost:9100']
可选
# 添加 Node Exporter 监控配置
- job_name: 'node exporter'
static_configs:
- targets: ['localhost1:9100', 'localhost2:9100', 'localhost3:9100']
中文注释
# 我的全局配置
global:
scrape_interval: 15s # 设置每隔15秒抓取一次数据。默认为每分钟一次。
evaluation_interval: 15s # 每隔15秒评估一次规则。默认为每分钟一次。
# scrape_timeout 使用全局默认值(10秒)。
# Alertmanager 配置
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# 一次加载规则,并根据全局 `evaluation_interval` 定期评估规则。
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# 抓取配置,包含一个用于抓取的端点:
# 这里是 Prometheus 本身。
scrape_configs:
# 作业名称将作为标签 `job=<job_name>` 添加到从此配置中抓取的任何时间序列上。
- job_name: "prometheus"
# metrics_path 默认为 '/metrics'
# scheme 默认为 'http'
static_configs:
- targets: ["localhost:9090"]
# 添加 PushGateway 监控配置
- job_name: 'pushgateway'
static_configs:
- targets: ['localhost:9091']
labels:
instance: pushgateway
# 添加 Node Exporter 监控配置
- job_name: 'node exporter'
static_configs:
- targets: ['localhost:9100']
pushgateway
Github主页:https://github.com/prometheus/pushgateway
下载pushgateway
cd /root && wget https://github.com/prometheus/pushgateway/releases/download/v1.11.0/pushgateway-1.11.0.linux-amd64.tar.gz
解压
tar -zxvf pushgateway-1.11.0.linux-amd64.tar.gz
mv pushgateway-1.11.0.linux-amd64/ pushgateway
rm -f pushgateway-1.11.0.linux-amd64.tar.gz
启动
cd pushgateway
./pushgateway --web.listen-address:9091 &
配置开机自启
#ubuntu
cd /lib/systemd/system
#centos
cd /etc/systemd/system
vi pushgateway.service
[Unit]
Description=Pushgateway
Documentation=Pushgateway
[Service]
ExecStart=/root/pushgateway/pushgateway \
--web.listen-address=:9091
Restart=on-failure
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable pushgateway
systemctl start pushgateway
systemctl status pushgateway
node_exporter
Github主页:https://github.com/prometheus/node_exporter
下载node_exporter
cd /root && 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/ node_exporter
rm -f node_exporter-1.8.2.linux-amd64.tar.gz
分发
xsync node_exporter
启动
cd node_exporter
./node_exporter --web.listen-address:9091 &
配置开机自启
#ubuntu
cd /lib/systemd/system
#centos
cd /etc/systemd/system
vi node_exporter.service
[Unit]
Description=Node_exporter
Documentation=Node_exporter
[Service]
ExecStart=/root/node_exporter/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable node_exporter
systemctl start node_exporter
systemctl status node_exporter
分发
sudo xsync /usr/lib/systemd/system/node_exporter.service
安装Grafana
Ubuntu and Debian(64 Bit)
sudo apt-get install -y adduser libfontconfig1 musl
wget https://dl.grafana.com/enterprise/release/grafana-enterprise_10.0.3_amd64.deb
sudo dpkg -i grafana-enterprise_10.0.3_amd64.deb
Red Hat, CentOS, RHEL, and Fedora(64 Bit)
下载Grafana
Download Grafana | Grafana Labs
sudo yum install -y https://dl.grafana.com/enterprise/release/grafana-enterprise-10.0.3-1.x86_64.rpm
安装RPM包
sudo rpm -Uvh grafana-enterprise-10.0.3-1.x86_64.rpm
启动Grafana
systemctl daemon-reload
systemctl enable grafana-server.service
systemctl start grafana-server.service
systemctl status grafana-server.service
访问 http://ip:3000,admin/admin
systemctl status prometheus
systemctl status pushgateway
systemctl status node_exporter
systemctl status grafana-server.service
Origin not allowed解决方法
编辑反向代理配置文件
将
proxy_set_header Host 127.0.0.1;
改为
proxy_set_header Host 你的域名;
然后重启nginx
sudo systemctl restart nginx
sudo systemctl status nginx
Grafana Dashboards下载
Node Exporter Dashboard 220413 ConsulManager自动同步版 | Grafana Labs
Linux主机详情 | Grafana Labs 12633
Node Exporter for Prometheus Dashboard based on 11074 | Grafana Labs 15172
Node Exporter Dashboard EN 20201010-StarsL.cn | Grafana Labs 11074