How to fetch .env values to assign alertmanager.yml file (not a valid value)

In my spring boot project, I connect alertmanager with prometheus and grafana but I cannot fetch properties values from .env and assign them to alertmanager.yml

Here is the alertmanager part of docker-compose.yml

  alertmanager:
    image: prom/alertmanager:latest
    container_name: alertmanager
    restart: unless-stopped
    ports:
      - "9093:9093"
    volumes:
      - ./data/alertmanager/config:/etc/alertmanager
    env_file:
      - .env
    command:
      - '--config.file=/etc/alertmanager/alertmanager.yml'
    networks:
      - pdfcompare_network

Here is the .env

ALERT_RESOLVE_TIMEOUT=5m
SMTP_SMARTHOST=smtp.gmail.com:587
SMTP_FROM=gmail_email
SMTP_AUTH_USERNAME=gmail_email
SMTP_AUTH_PASSWORD=gmail_authentication_password
SMTP_REQUIRE_TLS=true
ALERT_EMAIL_TO=gmail_email

Here is alertmanager.yml

global:
  resolve_timeout: ${ALERT_RESOLVE_TIMEOUT}
  smtp_smarthost: ${SMTP_SMARTHOST}
  smtp_from: ${SMTP_FROM}
  smtp_auth_username: ${SMTP_AUTH_USERNAME}
  smtp_auth_password: ${SMTP_AUTH_PASSWORD}
  smtp_require_tls: ${SMTP_REQUIRE_TLS}

route:
  group_by: ['alertname']
  group_wait: 30s
  group_interval: 5m
  repeat_interval: 5m
  receiver: 'default'

receivers:
  - name: 'default'
    webhook_configs:
      - url: 'http://grafana:3000'
    email_configs:
      - to: ${ALERT_EMAIL_TO}
        send_resolved: true

I got this error shown below

level=ERROR source=coordinator.go:117 msg="Loading configuration file failed" component=configuration file=/etc/alertmanager/alertmanager.yml err="not a valid duration string: \"${ALERT_RESOLVE_TIMEOUT}\""

How can I fix it?

Upvotes: 1

Views: 30

Answers (2)

Here is the solution

1 ) Create Dockerfile to Copy the configuration and entrypoint script into the image 2 ) Define entrypoint.sh to set values to alertmanager.yml 3 ) Revise

Here is the Dockerfile shown below

FROM prom/alertmanager:latest

# Copy the configuration and entrypoint script into the image
COPY config/alertmanager.yml /etc/alertmanager/alertmanager.yml
COPY entrypoint.sh /entrypoint.sh

# Copy the entrypoint script and set it as executable with octal permissions
COPY --chmod=0755 entrypoint.sh /entrypoint.sh

# Use the custom entrypoint
ENTRYPOINT ["/entrypoint.sh"]

Here is the entrypoint.sh shown below

#!/bin/sh
set -e

# Replace placeholders with environment variable values in the config file
sed -i "s|\${ALERT_RESOLVE_TIMEOUT}|${ALERT_RESOLVE_TIMEOUT}|g" /etc/alertmanager/alertmanager.yml
sed -i "s|\${SMTP_SMARTHOST}|${SMTP_SMARTHOST}|g" /etc/alertmanager/alertmanager.yml
sed -i "s|\${SMTP_FROM}|${SMTP_FROM}|g" /etc/alertmanager/alertmanager.yml
sed -i "s|\${SMTP_AUTH_USERNAME}|${SMTP_AUTH_USERNAME}|g" /etc/alertmanager/alertmanager.yml
sed -i "s|\${SMTP_AUTH_PASSWORD}|${SMTP_AUTH_PASSWORD}|g" /etc/alertmanager/alertmanager.yml
sed -i "s|\${SMTP_REQUIRE_TLS}|${SMTP_REQUIRE_TLS}|g" /etc/alertmanager/alertmanager.yml
sed -i "s|\${ALERT_EMAIL_TO}|${ALERT_EMAIL_TO}|g" /etc/alertmanager/alertmanager.yml

# Start Alertmanager
exec alertmanager --config.file=/etc/alertmanager/alertmanager.yml

Here is the alertmanager part of docker-compose.yml

alertmanager:
    build:
      context: ./data/alertmanager
      dockerfile: Dockerfile
    container_name: alertmanager
    restart: unless-stopped
    ports:
      - "9093:9093"
    env_file:
      - .env
    networks:
      - pdfcompare_network

Upvotes: 0

IamK
IamK

Reputation: 2994

It looks like alertmanager itself doesn't perform env variable substitution, you can try doing it for yourself. For example with a custom command:

alertmanager:
  command:
    - '/bin/sh'
    - '-c'
    - 'envsubst < /etc/alertmanager/alertmanager.template.yml > /etc/alertmanager/alertmanager.yml && alertmanager --config.file=/etc/alertmanager/alertmanager.yml'

Upvotes: 0

Related Questions