diff --git a/defaults/main.yml b/defaults/main.yml index 9f651db..9b5e280 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -14,10 +14,26 @@ grafana: password: changeme port: "3001" +prometheus_port: "9090" + +# Grafana and InfluxDB volumes +# grafana_volume_base: "/mnt/grafana" grafana_setup_path: "{{ grafana_volume_base }}/config" grafana_volume_grafana: "{{ grafana_volume_base }}/grafana" grafana_volume_influxdb: "{{ grafana_volume_base }}/influxdb" -grafana_uid: -grafana_gid: +# Prometheus volumes +# +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" diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml index 2e79620..411f33e 100644 --- a/molecule/default/molecule.yml +++ b/molecule/default/molecule.yml @@ -16,6 +16,7 @@ platforms: volumes: - /sys/fs/cgroup:/sys/fs/cgroup:ro - /var/run/docker.sock:/tmp/docker_mounted.sock + - /mnt:/mnt privileged: true pre_build_image: true provisioner: diff --git a/tasks/config.yml b/tasks/config.yml new file mode 100644 index 0000000..d069f7c --- /dev/null +++ b/tasks/config.yml @@ -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 \ No newline at end of file diff --git a/tasks/install.yml b/tasks/install.yml new file mode 100644 index 0000000..b780218 --- /dev/null +++ b/tasks/install.yml @@ -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 diff --git a/tasks/main.yml b/tasks/main.yml index 615f680..5df0fb8 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -1,72 +1,14 @@ --- # 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 - 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: "0750" - with_items: - - "{{ grafana_volume_influxdb | expanduser | realpath }}" +- name: "GRAFANA | Configure" + import_tasks: config.yml tags: - - configuration - become: true + - configure + - grafana -- name: 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 %}" - owner: "472" - group: "0" - mode: "0777" - with_items: - - "{{ grafana_volume_grafana | expanduser | realpath }}" - - "{{ grafana_volume_grafana | expanduser | realpath }}/plugins" +- name: "GRAFANA | Install" + import_tasks: install.yml tags: - - configuration - become: true - -- 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 + - install + - grafana diff --git a/templates/docker-compose.yml.j2 b/templates/docker-compose.yml.j2 index cc66e9a..16be660 100644 --- a/templates/docker-compose.yml.j2 +++ b/templates/docker-compose.yml.j2 @@ -1,7 +1,21 @@ version: '3.9' 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: image: influxdb:latest + container_name: monitoring_influxdb restart: always ports: - '{{ influxdb.port }}:8086' @@ -14,6 +28,7 @@ services: grafana: image: grafana/grafana-oss:latest + container_name: monitoring_grafana restart: always ports: - '{{ grafana.port }}:3000' @@ -22,14 +37,11 @@ services: - "{{ grafana_setup_path | expanduser | realpath }}/grafana-provisioning/:/etc/grafana/provisioning/" depends_on: - influxdb + - prometheus env_file: - "{{ grafana_setup_path | expanduser | realpath }}/env.grafana.conf" networks: - 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: grafana: {} diff --git a/templates/grafana-provisioning/datasources/datasource.yml.j2 b/templates/grafana-provisioning/datasources/datasource.yml.j2 index d2f2d4a..a0c0cb2 100644 --- a/templates/grafana-provisioning/datasources/datasource.yml.j2 +++ b/templates/grafana-provisioning/datasources/datasource.yml.j2 @@ -1,4 +1,4 @@ - lines (14 sloc) 345 Bytes +--- apiVersion: 1 datasources: - name: InfluxDB diff --git a/templates/prometheus-provisioning/prometheus.yml.j2 b/templates/prometheus-provisioning/prometheus.yml.j2 new file mode 100644 index 0000000..067e8ec --- /dev/null +++ b/templates/prometheus-provisioning/prometheus.yml.j2 @@ -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=` 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 %} + diff --git a/vars/main.yml b/vars/main.yml index 43674a4..cc6e520 100644 --- a/vars/main.yml +++ b/vars/main.yml @@ -1,2 +1,2 @@ --- -# vars file for grafana \ No newline at end of file +# vars file for grafana