Skip to content

Commit

Permalink
feat(grafana): add ES and loki
Browse files Browse the repository at this point in the history
  • Loading branch information
abolfazl8131 committed Dec 16, 2024
1 parent d7d738f commit 08876b1
Show file tree
Hide file tree
Showing 10 changed files with 180 additions and 18 deletions.
16 changes: 0 additions & 16 deletions app/media/MyGrafana/alertmanager.yml

This file was deleted.

12 changes: 12 additions & 0 deletions app/media/MyGrafana/loki.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: 1
datasources:
- name: string
uid: loki
type: loki
orgId: 1
url: string
access: proxy
jsonData:
timeout: 60
maxLines: 1000
editable: true
4 changes: 3 additions & 1 deletion app/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
from .docker_installation_models import *
from .jenkins import *
from .gitlab_models import *
from .alert_managers_models import *
from app.models.grafana.alert_managers_models import *
from app.models.grafana.elastcsearch_models import *
from app.models.grafana.loki_models import *
Empty file added app/models/grafana/__init__.py
Empty file.
File renamed without changes.
21 changes: 21 additions & 0 deletions app/models/grafana/elastcsearch_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from typing import List, Optional
from pydantic import BaseModel, validator, ValidationError



class ElasticSearchInput(BaseModel):
name:str
url:str
editable: bool = True
index:str = "[filebeat-]YYYY.MM.DD"
interval:str = "Daily"
timeField:str = "@timestamp"
logMessageField:str = "message"
logLevelField:str = "fields.level"







20 changes: 20 additions & 0 deletions app/models/grafana/loki_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from typing import List, Optional
from pydantic import BaseModel, validator, ValidationError

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

class LokiInput(BaseModel):
name:str
uid:str ="loki"
url:str
editable: bool = True
timeout:int = 60
maxLines:int = 1000
basic_auth:Optional[BasicAuth]





25 changes: 24 additions & 1 deletion app/routes/grafana_data_sources.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from app.app_instance import app
from app.models import (AlertManagerInput,Output)
from app.models import (AlertManagerInput,Output,ElasticSearchInput,LokiInput)
from app.template_generators.grafana_data_sources.alertmanager import alert_manager_template
from app.template_generators.grafana_data_sources.elasticsearch import elasticsearch_template
from app.template_generators.grafana_data_sources.loki import loki_template
import shutil
import os

Expand All @@ -15,3 +17,24 @@ async def alertmanager_template(request:AlertManagerInput) -> Output:

return Output(output='output')

@app.post("/api/grafana/elasticsearch")
async def elastic_template(request:ElasticSearchInput) -> Output:

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

elasticsearch_template(request)

return Output(output='output')

@app.post("/api/grafana/loki")
async def elastic_template(request:LokiInput) -> Output:

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

loki_template(request)

return Output(output='output')
36 changes: 36 additions & 0 deletions app/template_generators/grafana_data_sources/elasticsearch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import yaml
import os


def elasticsearch_template(input):

json_template = {
"apiVersion": 1,
"datasources": [
{
"name": input.name,
"type": "elasticsearch",
"url": input.url,
"access": "proxy",

"jsonData": {
"index": input.index,
"interval": input.interval,
"timeField": input.timeField,
"logMessageField": input.logMessageField,
"logLevelField": input.logLevelField,

},
"editable": input.editable,

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

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


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

def loki_template(input):
if input.basic_auth is None:
json_template = {
"apiVersion": 1,
"datasources": [
{
"name": input.name,
"uid": input.uid,
"type": "loki",
"orgId": 1,
"url": input.url,
"access": "proxy",

"jsonData": {
"timeout": input.timeout,
"maxLines": input.maxLines
},
"editable": input.editable,

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

file=open("app/media/MyGrafana/loki.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": "loki",
"url": input.url,
"access": "proxy",
"orgId": 1,

"jsonData": {
"timeout": input.timeout,
"maxLines": input.maxLines
},
"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, 'loki.yml')

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

0 comments on commit 08876b1

Please sign in to comment.