Skip to content

Commit

Permalink
feat(grafana): add grafana alertmanager
Browse files Browse the repository at this point in the history
  • Loading branch information
abolfazl8131 committed Dec 16, 2024
1 parent fb77323 commit d7d738f
Show file tree
Hide file tree
Showing 14 changed files with 283 additions and 2 deletions.
3 changes: 2 additions & 1 deletion app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
from app.routes.jcasc import *
from app.routes.docker import *
from app.routes.jenkins import *
from app.routes.gitlab import *
from app.routes.gitlab import *
from app.routes.grafana_data_sources import *
16 changes: 16 additions & 0 deletions app/media/MyGrafana/alertmanager.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apiVersion: 1
datasources:
- name: string
uid: string
type: alertmanager
url: string
access: proxy
orgId: 1
jsonData:
implementation: cortex
handleGrafanaManagedAlerts: true
editable: true
basicAuth: true
basicAuthUser: string
secureJsonData:
basicAuthPassword: string
21 changes: 21 additions & 0 deletions app/media/grafana_datasources/alertmanager.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
apiVersion: 1

datasources:
- name: Alertmanager
uid: alertmanager
type: alertmanager
url: http://localhost:9093
access: proxy
orgId: 1
jsonData:
# Valid options for implementation include mimir, cortex and prometheus
implementation: [prometheus|cortex|mimir]
# Whether or not Grafana should send alert instances to this Alertmanager
handleGrafanaManagedAlerts: [false|true]

editable: [true|false]
# optionally
basicAuth: true
basicAuthUser: my_user
secureJsonData:
basicAuthPassword: test_password
14 changes: 14 additions & 0 deletions app/media/grafana_datasources/elasticsearch.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: 1

datasources:
- name: elasticsearch-v7-filebeat
type: elasticsearch
access: proxy
url: http://localhost:9200
editable: [true|false]
jsonData:
index: '[filebeat-]YYYY.MM.DD'
interval: Daily
timeField: '@timestamp'
logMessageField: message
logLevelField: fields.level
19 changes: 19 additions & 0 deletions app/media/grafana_datasources/loki.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apiVersion: 1

datasources:
- name: Loki
uid: loki
type: loki
orgId: 1
access: proxy
editable: [true|false]
url: http://localhost:3100
jsonData:
timeout: 60
maxLines: 1000

# optionally
basicAuth: true
basicAuthUser: my_user
secureJsonData:
basicAuthPassword: test_password
16 changes: 16 additions & 0 deletions app/media/grafana_datasources/mimir.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apiVersion: 1

datasources:
- name: Mimir
uid: mimir
type: prometheus
access: proxy
orgId: 1
url: http://mimir-nginx.mimir.svc.cluster.local/prometheus
editable: [true|false]
version: 1
jsonData:
httpHeaderName1: "X-Scope-OrgID"
alertmanagerUid: "alertmanager"
secureJsonData:
httpHeaderValue1: "pods"
20 changes: 20 additions & 0 deletions app/media/grafana_datasources/mysql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
apiVersion: 1

datasources:
- name: MySQL
type: mysql
url: localhost:3306
user: grafana
editable: [true|false]
jsonData:
tlsAuth: true
tlsSkipVerify: true
database: grafana
maxOpenConns: 100 # Grafana v5.4+
maxIdleConns: 100 # Grafana v5.4+
maxIdleConnsAuto: true # Grafana v9.5.1+
connMaxLifetime: 14400 # Grafana v5.4+
secureJsonData:
password: ${GRAFANA_MYSQL_PASSWORD}
tlsClientCert: ${GRAFANA_TLS_CLIENT_CERT}
tlsCACert: ${GRAFANA_TLS_CA_CERT}
19 changes: 19 additions & 0 deletions app/media/grafana_datasources/postgresql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apiVersion: 1

datasources:
- name: Postgres
type: postgres
url: localhost:5432
user: grafana # Database user’s login/username
editable: [true|false]
secureJsonData:
password: 'Password!'
jsonData:
database: grafana
sslmode: 'disable' # disable/require/verify-ca/verify-full
maxOpenConns: 100 # Grafana v5.4+
maxIdleConns: 100 # Grafana v5.4+
maxIdleConnsAuto: true # Grafana v9.5.1+
connMaxLifetime: 14400 # Grafana v5.4+
postgresVersion: 903 # 903=9.3, 904=9.4, 905=9.5, 906=9.6, 1000=10
timescaledb: false
23 changes: 23 additions & 0 deletions app/media/grafana_datasources/prometheus.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
apiVersion: 1

datasources:
- name: Prometheus
uid: prometheus
type: prometheus
access: proxy
# Access mode - proxy (server in the UI) or direct (browser in the UI).
url: http://localhost:9090
editable: [true|false]
jsonData:
httpMethod: POST
manageAlerts: true
prometheusType: Prometheus
prometheusVersion: 2.44.0
cacheLevel: 'High'
disableRecordingRules: false
incrementalQueryOverlapWindow: 10m
exemplarTraceIdDestinations:
# Field with internal link pointing to data source in Grafana.
# datasourceUid value can be anything, but it should be unique across all defined data source uids.
- datasourceUid: my_jaeger_uid
name: traceID
25 changes: 25 additions & 0 deletions app/media/grafana_datasources/tempo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
apiVersion: 1

datasources:
- name: Tempo
type: tempo
access: proxy
orgId: 1
url: http://tempo-query-frontend.tempo.svc.cluster.local:3100
basicAuth: false
version: 1
editable: true
apiVersion: 1
uid: tempo
jsonData:
httpMethod: GET
tracesToLogsV2: # If you are going to link your tracing data with logs, configure <<tracesToLogsV2>>
datasourceUid: 'loki'
spanStartTimeShift: '-2m'
spanEndTimeShift: '2m'
filterByTraceID: true
filterBySpanID: true
serviceMap: # If you are going to add serviceGraph feature to tempo, configure <<serviceMap.datasourceUid>>
datasourceUid: 'Mimir-OtelMetrics-Tenant'
nodeGraph: # If you are going to add nodeGraph feature to tempo, enable <<nodeGraph>>
enabled: true
3 changes: 2 additions & 1 deletion app/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
from .compose_models import *
from .docker_installation_models import *
from .jenkins import *
from .gitlab_models import *
from .gitlab_models import *
from .alert_managers_models import *
28 changes: 28 additions & 0 deletions app/models/alert_managers_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from typing import List, Optional
from pydantic import BaseModel, validator, ValidationError

class BasicAuth(BaseModel):
basicAuthUser:str
basicAuthPassword:str


class AlertManagerInput(BaseModel):
name:str
url:str
uid:str
implementation:str

handleGrafanaManagedAlerts:bool = True
editable: bool = True
basic_auth:Optional[BasicAuth]

@validator("implementation")
def validator_implementation(cls,value):
valid = ['prometheus','cortex','mimir']
if value not in valid:
raise ValueError(f"implementation must be in {valid}")
return value




17 changes: 17 additions & 0 deletions app/routes/grafana_data_sources.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from app.app_instance import app
from app.models import (AlertManagerInput,Output)
from app.template_generators.grafana_data_sources.alertmanager import alert_manager_template
import shutil
import os

@app.post("/api/grafana/alertmanager")
async def alertmanager_template(request:AlertManagerInput) -> Output:

dir = 'app/media/MyGrafana'
if os.path.exists(dir):
shutil.rmtree(dir)

alert_manager_template(request)

return Output(output='output')

61 changes: 61 additions & 0 deletions app/template_generators/grafana_data_sources/alertmanager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import yaml
import os

def alert_manager_template(input):
if input.basic_auth is None:
json_template = {
"apiVersion": 1,
"datasources": [
{
"name": input.name,
"uid": input.uid,
"type": "alertmanager",
"url": input.url,
"access": "proxy",
"orgId": 1,
"jsonData": {
"implementation": input.implementation,
"handleGrafanaManagedAlerts": input.handleGrafanaManagedAlerts
},
"editable": input.editable,

}
]
}
dir = "app/media/MyGrafana"
os.makedirs(dir)
os.path.join(dir, 'alertmanager.yml')

file=open("app/media/MyGrafana/alertmanager.yml","w")
yaml.dump(json_template,file,default_flow_style=False,sort_keys=False)

else:
json_template = {
"apiVersion": 1,
"datasources": [
{
"name": input.name,
"uid": input.uid,
"type": "alertmanager",
"url": input.url,
"access": "proxy",
"orgId": 1,
"jsonData": {
"implementation": input.implementation,
"handleGrafanaManagedAlerts": input.handleGrafanaManagedAlerts
},
"editable": input.editable,
"basicAuth": True,
"basicAuthUser": input.basic_auth.basicAuthUser,
"secureJsonData": {
"basicAuthPassword": input.basic_auth.basicAuthPassword
}
}
]
}
dir = "app/media/MyGrafana"
os.makedirs(dir)
os.path.join(dir, 'alertmanager.yml')

file=open("app/media/MyGrafana/alertmanager.yml","w")
yaml.dump(json_template,file,default_flow_style=False,sort_keys=False)

0 comments on commit d7d738f

Please sign in to comment.