Prometheus+Grafana监控Linux主机

发布于:2024-07-08 ⋅ 阅读:(51) ⋅ 点赞:(0)

1、安装Prometheus

1.1 、下载Prometheus

下载网址

https://github.com/prometheus/prometheus/releases

选择需要的版本

wget https://github.com/prometheus/prometheus/releases/download/v2.53.0/prometheus-2.53.0.linux-amd64.tar.gz

1.2、安装Prometheus软件

1.2.1、创建用户组

groupadd prometheus
useradd -g prometheus -m -s /sbin/nologin prometheus

1.2.2、解压配置

tar -zxvf prometheus-2.53.0.linux-amd64.tar.gz
mv prometheus-2.53.0.linux-amd64 /usr/local/prometheus
sed -i 's/localhost/ 172.25.0.166/g' /usr/local/prometheus/prometheus.yml

1.2.3、将Prometheus添加至系统环境变量

vim /etc/profile
export PROMETHEUS_HOME=/usr/local/prometheus
PATH=$PATH:$PROMETHEUS_HOME
export PATH

重载系统环境变量文件

source /etc/profile

1.2.4、查看Prometheus的版本信息

prometheus --version	

1.2.5、创建数据目录

mkdir /usr/local/prometheus/data/

1.2.6、权限修改

chown -R prometheus.prometheus /usr/local/prometheus

1.2.7、检查并加载配置文件

cd /usr/local/prometheus/
./promtool check config prometheus.yml

1.2.8、创建systemd服务

cat <<EOF > /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=30d
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF

参数注解:

  • config.file: 指定配置文件
  • web.enable-lifecycle: 支持通过http请求重载配置
  • storage.tsdb.path: 指定数据存储目录(默认当前目录的的data目录,若不存在则新建)
  • storage.tsdb.retention.time: 指定数据保留时间(默认15d)

1.2.9、启动服务

systemctl daemon-reload
systemctl start prometheus
systemctl status prometheus && systemctl enable prometheus

1.2.10、确认端口已经被监听

ss -lnput | grep 9090
tcp     LISTEN   0        512                    *:9090                *:*       users:(("prometheus",pid=20863,fd=7))

1.2.11、检查并加载配置文件

http://172.25.0.166:9090/metrics

在这里插入图片描述


2、监控Linux主机

2.1 、在Linux主机安装exporter插件

2.1.1、下载node_exporter

下载网址

https://github.com/prometheus/node_exporter/releases

下载需要的版本

wget https://github.com/prometheus/node_exporter/releases/download/v1.8.1/node_exporter-1.8.1.linux-amd64.tar.gz

2.1.2、创建用户组

groupadd prometheus
useradd -g prometheus -m -s /sbin/nologin prometheus

2.1.3、安装并解压

tar -zxvf node_exporter-1.8.1.linux-amd64.tar.gz -C /usr/local/
cd /usr/local/
mv node_exporter-1.8.1.linux-amd64  node_exporter

2.1.4、权限修改

chown -R prometheus.prometheus /usr/local/node_exporter

2.1.5、将node_exporter执行文件目录添加至环境变量

vim /etc/profile
export NODE_EXPORTER=/usr/local/node_exporter
export PATH=$PATH:$NODE_EXPORTER
export PATH

重载系统环境变量文件

source /etc/profile

2.1.6、创建systemd服务

cat > /usr/lib/systemd/system/node_exporter.service <<EOF

[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

2.1.7、启动服务

systemctl daemon-reload
systemctl enable node_exporter && systemctl start node_exporter
systemctl status node_exporter

2.1.8、确认端口已经被监听

ss -lnput | grep 9100
tcp     LISTEN   0        512                    *:9100                *:*       users:(("node_exporter",pid=21904,fd=3))

2.2、将Linux主机的exporter插件添加至Prometheus软件服务

修改Prometheus配置文件

vim /usr/local/prometheus/prometheus.yml
scrape_configs:
... 
  - job_name: 'Linux Node'					# 添加监控项的名字
    static_configs:							
      - targets: ['172.25.0.166:9100']		# 监控主机的ip地址和端口号

重启prometheus服务

systemctl restart prometheus

2.3 、测试Prometheus的监控Linux主机状态

http://172.25.0.166:9090/targets?search=&scrapePool=Linux+Node

在这里插入图片描述

http://172.25.0.166:9100/metrics

在这里插入图片描述


3、使用Grafana展示数据

3.1、下载grafana

下载网址

https://grafana.com/grafana/download

下载需要的版本

wget  https://dl.grafana.com/enterprise/release/grafana-enterprise-11.1.0-1.x86_64.rpm

3.2、安装grafana

rpm -ivh grafana-enterprise-11.1.0-1.x86_64.rpm

3.3、启动grafana

systemctl daemon-reload 
systemctl enable grafana-server && systemctl start grafana-server

3.4、grafana中文化

vim /etc/grafana/grafana.ini
[users]
# 设置默认语言为中文 
default_language = zh-Hans

重启服务

systemctl restart grafana-server

3.5、下载导入grafana模板

https://grafana.com/grafana/dashboards/15172-node-exporter-for-prometheus-dashboard-based-on-11074/

在这里插入图片描述

3.6、加载grafana-piechart-panel插件

grafana-cli plugins install grafana-piechart-panel

tar -xvf grafana-piechart-panel.tar.gz
mv grafana-piechart-panel /var/lib/grafana/plugins/grafana-piechart-panel

3.7、浏览器访问

在这里插入图片描述


4、参考文献

https://prometheus.io/
https://blog.csdn.net/heian_99/article/details/126989356
https://www.cnblogs.com/saneri/p/14667301.html
https://blog.csdn.net/qq_31725371/article/details/114697770


网站公告

今日签到

点亮在社区的每一天
去签到