Grafana SaltStack Formula
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.3 KiB

# Copyright 2016 Mirantis, Inc.
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import sys
import glob
import os
import json
def usage():
h = """
Format and sanitize Grafana dashboards
The program will:
* Sort keys alphabetically.
* Remove sections:
* Override the time entry to {{"from": "now-1h","to": "now"}}
* Enable the sharedCrosshair tooltip
* Increment the version number
WARNING: this script modifies directly the given file(s).
If a DIRECTORY is provided, all files with suffix '.json' are modified.
if len(sys.argv) != 2:
arg = sys.argv[1]
if os.path.isdir(arg):
path = "{}/*.json".format(arg)
elif os.path.isfile(arg):
path = arg
print("'{}' no such file or directory".format(arg))
for f in glob.glob(path):
data = None
absf = os.path.abspath(f)
with open(absf) as out:
data = json.load(out)
for k, v in data.items():
if k == 'annotations':
for anno in v.get('list', []):
anno['datasource'] = 'lma'
if k == 'templating':
variables = v.get('list', [])
for o in variables:
if o['type'] == 'query':
o['options'] = []
o['current'] = {}
o['refresh_on_load'] = True
data['time'] = {'from': 'now-1h', 'to': 'now'}
data['sharedCrosshair'] = True
data['refresh'] = '1m'
data['id'] = None
if data.get('version', None):
data['version'] = data['version'] + 1
data['version'] = 1
with open(absf, 'w') as out:
out.write(json.dumps(data, indent=2, sort_keys=True))