调用grafana API创建图表

2018-10-12  本文已影响0人  萌木盖

用于自动化传入数据表名字即可创建热力图表

注意的是:新版本图表列表是panels而不是rows

import requests
import jsonpath
import json
from pprint import pprint

panels_height = 9
panels_width = 12
def get_old_panels():
    headers = {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
        'Authorization': 'Bearer 你自己的key值,去web端创建',
    }
    #  你的仪表盘需提前创建好, 不然这里没有办法查
    response = requests.get('http://172.17.0.212:3000/api/dashboards/uid/你的仪表盘名', headers=headers)
    pprint(response.text)
    jsonobj = json.loads(response.text)
    panels = jsonpath.jsonpath(jsonobj,"$..panels")[0]
    max_id = 1
    title_list = []
    for panel in panels:
        max_id = panel.get("id") if panel.get("id")> max_id else max_id
        title_list.append(panel.get('title'))
    return panels,max_id,title_list
def post_json(db_name):

    panels_new, max_id, title_list = get_old_panels()
    if db_name in title_list:
        print(title_list)
        print(db_name)
        return
    new_panel = '''{
      "aliasColors": {},
      "bars": false,
      "dashLength": 10,
      "dashes": false,
      "datasource": "数据源名",
      "fill": 1,
      "gridPos": {
        "h": 8,
        "w": 8,
        "x": 0,
        "y": 0
      },
      "id": %d,
      "legend": {
        "avg": false,
        "current": false,
        "max": false,
        "min": false,
        "show": true,
        "total": false,
        "values": false
      },
      "lines": true,
      "linewidth": 1,
      "nullPointMode": "null",
      "percentage": false,
      "pointradius": 5,
      "points": false,
      "renderer": "flot",
      "seriesOverrides": [],
      "spaceLength": 10,
      "stack": false,
      "steppedLine": false,
      "targets": [
        {
          "groupBy": [
            {
              "params": [
                "10s"
              ],
              "type": "time"
            },
            {
              "params": [
                "null"
              ],
              "type": "fill"
            }
          ],
          "measurement": %s,
          "orderByTime": "ASC",
          "policy": "default",
          "refId": "A",
          "resultFormat": "time_series",
          "select": [
            [
              {
                "params": [
                  "value"
                ],
                "type": "field"
              },
              {
                "params": [],
                "type": "mean"
              }
            ]
          ],
          "tags": []
        }
      ],
      "thresholds": [],
      "timeFrom": null,
      "timeShift": null,
      "title": %s,
      "tooltip": {
        "shared": true,
        "sort": 0,
        "value_type": "individual"
      },
      "type": "graph",
      "xaxis": {
        "buckets": null,
        "mode": "time",
        "name": null,
        "show": true,
        "values": []
      },
      "yaxes": [
        {
          "format": "short",
          "label": null,
          "logBase": 1,
          "max": null,
          "min": null,
          "show": true
        },
        {
          "format": "short",
          "label": null,
          "logBase": 1,
          "max": null,
          "min": null,
          "show": true
        }
      ],
      "yaxis": {
        "align": false,
        "alignLevel": null
      }
    }'''%(max_id+1, f'"{db_name}"', f'"{db_name}"')

    new_panel_json = json.loads(new_panel)
    panels_new.append(new_panel_json)
    data = '''
    {
      "dashboard": {
        "id": null,# 同下方uid一样,为null是为创建
        "uid": null,
        "title": "你的仪表盘名字",
    
        "timezone": "browser",
        "panels": %s  ,
        "templating": {
        "list": []
      },
        "annotations": {
        "list": []
      },
        "schemaVersion": 16,
        "version": 2
      },
      "folderId": 0,
      "overwrite": true
    }
    ''' % (str(json.dumps(panels_new)))
    pprint(data)


    headers = {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
        'Authorization': 'Bearer 你自己的key值,去web端创建',
    }

    response = requests.post(url="http://172.17.0.212:3000/api/dashboards/db", data=data, headers=headers)
    print(response)
    print(response.text)


if __name__=="__main__":
    post_json("chahao")# 这是数据源的表名
上一篇 下一篇

猜你喜欢

热点阅读