feat! Add prometheus.

This commit is contained in:
Laur Ivan 2023-04-13 00:50:07 +02:00
parent 778efcd7ec
commit f6ed84b8d6
9 changed files with 184 additions and 74 deletions

View File

@ -14,10 +14,26 @@ grafana:
password: changeme password: changeme
port: "3001" port: "3001"
prometheus_port: "9090"
# Grafana and InfluxDB volumes
#
grafana_volume_base: "/mnt/grafana" grafana_volume_base: "/mnt/grafana"
grafana_setup_path: "{{ grafana_volume_base }}/config" grafana_setup_path: "{{ grafana_volume_base }}/config"
grafana_volume_grafana: "{{ grafana_volume_base }}/grafana" grafana_volume_grafana: "{{ grafana_volume_base }}/grafana"
grafana_volume_influxdb: "{{ grafana_volume_base }}/influxdb" grafana_volume_influxdb: "{{ grafana_volume_base }}/influxdb"
grafana_uid: # Prometheus volumes
grafana_gid: #
grafana_volume_prometheus: "{{ grafana_volume_base }}/prometheus"
grafana_prometheus_project: my-project
grafana_prometheus_job_name: prometheus
grafana_prometheus_scrape_interval: '120s'
grafana_prometheus_targets:
- "10.0.0.35:9100"
- "10.0.0.35:8080"
grafana_uid: "472"
grafana_gid: "0"

View File

@ -16,6 +16,7 @@ platforms:
volumes: volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:ro - /sys/fs/cgroup:/sys/fs/cgroup:ro
- /var/run/docker.sock:/tmp/docker_mounted.sock - /var/run/docker.sock:/tmp/docker_mounted.sock
- /mnt:/mnt
privileged: true privileged: true
pre_build_image: true pre_build_image: true
provisioner: provisioner:

96
tasks/config.yml Normal file
View File

@ -0,0 +1,96 @@
---
- name: GRAFANA | Set up main directory
ansible.builtin.file:
state: directory
path: "{{ item }}"
owner: "{{ ansible_effective_user_id }}"
group: "{{ ansible_effective_group_id }}"
mode: "0750"
with_items:
- "{{ grafana_setup_path | expanduser | realpath }}/grafana-provisioning/"
- "{{ grafana_setup_path | expanduser | realpath }}/grafana-provisioning/datasources/"
tags:
- configuration
become: true
- name: GRAFANA | Set up prometheus configuration directory
ansible.builtin.file:
state: directory
path: "{{ item }}"
owner: "{{ ansible_effective_user_id }}"
group: "{{ ansible_effective_group_id }}"
mode: "0755"
with_items:
- "{{ grafana_setup_path | expanduser | realpath }}/prometheus/"
tags:
- configuration
become: true
- name: GRAFANA | Set up prometheus data directory
ansible.builtin.file:
state: directory
path: "{{ item }}"
owner: "{{ ansible_effective_user_id }}"
group: "{{ ansible_effective_group_id }}"
mode: "0777"
with_items:
- "{{ grafana_volume_prometheus | expanduser | realpath }}"
tags:
- configuration
become: true
- name: GRAFANA | Set up influxdb mount point
ansible.builtin.file:
state: directory
path: "{{ item }}"
mode: "0750"
with_items:
- "{{ grafana_volume_influxdb | expanduser | realpath }}"
tags:
- configuration
become: true
- name: GRAFANA | Set up grafana mount point
ansible.builtin.file:
state: directory
path: "{{ item }}"
owner: "{% if grafana_uid %}{{ grafana_uid }}{% else %}{{ ansible_effective_user_id }}{% endif %}"
group: "{% if grafana_gid %}{{ grafana_gid }}{% else %}{{ ansible_effective_group_id }}{% endif %}"
mode: "0777"
with_items:
- "{{ grafana_volume_grafana | expanduser | realpath }}"
- "{{ grafana_volume_grafana | expanduser | realpath }}/plugins"
tags:
- configuration
become: true
- name: GRAFANA | Write docker-compose configuration files
ansible.builtin.template:
src: "{{ item }}.j2"
dest: "{{ grafana_setup_path | expanduser | realpath }}/{{ item }}"
mode: '0640'
loop:
- "docker-compose.yml"
- "env.grafana.conf"
- "env.influxdb.conf"
tags:
- configuration
- name: GRAFANA | Write InfluxDB provisioning files
ansible.builtin.template:
src: "grafana-provisioning/datasources/{{ item }}.j2"
dest: "{{ grafana_setup_path | expanduser | realpath }}/grafana-provisioning/datasources/{{ item }}"
mode: '0640'
loop:
- "datasource.yml"
tags:
- configuration
- name: GRAFANA | Write Prometheus configuration
ansible.builtin.template:
src: "prometheus-provisioning/prometheus.yml.j2"
dest: "{{ grafana_setup_path | expanduser | realpath }}/prometheus/prometheus.yml"
mode: '0644'
tags:
- configuration

7
tasks/install.yml Normal file
View File

@ -0,0 +1,7 @@
---
- name: Ensure all requested components are running.
community.docker.docker_compose:
project_src: "{{ grafana_setup_path | expanduser | realpath }}"
build: false
become: true

View File

@ -1,72 +1,14 @@
--- ---
# tasks file for grafana # tasks file for grafana
- name: Set up main directory
ansible.builtin.file:
state: directory
path: "{{ item }}"
owner: "{{ ansible_effective_user_id }}"
group: "{{ ansible_effective_group_id }}"
mode: "0750"
with_items:
- "{{ grafana_setup_path | expanduser | realpath }}/grafana-provisioning/"
- "{{ grafana_setup_path | expanduser | realpath }}/grafana-provisioning/datasources/"
tags:
- configuration
become: true
- name: Set up influxdb mount point - name: "GRAFANA | Configure"
ansible.builtin.file: import_tasks: config.yml
state: directory
path: "{{ item }}"
owner: "{% if grafana_uid %}{{ grafana_uid }}{% else %}{{ ansible_effective_user_id }}{% endif %}"
group: "{% if grafana_gid %}{{ grafana_gid }}{% else %}{{ ansible_effective_group_id }}{% endif %}"
mode: "0750"
with_items:
- "{{ grafana_volume_influxdb | expanduser | realpath }}"
tags: tags:
- configuration - configure
become: true - grafana
- name: Set up grafana mount point - name: "GRAFANA | Install"
ansible.builtin.file: import_tasks: install.yml
state: directory
path: "{{ item }}"
# owner: "{% if grafana_uid %}{{ grafana_uid }}{% else %}{{ ansible_effective_user_id }}{% endif %}"
# group: "{% if grafana_gid %}{{ grafana_gid }}{% else %}{{ ansible_effective_group_id }}{% endif %}"
owner: "472"
group: "0"
mode: "0777"
with_items:
- "{{ grafana_volume_grafana | expanduser | realpath }}"
- "{{ grafana_volume_grafana | expanduser | realpath }}/plugins"
tags: tags:
- configuration - install
become: true - grafana
- name: Write configuration files
ansible.builtin.template:
src: "{{ item }}.j2"
dest: "{{ grafana_setup_path | expanduser | realpath }}/{{ item }}"
mode: '0640'
loop:
- "docker-compose.yml"
- "env.grafana.conf"
- "env.influxdb.conf"
tags:
- configuration
- name: Write datasource provisioning files
ansible.builtin.template:
src: "grafana-provisioning/datasources/{{ item }}.j2"
dest: "{{ grafana_setup_path | expanduser | realpath }}/grafana-provisioning/datasources/{{ item }}"
mode: '0640'
loop:
- "datasource.yml"
tags:
- configuration
- name: Ensure all requested components are running.
community.docker.docker_compose:
project_src: "{{ grafana_setup_path | expanduser | realpath }}"
build: false
become: true

View File

@ -1,7 +1,21 @@
version: '3.9' version: '3.9'
services: services:
prometheus:
image: prom/prometheus:latest
container_name: monitoring_prometheus
restart: unless-stopped
volumes:
- '{{ grafana_setup_path | expanduser | realpath }}/prometheus:/etc/prometheus'
- '{{ grafana_volume_prometheus | expanduser | realpath }}:/prometheus'
ports:
{% if prometheus_port is defined %}- "{{ prometheus_port }}:9090"{% endif %}
networks:
- grafana
influxdb: influxdb:
image: influxdb:latest image: influxdb:latest
container_name: monitoring_influxdb
restart: always restart: always
ports: ports:
- '{{ influxdb.port }}:8086' - '{{ influxdb.port }}:8086'
@ -14,6 +28,7 @@ services:
grafana: grafana:
image: grafana/grafana-oss:latest image: grafana/grafana-oss:latest
container_name: monitoring_grafana
restart: always restart: always
ports: ports:
- '{{ grafana.port }}:3000' - '{{ grafana.port }}:3000'
@ -22,14 +37,11 @@ services:
- "{{ grafana_setup_path | expanduser | realpath }}/grafana-provisioning/:/etc/grafana/provisioning/" - "{{ grafana_setup_path | expanduser | realpath }}/grafana-provisioning/:/etc/grafana/provisioning/"
depends_on: depends_on:
- influxdb - influxdb
- prometheus
env_file: env_file:
- "{{ grafana_setup_path | expanduser | realpath }}/env.grafana.conf" - "{{ grafana_setup_path | expanduser | realpath }}/env.grafana.conf"
networks: networks:
- grafana - grafana
# Run as user:
# owner: "{% if grafana_uid %}{{ grafana_uid }}{% else %}{{ ansible_effective_user_id }}{% endif %}"
# group: "{% if grafana_gid %}{{ grafana_gid }}{% else %}{{ ansible_effective_group_id }}{% endif %}"
networks: networks:
grafana: {} grafana: {}

View File

@ -1,4 +1,4 @@
lines (14 sloc) 345 Bytes ---
apiVersion: 1 apiVersion: 1
datasources: datasources:
- name: InfluxDB - name: InfluxDB

View File

@ -0,0 +1,36 @@
# my global config
global:
scrape_interval: 120s # By default, scrape targets every 15 seconds.
evaluation_interval: 120s # By default, scrape targets every 15 seconds.
# scrape_timeout is set to the global default (10s).
# Attach these labels to any time series or alerts when communicating with
# external systems (federation, remote storage, Alertmanager).
external_labels:
monitor: "{{ grafana_prometheus_project }}"
# Load and evaluate rules in this file every 'evaluation_interval' seconds.
rule_files:
# - "alert.rules"
# - "first.rules"
# - "second.rules"
# 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: "{{ grafana_prometheus_job_name }}"
# Override the global default and scrape targets from this job every 5 seconds.
scrape_interval: "{{ grafana_prometheus_scrape_interval }}"
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets:
- 'localhost:9090'
{% for target in grafana_prometheus_targets %}
- "{{ target }}""
{% endfor %}