prometheus #3

Merged
Ghost merged 3 commits from prometheus into main 2025-02-25 13:22:07 +03:00
40 changed files with 684 additions and 21 deletions

6
docker/docker.yml Normal file
View File

@ -0,0 +1,6 @@
- name: Install docker
hosts: all
become: yes
roles:
- role: docker-common
tags: docker

View File

@ -11,48 +11,42 @@
- podman-docker - podman-docker
- containerd - containerd
- runc - runc
become: yes
- name: Update apt cache - name: Update APT cache
apt: apt:
update_cache: yes update_cache: yes
become: yes
- name: Install required packages - name: Install dependencies
apt: apt:
name: name:
- ca-certificates - ca-certificates
- curl - curl
- gnupg
state: present state: present
become: yes
- name: Create directory for Docker keyring - name: Create directory for GPG keys
file: file:
path: /etc/apt/keyrings path: /etc/apt/keyrings
state: directory state: directory
mode: '0755' mode: '0755'
become: yes
- name: Download Docker GPG key - name: Download Docker GPG key
get_url: get_url:
url: https://download.docker.com/linux/debian/gpg url: https://download.docker.com/linux/debian/gpg
dest: /etc/apt/keyrings/docker.asc dest: /etc/apt/keyrings/docker.asc
mode: '0644' mode: '0644'
become: yes
- name: Add Docker repository - name: Add Docker repository
apt_repository: shell: |
repo: "deb [arch={{ ansible_architecture }} signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian {{ ansible_distribution_release }} stable" echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] \
state: present https://download.docker.com/linux/debian $(lsb_release -cs) stable" \
filename: docker.list | tee /etc/apt/sources.list.d/docker.list > /dev/null
become: yes
- name: Update apt cache after adding Docker repository - name: Update APT cache after adding Docker repository
apt: apt:
update_cache: yes update_cache: yes
become: yes
- name: Install Docker packages - name: Install Docker and required packages
apt: apt:
name: name:
- docker-ce - docker-ce
@ -61,11 +55,9 @@
- docker-buildx-plugin - docker-buildx-plugin
- docker-compose-plugin - docker-compose-plugin
state: present state: present
become: yes
- name: Ensure Docker service is running and enabled - name: Enable and start Docker service
service: systemd:
name: docker name: docker
state: started
enabled: yes enabled: yes
become: yes state: started

13
monitoring/main.yml Normal file
View File

@ -0,0 +1,13 @@
- name: Installing prometheus
become: yes
hosts: all
roles:
- role: prometheus-common
tags: prometheus
- name: Installing grafana
become: yes
hosts: all
roles:
- role: grafana-common
tags: grafana

View File

@ -0,0 +1,24 @@
# grafana
## Описание роли
Эта роль предназначена для установки и настройки Grafana на целевой машине. Она скачивает нужный deb-пакет, устанавливает все необходимые зависимости, выполняет установку Grafana, а также настраивает и запускает сервис Grafana.
## Требования
- Ansible версии 2.9 или выше
- Операционная система на базе Debian (Ubuntu, Debian и другие)
## Переменные роли
Следующие переменные могут быть настроены в файле `vars/main.yml`, `defaults/main.yml` или переданы непосредственно в playbook:
- `grafana_version`: Версия Grafana для установки. По умолчанию `"11.4.0"`.
- `grafana_arch`: Архитектура системы для пакета Grafana. По умолчанию `"amd64"`.
- `destination_directory`: Каталог для загрузки пакета. По умолчанию `"/home/user"`.
Эти переменные можно изменить в соответствии с требованиями вашей системы.
## Зависимости
Нет.

View File

@ -0,0 +1,2 @@
---
# defaults file for grafana-common

View File

@ -0,0 +1,2 @@
---
# handlers file for grafana-common

View File

@ -0,0 +1,34 @@
galaxy_info:
author: your name
description: your role description
company: your company (optional)
# If the issue tracker for your role is not on github, uncomment the
# next line and provide a value
# issue_tracker_url: http://example.com/issue/tracker
# Choose a valid license ID from https://spdx.org - some suggested licenses:
# - BSD-3-Clause (default)
# - MIT
# - GPL-2.0-or-later
# - GPL-3.0-only
# - Apache-2.0
# - CC-BY-4.0
license: license (GPL-2.0-or-later, MIT, etc)
min_ansible_version: 2.1
# If this a Container Enabled role, provide the minimum Ansible Container version.
# min_ansible_container_version:
galaxy_tags: []
# List tags for your role here, one per line. A tag is a keyword that describes
# and categorizes the role. Users find roles by searching for tags. Be sure to
# remove the '[]' above, if you add tags to this list.
#
# NOTE: A tag is limited to a single word comprised of alphanumeric characters.
# Maximum 20 tags per role.
dependencies: []
# List your role dependencies here, one per line. Be sure to remove the '[]' above,
# if you add dependencies to this list.

View File

@ -0,0 +1,34 @@
---
- name: Download Grafana deb package
ansible.builtin.get_url:
url: "https://dl.grafana.com/oss/release/grafana_{{ grafana_version }}_{{ grafana_arch }}.deb"
dest: "{{ destination_directory }}"
- name: Update repos and upgrade system
apt:
update_cache: yes
upgrade: dist
autoremove: yes
- name: Install dependencies
apt:
name:
- adduser
- libfontconfig1
- musl
state: present
- name: Install Grafana from deb package
ansible.builtin.apt:
deb: "{{ destination_directory }}/grafana_{{ grafana_version }}_{{ grafana_arch }}.deb"
state: present
- name: Remove downloaded deb package
ansible.builtin.file:
path: "{{ destination_directory }}/grafana_{{ grafana_version }}_{{ grafana_arch }}.deb"
state: absent
- name: Enable and start Grafana service
ansible.builtin.systemd:
name: grafana-server
enabled: yes
state: started

View File

@ -0,0 +1,2 @@
localhost

View File

@ -0,0 +1,5 @@
---
- hosts: localhost
remote_user: root
roles:
- grafana-common

View File

@ -0,0 +1,4 @@
---
grafana_version: 11.4.0
grafana_arch: amd64
destination_directory: /home/user

View File

@ -0,0 +1,39 @@
# prometheus-common
## Описание роли
Эта роль устанавливает и настраивает Prometheus на целевой машине, обеспечивая создание всех необходимых директорий и зависимостей, скачивание и установку Prometheus, а также развертывание файлов конфигурации и systemd-сервиса. Роль также гарантирует, что Prometheus будет запущен как фоновый сервис и настроен на автозапуск при старте системы.
## Требования
- Версия Ansible 2.9 или выше
- Операционная система на базе Debian (Ubuntu, Debian и другие)
- Указание версии Prometheus в файле `vars/main.yml`
## Переменные роли
Следующие переменные могут быть настроены в файлах `vars/main.yml` или `defaults/main.yml`, или переданы напрямую в роль:
- `prometheus_user`: Пользователь, под которым будет работать Prometheus. По умолчанию `prometheus`.
- `prometheus_group`: Группа, под которой будет работать Prometheus. По умолчанию `prometheus`.
- `prometheus_install_dir`: Каталог, в который будет установлен Prometheus. По умолчанию `/usr/local/bin`.
- `prometheus_data_dir`: Каталог для хранения данных Prometheus. По умолчанию `/var/lib/prometheus`.
- `prometheus_config_dir`: Каталог для хранения конфигурации Prometheus. По умолчанию `/etc/prometheus`.
- `prometheus_version`: Версия Prometheus для установки. По умолчанию `2.35.0`.
Эти значения можно изменить в соответствии с требованиями вашей системы или инфраструктуры.
## Зависимости
Нет.
## Пример использования
Вот пример playbook, который использует эту роль:
```yaml
---
- hosts: servers
become: yes
roles:
- prometheus-common

View File

@ -0,0 +1,2 @@
---
# defaults file for prometheus-common

View File

@ -0,0 +1,17 @@
[Unit]
Description=Background service of Prometheus
Wants=network-online.target
After=network-online.target
[Service]
User=prometheus
Group=prometheus
Type=simple
ExecStart=/usr/local/bin/prometheus \
--config.file /etc/prometheus/prometheus.yml \
--storage.tsdb.path /var/lib/prometheus/ \
--web.console.templates=/etc/prometheus/consoles \
--web.console.libraries=/etc/prometheus/console_libraries
[Install]
WantedBy=multi-user.target

View File

@ -0,0 +1,7 @@
global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_configs:
- job_name: "prometheus"
static_configs:
- targets: ["192.168.2.34:9100"]

View File

@ -0,0 +1,5 @@
---
- name: restart prometheus
systemd:
name: prometheus
state: restarted

View File

@ -0,0 +1,34 @@
galaxy_info:
author: your name
description: your role description
company: your company (optional)
# If the issue tracker for your role is not on github, uncomment the
# next line and provide a value
# issue_tracker_url: http://example.com/issue/tracker
# Choose a valid license ID from https://spdx.org - some suggested licenses:
# - BSD-3-Clause (default)
# - MIT
# - GPL-2.0-or-later
# - GPL-3.0-only
# - Apache-2.0
# - CC-BY-4.0
license: license (GPL-2.0-or-later, MIT, etc)
min_ansible_version: 2.1
# If this a Container Enabled role, provide the minimum Ansible Container version.
# min_ansible_container_version:
galaxy_tags: []
# List tags for your role here, one per line. A tag is a keyword that describes
# and categorizes the role. Users find roles by searching for tags. Be sure to
# remove the '[]' above, if you add tags to this list.
#
# NOTE: A tag is limited to a single word comprised of alphanumeric characters.
# Maximum 20 tags per role.
dependencies: []
# List your role dependencies here, one per line. Be sure to remove the '[]' above,
# if you add dependencies to this list.

View File

@ -0,0 +1,86 @@
---
- name: Install dependencies
apt:
name:
- wget
- tar
- curl
state: present
update_cache: yes
when: ansible_os_family == "Debian"
- name: Create prometheus user and group
user:
name: "{{ prometheus_user }}"
group: "{{ prometheus_group }}"
system: yes
create_home: no
- name: Create required directories
file:
path: "{{ item }}"
state: directory
owner: "{{ prometheus_user }}"
group: "{{ prometheus_group }}"
mode: '0755'
with_items:
- "{{ prometheus_install_dir }}"
- "{{ prometheus_data_dir }}"
- "{{ prometheus_config_dir }}"
- name: Download Prometheus
get_url:
url: "https://github.com/prometheus/prometheus/releases/download/v{{ prometheus_version }}/prometheus-{{ prometheus_version }}.linux-amd64.tar.gz"
dest: "/tmp/prometheus-{{ prometheus_version }}.tar.gz"
- name: Extract Prometheus
unarchive:
src: "/tmp/prometheus-{{ prometheus_version }}.tar.gz"
dest: "{{ prometheus_install_dir }}"
remote_src: yes
- name: Move binaries to /usr/local/bin
command:
cmd: mv "{{ prometheus_install_dir }}/prometheus-{{ prometheus_version }}.linux-amd64/{{ item }}" /usr/local/bin/
with_items:
- "prometheus"
- "promtool"
- name: Set ownership of Prometheus files
file:
path: "{{ item }}"
owner: "{{ prometheus_user }}"
group: "{{ prometheus_group }}"
recurse: yes
with_items:
- "{{ prometheus_install_dir }}"
- "{{ prometheus_data_dir }}"
- "{{ prometheus_config_dir }}"
- name: Copy prometheus.yml configuration
copy:
src: prometheus.yml
dest: "{{ prometheus_config_dir }}/prometheus.yml"
owner: "{{ prometheus_user }}"
group: "{{ prometheus_group }}"
mode: '0644'
notify:
- restart prometheus
- name: Copy systemd service file
copy:
src: prometheus.service
dest: /etc/systemd/system/prometheus.service
owner: root
group: root
mode: '0644'
- name: Reload systemd
systemd:
daemon_reload: yes
- name: Enable Prometheus service
systemd:
name: prometheus
enabled: yes
state: started

View File

@ -0,0 +1,2 @@
localhost

View File

@ -0,0 +1,5 @@
---
- hosts: localhost
remote_user: root
roles:
- prometheus-common

View File

@ -0,0 +1,9 @@
---
prometheus_version: "3.1.0"
prometheus_user: "prometheus"
prometheus_group: "prometheus"
prometheus_install_dir: "/opt/prometheus"
prometheus_data_dir: "/var/lib/prometheus"
prometheus_config_dir: "/etc/prometheus"
prometheus_bin: "/usr/local/bin/prometheus"
prometheus_web_listen_address: ":9090"

13
prometheus/main.yml Normal file
View File

@ -0,0 +1,13 @@
- name: Installing prometheus
become: yes
hosts: all
roles:
- role: prometheus-common
tags: prometheus
- name: Installing grafana
become: yes
hosts: all
roles:
- role: grafana-common
tags: grafana

View File

@ -0,0 +1,38 @@
Role Name
=========
A brief description of the role goes here.
Requirements
------------
Any pre-requisites that may not be covered by Ansible itself or the role should be mentioned here. For instance, if the role uses the EC2 module, it may be a good idea to mention in this section that the boto package is required.
Role Variables
--------------
A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well.
Dependencies
------------
A list of other roles hosted on Galaxy should go here, plus any details in regards to parameters that may need to be set for other roles, or variables that are used from other roles.
Example Playbook
----------------
Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too:
- hosts: servers
roles:
- { role: username.rolename, x: 42 }
License
-------
BSD
Author Information
------------------
An optional section for the role authors to include contact information, or a website (HTML is not allowed).

View File

@ -0,0 +1,2 @@
---
# defaults file for grafana-common

View File

@ -0,0 +1,2 @@
---
# handlers file for grafana-common

View File

@ -0,0 +1,34 @@
galaxy_info:
author: your name
description: your role description
company: your company (optional)
# If the issue tracker for your role is not on github, uncomment the
# next line and provide a value
# issue_tracker_url: http://example.com/issue/tracker
# Choose a valid license ID from https://spdx.org - some suggested licenses:
# - BSD-3-Clause (default)
# - MIT
# - GPL-2.0-or-later
# - GPL-3.0-only
# - Apache-2.0
# - CC-BY-4.0
license: license (GPL-2.0-or-later, MIT, etc)
min_ansible_version: 2.1
# If this a Container Enabled role, provide the minimum Ansible Container version.
# min_ansible_container_version:
galaxy_tags: []
# List tags for your role here, one per line. A tag is a keyword that describes
# and categorizes the role. Users find roles by searching for tags. Be sure to
# remove the '[]' above, if you add tags to this list.
#
# NOTE: A tag is limited to a single word comprised of alphanumeric characters.
# Maximum 20 tags per role.
dependencies: []
# List your role dependencies here, one per line. Be sure to remove the '[]' above,
# if you add dependencies to this list.

View File

@ -0,0 +1,34 @@
---
- name: Download Grafana deb package
ansible.builtin.get_url:
url: "https://dl.grafana.com/oss/release/grafana_{{ grafana_version }}_{{ grafana_arch }}.deb"
dest: "{{ destination_directory }}"
- name: Update repos and upgrade system
apt:
update_cache: yes
upgrade: dist
autoremove: yes
- name: Install dependencies
apt:
name:
- adduser
- libfontconfig1
- musl
state: present
- name: Install Grafana from deb package
ansible.builtin.apt:
deb: "{{ destination_directory }}/grafana_{{ grafana_version }}_{{ grafana_arch }}.deb"
state: present
- name: Remove downloaded deb package
ansible.builtin.file:
path: "{{ destination_directory }}/grafana_{{ grafana_version }}_{{ grafana_arch }}.deb"
state: absent
- name: Enable and start Grafana service
ansible.builtin.systemd:
name: grafana-server
enabled: yes
state: started

View File

@ -0,0 +1,2 @@
localhost

View File

@ -0,0 +1,5 @@
---
- hosts: localhost
remote_user: root
roles:
- grafana-common

View File

@ -0,0 +1,4 @@
---
grafana_version: 11.4.0
grafana_arch: amd64
destination_directory: /home/user

View File

@ -0,0 +1,38 @@
Role Name
=========
A brief description of the role goes here.
Requirements
------------
Any pre-requisites that may not be covered by Ansible itself or the role should be mentioned here. For instance, if the role uses the EC2 module, it may be a good idea to mention in this section that the boto package is required.
Role Variables
--------------
A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well.
Dependencies
------------
A list of other roles hosted on Galaxy should go here, plus any details in regards to parameters that may need to be set for other roles, or variables that are used from other roles.
Example Playbook
----------------
Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too:
- hosts: servers
roles:
- { role: username.rolename, x: 42 }
License
-------
BSD
Author Information
------------------
An optional section for the role authors to include contact information, or a website (HTML is not allowed).

View File

@ -0,0 +1,2 @@
---
# defaults file for prometheus-common

View File

@ -0,0 +1,17 @@
[Unit]
Description=Background service of Prometheus
Wants=network-online.target
After=network-online.target
[Service]
User=prometheus
Group=prometheus
Type=simple
ExecStart=/usr/local/bin/prometheus \
--config.file /etc/prometheus/prometheus.yml \
--storage.tsdb.path /var/lib/prometheus/ \
--web.console.templates=/etc/prometheus/consoles \
--web.console.libraries=/etc/prometheus/console_libraries
[Install]
WantedBy=multi-user.target

View File

@ -0,0 +1,7 @@
global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_configs:
- job_name: "prometheus"
static_configs:
- targets: ["192.168.2.34:9100"]

View File

@ -0,0 +1,5 @@
---
- name: restart prometheus
systemd:
name: prometheus
state: restarted

View File

@ -0,0 +1,34 @@
galaxy_info:
author: your name
description: your role description
company: your company (optional)
# If the issue tracker for your role is not on github, uncomment the
# next line and provide a value
# issue_tracker_url: http://example.com/issue/tracker
# Choose a valid license ID from https://spdx.org - some suggested licenses:
# - BSD-3-Clause (default)
# - MIT
# - GPL-2.0-or-later
# - GPL-3.0-only
# - Apache-2.0
# - CC-BY-4.0
license: license (GPL-2.0-or-later, MIT, etc)
min_ansible_version: 2.1
# If this a Container Enabled role, provide the minimum Ansible Container version.
# min_ansible_container_version:
galaxy_tags: []
# List tags for your role here, one per line. A tag is a keyword that describes
# and categorizes the role. Users find roles by searching for tags. Be sure to
# remove the '[]' above, if you add tags to this list.
#
# NOTE: A tag is limited to a single word comprised of alphanumeric characters.
# Maximum 20 tags per role.
dependencies: []
# List your role dependencies here, one per line. Be sure to remove the '[]' above,
# if you add dependencies to this list.

View File

@ -0,0 +1,86 @@
---
- name: Install dependencies
apt:
name:
- wget
- tar
- curl
state: present
update_cache: yes
when: ansible_os_family == "Debian"
- name: Create prometheus user and group
user:
name: "{{ prometheus_user }}"
group: "{{ prometheus_group }}"
system: yes
create_home: no
- name: Create required directories
file:
path: "{{ item }}"
state: directory
owner: "{{ prometheus_user }}"
group: "{{ prometheus_group }}"
mode: '0755'
with_items:
- "{{ prometheus_install_dir }}"
- "{{ prometheus_data_dir }}"
- "{{ prometheus_config_dir }}"
- name: Download Prometheus
get_url:
url: "https://github.com/prometheus/prometheus/releases/download/v{{ prometheus_version }}/prometheus-{{ prometheus_version }}.linux-amd64.tar.gz"
dest: "/tmp/prometheus-{{ prometheus_version }}.tar.gz"
- name: Extract Prometheus
unarchive:
src: "/tmp/prometheus-{{ prometheus_version }}.tar.gz"
dest: "{{ prometheus_install_dir }}"
remote_src: yes
- name: Move binaries to /usr/local/bin
command:
cmd: mv "{{ prometheus_install_dir }}/prometheus-{{ prometheus_version }}.linux-amd64/{{ item }}" /usr/local/bin/
with_items:
- "prometheus"
- "promtool"
- name: Set ownership of Prometheus files
file:
path: "{{ item }}"
owner: "{{ prometheus_user }}"
group: "{{ prometheus_group }}"
recurse: yes
with_items:
- "{{ prometheus_install_dir }}"
- "{{ prometheus_data_dir }}"
- "{{ prometheus_config_dir }}"
- name: Copy prometheus.yml configuration
copy:
src: prometheus.yml
dest: "{{ prometheus_config_dir }}/prometheus.yml"
owner: "{{ prometheus_user }}"
group: "{{ prometheus_group }}"
mode: '0644'
notify:
- restart prometheus
- name: Copy systemd service file
copy:
src: prometheus.service
dest: /etc/systemd/system/prometheus.service
owner: root
group: root
mode: '0644'
- name: Reload systemd
systemd:
daemon_reload: yes
- name: Enable Prometheus service
systemd:
name: prometheus
enabled: yes
state: started

View File

@ -0,0 +1,2 @@
localhost

View File

@ -0,0 +1,5 @@
---
- hosts: localhost
remote_user: root
roles:
- prometheus-common

View File

@ -0,0 +1,9 @@
---
prometheus_version: "3.1.0"
prometheus_user: "prometheus"
prometheus_group: "prometheus"
prometheus_install_dir: "/opt/prometheus"
prometheus_data_dir: "/var/lib/prometheus"
prometheus_config_dir: "/etc/prometheus"
prometheus_bin: "/usr/local/bin/prometheus"
prometheus_web_listen_address: ":9090"