Skip to content
This repository was archived by the owner on Nov 28, 2024. It is now read-only.

Commit e895d67

Browse files
author
Ryan Rupp
committed
Made a change so now the zabbix URL is specified via the system property zabbix.zabbixUrl instead of specifying the host/port because given the full URL is more flexible e.g. if they're using https or the endpoint is not the same (previously assumed /zabbix). The zabbix URL specifies the web servers URL where the the API can be accessed e.g. http://myzabbixhost.com
1 parent c402e87 commit e895d67

File tree

6 files changed

+38
-45
lines changed

6 files changed

+38
-45
lines changed

settings.sh

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ PID_FILE="/tmp/zabbix_java.pid"
3535
# Default:
3636
# START_POLLERS=0
3737

38+
### Option: zabbix.zabbixUrl
39+
# The URL of the zabbix front end server
40+
# which will be used for API calls.
41+
#
42+
# Mandatory: no
43+
# Default:
44+
# ZABBIX_URL=http://localhost/zabbix
45+
3846
### Option: zabbix.apiUser
3947
# The API username to use
4048
#
@@ -47,21 +55,4 @@ PID_FILE="/tmp/zabbix_java.pid"
4755
#
4856
# Mandatory: no
4957
# Default:
50-
# API_PASSWORD=zabbix
51-
52-
### Option: zabbix.apiHost
53-
# The host name or IP address
54-
# of the Zabbix front end which
55-
# the API connection will use.
56-
#
57-
# Mandatory: no
58-
# Default:
59-
# API_HOST=localhost
60-
61-
### Option: zabbix.apiPort
62-
# The Zabbix frontend port for API usage.
63-
#
64-
# Mandatory: no
65-
# Default:
66-
# API_PORT=80
67-
58+
# API_PASSWORD=zabbix

src/com/zabbix/gateway/ConfigurationManager.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,9 @@ class ConfigurationManager
3535
public static final String LISTEN_IP = "listenIP";
3636
public static final String LISTEN_PORT = "listenPort";
3737
public static final String START_POLLERS = "startPollers";
38+
public static final String ZABBIX_URL = "zabbixUrl";
3839
public static final String API_USER = "apiUser";
3940
public static final String API_PASSWORD = "apiPassword";
40-
public static final String API_HOST = "apiHost";
41-
public static final String API_PORT = "apiPort";
4241

4342
private static ConfigurationParameter[] parameters =
4443
{
@@ -81,10 +80,8 @@ public void execute(Object value)
8180
new ConfigurationParameter(API_PASSWORD, ConfigurationParameter.TYPE_STRING, "zabbix",
8281
null,
8382
null),
84-
new ConfigurationParameter(API_HOST, ConfigurationParameter.TYPE_INETADDRESS, getAddressDefault(),
85-
null,
86-
null),
87-
new ConfigurationParameter(API_PORT, ConfigurationParameter.TYPE_INTEGER, 80,
83+
new ConfigurationParameter(ZABBIX_URL, ConfigurationParameter.TYPE_STRING,
84+
"http://localhost/zabbix",
8885
null,
8986
null)
9087
};

src/com/zabbix/gateway/JavaGateway.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,7 @@ public void run()
113113
// Setup the JmxConfigurationManager which will handle retrieving the proper JmxConfiguration
114114
// to support additional properties such as specifying the JMX protocol and endpoint
115115
JmxConfigurationManager jmxManager = new JmxConfigurationManager(
116-
(InetAddress) ConfigurationManager.getParameter(ConfigurationManager.API_HOST).getValue(),
117-
ConfigurationManager.getIntegerParameterValue(ConfigurationManager.API_PORT),
116+
ConfigurationManager.getStringParameterValue(ConfigurationManager.ZABBIX_URL),
118117
ConfigurationManager.getStringParameterValue(ConfigurationManager.API_USER),
119118
apiPassword);
120119

src/com/zabbix/gateway/JmxConfigurationManager.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
package com.zabbix.gateway;
2121

2222
import java.io.IOException;
23-
import java.net.InetAddress;
2423
import java.util.List;
2524
import java.util.concurrent.ConcurrentHashMap;
2625
import java.util.concurrent.ConcurrentMap;
@@ -48,8 +47,8 @@ public class JmxConfigurationManager {
4847
private final ConcurrentMap<String, JmxConfiguration> _jmxConfigs =
4948
new ConcurrentHashMap<String, JmxConfiguration>();
5049

51-
public JmxConfigurationManager(InetAddress apiHost, Integer apiPort, String apiUser, String apiPassword) {
52-
_zabbixApi = new ZabbixApi(apiHost, apiPort,
50+
public JmxConfigurationManager(String zabbixUrl, String apiUser, String apiPassword) {
51+
_zabbixApi = new ZabbixApi(zabbixUrl,
5352
apiUser, apiPassword);
5453
}
5554

src/com/zabbix/gateway/ZabbixApi.java

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import java.io.OutputStreamWriter;
2626
import java.io.StringWriter;
2727
import java.net.HttpURLConnection;
28-
import java.net.InetAddress;
28+
import java.net.MalformedURLException;
2929
import java.net.URL;
3030
import java.util.ArrayList;
3131
import java.util.List;
@@ -76,20 +76,31 @@ public class ZabbixApi {
7676
final static int SOCKET_TIMEOUT = 1000; // 1 second
7777
final static int READ_TIMEOUT = 2000; // 2 seconds
7878

79-
private String _zabbixConn;
80-
private String _authKey;
81-
private int _requestId;
79+
private final URL _zabbixApiUrl;
80+
private final String _authKey;
81+
private final int _requestId;
8282

8383
/**
8484
* Creates a new Zabbix API connection and logs it in.
85-
* @param host The Zabbix API frontend host or IP address
86-
* @param port The Zabbix API frontend port
85+
* @param zabbixUrl The root url of the Zabbix server e.g. http://myzabbixserver
8786
* @param username The Zabbix API username (must be a valid user)
8887
* @param password The Zabbix API password for the user
8988
*/
90-
public ZabbixApi(InetAddress host, Integer port, String username, String password) {
91-
_zabbixConn = host.getHostAddress() + ":" + port;
92-
logger.debug("Initializing Zabbix API with connection: " + _zabbixConn);
89+
public ZabbixApi(String zabbixUrl, String username, String password) {
90+
final StringBuilder urlBuilder = new StringBuilder(zabbixUrl);
91+
if (!zabbixUrl.endsWith("/")) {
92+
urlBuilder.append('/');
93+
}
94+
urlBuilder.append("api_jsonrpc.php");
95+
96+
try {
97+
_zabbixApiUrl = new URL(urlBuilder.toString());
98+
}
99+
catch (MalformedURLException e) {
100+
throw new IllegalArgumentException("An invalid Zabbix URL was specified: " + zabbixUrl);
101+
}
102+
103+
logger.debug("Initializing Zabbix API with connection: " + _zabbixApiUrl);
93104
_requestId = 1;
94105
_authKey = login(username, password, _requestId);
95106
}
@@ -264,9 +275,8 @@ private JSONObject zabbixApiCall(String jrpc) throws IOException, JSONException
264275
BufferedReader rd;
265276
OutputStreamWriter wr;
266277
HttpURLConnection conn;
267-
URL zabbixApiUrl = new URL("http://" + _zabbixConn + "/zabbix/api_jsonrpc.php");
268278

269-
conn = (HttpURLConnection)zabbixApiUrl.openConnection();
279+
conn = (HttpURLConnection)_zabbixApiUrl.openConnection();
270280
conn.setRequestProperty(CONTENT_TYPE, APP_JRPC);
271281
conn.setRequestMethod("POST");
272282
conn.setDoOutput(true);

startup.sh

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,8 @@ fi
2929
if [ -n "$API_PASSWORD" ]; then
3030
ZABBIX_OPTIONS="$ZABBIX_OPTIONS -Dzabbix.apiPassword=$API_PASSWORD"
3131
fi
32-
if [ -n "$API_HOST" ]; then
33-
ZABBIX_OPTIONS="$ZABBIX_OPTIONS -Dzabbix.apiHost=$API_HOST"
34-
fi
35-
if [ -n "$API_PORT" ]; then
36-
ZABBIX_OPTIONS="$ZABBIX_OPTIONS -Dzabbix.apiPort=$API_PORT"
32+
if [ -n "$ZABBIX_URL" ]; then
33+
ZABBIX_OPTIONS="$ZABBIX_OPTIONS -Dzabbix.zabbixUrl=$ZABBIX_URL"
3734
fi
3835
if [ -n "$PID_FILE" ]; then
3936
ZABBIX_OPTIONS="$ZABBIX_OPTIONS -Dzabbix.pidFile=$PID_FILE"

0 commit comments

Comments
 (0)