Обмен технологиями

Prometheus Grafana контролирует хосты Linux

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

1. Установите Прометей

1.1. Скачать Прометей

URL-адрес загрузки

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

Выберите нужную версию

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

1.2. Установите программное обеспечение Прометей.

1.2.1. Создать группу пользователей.

groupadd prometheus
useradd -g prometheus -m -s /sbin/nologin prometheus
  • 1
  • 2

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

1.2.3 Добавьте Прометей в переменные системной среды.

vim /etc/profile
  • 1
export PROMETHEUS_HOME=/usr/local/prometheus
PATH=$PATH:$PROMETHEUS_HOME
export PATH
  • 1
  • 2
  • 3

Перезагрузить файл переменных системной среды.

source /etc/profile

  • 1
  • 2

1.2.4 Проверьте информацию о версии Прометея.

prometheus --version	
  • 1

1.2.5. Создать каталог данных.

mkdir /usr/local/prometheus/data/
  • 1

1.2.6. Изменение разрешения.

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

1.2.7. Проверьте и загрузите файл конфигурации.

cd /usr/local/prometheus/
./promtool check config prometheus.yml
  • 1
  • 2

1.2.8. Создайте системный сервис.

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

Примечания к параметрам:

  • config.file: укажите файл конфигурации.
  • web.enable-lifecycle: поддерживает перезагрузку конфигурации через HTTP-запрос.
  • Storage.tsdb.path: укажите каталог хранения данных (по умолчанию — каталог данных текущего каталога, если он не существует, создайте новый)
  • Storage.tsdb.retention.time: укажите время хранения данных (по умолчанию 15 дней).

1.2.9. Запустите службу.

systemctl daemon-reload
systemctl start prometheus
systemctl status prometheus && systemctl enable prometheus
  • 1
  • 2
  • 3

1.2.10 Подтвердите, что порт контролируется.

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

1.2.11 Проверьте и загрузите файл конфигурации.

http://172.25.0.166:9090/metrics
  • 1

Вставьте сюда описание изображения


2. Мониторинг хостов Linux

2.1. Установите плагин экспорта на хост Linux.

2.1.1. Загрузите node_exporter.

URL-адрес загрузки

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

Загрузите необходимую версию

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

2.1.2. Создать группу пользователей.

groupadd prometheus
useradd -g prometheus -m -s /sbin/nologin prometheus
  • 1
  • 2

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
  • 1
  • 2
  • 3

2.1.4. Изменение разрешения.

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

2.1.5 Добавьте каталог исполняемого файла node_exporter в переменную среды.

vim /etc/profile
  • 1
export NODE_EXPORTER=/usr/local/node_exporter
export PATH=$PATH:$NODE_EXPORTER
export PATH
  • 1
  • 2
  • 3

Перезагрузить файл переменных системной среды.

source /etc/profile
  • 1

2.1.6. Создайте системный сервис.

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

2.1.7. Запустите службу.

systemctl daemon-reload
systemctl enable node_exporter && systemctl start node_exporter
systemctl status node_exporter
  • 1
  • 2
  • 3

2.1.8 Подтвердите, что порт контролируется.

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

2.2. Добавьте плагин экспорта хоста Linux в программный сервис Prometheus.

Изменить файл конфигурации Прометея

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

Перезапустите службу Прометея

systemctl restart prometheus
  • 1

2.3. Тестирование Prometheus для мониторинга состояния хоста Linux.

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

Вставьте сюда описание изображения

http://172.25.0.166:9100/metrics
  • 1

Вставьте сюда описание изображения


3. Используйте Grafana для отображения данных

3.1. Скачать графану.

URL-адрес загрузки

https://grafana.com/grafana/download
  • 1

Загрузите необходимую версию

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

3.2. Установите графану.

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

3.3. Запустить графану.

systemctl daemon-reload 
systemctl enable grafana-server && systemctl start grafana-server
  • 1
  • 2

3.4, графана, китайская культура

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

Перезапустить службу

systemctl restart grafana-server
  • 1

3.5 Загрузите и импортируйте шаблон графаны.

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

Вставьте сюда описание изображения

3.6. Загрузите плагин grafana-piechart-panel.

grafana-cli plugins install grafana-piechart-panel
  • 1

или

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

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