loki_query.py 816 B

12345678910111213141516171819202122232425262728
  1. import requests
  2. import json
  3. def test_loki_connection():
  4. url = "http://192.168.50.108:3100/loki/api/v1/query_range"
  5. headers = {"Content-Type": "application/json"}
  6. # 最小化测试查询
  7. payload = {
  8. "query": "{}",
  9. "limit": 1,
  10. "start": "2023-07-20T00:00:00Z",
  11. "end": "2023-07-20T01:00:00Z"
  12. }
  13. try:
  14. r = requests.post(url, data=json.dumps(payload), headers=headers)
  15. print(f"状态码: {r.status_code}")
  16. print(f"响应: {r.text[:200]}...") # 截断长响应
  17. return r.status_code == 200
  18. except Exception as e:
  19. print(f"连接失败: {e}")
  20. return False
  21. if test_loki_connection():
  22. print("Loki连接测试成功!")
  23. else:
  24. print("请检查:1. Loki服务状态 2. 网络连接 3. 防火墙设置")