Prometheus(普罗米修斯)是一套开源的监控&报警&时间序列数据库的组合,起始是由SoundCloud公司开发的.Prometheus基本原理是通过HTTP协议周期性抓取被监控组件的状态,这样做的好处是任意组件只要提供HTTP接口就可以接入监控系统,不需要任何SDK或者其他的集成过程。这样做非常适合虚拟化环境比如VM或者Docker 。
node_exporter用于收集主机运行信息,比如CPU、内存、磁盘等资源使用情况。正常情况下是安装在节点服务器上,本次操作都是一个服务器上操作。
……
Grafana 是一个监控仪表系统,它是由 Grafana Labs 公司开源的的一个系统监测 (System Monitoring) 工具。它可以大大帮助你简化监控的复杂度,你只需要提供你需要监控的数据,它就可以帮你生成各种可视化仪表。同时它还有报警功能,可以在系统出现问题时通知你。
Prometheus安装部署及监控linux主机 - 梦徒 - 博客园 (cnblogs.com)
Linux主机详情 dashboard for Grafana | Grafana Labs
Prometheus自带指标整理_追光叶子的博客-CSDN博客_prometheus默认指标
Prometheus配置文件prometheus.yml 四个模块详解 – 21运维 (21yunwei.com)
Prometheus监控Linux服务器 - 简书 (jianshu.com)
Grafana 中文入门教程 | 构建你的第一个仪表盘 - 腾讯云开发者社区-腾讯云 (tencent.com)
Prometheus 中文文档
Grafana 采用 Nginx 反向代理
Grafana 配置文件详解
下载太慢,使用三方网站下载,暂不知道能用多久
https://d.serctl.com/
prometheus下载
主程序
prometheus警报组件alertmenager,独立
prometheus组件node_exporter 收集主机运行信息
创建临时目录,因为软件都是下载到window,需要上传到服务器。具体怎么操作,请百度或使用工具。这里就不说了,我用的是finalshell
mkdir prometheus
cd prometheus/
wget https://github.com/prometheus/prometheus/releases/download/v2.37.0/prometheus-2.37.0.linux-amd64.tar.gz
# 因为直接下载外网软件太慢,故使用第三方网站下载,在上传到服务器
#创建用户组
groupadd prometheus
useradd -g prometheus -m -s /sbin/nologin prometheus
# 解压配置
tar -zxvf prometheus-2.26.0.linux-amd64.tar.gz
mv prometheus-2.26.0.linux-amd64 /usr/local/prometheus
# 替换配置文件内容 localhost 为自己服务器ip
sed -i 's/localhost/{服务器ip}/g' /usr/local/prometheus/prometheus.yml
cat /usr/local/prometheus/prometheus.yml
# 创建数据目录
mkdir /usr/local/prometheus/data/
ll /usr/local/prometheus
# 权限修改
chown -R prometheus.prometheus /usr/local/prometheus
ll /usr/local | grep prometheus
# 检查并加载配置文件
cd /usr/local/prometheus
./promtool check config prometheus.yml
#### 创建systemd服务
cat < /usr/lib/systemd/system/prometheus.service
> [Unit]
> Description=prometheus
> After=network.target
> [Service]
> Type=simple
> User=prometheus
> ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --storage.tsdb.path=/usr/local/prometheus/data/ --web.enable-lifecycle --storage.tsdb.retention.time=365d
> Restart=on-failure
> [Install]
> WantedBy=multi-user.target
> EOF
# --config.file: 指定配置文件
# --web.enable-lifecycle: 支持通过http请求重载配置
# --storage.tsdb.path: 指定数据存储目录(默认当前目录的的data目录,若不存在则新建)
# --storage.tsdb.retention.time: 指定数据保留时间(默认15d)
#####
# 启动服务
systemctl daemon-reload
systemctl start prometheus
systemctl status prometheus && systemctl enable prometheus
# 确认端口已经被监听
ss -lnput | grep 9090
tcp LISTEN 0 128 :::9090 :::* users:(("prometheus",pid=12579,fd=7))
地址:192.168.xxx.xxx:9090/metrics
curl http://127.0.0.1:9090/-/healthy
Prometheus is Healthy.
curl http://127.0.0.1:9090/-/ready
Prometheus is Ready.
curl http://127.0.0.1:9090/metrics
. . .
# 通过web接口重载,启动时需增加选项 --web.enable-lifecycle
curl -XPOST http://127.0.0.1:9090/-/reload
# 停止Prometheus
# 同样需启动时增加--web.enable-lifecycle选项
curl -XPUT http://127.0.0.1:9090/-/quit
#创建用户组
# groupadd prometheus
# useradd -g prometheus -m -s /sbin/nologin prometheus
#安装并解压
tar -zxvf node_exporter-1.1.2.linux-amd64.tar.gz -C /usr/local/
mv /usr/local/node_exporter-1.1.2.linux-amd64 /usr/local/node_exporter
ll /usr/local/ | grep node
# 权限修改
chown -R prometheus.prometheus /usr/local/node_exporter
ll /usr/local/ | grep node
# 创建systemd服务
cat > /usr/lib/systemd/system/node_exporter.service < [Unit]
> Description=node_export
> Documentation=https://github.com/prometheus/node_exporter
> After=network.target
> [Service]
> Type=simple
> User=prometheus
> ExecStart=/usr/local/node_exporter/node_exporter
> Restart=on-failure
> [Install]
> WantedBy=multi-user.target
EOF
# 启动服务
systemctl daemon-reload
systemctl enable node_exporter && systemctl start node_exporter
systemctl status node_exporter
ss -lnput | grep 9100
tcp LISTEN 0 128 :::9100 :::* users:(("node_exporter",pid=4666,fd=3))
在prometheus的yml增加检测节点,然后重启prometheus
vim /usr/local/prometheus/prometheus.yml
####
# 增加监控节点
- job_name: "centos7_test"
static_configs:
- targets: ["192.168.xxx.xxx:9100"]
####
systemctl restart prometheus
地址:192.168.xxx.xxx:9090/targets
未配置时候
配置后
# 因为直接下载外网软件太慢,故使用第三方网站下载,在上传到服务器
wget https://dl.grafana.com/oss/release/grafana-9.0.7-1.x86_64.rpm
yum -y install grafana-9.0.7-1.x86_64.rpm
systemctl daemon-reload
systemctl enable grafana-server && systemctl start grafana-server
systemctl status grafana-server
ss -lnput | grep 3000
tcp LISTEN 0 128 :::3000 :::* users:(("grafana-server",pid=21914,fd=15))
地址:http://192.168.200.148:3000/
访问grafana的3000端口进行登录,默认用户名和密码为: admin/admin。第一次登陆修改更改密码!
Configuration -> Data Sources -> Prometheus
URL:http://localhost:9090
添加后
默认路径
Setting Default value
GF_PATHS_CONFIG /etc/grafana/grafana.ini
GF_PATHS_DATA /var/lib/grafana
GF_PATHS_HOME /usr/share/grafana
GF_PATHS_LOGS /var/log/grafana
GF_PATHS_PLUGINS /var/lib/grafana/plugins
GF_PATHS_PROVISIONING /etc/grafana/provisioning
服务器插件路径
直接修改面包json内title会提示属于插件无法修改,可以使用另存功能,马上进来一次改完,然后更新就可以了
nginx增加配置
vim /usr/local/nginx/conf/nginx.conf
###
gzip on; # 开启压缩
...
location /prometheus/{
# 因为存在1.6M的js,需要设置nginx的缓存配置调大
proxy_buffering on;
proxy_buffer_size 4k;
proxy_buffers 8 2M;
proxy_busy_buffers_size 2M;
proxy_pass http://127.0.0.1:9090/;
}
location /grafana{
# 因为存在2M的js,需要设置nginx的缓存配置调大
proxy_buffering on;
proxy_buffer_size 4k;
proxy_buffers 8 4M;
proxy_busy_buffers_size 4M;
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:3000/grafana;
}
location ^~ /grafana/api/live {
# 这是grafana的webSocket连接
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:3000/grafana/api/live;
}
###
./nginx -t
#nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
#nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
./nginx -s reload
grafana直接用nginx代理会无法访问
需要修改grafana.ini
,分号(;)是.ini文件中标准的注释行的方式。
cat -n /etc/grafana/grafana.ini
###
41 ;http_port = 3000
44 ;domain = localhost
48 ;enforce_domain = false
52 ;root_url = %(protocol)s://%(domain)s:%(http_port)s/
55 ;serve_from_sub_path = false
###
vim /etc/grafana/grafana.ini
:set number # 显示行号
###
40 # The http port to use
41 ;http_port = 3000
42 http_port = 3000
43 # The public facing domain name used to access grafana from a browser
44 ;domain = localhost
45 domain = localhost
46 # Redirect to correct domain if host header does not match domain
47 # Prevents DNS rebinding attacks
48 ;enforce_domain = false
49
50 # The full public facing url you use in browser, used for redirects and emails
51 # If you use reverse proxy and sub path specify full url (with sub path)
52 ;root_url = %(protocol)s://%(domain)s:%(http_port)s/
53 root_url = %(protocol)s://%(domain)s:%(http_port)s/grafana
54
55 # Serve Grafana from subpath specified in `root_url` setting. By default it is set to `false` for compatibility reasons.
56 ;serve_from_sub_path = false
57 serve_from_sub_path = true
###
systemctl restart grafana-server
直连http://192.168.xxx.xxx:3000/grafana
,能够正常登陆
代理连接http://192.168.xxx.xxx/grafana
,也能够正常登陆
错误处理记录:
nginx代理后打开grafana页面If you‘re seeing this Grafana has failed to load its application files
折腾了半天才发现最终原因
这个比较简单,配置了nginx然后 修改prometheus的启动命令
cat -n /usr/lib/systemd/system/prometheus.service
###
1 [Unit]
2 Description=prometheus
3 After=network.target
4 [Service]
5 Type=simple
6 User=prometheus
7 ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --storage.tsdb.path=/usr/local/prometheus/data/ --web.enable-lifecycle --storage.tsdb.retention.time=365d
8 Restart=on-failure
9 [Install]
10 WantedBy=multi-user.target
###
# 启动命令 增加参数web.external-url
vim /usr/lib/systemd/system/prometheus.service
###
ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --storage.tsdb.path=/usr/local/prometheus/data/ --web.enable-lifecycle --st orage.tsdb.retention.time=365d --web.external-url=promethues
###
# 看看对应的软连接文件是否变更
cat -n /etc/systemd/system/multi-user.target.wants/prometheus.service
systemctl daemon-reload
vim /usr/local/prometheus/prometheus.yml
###
- job_name: "prometheus"
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
metrics_path: "/promethues/metrics"
static_configs:
- targets: ["127.0.0.1:9090"]
###
systemctl restart prometheus
直连地址:http://192.168.xxx.xxx:9090/prometheus/targets?search=
nginx代理的方式访问:http://192.168.xxx.xxx/prometheus/targets?search=