Skip to content

Commit 08876b1

Browse files
committed
feat(grafana): add ES and loki
1 parent d7d738f commit 08876b1

10 files changed

Lines changed: 180 additions & 18 deletions

File tree

app/media/MyGrafana/alertmanager.yml

Lines changed: 0 additions & 16 deletions
This file was deleted.

app/media/MyGrafana/loki.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
apiVersion: 1
2+
datasources:
3+
- name: string
4+
uid: loki
5+
type: loki
6+
orgId: 1
7+
url: string
8+
access: proxy
9+
jsonData:
10+
timeout: 60
11+
maxLines: 1000
12+
editable: true

app/models/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@
77
from .docker_installation_models import *
88
from .jenkins import *
99
from .gitlab_models import *
10-
from .alert_managers_models import *
10+
from app.models.grafana.alert_managers_models import *
11+
from app.models.grafana.elastcsearch_models import *
12+
from app.models.grafana.loki_models import *

app/models/grafana/__init__.py

Whitespace-only changes.
File renamed without changes.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from typing import List, Optional
2+
from pydantic import BaseModel, validator, ValidationError
3+
4+
5+
6+
class ElasticSearchInput(BaseModel):
7+
name:str
8+
url:str
9+
editable: bool = True
10+
index:str = "[filebeat-]YYYY.MM.DD"
11+
interval:str = "Daily"
12+
timeField:str = "@timestamp"
13+
logMessageField:str = "message"
14+
logLevelField:str = "fields.level"
15+
16+
17+
18+
19+
20+
21+

app/models/grafana/loki_models.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from typing import List, Optional
2+
from pydantic import BaseModel, validator, ValidationError
3+
4+
class BasicAuth(BaseModel):
5+
basicAuthUser:str
6+
basicAuthPassword:str
7+
8+
class LokiInput(BaseModel):
9+
name:str
10+
uid:str ="loki"
11+
url:str
12+
editable: bool = True
13+
timeout:int = 60
14+
maxLines:int = 1000
15+
basic_auth:Optional[BasicAuth]
16+
17+
18+
19+
20+

app/routes/grafana_data_sources.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from app.app_instance import app
2-
from app.models import (AlertManagerInput,Output)
2+
from app.models import (AlertManagerInput,Output,ElasticSearchInput,LokiInput)
33
from app.template_generators.grafana_data_sources.alertmanager import alert_manager_template
4+
from app.template_generators.grafana_data_sources.elasticsearch import elasticsearch_template
5+
from app.template_generators.grafana_data_sources.loki import loki_template
46
import shutil
57
import os
68

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

1618
return Output(output='output')
1719

20+
@app.post("/api/grafana/elasticsearch")
21+
async def elastic_template(request:ElasticSearchInput) -> Output:
22+
23+
dir = 'app/media/MyGrafana'
24+
if os.path.exists(dir):
25+
shutil.rmtree(dir)
26+
27+
elasticsearch_template(request)
28+
29+
return Output(output='output')
30+
31+
@app.post("/api/grafana/loki")
32+
async def elastic_template(request:LokiInput) -> Output:
33+
34+
dir = 'app/media/MyGrafana'
35+
if os.path.exists(dir):
36+
shutil.rmtree(dir)
37+
38+
loki_template(request)
39+
40+
return Output(output='output')
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import yaml
2+
import os
3+
4+
5+
def elasticsearch_template(input):
6+
7+
json_template = {
8+
"apiVersion": 1,
9+
"datasources": [
10+
{
11+
"name": input.name,
12+
"type": "elasticsearch",
13+
"url": input.url,
14+
"access": "proxy",
15+
16+
"jsonData": {
17+
"index": input.index,
18+
"interval": input.interval,
19+
"timeField": input.timeField,
20+
"logMessageField": input.logMessageField,
21+
"logLevelField": input.logLevelField,
22+
23+
},
24+
"editable": input.editable,
25+
26+
}
27+
]
28+
}
29+
dir = "app/media/MyGrafana"
30+
os.makedirs(dir)
31+
os.path.join(dir, 'elasticsearch.yml')
32+
33+
file=open("app/media/MyGrafana/elasticsearch.yml","w")
34+
yaml.dump(json_template,file,default_flow_style=False,sort_keys=False)
35+
36+
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import yaml
2+
import os
3+
4+
def loki_template(input):
5+
if input.basic_auth is None:
6+
json_template = {
7+
"apiVersion": 1,
8+
"datasources": [
9+
{
10+
"name": input.name,
11+
"uid": input.uid,
12+
"type": "loki",
13+
"orgId": 1,
14+
"url": input.url,
15+
"access": "proxy",
16+
17+
"jsonData": {
18+
"timeout": input.timeout,
19+
"maxLines": input.maxLines
20+
},
21+
"editable": input.editable,
22+
23+
}
24+
]
25+
}
26+
dir = "app/media/MyGrafana"
27+
os.makedirs(dir)
28+
os.path.join(dir, 'loki.yml')
29+
30+
file=open("app/media/MyGrafana/loki.yml","w")
31+
yaml.dump(json_template,file,default_flow_style=False,sort_keys=False)
32+
33+
else:
34+
json_template = {
35+
"apiVersion": 1,
36+
"datasources": [
37+
{
38+
"name": input.name,
39+
"uid": input.uid,
40+
"type": "loki",
41+
"url": input.url,
42+
"access": "proxy",
43+
"orgId": 1,
44+
45+
"jsonData": {
46+
"timeout": input.timeout,
47+
"maxLines": input.maxLines
48+
},
49+
"editable": input.editable,
50+
"basicAuth": True,
51+
"basicAuthUser": input.basic_auth.basicAuthUser,
52+
"secureJsonData": {
53+
"basicAuthPassword": input.basic_auth.basicAuthPassword
54+
}
55+
}
56+
]
57+
}
58+
dir = "app/media/MyGrafana"
59+
os.makedirs(dir)
60+
os.path.join(dir, 'loki.yml')
61+
62+
file=open("app/media/MyGrafana/loki.yml","w")
63+
yaml.dump(json_template,file,default_flow_style=False,sort_keys=False)
64+

0 commit comments

Comments
 (0)