-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathes-cluster-state.py
More file actions
47 lines (37 loc) · 1.21 KB
/
es-cluster-state.py
File metadata and controls
47 lines (37 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Check Cluster State
# ====
# This notebook check the state of ES cluster and if needed creates alarm.
# It is run once per hour from a cron job.
import sys
from alerts import alarms
from elasticsearch import Elasticsearch
# load ES_HOST, ES_USER, ES_PASS from environment
import os
from dotenv import load_dotenv
load_dotenv()
env = {}
for var in ['ES_HOST', 'ES_USER', 'ES_PASS']:
env[var] = os.environ.get(var, None)
if not env[var]:
print('environment variable {} not set!'.format(var))
sys.exit(1)
es = Elasticsearch(
hosts=[{'host': env['ES_HOST'], 'port': 9200, 'scheme': 'https'}],
basic_auth=(env['ES_USER'], env['ES_PASS']),
request_timeout=60)
if es.ping():
print('connected to ES.')
else:
print('no connection to ES.')
sys.exit(1)
report = es.health_report()
print(report)
if report['status'] == 'green':
sys.exit(0)
ALARM = alarms('Analytics', 'Elasticsearch', 'status')
if report['status'] == 'red':
ALARM.addAlarm(
body='Alert on Elastic cluster state [ES in red]', tags=['red'])
if report['status'] == 'yellow':
ALARM.addAlarm(
body='Alert on Elastic cluster state [ES in yellow]', tags=['yellow'])