---
service_port_grafana: 3000
service_port_prometheus: 9090
service_port_alertmanager: 9093

prometheus_id: "{{ inventory_hostname }}-prometheus"
alertmanager_id: "{{ inventory_hostname }}-alertmanager"
grafana_id: "{{ inventory_hostname }}-grafana"
prom2teams_id: "{{ inventory_hostname }}-prom2teams"

prometheus_docker:
  networks:
    - name: back-tier
      external: true

    - name: front-tier
      external: true

  volumes: "{{ prometheus_docker_volumes | selectattr('name', 'in', prometheus_docker_volume_names | join(',')) }}"

  services: "{{ prometheus_services | selectattr('name', 'in', prometheus_service_names | join(',')) }}"

prometheus_docker_volumes:
  - name: "{{ alertmanager_id }}-data"
  - name: "{{ grafana_id }}-data"

prometheus_services:
  - name: "{{ prometheus_id }}"
    image_name: "{{ prometheus_image_name }}"
    image_version: "{{ prom_prometheus_version }}"
    user: '"root"'
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.{{ prometheus_id }}.service={{ prometheus_id }}"
      - "traefik.http.routers.{{ prometheus_id }}.rule=Host(`{{ inventory_hostname }}-prometheus.{{ domain }}`)"
      - "traefik.http.routers.{{ prometheus_id }}.entrypoints=websecure"
      - "traefik.http.routers.{{ prometheus_id }}.tls=true"
      - "traefik.http.routers.{{ prometheus_id }}.tls.certresolver=letsencrypt"
      - "traefik.http.services.{{ prometheus_id }}.loadbalancer.server.port={{ service_port_prometheus }}"
      - "traefik.http.routers.{{ prometheus_id }}.middlewares={{ prometheus_id }}-basicauth"
      - "traefik.http.middlewares.{{ prometheus_id }}-basicauth.basicauth.users={{ prometheus_admin_username }}:{{ prometheus_admin_password_htpasswd }}"
    command:
      - "--config.file=/etc/prometheus/prometheus.yml"
      - "--storage.tsdb.path=/prometheus"
      - "--web.console.libraries=/usr/share/prometheus/console_libraries"
      - "--web.console.templates=/usr/share/prometheus/consoles"
      - "--web.external-url={{ http_s }}://{{ inventory_hostname }}-prometheus.{{ domain }}"
      - "--web.enable-lifecycle"
      - "--storage.tsdb.retention.time={{ prometheus_tsdb_rentention_time }}"
    volumes:
      - "./config/prometheus/:/etc/prometheus/:ro"
      - "{{ prometheus_datadir + '/' + 'data' | default(prometheus_id + '-data') }}:/prometheus"
    networks:
      - back-tier
      - front-tier
    extra_hosts: "{{ prometheus_extra_hosts | default([]) }}"

  - name: "{{ grafana_id }}"
    image_name: "{{ grafana_image_name }}"
    image_version: "{{ prom_grafana_version }}"
    user: '"472"'
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.{{ grafana_id }}.service={{ grafana_id }}"
      - "traefik.http.routers.{{ grafana_id }}.rule=Host(`{{ inventory_hostname }}-grafana.{{ domain }}`)"
      - "traefik.http.routers.{{ grafana_id }}.entrypoints=websecure"
      - "traefik.http.routers.{{ grafana_id }}.tls=true"
      - "traefik.http.routers.{{ grafana_id }}.tls.certresolver=letsencrypt"
      - "traefik.http.services.{{ grafana_id }}.loadbalancer.server.port={{ service_port_grafana }}"
    volumes:
      - "./config/grafana/provisioning/:/etc/grafana/provisioning/"
      - "./config/grafana/conf/defaults.ini:/usr/share/grafana/conf/defaults.ini"
      - "{{ grafana_id }}-data:/var/lib/grafana"
    networks:
      - back-tier
      - front-tier
    env_file:
      - "./config/grafana/config.monitoring"

  - name: "{{ alertmanager_id }}"
    image_name: "{{ alertmanager_image_name }}"
    image_version: "{{ prom_alertmanager_version }}"
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.{{ alertmanager_id }}.service={{ alertmanager_id }}"
      - "traefik.http.routers.{{ alertmanager_id }}.rule=Host(`{{ inventory_hostname }}-alertmanager.{{ domain }}`)"
      - "traefik.http.routers.{{ alertmanager_id }}.entrypoints=websecure"
      - "traefik.http.routers.{{ alertmanager_id }}.tls=true"
      - "traefik.http.routers.{{ alertmanager_id }}.tls.certresolver=letsencrypt"
      - "traefik.http.services.{{ alertmanager_id }}.loadbalancer.server.port={{ service_port_alertmanager }}"
      - "traefik.http.routers.{{ alertmanager_id }}.middlewares={{ alertmanager_id }}-basicauth"
      - "traefik.http.middlewares.{{ alertmanager_id }}-basicauth.basicauth.users={{ alertmanager_admin_username }}:\
        {{ alertmanager_admin_password_htpasswd }}"
    command:
      - "--config.file=/etc/alertmanager/config.yml"
      - "--storage.path=/alertmanager"
      - "--web.external-url={{ http_s }}://{{ inventory_hostname }}-alertmanager.{{ domain }}"
    environment:
      - 'LS_JAVA_OPTS: "-Xmx1G -Xms1G"'
    volumes:
      - "./config/alertmanager/:/etc/alertmanager/:ro"
      - "{{ alertmanager_id }}-data:/alertmanager"
    networks:
      - back-tier
      - front-tier

  - name: "{{ prom2teams_id }}"
    image_name: "{{ prom2teams_image_name }}"
    image_version: "{{ prom_prom2teams_version }}"
    environment:
      - 'PROM2TEAMS_PROMETHEUS_METRICS: "true"'
      - 'DEBUG_METRICS: "true"'
    ports:
      - external: 8089
        internal: 8089
    volumes:
      - "./config/prom2teams/config.ini:/opt/prom2teams/config.ini"
    networks:
      - back-tier
