1+ #! /bin/bash
2+
3+ echo " Starting ElastAlert with Prometheus monitoring..."
4+
5+ export ELASTALERT_CONFIG_FILE=" /app/elastalert_config.yml"
6+ export ELASTALERT_CONFIG_PROCESSED=" /app/elastalert_config_processed.yml"
7+ export PROMETHEUS_PORT=" 9091"
8+
9+ echo " Setting up SSL certificates..."
10+ mkdir -p /app/certs/ca
11+
12+ if [ -f " /app/certs/ca/ca.crt" ]; then
13+ echo " SSL certificate found, using verify_certs=true"
14+ else
15+ echo " WARNING: SSL certificate not found, falling back to verify_certs=false"
16+ sed ' s/verify_certs: true/verify_certs: false/' " $ELASTALERT_CONFIG_FILE " > " $ELASTALERT_CONFIG_PROCESSED "
17+ sed -i ' /ca_certs:/d' " $ELASTALERT_CONFIG_PROCESSED "
18+ export ELASTALERT_CONFIG_FILE=" $ELASTALERT_CONFIG_PROCESSED "
19+ fi
20+
21+ echo " Starting in daemon mode..."
22+
23+ if [ ! -f " $ELASTALERT_CONFIG_FILE " ]; then
24+ echo " ERROR: Configuration file not found: $ELASTALERT_CONFIG_FILE "
25+ exit 1
26+ fi
27+
28+ echo " Configuration file found: $ELASTALERT_CONFIG_FILE "
29+ echo " Testing Elasticsearch connectivity..."
30+
31+ python3 -c "
32+ import yaml
33+ import requests
34+ import os
35+ from requests.auth import HTTPBasicAuth
36+ from urllib3 import disable_warnings
37+ from urllib3.exceptions import InsecureRequestWarning
38+
39+ disable_warnings(InsecureRequestWarning)
40+
41+ es_password_env = os.environ.get('ES_PASSWORD')
42+
43+ # Get config file path from shell argument
44+ config_file = '$ELASTALERT_CONFIG_FILE '
45+
46+ try:
47+ with open(config_file, 'r') as f:
48+ config = yaml.safe_load(f)
49+ except FileNotFoundError:
50+ print(f'ERROR: Config file not found in Python: {config_file}')
51+ exit(1)
52+ except Exception as e:
53+ print(f'ERROR: Failed to load YAML: {e}')
54+ exit(1)
55+
56+ es_host = config.get('es_host')
57+ es_port = config.get('es_port')
58+ es_username = config.get('es_username')
59+
60+ if not all([es_host, es_port, es_username, es_password_env]):
61+ print('ERROR: Missing required configuration (host, port, username, or ES_PASSWORD environment variable).')
62+ exit(1)
63+
64+ url = f'https://{es_host}:{es_port}/_cluster/health'
65+ try:
66+ # Use the password from the environment variable
67+ response = requests.get(url, auth=HTTPBasicAuth(es_username, es_password_env), verify=False, timeout=10)
68+ if response.status_code == 200:
69+ print('SUCCESS: Elasticsearch connection successful')
70+ else:
71+ # Use a print statement that is compatible with older Python versions if needed
72+ print('ERROR: Elasticsearch connection failed. Status code: ' + str(response.status_code))
73+ exit(1)
74+ except Exception as e:
75+ print('ERROR: Elasticsearch connection error: ' + str(e))
76+ exit(1)
77+ "
78+
79+ if [ $? -ne 0 ]; then
80+ echo " ERROR: Elasticsearch connectivity test failed"
81+ exit 1
82+ fi
83+
84+ echo " Creating ElastAlert writeback index if needed..."
85+ elastalert-create-index --config " $ELASTALERT_CONFIG_FILE " --index elastalert_status || true
86+
87+ echo " Starting ElastAlert daemon..."
88+ echo " Prometheus metrics will be available on port $PROMETHEUS_PORT "
89+ echo " ElastAlert UI accessible for monitoring"
90+
91+ exec elastalert --config " $ELASTALERT_CONFIG_FILE " --verbose
0 commit comments