Grafana: 添加数据源而不使用 web gui

创建于 2015-04-15  ·  40评论  ·  资料来源: grafana/grafana

你好,
我只是想知道是否可以“从命令行或配置文件”添加数据源,而不是使用 Web 界面的“添加源”选项...
我想做一个安装过程,自动安装 influxdb + grafana 安装并在 grafana 中自动创建“localhost”influxdb 数据源。 这可行吗?
谢谢

最有用的评论

这个问题可以重开吗?
我非常想要一个类似于dashboards.json的选项来拥有一个可以定义所需数据源的目录

所有40条评论

它是,通过 web api:

curl ' http://localhost :3000/api/datasources' -X PUT --data-binary '{"name":"test","type":"influxdb_08","url":" http://localhost :8086","access":"proxy","isDefault":true,"database":"asd","user":"asd","password":"asd"}'

问题是要使上述工作正常运行,您需要一个 API 密钥,而要创建一个 api 密钥,您需要 UI。 解决方案是能够使用带有用户名/密码的api(例如使用管理员用户的用户名/密码)

谢谢,我会试试这个,看看:-)
主要问题是我找不到任何关于 web api 的文档......也许我错过了一些东西。

这是我从观看 http 请求中得出的一些用于执行此操作的 Python 代码:

import requests

[ provide credential variables etc ]

grafana_url = os.path.join('http://', '%s:%u' % (grafana_host, grafana_port))
session = requests.Session()
login_post = session.post(
   os.path.join(grafana_url, 'login'),
   data=json.dumps({
      'user': grafana_user,
      'email': '',
      'password': grafana_password }),
   headers={'content-type': 'application/json'})

# Get list of datasources
datasources_get = session.get(os.path.join(grafana_url, 'api', 'datasources'))
datasources = datasources_get.json()

# Add new datasource
datasources_put = session.put(
   os.path.join(grafana_url, 'api', 'datasources'),
   data=json.dumps({
      'access': 'direct',
      'database': ifdb_database,
      'name': datasource_name,
      'password': ifdb_password,
      'type': 'influxdb_08',
      'url': 'http://%s:%u' % (ifdb_host, ifdb_port),
      'user': ifdb_user}),
      headers={'content-type': 'application/json'})

谢谢,
由于它将成为安装的一部分,我认为“卷曲”方式将成为刺戳。 但我也会尝试 python 的东西,因为它也可能有用。 再次感谢,我会让你知道:-)
基督教

我将尝试在 2.1 中使这更容易,并制作一个 CLI 工具,使与 Web api 的交互更容易

嗨,再次 torkedo,
你说“问题是上面的工作你需要一个 API 密钥,而要创建一个 api 密钥你需要 UI。”
并且当我尝试发送这样的 curl 命令时,我得到了 {"message":"Access denied"}
你能解释一下“解决方案是能够使用带有用户名/密码的api(例如使用管理员用户的用户名/密码)”的意思吗?
谢谢...

大家好,
PRI-mcallen 的 Python 解决方案对我来说效果很好:-) 我会继续这样做。
我关闭话题。
谢谢!!

@PRI-mcallen 感谢 python 脚本,基于此我想出了以下内容(主要是 Bash 围绕curl和 cookies/cookie-jar),因为我的环境中没有 python 解释器,我在那里需要运行它,也许对其他人有用。

这是在 docker 中运行 grafana 的真正麻烦。 我们使用 docker 编排工具 (maestro-ng)。 我们有一个用于 grafana 的 docker 镜像,我们希望能够自动配置石墨数据源。 编排工具创建了 Graphite 容器,然后是依赖于它的 grafana 容器。 该工具将石墨容器的地址作为环境变量注入到 grafana 容器中。 我们希望在 grafana 容器中运行的启动脚本能够自动配置 Graphite 数据源。 我们曾经能够在 grafana 1.9 中使用 config.js 来做到这一点,但现在只有 HTTP API(没有记录)来做到这一点。

@iangkent ,有关如何使用 HTTP API 的示例,请参阅我上面发布的 python 代码段(通过观察交易进行逆向工程)。

你好

我正在测试@PRI-mcallen 在 2.1.0-pre1 中发布的 python 片段,似乎有些东西坏了(因为在 2.0.2 和 2.0.3-pre1 中它有效)

我从添加数据源得到的响应是

datasources_put.json()
{u'message': u'Not found'}

从 grafana 日志中:

2015/07/13 11:27:49 [I] 完成 /api/datasources 404 Not Found in 1.169711ms

我尝试直接使用 curl 和相同的行为(使用 cookie 身份验证或用户/密码)

curl --user admin:admin \
-X 放 \
-H '内容类型:应用程序/json;字符集=UTF-8' \
--data-binary "{\"name\":\"test4\",\"isDefault\":\"true\",\"type\":\"influxdb_08\",\"url\":\ " http://localhost :8086\",\"access\":\"proxy\",\"database\":\"test4\",\"user\":\"test4\",\"password \":\"test4\"}" \
http://172.16.149.149 :3000/api/datasources”。

{“未找到信息”}

有没有人知道如何排除故障或修复它?

提前致谢
埃弗拉恩

@3fr61n 2.1 中的 HTTP api 有一个小的突破性变化(在变更日志中)

数据源 HTTP api 重大更改,添加数据源现在是 POST /api/datasources/,更新现在是 PUT /api/datasources/:id

谢谢!!! 它现在工作

在 grafana 2.1 中,您现在可以使用基本身份验证对 API 进行身份验证,因此您无需登录并使用 cookie 来创建 api 密钥(或添加数据源)
https://github.com/grafana/grafana/issues/2218#issuecomment -117041541

curl -i -XPOST ' http://admin:admin@hostname :3000/api/datasources' --data-binary '{\"name\":\"influxdb_09x\",\"type\":\"influxdb \",\"access\":\"proxy\",\"url\":\" http://hostname :8086\",\"password\":\"root\",\"user\" :\"root\",\"database\":\"cadvisor\",\"basicAuth\":true,\"basicAuthUser\":\"admin\",\"basicAuthPassword\":\"admin\" ,\"isDefault\":true,\"jsonData\":null}'

我得到 "[{"fieldNames":["Name"],"classification":"RequiredError","message":"Required"},{"fieldNames":["Type"],"classification":" RequiredError","message":"Required"},{"fieldNames":["Access"],"classification":"RequiredError","message":"Required"}]"

您需要标题 ContentType application/json

curl -i -XPOST ' http://admin:admin@hostname :3000/api/datasources' --header "ContentType application/json" --data-binary '{\"name\":\"influxdb2\", \"type\":\"influxdb_09x\",\"access\":\"proxy\",\"url\":\" http://hostname :8086\",\"password\":\" root\",\"user\":\"root\",\"database\":\"cadvisor\",\"basicAuth\":true,\"basicAuthUser\":\"admin\",\" basicAuthPassword\":\"admin\",\"isDefault\":true,\"jsonData\":null}'

仍然返回上面的错误:/

看起来不对,缺少冒号 ,-H 'Content-Type: application/json;charset=UTF-8' \

我正在使用官方的 docker Graphite (hopsoft/graphite-statsd) 和 grafana (grafana/grafana) 图像,以下命令对我有用:

curl 'http://admin:[email protected]:3000/api/datasources' -X POST -H 'Content-Type: application/json;charset=UTF-8' --data-binary '{"name":"localGraphite","type":"graphite","url":"http://192.168.99.100","access":"proxy","isDefault":true,"database":"asd"}'

在 grafana 2.6 和 influxdb 0.10 中,以下 curl 正常工作:

curl 'http://admin:[email protected]:3000/api/datasources' -X POST -H 'Content-Type: application/json;charset=UTF-8' --data-binary '{"name":"influx","type":"influxdb","url":"http://localhost:8086","access":"proxy","isDefault":true,"database":"mydb","user":"admin","password":"admin"}'

您可以使用以下命令检查数据源配置:
curl 'http://admin:[email protected]:3000/api/datasources'

这个问题已在三月份关闭,但有没有办法以更简洁的方式将其自动化以在 Docker 中使用?

这个问题已在三月份关闭,但有没有办法以更简洁的方式将其自动化以在 Docker 中使用?

我们在 Docker 中的run2.sh所做的事情(在run.sh grafana/grafana ):

echo 'Starting Grafana...'
/run.sh "$@" &
AddDataSource() {
  curl 'http://localhost:3000/api/datasources' \
    -X POST \
    -H 'Content-Type: application/json;charset=UTF-8' \
    --data-binary \
    '{"name":"Prometheus","type":"prometheus","url":"http://prometheus:9090","access":"proxy","isDefault":true}'
}
until AddDataSource; do
  echo 'Configuring Grafana...'
  sleep 1
done
echo 'Done!'
wait

但我同意这是一个非常悲伤的设计。 任何其他应用程序都可以在启动之前进行配置。 Grafana 你得先启动再配置。 奇怪的。

@torkelo ,Grafana 工具
当我尝试使用 Grafana API 使用命令在组织中添加用户时,出现错误“找不到用户” -

curl 'http://:@:3000/api/orgs/6/users' -X POST -H 'Content-Type: application/json;charset=UTF-8' --data-binary '{"loginOrEmail": "user1","role":"查看者"}'

我输入了正确的基本身份验证凭据。 这个问题有什么解决方案吗?
谢谢!

用户必须与该 API 调用一起存在,您可以使用邀请 API 创建用户邀请

或者通过ansible:

# Check that a page returns a status 200 and fail if the word AWESOME is not
# in the page contents.
- uri:
    url: http://localhost:3000/api/datasources/name/Prometheus
    user: admin
    password: changeme
    force_basic_auth: yes
  register: grafana_prometheus
  failed_when: false
  changed_when: false

- name: Enable Prometheus Datasource
  uri:
    url: http://localhost:3000/api/datasources
    method: POST
    user: admin
    password: changeme
    body:
      name: "Prometheus"
      type: "prometheus"
      url: "http://prometheus:9090"
      access: "proxy"
      isDefault: true
    force_basic_auth: yes
    status_code: 201
    body_format: json
  when: grafana_prometheus.status == 404

另一种可能的做法是直接写入 Grafana 的数据库。 我通常启动 Grafana 以创建它的数据库,停止它并使用 sqlite 将源添加到数据库中。

对于 Grafana 4.0.2,它将类似于:

cat <<EOF | sqlite3 grafana.db || echo "Failed to add data source."
INSERT INTO data_source VALUES (2,1,0,'prometheus','prometheus','proxy','http://address',NULL,NULL,NULL,0,NULL,NULL,0,'{}','2017-01-15 20:00:00','2017-01-15 20:00:00',0,'{}');
EOF

我通过使用 CLI 来做到这一点。 我为 Grafana 编写的工具 - wizzy https://github.com/utkarshcmu/wizzy

这个问题可以重开吗?
我非常想要一个类似于dashboards.json的选项来拥有一个可以定义所需数据源的目录

这也是它的一大块 saltstack 状态(与上面的 ansible 代码非常相似)。 它非常丑陋,但似乎可以完成这项工作:

  http.query:
    - name: http://localhost:3000/api/datasources
    - method: POST
    - username: admin
    - password: admin
    - data: |
        {"name": "Prometheus",
        "type": "prometheus",
        "url": "http://localhost:9090",
        "access": "proxy",
        "isDefault": true}
    - header_list:
      - 'Content-Type: application/json'
    - status: 200
    - unless:
      - curl -f http://admin:admin<strong i="6">@localhost</strong>:3000/api/datasources/name/Prometheus

请重新打开这个,搞乱 curl 和 sqlite 比 docker 更烦人。 像dashboard.json 一样干净的解决方案会很棒!

+1 我们需要在静态配置中进行配置(例如,当使用 docker-compose 使用 influxdb 数据源自动设置 grafana 时)

+1 使用 json 文件,我们可以轻松地从 puppet 中为每个 ES 索引生成数据源,而不是运行侧脚本

+1

2017 年 5 月 25 日星期四下午 5:36,安东·蒂莫菲耶夫通知@github.com
写道:

+1 使用 json 文件,我们可以轻松地为每个 ES 索引生成一个数据源
直接从 puppet 开始,而不是运行辅助脚本


您收到此消息是因为您订阅了此线程。
直接回复本邮件,在GitHub上查看
https://github.com/grafana/grafana/issues/1789#issuecomment-304026003
或静音线程
https://github.com/notifications/unsubscribe-auth/AFrnv_CDln7rDTlqZK6r_stAFiek7jaHks5r9ZHfgaJpZM4EBJaL
.

👍 能够查询 consul 的数据源(它已经是 json)

我想知道如何通过 HTTP API 或 conf 文件配置弹性搜索详细信息(例如索引名称、时间字段名称和版本)。 我只是找不到这些配置的关键。 默认行为是什么? 它是否像@timtofan所说的那样为每个索引创建数据源?

curl --user admin:admin ' http://IPADDR :3000/api/datasources' -X POST -H 'Content-Type: application/json;charset=UTF-8' --data-binary '{"name" :"test","type":"prometheus","url":" http://localhost :9090","access":"proxy","basicAuth":false}'

"isDefault": 如果你想将数据源设为默认值,则为 true

这是我添加普罗米修斯数据源的方式:
curl --user admin:admin ' http://IPADDR :3000/api/datasources' -X POST -H 'Content-Type: application/json;charset=UTF-8' --data-binary '{"name" :"test","isDefault":true,"type":"prometheus","url":" http://localhost :9090","access":"proxy","basicAuth":false}'

我可以将配置文件中的数据源添加到所有组织吗?
会很方便。

好的,我知道这已经关闭了,但我只是想分享一下 Grafana 5 允许从代码定义仪表板。 更多信息: http :

此页面是否有帮助?
0 / 5 - 0 等级