Skip to content

Commit

Permalink
Added alerts_prod_slack_channel and alerts_nonprod_slack_channel popu… (
Browse files Browse the repository at this point in the history
#48)

* Added alerts_prod_slack_channel and alerts_nonprod_slack_channel population code

* Successfully tested code

* Updated to address Matt's comments

* Update README.md

Removed the variable mention in README.md as the value is hardcoded in script

* Update github_discovery.py

Moved  getting data from alertmanager in main loop
  • Loading branch information
Sandhya1874 authored Dec 3, 2024
1 parent d22edcf commit aa4b442
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 8 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Retrieval of key data from files (if they exist):
- `applicationinsights.json` - for azure app insights cloudRole_name
- `package.json` - for azure app insights cloudRole_name

Retrieval of data from Alertmanager endpoint:

## Requirements
The following secrets are required:
Expand All @@ -40,5 +41,3 @@ The following secrets are required:
- **`SLACK_BOT_TOKEN`** - this uses the [`hmpps-sre-app`](https://api.slack.com/apps/A07BZTDHRNK/general) Slack app
- **`SERVICE_CATALOGUE_API_ENDPOINT`** / **`SERVICE_CATALOGUE_API_KEY`** - Service Catalogue API token
- **`SC_FILTER`** (eg. `&filters[name][$contains]=-`) - Service Catalogue filter - **required for dev**


60 changes: 54 additions & 6 deletions github_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@
)
SC_PRODUCT_ENDPOINT = f'{SC_API_ENDPOINT}/v1/products?populate=environments{SC_PRODUCT_FILTER}{SC_PAGINATION_PAGE_SIZE}{SC_SORT}'
SC_PRODUCT_UPDATE_ENDPOINT = f'{SC_API_ENDPOINT}/v1/products'

ALERTMANAGER_ENDPOINT = 'http://monitoring-alerts-service.cloud-platform-monitoring-alerts:8080/alertmanager/status'
alertmanager_json_data = ''

class HealthHttpRequestHandler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
Expand Down Expand Up @@ -314,9 +315,43 @@ def get_slack_channel_name_by_id(slack_channel_id):
log.debug(f'Slack channel name for {slack_channel_id} is {slack_channel_name}')
return slack_channel_name

def get_alertmanager_data():
try:
response = requests.get(ALERTMANAGER_ENDPOINT, verify=False)
if response.status_code == 200:
alertmanager_data = response.json()
config_data = alertmanager_data['config']
formatted_config_data = config_data["original"].replace('\\n', '\n')
yaml_config_data = yaml.safe_load(formatted_config_data)
json_config_data = json.loads(json.dumps(yaml_config_data))
return json_config_data
else:
log.error(f"Error: {response.status_code}")
except requests.exceptions.SSLError as e:
log.error(f"SSL Error: {e}")
except requests.exceptions.RequestException as e:
log.error(f"Request Error: {e}")
except json.JSONDecodeError as e:
log.error(f"JSON Decode Error: {e}")

def find_channel_by_severity_label(alert_severity_label):
# Find the receiver name for the given severity
receiver_name = ''
for route in alertmanager_json_data['route']['routes']:
if route['match'].get('severity') == alert_severity_label:
receiver_name = route['receiver']
break
# Find the channel for the receiver name
if receiver_name:
for receiver in alertmanager_json_data['receivers']:
if receiver['name'] == receiver_name:
slack_configs = receiver.get('slack_configs', [])
if slack_configs:
return slack_configs[0].get('channel')
else :
return ''

def process_repo(**component):

allow_list_key = 'allowlist'
c_name = component['attributes']['name']
c_id = component['id']
Expand Down Expand Up @@ -619,10 +654,20 @@ def process_repo(**component):
alert_severity_label = None
try:
alert_severity_label = values['generic-prometheus-alerts']['alertSeverity']
alert_severity_label_envs.update({env: {'alert_everity_label': alert_severity_label}})
alert_severity_label_envs.update({env: {'alert_severity_label': alert_severity_label}})
except KeyError:
pass

# Update alerts_prod_slack_channel and alerts_non_prod_slack_channel
if 'preprod' in alert_severity_label_envs:
alert_severity_label = alert_severity_label_envs["preprod"]["alert_severity_label"]
channel = find_channel_by_severity_label(alert_severity_label)
data.update({'alerts_nonprod_slack_channel': channel})
if 'prod' in alert_severity_label_envs:
alert_severity_label = alert_severity_label_envs["prod"]["alert_severity_label"]
channel = find_channel_by_severity_label(alert_severity_label)
data.update({'alerts_prod_slack_channel': channel})

environments = []
if repo.name in bootstrap_projects:
p = bootstrap_projects[repo.name]
Expand Down Expand Up @@ -665,7 +710,7 @@ def process_repo(**component):
e.update({'name': 'dev', 'type': 'dev', 'url': dev_url})

if 'dev' in alert_severity_label_envs:
label = alert_severity_label_envs["dev"]["alert_everity_label"]
label = alert_severity_label_envs["dev"]["alert_severity_label"]
e.update({'alert_severity_label': label})

try:
Expand All @@ -692,7 +737,7 @@ def process_repo(**component):
e.update({'name': 'development', 'type': 'dev', 'url': dev_url})

if 'development' in alert_severity_label_envs:
label = alert_severity_label_envs["developement"]["alert_everity_label"]
label = alert_severity_label_envs["developement"]["alert_severity_label"]
e.update({'alert_severity_label': label})

try:
Expand Down Expand Up @@ -787,7 +832,7 @@ def process_repo(**component):
env_url = False

if env_name in alert_severity_label_envs:
label = alert_severity_label_envs[env_name]["alert_everity_label"]
label = alert_severity_label_envs[env_name]["alert_severity_label"]
e.update({'alert_severity_label': label})

if 'namespace' in c:
Expand Down Expand Up @@ -1078,6 +1123,9 @@ def process_products(data):
httpHealth = threading.Thread(target=startHttpServer, daemon=True)
httpHealth.start()

# Get alertmanager data
alertmanager_json_data = get_alertmanager_data()

# Get projects.json from bootstrap repo for namespaces data
bootstrap_repo = gh.get_repo('ministryofjustice/hmpps-project-bootstrap')
bootstrap_projects_json = get_file_json(bootstrap_repo, 'projects.json')
Expand Down

0 comments on commit aa4b442

Please sign in to comment.