Skip to content

Commit

Permalink
provision grafana default datasource with grafana.py rather than config
Browse files Browse the repository at this point in the history
  • Loading branch information
Vonng committed Mar 21, 2023
1 parent bbcab78 commit 9afa25d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
26 changes: 24 additions & 2 deletions files/grafana/grafana.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@
PORTALS = os.environ.get("INFRA_PORTAL", "")
USE_HTTPS = os.environ.get("USE_HTTPS", "false")

METADB_PASSWORD = 'DBUser.Viewer'
DEFAULT_DATASOURCES = {
'ds-prometheus': {'uid': 'ds-prometheus', 'orgId': 1, 'name': 'Prometheus', 'type': 'prometheus', 'typeName': 'Prometheus', 'typeLogoUrl': 'public/app/plugins/datasource/prometheus/img/prometheus_logo.svg', 'access': 'proxy',
'url': 'http://127.0.0.1:9090', 'user': '', 'database': '', 'basicAuth': False, 'isDefault': True, 'jsonData': {'queryTimeout': '60s', 'timeInterval': '2s', 'tlsAuth': False, 'tlsAuthWithCACert': False}, 'readOnly': False},
'ds-meta': {'uid': 'ds-meta', 'orgId': 1, 'name': 'Meta', 'type': 'postgres', 'typeName': 'PostgreSQL', 'access': 'proxy', 'url': '127.0.0.1:5432', 'user': 'dbuser_view', 'database': 'meta', 'basicAuth': False, 'isDefault': False, 'readOnly': True,
'jsonData': {'connMaxLifetime': 14400, 'maxIdleConns': 10, 'maxOpenConns': 64, 'postgresVersion': 1500, 'sslmode': 'require', 'tlsAuth': False, 'tlsAuthWithCACert': False}, 'secureJsonData': { 'password': METADB_PASSWORD }},
'ds-loki': {'uid': 'ds-loki', 'orgId': 1, 'name': 'Loki', 'type': 'loki', 'typeName': 'Loki', 'access': 'proxy', 'url': 'http://127.0.0.1:3100', 'basicAuth': False, 'isDefault': False, 'jsonData': {}, 'readOnly': False}}


##########################################
# load dashboard
############################w##############
Expand Down Expand Up @@ -142,8 +151,11 @@ def get_datasource_id_by_name(name):
return get('datasources/id/%s' % name).get('id')


def add_datasource(ds):
return put('datasources', ds)
def create_datasource(ds):
return post('datasources', ds)

def update_datasource(uid, ds):
return put('datasources/uid/%s'% uid, ds)


def ds_query(dsID, query):
Expand Down Expand Up @@ -332,13 +344,20 @@ def clean_all():
print("clean: folder %s" % f)
del_folder(f)

def add_default_datasource():
for k, v in DEFAULT_DATASOURCES.items():
print("init: data source %s" % k)
create_datasource(v)
update_datasource(k, v)


def usage():
print("""
grafana.py [init|load|dump|clean]
init [dashboard_dir=.] # provisioning grafana
load [dashboard_dir=.] # load folders & dashboards
dump [dashboard_dir=.] # dump folders & dashboards
ds # init data sources
clean # clean folders & dashboards
""")

Expand Down Expand Up @@ -367,11 +386,14 @@ def usage():
exit(2)

if action == 'init':
add_default_datasource()
init_all(dashboard_dir_path)
elif action == 'load':
load_all(dashboard_dir_path)
elif action == 'dump':
dump_all(dashboard_dir_path)
elif action == 'ds':
add_default_datasource()
else:
usage()
exit(3)
9 changes: 5 additions & 4 deletions roles/infra/tasks/grafana.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
file: path={{ item }} state=absent
with_items:
- /etc/grafana/provisioning/dashboards/pigsty.yml
- /etc/grafana/provisioning/datasources/pigsty.yml
# - /etc/grafana/provisioning/datasources/pigsty.yml

- name: remove grafana data
tags: grafana_rmdata
Expand Down Expand Up @@ -55,13 +55,14 @@
# Use RPM default config
# - name: copy grafana systemd service
# copy: src=grafana.service dest=/usr/lib/systemd/system/grafana-server.service
#
# default datasources are registered in grafana.py since grafana 9.4 has a bug
# - name: render provisioning datasources
# template: src=grafana/datasource.yml.j2 dest=/etc/grafana/provisioning/datasources/pigsty.yml owner=grafana group=grafana

- name: render /etc/grafana/grafana.ini
template: src=grafana/grafana.ini.j2 dest=/etc/grafana/grafana.ini owner=grafana group=grafana

- name: render provisioning datasources
template: src=grafana/datasource.yml.j2 dest=/etc/grafana/provisioning/datasources/pigsty.yml owner=grafana group=grafana

- name: render provisioning dashboards
template: src=grafana/dashboard.yml.j2 dest=/etc/grafana/provisioning/dashboards/pigsty.yml owner=grafana group=grafana

Expand Down

0 comments on commit 9afa25d

Please sign in to comment.