我正在尝试连接到Zabbix服务器。我可以通过Web浏览器访问在端口10051上运行的Zabbix服务器,但是,我无法使用Java API连接到同一服务器。以下是我用来连接的代码
import io.github.cgi.zabbix.api.DefaultZabbixApi;
import io.github.cgi.zabbix.api.ZabbixApi;
public class TestClass {
ZabbixApi zabbixApi;
public static void main(String[] args) {
TestClass tc = new TestClass();
tc.before();
tc.testLogin();
// tc.testVersion();
}
public void before() {
String url = "http://xxx.xxx.xxx.xxx:10051/zabbix/api_jsonrpc.php";
zabbixApi = new DefaultZabbixApi(url);
zabbixApi.init();
}
public void testLogin() {
String user = "admin";
String password = "zabbix";
boolean login = zabbixApi.login(user, password);
System.out.println("login result:" + login);
}
public void testVersion() {
String version = zabbixApi.apiVersion();
System.err.println(version);
System.out.println("version := " + version);
}
}
Classpath是:
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="lib" path="D:/Software_Development/Jars/zabbix-api-0.0.2.jar"/>
<classpathentry kind="lib" path="D:/Software_Development/Jars/httpclient-4.5.3.jar"/>
<classpathentry kind="lib" path="D:/Software_Development/Jars/jackson-all-1.9.9.jar/jackson-all-1.9.9.jar"/>
<classpathentry kind="lib" path="D:/Software_Development/Jars/httpcore-4.4.8.jar"/>
<classpathentry kind="lib" path="D:/Software_Development/Jars/commons-logging-1.2/commons-logging-1.2.jar"/>
<classpathentry kind="lib" path="D:/Software_Development/Jars/slf4j-api-1.7.25.jar"/>
<classpathentry kind="output" path="bin"/>
运行此代码时出错:
Exception in thread "main" java.lang.RuntimeException: DefaultZabbixApi call exception!
at io.github.cgi.zabbix.api.DefaultZabbixApi.call(DefaultZabbixApi.java:142)
at io.github.cgi.zabbix.api.DefaultZabbixApi.login(DefaultZabbixApi.java:72)
at TestClass.testLogin(TestClass.java:25)
at TestClass.main(TestClass.java:12)
Caused by: org.apache.http.client.ClientProtocolException
at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:839)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56)
at io.github.cgi.zabbix.api.DefaultZabbixApi.call(DefaultZabbixApi.java:138)
... 3 more
Caused by: org.apache.http.ProtocolException: The server failed to respond with a valid HTTP response
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:149)
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:56)
at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:259)
at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:286)
at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:257)
at org.apache.http.impl.conn.ManagedClientConnectionImpl.receiveResponseHeader(ManagedClientConnectionImpl.java:207)
at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:273)
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:125)
at org.apache.http.impl.client.DefaultRequestDirector.tryExecute(DefaultRequestDirector.java:684)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:486)
at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:835)
... 7 more
使用Java API连接到Zabbix服务器需要帮助。
答案 0 :(得分:2)
您正在连接到zabbix-server(端口10051)。 API是zabbix-frontend的一部分,因此请连接到您的Web服务器(端口取决于您的Web服务器配置,通常为80)。
答案 1 :(得分:0)
将以下网址更改为端口80或正在运行zabbix的网址。
String url =“http://xxx.xxx.xxx.xxx:10051/zabbix/api_jsonrpc.php”;