如何从容器内部连接zabbix api?

时间:2019-01-17 07:02:31

标签: java docker zabbix

我们正在运行zabbix的3个组件。使用docker-compose文件将Zabbix-webui,zabbix服务器和mysql数据库作为docker容器。 已经创建了另一个已被dockerized并尝试连接到zabbix api的springboot应用程序。所有4张图像都在同一个撰写文件中运行。

我们如何连接到zabbix api并在应用程序中获取api结果?

1 个答案:

答案 0 :(得分:1)

让我们假设这个docker-compose.yml文件:

version: '2'

services:
  zabbix-frontend:
    restart: always
    image: zabbix/zabbix-web-nginx-mysql
    environment:
     - DB_SERVER_HOST=someserver
  [...]

  some-application:
    restart: always
    image: yourapplication:latest

您的some-application容器应连接到http://zabbix-frontend才能使用API​​。

要在应用程序容器(示例中为a5d95c2cc9a2)内测试http的可达性:

# docker exec -it a5d95c2cc9a2 sh

sh-4.2# curl -i -X POST -H 'Content-type:application/json' \
   -d '{"jsonrpc":"2.0","method":"user.login", \
   "params":{ "user":"youruser","password":"somepassword"},"auth":null,"id":0}' \
   http://zabbix-frontend/api_jsonrpc.php

HTTP/1.1 200 OK
Server: nginx
Date: Mon, 21 Jan 2019 19:17:55 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: keep-alive
Keep-Alive: timeout=20
X-Powered-By: PHP/7.1.17
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Content-Type
Access-Control-Allow-Methods: POST
Access-Control-Max-Age: 1000

{"jsonrpc":"2.0","result":"xxxxxxxxxxxxxxxxx","id":0}