Grafana+Prometheus监控Linux

407 阅读2分钟

1. 准备工作

1.1 关闭防火墙

systemctl stop firewalld
systemctl disable firewalld

2. 安装go环境

安装包放到/usr/local目录下。

2.1 解压

tar -xvf go1.13.1.linux-amd64.tar.gz

2.2 配置环境变量

vim /etc/profile

在最后一行添加

export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin

2.3 刷新

source /etc/profile

2.4 检查

go version

image.png

3. 安装Prometheus

下载地址:prometheus.io/download

安装包放到/usr/local目录下。

3.1 解压

tar -xvf prometheus-2.38.0-rc.0.linux-amd64.tar.gz
mv prometheus-2.38.0-rc.0.linux-amd64/ prometheus

3.2 以服务方式启动

添加系统服务

vim /etc/systemd/system/prometheus.service

写入以下内容

[Unit]
Description=Prometheus Monitoring System
Documentation=Prometheus Monitoring System
 
[Service]
ExecStart=/usr/local/prometheus/prometheus \
  --config.file=/usr/local/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

image.png

4. 安装Grafana

4.1 下载安装

此处安装采用源码编译的方式安装。在监控主机/usr/local 目录下 下载安装包,并安装

wget https://dl.grafana.com/enterprise/release/grafana-enterprise-9.0.7-1.x86_64.rpm
yum install grafana-enterprise-9.0.7-1.x86_64.rpm

4.2 启动服务

systemctl daemon-reload
systemctl enable grafana-server.service
systemctl start grafana-server.service

4.3 访问

http://172.17.43.58:3000/ 用户名密码均为admin

image.png

5. 安装node-exxporter

以下操作皆在被监控主机上操作。

5.1 关闭防火墙

参考第一节

5.2 安装

下载安装包至/usr/local目录下

tar -zvxf node_exporter-1.4.0-rc.0.linux-amd64.tar.gz
mv node_exporter-1.4.0-rc.0.linux-amd64/ node_exporter

5.3 以服务方式启动

vim /etc/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

启动并检查

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

image.png

6. 修改Prometheus配置监控Linux

6.1 修改Prometheus配置

vim /usr/local/prometheus/prometheus.yml

添加以下内容:

  - job_name: 'Linux'
    static_configs:
    - targets: ['172.17.43.58:9100']
      labels:
        instance: 172.17.43.58

完整文件内容如下:

# 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"]

  - job_name: 'Linux'
    static_configs:
    - targets: ['172.17.43.58:9100']
      labels:
        instance: 172.17.43.58

6.2 重启Prometheus服务

systemctl restart prometheus

访问:http://172.17.43.58:9090/targets 检查内容,

image.png

7. 配置Grafana

7.1 添加数据源

image.png image.png

7.2 导入dashboard模板

image.png 输入模板id后点load image.png 模板可在Dashboards | Grafana Labs查找 选定模板后进入详情页面,点Copy ID image.png load后按如下操作 image.png 之后就可以看到监控图了:

image.png 大功告成!!!