Skip to content

Commit 7574958

Browse files
authored
Merge pull request #154 from devopshobbies/grafana/terraform
Adding tfvars for grafana terraform
2 parents fa7552d + 1c2c7f4 commit 7574958

File tree

6 files changed

+316
-2
lines changed

6 files changed

+316
-2
lines changed

app/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@
66
from app.routes.docker import *
77
from app.routes.jenkins import *
88
from app.routes.gitlab import *
9-
from app.routes.grafana_data_sources import *
9+
from app.routes.grafana_data_sources import *
10+
from app.routes.grafana_terraform import *

app/media/terraform.tfvars

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
2+
# Grafana Connection Variables
3+
grafana_connection = {
4+
"url" = "http://localhost:8080",
5+
"auth" = ""
6+
}
7+
8+
9+
10+
# Grafana_Contact_Point Variables
11+
create_contact_point = true
12+
contact_point_name = "My Contact Point"
13+
use_email = false
14+
use_slack = true
15+
email_contact_point = {
16+
17+
message = "{ len .Alerts.Firing } firing."
18+
subject = "{{ template \"default.title\" .}}"
19+
single_email = true
20+
disable_resolve_message = false
21+
}
22+
23+
slack_contact_point = {
24+
url = "https://hooks.slack.com/<YOUR_SLACK_WEBHOOK_URL>"
25+
text = <<EOT
26+
{{ len .Alerts.Firing }} alerts are firing
27+
28+
Alert summaries:
29+
{{ range .Alerts.Firing }}
30+
{{ template "Alert Instance Template" . }}
31+
{{ end }}
32+
EOT
33+
}
34+
35+
36+
# 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
37+
38+
39+
# Grafana_Message_Template Variables
40+
create_message_template = true
41+
message_template_name = "Alert Instance Template"
42+
message_template_content = <<EOT
43+
{{ define "Alert Instance Template" }}
44+
Firing: {{ .Labels.alertname }}
45+
Silence: {{ .SilenceURL }}
46+
{{ end }}
47+
EOT
48+
49+
50+
51+
# Grafana_Mute_Timing Variables
52+
create_mute_timing = true
53+
mute_timing = {
54+
name = "My Mute Timing"
55+
start = "04:56"
56+
end = "04:57"
57+
weekdays = ["monday", "tuesday:thursday"]
58+
days_of_month = ["1:7", "-1"]
59+
months = ["1:3", "december"]
60+
years = []
61+
}
62+
63+
64+
65+
66+
# Grafana_Notification_Policy Variables
67+
create_notification_policy = true
68+
notification_policy_config = {
69+
group_by = ["..."]
70+
group_wait = "45s"
71+
group_interval = "6m"
72+
repeat_interval = "3h"
73+
}
74+
75+
76+
policies = []
77+
/* policies = [
78+
{
79+
matchers = [
80+
{ label = "mylabel", match = "=", value = "myvalue" },
81+
{ label = "alertname", match = "=", value = "CPU Usage" },
82+
{ label = "Name", match = "=~", value = "host.*|host-b.*" }
83+
]
84+
contact_point = "a_contact_point_1"
85+
continue = true
86+
mute_timings = ["mute_timing_1"]
87+
group_by = ["group1_sub"]
88+
},
89+
{
90+
matchers = [
91+
{ label = "sublabel", match = "=", value = "subvalue" }
92+
]
93+
contact_point = "a_contact_point_1"
94+
continue = false
95+
mute_timings = ["mute_timing_2"]
96+
group_by = ["group2_sub"]
97+
}
98+
99+
100+
] */
101+
102+
103+

app/models/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@
1414
from app.models.grafana.mysql_models import *
1515
from app.models.grafana.postgresql_models import *
1616
from app.models.grafana.prometheus_models import *
17-
from app.models.grafana.tempo_models import *
17+
from app.models.grafana.tempo_models import *
18+
from app.models.grafana.terraform_alert import *
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from typing import Optional, List
2+
from pydantic import BaseModel,PrivateAttr,Field
3+
4+
class ContactPoint(BaseModel):
5+
use_email:bool = True
6+
use_slack:bool = True
7+
class GrafanaTerraform(BaseModel):
8+
create_contact_point:Optional[ContactPoint]
9+
create_message_template:bool = True
10+
create_mute_timing:bool = True
11+
create_notification_policy:bool = True
12+
13+

app/routes/grafana_terraform.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from app.app_instance import app
2+
from app.models import (GrafanaTerraform, Output)
3+
from fastapi.responses import FileResponse
4+
from app.template_generators.terraform.tfvars.grafana import grafana_tfvars
5+
import shutil
6+
import os
7+
import zipfile
8+
def zip_folder(folder_path: str, output_zip_path: str):
9+
"""Zip the entire folder."""
10+
with zipfile.ZipFile(output_zip_path, 'w', zipfile.ZIP_DEFLATED) as zip_file:
11+
for root, dirs, files in os.walk(folder_path):
12+
for file in files:
13+
file_path = os.path.join(root, file)
14+
# Add file to the zip file
15+
zip_file.write(file_path, os.path.relpath(file_path, folder_path))
16+
@app.post("/api/grafana/terraform")
17+
async def grafana_terraform_template_route(request:GrafanaTerraform) -> Output:
18+
19+
dir = 'app/media/terraform.tfvars'
20+
21+
file_response = grafana_tfvars(request)
22+
with open(dir,'w')as f:
23+
f.write(file_response)
24+
25+
return FileResponse(dir, media_type='application/zip', filename=f"terraform.tfvars")
26+
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
2+
3+
def grafana_tfvars(input):
4+
5+
grafana_connection = """{
6+
"url" = "http://localhost:8080",
7+
"auth" = ""
8+
}
9+
"""
10+
slack_contact_point = """{
11+
url = "https://hooks.slack.com/<YOUR_SLACK_WEBHOOK_URL>"
12+
text = <<EOT
13+
{{ len .Alerts.Firing }} alerts are firing
14+
15+
Alert summaries:
16+
{{ range .Alerts.Firing }}
17+
{{ template "Alert Instance Template" . }}
18+
{{ end }}
19+
EOT
20+
}
21+
22+
"""
23+
24+
mute_timing = """{
25+
name = "My Mute Timing"
26+
start = "04:56"
27+
end = "04:57"
28+
weekdays = ["monday", "tuesday:thursday"]
29+
days_of_month = ["1:7", "-1"]
30+
months = ["1:3", "december"]
31+
years = []
32+
}
33+
34+
"""
35+
36+
notification_policy_config = """{
37+
group_by = ["..."]
38+
group_wait = "45s"
39+
group_interval = "6m"
40+
repeat_interval = "3h"
41+
}
42+
43+
"""
44+
45+
policies = """{
46+
matchers = [
47+
{ label = "mylabel", match = "=", value = "myvalue" },
48+
{ label = "alertname", match = "=", value = "CPU Usage" },
49+
{ label = "Name", match = "=~", value = "host.*|host-b.*" }
50+
]
51+
contact_point = "a_contact_point_1"
52+
continue = true
53+
mute_timings = ["mute_timing_1"]
54+
group_by = ["group1_sub"]
55+
},
56+
{
57+
matchers = [
58+
{ label = "sublabel", match = "=", value = "subvalue" }
59+
]
60+
contact_point = "a_contact_point_1"
61+
continue = false
62+
mute_timings = ["mute_timing_2"]
63+
group_by = ["group2_sub"]
64+
}
65+
66+
"""
67+
subject = "{{ template \\\"default.title\\\" .}}"
68+
message_template_content = """<<EOT
69+
{{ define "Alert Instance Template" }}
70+
Firing: {{ .Labels.alertname }}
71+
Silence: {{ .SilenceURL }}
72+
{{ end }}
73+
EOT
74+
"""
75+
if input.create_contact_point is None:
76+
tfvars_file = f'''
77+
# Grafana Connection Variables
78+
grafana_connection = {grafana_connection}
79+
80+
81+
# Grafana_Contact_Point Variables
82+
create_contact_point = false
83+
contact_point_name = "My Contact Point"
84+
use_email = false
85+
use_slack = false
86+
email_contact_point = {{
87+
88+
message = "{{ len .Alerts.Firing }} firing."
89+
subject = "{subject}"
90+
single_email = true
91+
disable_resolve_message = false
92+
}}
93+
94+
slack_contact_point = {slack_contact_point}
95+
# Use {{ template "Alert Instance Template" . }} or any other template if you plan \
96+
# to create one. Otherwise, remove it from the text section of Slack in the above example
97+
98+
99+
# Grafana_Message_Template Variables
100+
create_message_template = {str(input.create_message_template).lower()}
101+
message_template_name = "Alert Instance Template"
102+
message_template_content = {message_template_content}
103+
104+
# Grafana_Mute_Timing Variables
105+
create_mute_timing = {str(input.create_mute_timing).lower()}
106+
mute_timing = {mute_timing}
107+
108+
109+
# Grafana_Notification_Policy Variables
110+
create_notification_policy = {str(input.create_notification_policy).lower()}
111+
notification_policy_config = {notification_policy_config}
112+
policies = []
113+
/* policies = [
114+
{policies}
115+
] */
116+
117+
118+
'''
119+
120+
return tfvars_file
121+
122+
else:
123+
124+
125+
tfvars_file = f"""
126+
# Grafana Connection Variables
127+
grafana_connection = {grafana_connection}
128+
129+
130+
# Grafana_Contact_Point Variables
131+
create_contact_point = true
132+
contact_point_name = "My Contact Point"
133+
use_email = {str(input.create_contact_point.use_email).lower()}
134+
use_slack = {str(input.create_contact_point.use_slack).lower()}
135+
email_contact_point = {{
136+
137+
message = "{{ len .Alerts.Firing }} firing."
138+
subject = "{subject}"
139+
single_email = true
140+
disable_resolve_message = false
141+
}}
142+
143+
slack_contact_point = {slack_contact_point}
144+
# Use {{ template "Alert Instance Template" . }} or any other template if you plan \
145+
# to create one. Otherwise, remove it from the text section of Slack in the above example
146+
147+
148+
# Grafana_Message_Template Variables
149+
create_message_template = {str(input.create_message_template).lower()}
150+
message_template_name = "Alert Instance Template"
151+
message_template_content = {message_template_content}
152+
153+
154+
# Grafana_Mute_Timing Variables
155+
create_mute_timing = {str(input.create_mute_timing).lower()}
156+
mute_timing = {mute_timing}
157+
158+
159+
# Grafana_Notification_Policy Variables
160+
create_notification_policy = {str(input.create_notification_policy).lower()}
161+
notification_policy_config = {notification_policy_config}
162+
policies = []
163+
/* policies = [
164+
{policies}
165+
] */
166+
167+
168+
"""
169+
170+
return tfvars_file

0 commit comments

Comments
 (0)