-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #156 from devopshobbies/dev
Generating grafana_alerting tfvars
- Loading branch information
Showing
6 changed files
with
316 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
|
||
# Grafana Connection Variables | ||
grafana_connection = { | ||
"url" = "http://localhost:8080", | ||
"auth" = "" | ||
} | ||
|
||
|
||
|
||
# Grafana_Contact_Point Variables | ||
create_contact_point = true | ||
contact_point_name = "My Contact Point" | ||
use_email = false | ||
use_slack = true | ||
email_contact_point = { | ||
addresses = ["one@company.org", "two@company.org"] | ||
message = "{ len .Alerts.Firing } firing." | ||
subject = "{{ template \"default.title\" .}}" | ||
single_email = true | ||
disable_resolve_message = false | ||
} | ||
|
||
slack_contact_point = { | ||
url = "https://hooks.slack.com/<YOUR_SLACK_WEBHOOK_URL>" | ||
text = <<EOT | ||
{{ len .Alerts.Firing }} alerts are firing | ||
Alert summaries: | ||
{{ range .Alerts.Firing }} | ||
{{ template "Alert Instance Template" . }} | ||
{{ end }} | ||
EOT | ||
} | ||
|
||
|
||
# Use { template "Alert Instance Template" . } or any other template if you plan # to create one. Otherwise, remove it from the text section of Slack in the above example | ||
|
||
|
||
# Grafana_Message_Template Variables | ||
create_message_template = true | ||
message_template_name = "Alert Instance Template" | ||
message_template_content = <<EOT | ||
{{ define "Alert Instance Template" }} | ||
Firing: {{ .Labels.alertname }} | ||
Silence: {{ .SilenceURL }} | ||
{{ end }} | ||
EOT | ||
|
||
|
||
|
||
# Grafana_Mute_Timing Variables | ||
create_mute_timing = true | ||
mute_timing = { | ||
name = "My Mute Timing" | ||
start = "04:56" | ||
end = "04:57" | ||
weekdays = ["monday", "tuesday:thursday"] | ||
days_of_month = ["1:7", "-1"] | ||
months = ["1:3", "december"] | ||
years = [] | ||
} | ||
|
||
|
||
|
||
|
||
# Grafana_Notification_Policy Variables | ||
create_notification_policy = true | ||
notification_policy_config = { | ||
group_by = ["..."] | ||
group_wait = "45s" | ||
group_interval = "6m" | ||
repeat_interval = "3h" | ||
} | ||
|
||
|
||
policies = [] | ||
/* policies = [ | ||
{ | ||
matchers = [ | ||
{ label = "mylabel", match = "=", value = "myvalue" }, | ||
{ label = "alertname", match = "=", value = "CPU Usage" }, | ||
{ label = "Name", match = "=~", value = "host.*|host-b.*" } | ||
] | ||
contact_point = "a_contact_point_1" | ||
continue = true | ||
mute_timings = ["mute_timing_1"] | ||
group_by = ["group1_sub"] | ||
}, | ||
{ | ||
matchers = [ | ||
{ label = "sublabel", match = "=", value = "subvalue" } | ||
] | ||
contact_point = "a_contact_point_1" | ||
continue = false | ||
mute_timings = ["mute_timing_2"] | ||
group_by = ["group2_sub"] | ||
} | ||
] */ | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from typing import Optional, List | ||
from pydantic import BaseModel,PrivateAttr,Field | ||
|
||
class ContactPoint(BaseModel): | ||
use_email:bool = True | ||
use_slack:bool = True | ||
class GrafanaTerraform(BaseModel): | ||
create_contact_point:Optional[ContactPoint] | ||
create_message_template:bool = True | ||
create_mute_timing:bool = True | ||
create_notification_policy:bool = True | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from app.app_instance import app | ||
from app.models import (GrafanaTerraform, Output) | ||
from fastapi.responses import FileResponse | ||
from app.template_generators.terraform.tfvars.grafana import grafana_tfvars | ||
import shutil | ||
import os | ||
import zipfile | ||
def zip_folder(folder_path: str, output_zip_path: str): | ||
"""Zip the entire folder.""" | ||
with zipfile.ZipFile(output_zip_path, 'w', zipfile.ZIP_DEFLATED) as zip_file: | ||
for root, dirs, files in os.walk(folder_path): | ||
for file in files: | ||
file_path = os.path.join(root, file) | ||
# Add file to the zip file | ||
zip_file.write(file_path, os.path.relpath(file_path, folder_path)) | ||
@app.post("/api/grafana/terraform") | ||
async def grafana_terraform_template_route(request:GrafanaTerraform) -> Output: | ||
|
||
dir = 'app/media/terraform.tfvars' | ||
|
||
file_response = grafana_tfvars(request) | ||
with open(dir,'w')as f: | ||
f.write(file_response) | ||
|
||
return FileResponse(dir, media_type='application/zip', filename=f"terraform.tfvars") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
|
||
|
||
def grafana_tfvars(input): | ||
|
||
grafana_connection = """{ | ||
"url" = "http://localhost:8080", | ||
"auth" = "" | ||
} | ||
""" | ||
slack_contact_point = """{ | ||
url = "https://hooks.slack.com/<YOUR_SLACK_WEBHOOK_URL>" | ||
text = <<EOT | ||
{{ len .Alerts.Firing }} alerts are firing | ||
Alert summaries: | ||
{{ range .Alerts.Firing }} | ||
{{ template "Alert Instance Template" . }} | ||
{{ end }} | ||
EOT | ||
} | ||
""" | ||
|
||
mute_timing = """{ | ||
name = "My Mute Timing" | ||
start = "04:56" | ||
end = "04:57" | ||
weekdays = ["monday", "tuesday:thursday"] | ||
days_of_month = ["1:7", "-1"] | ||
months = ["1:3", "december"] | ||
years = [] | ||
} | ||
""" | ||
|
||
notification_policy_config = """{ | ||
group_by = ["..."] | ||
group_wait = "45s" | ||
group_interval = "6m" | ||
repeat_interval = "3h" | ||
} | ||
""" | ||
|
||
policies = """{ | ||
matchers = [ | ||
{ label = "mylabel", match = "=", value = "myvalue" }, | ||
{ label = "alertname", match = "=", value = "CPU Usage" }, | ||
{ label = "Name", match = "=~", value = "host.*|host-b.*" } | ||
] | ||
contact_point = "a_contact_point_1" | ||
continue = true | ||
mute_timings = ["mute_timing_1"] | ||
group_by = ["group1_sub"] | ||
}, | ||
{ | ||
matchers = [ | ||
{ label = "sublabel", match = "=", value = "subvalue" } | ||
] | ||
contact_point = "a_contact_point_1" | ||
continue = false | ||
mute_timings = ["mute_timing_2"] | ||
group_by = ["group2_sub"] | ||
} | ||
""" | ||
subject = "{{ template \\\"default.title\\\" .}}" | ||
message_template_content = """<<EOT | ||
{{ define "Alert Instance Template" }} | ||
Firing: {{ .Labels.alertname }} | ||
Silence: {{ .SilenceURL }} | ||
{{ end }} | ||
EOT | ||
""" | ||
if input.create_contact_point is None: | ||
tfvars_file = f''' | ||
# Grafana Connection Variables | ||
grafana_connection = {grafana_connection} | ||
# Grafana_Contact_Point Variables | ||
create_contact_point = false | ||
contact_point_name = "My Contact Point" | ||
use_email = false | ||
use_slack = false | ||
email_contact_point = {{ | ||
addresses = ["one@company.org", "two@company.org"] | ||
message = "{{ len .Alerts.Firing }} firing." | ||
subject = "{subject}" | ||
single_email = true | ||
disable_resolve_message = false | ||
}} | ||
slack_contact_point = {slack_contact_point} | ||
# Use {{ template "Alert Instance Template" . }} or any other template if you plan \ | ||
# to create one. Otherwise, remove it from the text section of Slack in the above example | ||
# Grafana_Message_Template Variables | ||
create_message_template = {str(input.create_message_template).lower()} | ||
message_template_name = "Alert Instance Template" | ||
message_template_content = {message_template_content} | ||
# Grafana_Mute_Timing Variables | ||
create_mute_timing = {str(input.create_mute_timing).lower()} | ||
mute_timing = {mute_timing} | ||
# Grafana_Notification_Policy Variables | ||
create_notification_policy = {str(input.create_notification_policy).lower()} | ||
notification_policy_config = {notification_policy_config} | ||
policies = [] | ||
/* policies = [ | ||
{policies} | ||
] */ | ||
''' | ||
|
||
return tfvars_file | ||
|
||
else: | ||
|
||
|
||
tfvars_file = f""" | ||
# Grafana Connection Variables | ||
grafana_connection = {grafana_connection} | ||
# Grafana_Contact_Point Variables | ||
create_contact_point = true | ||
contact_point_name = "My Contact Point" | ||
use_email = {str(input.create_contact_point.use_email).lower()} | ||
use_slack = {str(input.create_contact_point.use_slack).lower()} | ||
email_contact_point = {{ | ||
addresses = ["one@company.org", "two@company.org"] | ||
message = "{{ len .Alerts.Firing }} firing." | ||
subject = "{subject}" | ||
single_email = true | ||
disable_resolve_message = false | ||
}} | ||
slack_contact_point = {slack_contact_point} | ||
# Use {{ template "Alert Instance Template" . }} or any other template if you plan \ | ||
# to create one. Otherwise, remove it from the text section of Slack in the above example | ||
# Grafana_Message_Template Variables | ||
create_message_template = {str(input.create_message_template).lower()} | ||
message_template_name = "Alert Instance Template" | ||
message_template_content = {message_template_content} | ||
# Grafana_Mute_Timing Variables | ||
create_mute_timing = {str(input.create_mute_timing).lower()} | ||
mute_timing = {mute_timing} | ||
# Grafana_Notification_Policy Variables | ||
create_notification_policy = {str(input.create_notification_policy).lower()} | ||
notification_policy_config = {notification_policy_config} | ||
policies = [] | ||
/* policies = [ | ||
{policies} | ||
] */ | ||
""" | ||
|
||
return tfvars_file |