Merge "[CONTRIB] Add requests/limits to EJBCA"
[oom.git] / kubernetes / sdnc / components / sdnc-prom / resources / bin / sdnc.monitor
1 #!/usr/bin/env python2
2 {{/*
3 # encoding: utf-8
4
5 # Copyright © 2018 Amdocs
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 #       http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 */}}
19
20 import sys
21 import os
22 import json
23 import requests
24 from datetime import datetime
25
26 consul_server = "consul-server:8500"
27 message_router = "message-router:3904"
28 topic = '{{.Values.config.messageRouterTopic}}'
29 log_file='/app/monitor.log'
30 status_file='/app/.health'
31 logEnabled=False
32
33 siteName='sdnc01'
34 if os.environ.get('SDNC_IS_PRIMARY_CLUSTER', 'true') == 'false':
35     siteName='sdnc02'
36
37 debug=False
38 if len(sys.argv) > 1 and sys.argv[1] == '--debug':
39     debug=True
40
41 def get_state(healthcheck):
42     response = requests.get("http://" + consul_server + "/v1/health/checks/" + healthcheck)
43     if response.status_code != 200:
44         raise RuntimeError("HTTP " + str(response.status_code))
45     data = response.json()
46     if len(data) == 0:
47         raise RuntimeError(healthcheck + " not found")
48     if len(data) > 1:
49         raise RuntimeError("Multiple states for " + healthcheck + " found")
50
51     return data[0]
52
53
54 def log(message):
55     if logEnabled:
56         with open(log_file, 'a') as f:
57             f.write(str(datetime.now()) + " " + message + "\n")
58
59 def healthcheck(checks, failFirst=True):
60     if len(checks) == 0:
61         return True
62
63     for check in checks:
64         if type(check) is list:
65             passing = healthcheck(check, False)
66         else:
67             state = get_state(check)
68             status = state['Status']
69             passing = status == "passing" or status == "warning"
70             log(check + " " + status)
71             if debug:
72                 if status == "passing":
73                     color = "\033[32m" # green
74                 elif status == "warning":
75                     color = "\033[33m" # yellow
76                 else:
77                     color = "\033[31m" # red
78                 print check, color + status + "\033[0m"
79                 if not passing:
80                     print "\tCause:", state['Output']
81
82
83         if passing:
84             if not failFirst:
85                 # found a passing check so can stop here
86                 return True
87         else:
88             if failFirst:
89                 # found a failing check so can stop here
90                 return False
91
92     return failFirst
93
94
95 try:
96     with open("/app/config/healthchecks.json") as f:
97         checks = json.load(f)
98
99     try:
100         with open(status_file) as f:
101            previous_result = f.read()
102     except IOError:
103         # file doesn't exist
104         previous_result = 'unknown'
105
106     if healthcheck(checks):
107         result = "healthy"
108     else:
109         result = "unhealthy"
110
111     print result
112
113     # save current result to file
114     with open(status_file, 'w') as f:
115         f.write(result)
116
117     if previous_result != 'unknown' and result != previous_result:
118         payload = { 'type' : 'health-change', 'status': result, 'site': siteName, 'deployment': '{{.Values.config.deployment}}', 'timestamp': str(datetime.now())  }
119         log("Posting event " + str(payload))
120         try:
121             requests.post("http://" + message_router + "/events/" + topic, data=json.dumps(payload), headers={ 'Content-Type' : 'application/json' } )
122         except Exception:
123             # events are best-effort
124             pass
125
126 except Exception as e:
127     sys.exit(str(e))