Skip to content

Commit 222946b

Browse files
committed
fix(grafana_terraform): fix grafana alerting terraform
1 parent ff5e61b commit 222946b

File tree

5 files changed

+148
-2
lines changed

5 files changed

+148
-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: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
2+
# Grafana Connection Variables
3+
grafana_connection = {
4+
"url" = "http://localhost:8080",
5+
"auth" = ""
6+
}
7+
8+
9+
# Grafana_Contact_Point Variables
10+
create_contact_point = true
11+
contact_point_name = "My Contact Point"
12+
use_email = true
13+
use_slack = true
14+
email_contact_point = {
15+
16+
message = "{ len .Alerts.Firing } firing."
17+
subject = "{ template "default.title" .}"
18+
single_email = true
19+
disable_resolve_message = false
20+
}
21+
22+
slack_contact_point =
23+
{
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+
# Grafana_Mute_Timing Variables
51+
create_mute_timing = true
52+
mute_timing =
53+
{
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+
{
70+
group_by = ["..."]
71+
group_wait = "45s"
72+
group_interval = "6m"
73+
repeat_interval = "3h"
74+
}
75+
76+
77+
policies = []
78+
/* policies = [
79+
80+
{
81+
matchers = [
82+
{ label = "mylabel", match = "=", value = "myvalue" },
83+
{ label = "alertname", match = "=", value = "CPU Usage" },
84+
{ label = "Name", match = "=~", value = "host.*|host-b.*" }
85+
]
86+
contact_point = "a_contact_point_1"
87+
continue = true
88+
mute_timings = ["mute_timing_1"]
89+
group_by = ["group1_sub"]
90+
},
91+
{
92+
matchers = [
93+
{ label = "sublabel", match = "=", value = "subvalue" }
94+
]
95+
contact_point = "a_contact_point_1"
96+
continue = false
97+
mute_timings = ["mute_timing_2"]
98+
group_by = ["group2_sub"]
99+
}
100+
101+
102+
] */
103+
104+
105+

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+

0 commit comments

Comments
 (0)