# Grafana

#### Задание:

7\. Настройте мониторинг с помощью NodeExporter, Prometheus и Grafana.

* 1\. Создайте в домашней директории пользователя altlinux файл monitoring.yml для Docker Compose
  * 1\. Используйте контейнеры NodeExporter, Prometheus и Grafana для сбора, обработки и отображения метрик.
  * 2\. Настройте Dashboard в Grafana, в котором будет отображаться загрузка CPU, объём свободной оперативной памяти и места на диске.&#x20;
  * 3\. Интерфейс Grafana должен быть доступен по внешнему адресу на порту 3000.

#### Выполнение:

* В домашней директории пользователя **altlinux** создаём файл **monitoring.yml**:

```
vim ~/monitoring.yml
```

* * содержимое:

```
version: "3.9"
services:
  grafana:
    container_name: Grafana
    image: grafana/grafana
    ports:
      - "3000:3000"
    volumes:
      - grafana-data:/var/lib/grafana
      - grafana-configs:/etc/grafana

  prometheus:
    container_name: Prometheus
    image: prom/prometheus
    ports:
      - "9090:9090"
    volumes:
      - prom-data:/prometheus
      - prom-configs:/etc/prometheus

  node-exporter:
    container_name: NodeExporter
    image: prom/node-exporter
    ports:
      - "9100:9100"
    volumes:
      - /proc:/host/proc:ro
      - /sys:/host/sys:ro
      - /:/rootfs:ro
    command:
      - '--path.procfs=/host/proc'
      - '--path.sysfs=/host/sys'
      - '--collector.filesystem.mount-points-exclude'
      - '^/(sys|proc|dev|host|etc|rootfs/var/lib/docker/containers|rootfs/var/lib/docker/overlay2|rootfs/run/docker/netns|rootfs/var/lib/docker/aufs)($$|/)'
volumes:
  grafana-data:
  grafana-configs:
  prom-data:
  prom-configs:
```

Данный файл представляет описание трёх контейнеров:  **grafana, prometheus** и **node-exporter:**

* * **grafana** - это то, что мы будем видеть в браузере, это визуализатор метрик;
  * **prometheus** - это  своего рода база данных  в которой хранятся метрики;
  * **node-exporter** - это контейнер, который будет собирать метрики и отдавать на порту 9100

Схема работы получается следующая: **prometheus** забирает метрики с **node-exporter,** а **grafana** забирает метрики с **prometheus**

* Выполняем сборку и запуск **стека контейнеров** описанных в файле **monitoring.yml**:

```
docker-compose -f monitoring.yml up -d
```

* Проверяем:

<img src="https://sysahelper.ru/pluginfile.php/310/mod_page/content/2/image.png" alt="" height="154" width="915">

* Переходим в браузер **http\://<внешний IP адрес ControlVM>:3000**
  * для доступа в веб-интерфейс **Grafana** - стандартный логин и пароль "**admin**":

<img src="https://sysahelper.ru/pluginfile.php/310/mod_page/content/2/image%20%281%29.png" alt="" height="611" width="928">

* Задаём новый пароль:

<img src="https://sysahelper.ru/pluginfile.php/310/mod_page/content/2/image%20%282%29.png" alt="" height="641" width="926">

* Добавляем в **Grafana - Prometheus:**
  * на главном меню нажимаем **Add your first data source**

![](https://sysahelper.ru/pluginfile.php/310/mod_page/content/2/image%20%283%29.png)

* выбираем **Prometheus**:

<img src="https://sysahelper.ru/pluginfile.php/310/mod_page/content/2/image%20%284%29.png" alt="" height="636" width="925">

* вводим адрес контейнера с **Prometheus**:

<img src="https://sysahelper.ru/pluginfile.php/310/mod_page/content/2/image%20%285%29.png" alt="" height="506" width="926">

* Затем внизу на этой де странице нажимаем **Save and Test**:

![](https://sysahelper.ru/pluginfile.php/310/mod_page/content/2/image%20%286%29.png)

* * результат теста:

![](https://sysahelper.ru/pluginfile.php/310/mod_page/content/2/image%20%287%29.png)

* Далее необходимо экспортировать **grafana dashboard:**
  * [grafana dashboard node\_exporter](https://grafana.com/grafana/dashboards/12486-node-exporter-full/)
    * переходим на сайт и нажимаем **Download JSON**

<img src="https://sysahelper.ru/pluginfile.php/310/mod_page/content/2/image%20%288%29.png" alt="" height="661" width="1010">

![](https://sysahelper.ru/pluginfile.php/310/mod_page/content/2/image%20%289%29.png)

* Открываем скаченный файл и копируем его содержимое, затем переходим в наш веб-интерфейс **grafana**:
  * нажимаем **Create your first dashboard**:

<img src="https://sysahelper.ru/pluginfile.php/310/mod_page/content/2/image%20%2810%29.png" alt="" height="424" width="1031">

* Нажимаем **Import dashboard**:

<img src="https://sysahelper.ru/pluginfile.php/310/mod_page/content/2/image%20%2811%29.png" alt="" height="557" width="1034">

* Вставляем скопированное содержимое из скаченного файла и нажимаем **Load**:

<img src="https://sysahelper.ru/pluginfile.php/310/mod_page/content/2/image%20%2812%29.png" alt="" height="771" width="1036">

* Затем выбираем наш **Prometheus** и нажимаем **Import**:

<img src="https://sysahelper.ru/pluginfile.php/310/mod_page/content/2/image%20%2813%29.png" alt="" height="669" width="1035">

* Результат:

<img src="https://sysahelper.ru/pluginfile.php/310/mod_page/content/2/image%20%2814%29.png" alt="" height="426" width="1033">

*На данном этапе пока ещё результатов нет, так как необходимо добавить **node-exporter** в **prometheus.yml** расположенный в volumes docker*

* Открываем на редактирование файл по пути **/var/lib/docker/volumes/altlinux\_prom-configs/\_data/prometheus.yml:**

```
sudo vim /var/lib/docker/volumes/altlinux_prom-configs/_data/prometheus.yml
```

* * добавляем в конец информацию о нашем контейнере с node-exporter:

![](https://sysahelper.ru/pluginfile.php/310/mod_page/content/2/image%20%2821%29.png)

* Далее переходим в веб-интерфейс **prometheus** по **http\://<внешний IP адрес ControlVM>:9090**
  * перейти в раздел **Status** -> **Targets**

<img src="https://sysahelper.ru/pluginfile.php/310/mod_page/content/2/image%20%2816%29.png" alt="" height="411" width="927">

* Должен появиться новый Targets который мы добавили в файле **prometheus.yml**

<img src="https://sysahelper.ru/pluginfile.php/310/mod_page/content/2/image%20%2817%29.png" alt="" height="395" width="962">

* Чтобы он появился необходимо перезапустить контейнеры:

```
docker-compose -f monitoring.yml restart
```

* * результат:

![](https://sysahelper.ru/pluginfile.php/310/mod_page/content/2/image%20%2818%29.png)

* Возвращаемся в веб-интерфейс **prometheus**:

<img src="https://sysahelper.ru/pluginfile.php/310/mod_page/content/2/image%20%2820%29.png" alt="" height="391" width="959">

* Переходим в веб-интерфейс **Grafana**:
  * наблюдаем необходимую по заданию информация - CPU, объём свободной оперативной памяти и места на диске:&#x20;

<img src="https://sysahelper.ru/pluginfile.php/310/mod_page/content/2/image%20%2822%29.png" alt="" height="399" width="962">

Последнее изменение: четверг, 15 февраля 2024, 20:20


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://seraphim.gitbook.io/volodyazetkin2024gitbook/modul-v.-docker-compose-sozdanie-lokalnogo-docker-registry/grafana.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
