向Grafana添加PostgreSQL数据源时插件健康检查失败
Grafana PostgreSQL数据源健康检查失败及连接错误排查
问题背景
使用Grafana Docker容器与PostgreSQL Docker容器,通过Python脚本调用Grafana HTTP API添加PostgreSQL数据源,已确认PostgreSQL用户为超级用户,但出现插件健康检查失败,执行查询时触发连接错误。
相关代码
datasource = { "name": "postgres", "type": "postgres", "host": f"http://{database.config.host}", "database": database.config.database, "user": database.config.user, "password": database.config.password, "access": "proxy", "port": database.config.port, "sslmode": "disable", } headers = { "Content-Type": "application/json", "Authorization": f"Bearer {self.api_key}", } response = requests.post( self.grafana_url + self.API_DATASOURCES, json=datasource, headers=headers, timeout=2, )
日志信息(中文翻译)
logger=context t=2023-05-24T23:47:11.909546523Z level=warn msg="无法从Cookie中查找会话" error="未找到用户令牌" logger=context userId=0 orgId=0 uname= t=2023-05-24T23:47:11.909856964Z level=warn msg=未授权 error="未找到用户令牌" remote_addr=192.168.16.1 traceID= logger=context userId=0 orgId=0 uname= t=2023-05-24T23:47:11.909908982Z level=info msg="请求完成" method=GET path=/api/live/ws status=401 remote_addr=192.168.16.1 time_ms=0 duration=790.303µs size=40 referer= handler=/api/live/ws logger=context t=2023-05-24T23:47:24.906185083Z level=warn msg="无法从Cookie中查找会话" error="未找到用户令牌" logger=context userId=0 orgId=0 uname= t=2023-05-24T23:47:24.906382401Z level=warn msg=未授权 error="未找到用户令牌" remote_addr=192.168.16.1 traceID= logger=context userId=0 orgId=0 uname= t=2023-05-24T23:47:24.906419291Z level=info msg="请求完成" method=GET path=/api/live/ws status=401 remote_addr=192.168.16.1 time_ms=0 duration=416.706µs size=40 referer= handler=/api/live/ws logger=context t=2023-05-24T23:47:26.901251349Z level=warn msg="无法从Cookie中查找会话" error="未找到用户令牌" logger=context userId=0 orgId=0 uname= t=2023-05-24T23:47:26.901763659Z level=warn msg=未授权 error="未找到用户令牌" remote_addr=192.168.16.1 traceID= logger=context userId=0 orgId=0 uname= t=2023-05-24T23:47:26.901862131Z level=info msg="请求完成" method=GET path=/api/live/ws status=401 remote_addr=192.168.16.1 time_ms=1 duration=1.014872ms size=40 referer= handler=/api/live/ws logger=context t=2023-05-24T23:47:33.749570154Z level=warn msg="无法从Cookie中查找会话" error="未找到用户令牌" logger=context userId=0 orgId=0 uname= t=2023-05-24T23:47:33.749850255Z level=info msg="请求完成" method=GET path=/connections/your-connections/datasources/edit/edec663f-a7cc-4316-9aa1-d672dee7d717 status=302 remote_addr=192.168.16.1 time_ms=0 duration=523.514µs size=29 referer=http://localhost:3000/login handler=/connections/your-connections/datasources/edit/* logger=context t=2023-05-24T23:47:33.751853587Z level=warn msg="无法从Cookie中查找会话" error="未找到用户令牌" logger=context t=2023-05-24T23:47:36.899061961Z level=warn msg="无法从Cookie中查找会话" error="未找到用户令牌" logger=context userId=0 orgId=0 uname= t=2023-05-24T23:47:36.899246767Z level=warn msg=未授权 error="未找到用户令牌" remote_addr=192.168.16.1 traceID= logger=context userId=0 orgId=0 uname= t=2023-05-24T23:47:36.899285702Z level=info msg="请求完成" method=GET path=/api/live/ws status=401 remote_addr=192.168.16.1 time_ms=0 duration=439.616µs size=40 referer= handler=/api/live/ws logger=context t=2023-05-24T23:47:42.741638708Z level=warn msg="无法从Cookie中查找会话" error="未找到用户令牌" logger=http.server t=2023-05-24T23:47:42.75135848Z level=info msg="登录成功" User=admin@localhost logger=context userId=1 orgId=1 uname=newuser t=2023-05-24T23:47:42.894368364Z level=info msg="请求完成" method=GET path=/api/live/ws status=-1 remote_addr=192.168.16.1 time_ms=0 duration=841.72µs size=0 referer= handler=/api/live/ws logger=context userId=1 orgId=1 uname=newuser t=2023-05-24T23:47:42.912548509Z level=info msg="请求完成" method=GET path=/api/datasources/uid/edec663f-a7cc-4316-9aa1-d672dee7d717 status=404 remote_addr=192.168.16.1 time_ms=0 duration=461.639µs size=35 referer=http://localhost:3000/connections/your-connections/datasources/edit/edec663f-a7cc-4316-9aa1-d672dee7d717 handler=/api/datasources/uid/:uid logger=context userId=1 orgId=1 uname=newuser t=2023-05-24T23:47:42.946075661Z level=info msg="请求完成" method=GET path=/api/datasources/edec663f-a7cc-4316-9aa1-d672dee7d717 status=400 remote_addr=192.168.16.1 time_ms=0 duration=341.626µs size=27 referer=http://localhost:3000/connections/your-connections/datasources/edit/edec663f-a7cc-4316-9aa1-d672dee7d717 handler=/api/datasources/:id logger=context userId=1 orgId=1 uname=newuser t=2023-05-24T23:47:56.90833634Z level=info msg="请求完成" method=GET path=/api/live/ws status=-1 remote_addr=192.168.16.1 time_ms=1 duration=1.658ms size=0 referer= handler=/api/live/ws logger=context userId=1 orgId=1 uname=newuser t=2023-05-24T23:48:00.997970403Z level=error msg="插件健康检查失败" error="无法检查插件健康:健康检查失败" remote_addr=192.168.16.1 traceID= logger=context userId=1 orgId=1 uname=newuser t=2023-05-24T23:48:00.998018152Z level=error msg="请求完成" method=GET path=/api/datasources/uid/fb4800be-7c42-40fb-be33-f3e0d87ebaab/health status=500 remote_addr=192.168.16.1 time_ms=0 duration=623.119µs size=53 referer=http://localhost:3000/connections/your-connections/datasources/edit/fb4800be-7c42-40fb-be33-f3e0d87ebaab handler=/api/datasources/uid/:uid/health
排查与解决方案
1. 修正数据源Host配置
PostgreSQL数据源的host字段不需要http://前缀,只需要填写容器名称或IP地址。修改代码:
"host": f"{database.config.host}", # 移除http://前缀
2. 检查Docker容器网络连通性
- 确认Grafana与PostgreSQL容器在同一Docker网络中,若不在需添加到同一网络。
- 进入Grafana容器,执行
ping <postgres容器名称/IP>测试网络可达性。
3. 验证PostgreSQL连接权限
- 修改PostgreSQL的
pg_hba.conf文件,添加允许Grafana容器IP段连接的规则:host 目标数据库名 目标用户名 grafana容器IP/24 scram-sha-256 - 重启PostgreSQL容器使配置生效。
4. 确认端口参数类型
检查port字段是否为整数,若配置中读取的是字符串需转换:
"port": int(database.config.port),
5. 启用调试日志定位细节
设置Grafana容器环境变量GF_LOG_LEVEL=debug,重启后查看日志,获取更具体的连接失败原因(如密码错误、数据库不存在等)。
内容的提问来源于stack exchange,提问作者An old man in the sea.
相关产品推荐
相关产品推荐

