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

Commit 892be01

Browse files
committed
Resolves #1 - Allow the Zabbix API to connect to other frontend ports outside of the default port 80 by allowing new configuration parameters for the apiHost and apiPort set via the settings.sh file.
1 parent 238fd7b commit 892be01

File tree

5 files changed

+58
-5
lines changed

5 files changed

+58
-5
lines changed

settings.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,19 @@ PID_FILE="/tmp/zabbix_java.pid"
4848
# Default:
4949
# API_PASSWORD=zabbix
5050

51+
### Option: zabbix.apiHost
52+
# The host name or IP address
53+
# of the Zabbix front end which
54+
# the API connection will use.
55+
#
56+
# Mandatory: no
57+
# Default:
58+
# API_HOST=localhost
59+
60+
### Option: zabbix.apiPort
61+
# The Zabbix frontend port for API usage.
62+
#
63+
# Mandatory: no
64+
# Default:
65+
# API_PORT=80
66+

src/com/zabbix/gateway/ConfigurationManager.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
import java.io.File;
2323
import java.io.IOException;
24+
import java.net.InetAddress;
25+
import java.net.UnknownHostException;
2426

2527
import org.slf4j.Logger;
2628
import org.slf4j.LoggerFactory;
@@ -35,6 +37,8 @@ class ConfigurationManager
3537
public static final String START_POLLERS = "startPollers";
3638
public static final String API_USER = "apiUser";
3739
public static final String API_PASSWORD = "apiPassword";
40+
public static final String API_HOST = "apiHost";
41+
public static final String API_PORT = "apiPort";
3842

3943
private static ConfigurationParameter[] parameters =
4044
{
@@ -75,6 +79,12 @@ public void execute(Object value)
7579
null,
7680
null),
7781
new ConfigurationParameter(API_PASSWORD, ConfigurationParameter.TYPE_STRING, "zabbix",
82+
null,
83+
null),
84+
new ConfigurationParameter(API_HOST, ConfigurationParameter.TYPE_INETADDRESS, getAddressDefault(),
85+
null,
86+
null),
87+
new ConfigurationParameter(API_PORT, ConfigurationParameter.TYPE_INTEGER, 80,
7888
null,
7989
null)
8090
};
@@ -119,4 +129,13 @@ public static String getPackage()
119129
{
120130
return "com.zabbix.gateway";
121131
}
132+
133+
private static InetAddress getAddressDefault() {
134+
try {
135+
return InetAddress.getLocalHost();
136+
} catch (UnknownHostException e) {
137+
logger.error("An exception occurred when trying to resolve localhost");
138+
throw new RuntimeException(e);
139+
}
140+
}
122141
}

src/com/zabbix/gateway/JmxConfigurationManager.java

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

2222
import java.io.IOException;
23+
import java.net.InetAddress;
2324
import java.util.List;
2425
import java.util.concurrent.ConcurrentHashMap;
2526
import java.util.concurrent.ConcurrentMap;
@@ -47,7 +48,9 @@ public class JmxConfigurationManager {
4748
private static final ZabbixApi _zabbixApi;
4849

4950
static {
50-
_zabbixApi = new ZabbixApi("localhost",
51+
_zabbixApi = new ZabbixApi(
52+
(InetAddress) ConfigurationManager.getParameter(ConfigurationManager.API_HOST).getValue(),
53+
ConfigurationManager.getIntegerParameterValue(ConfigurationManager.API_PORT),
5154
ConfigurationManager.getStringParameterValue(ConfigurationManager.API_USER),
5255
ConfigurationManager.getStringParameterValue(ConfigurationManager.API_PASSWORD));
5356
}

src/com/zabbix/gateway/ZabbixApi.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.io.OutputStreamWriter;
2626
import java.io.StringWriter;
2727
import java.net.HttpURLConnection;
28+
import java.net.InetAddress;
2829
import java.net.URL;
2930
import java.util.ArrayList;
3031
import java.util.List;
@@ -71,12 +72,20 @@ public class ZabbixApi {
7172
final static String MACRO = "macro";
7273
final static String VALUE = "value";
7374

74-
private String _zabbixIp;
75+
private String _zabbixConn;
7576
private String _authKey;
7677
private int _requestId;
7778

78-
public ZabbixApi(String ip, String username, String password) {
79-
_zabbixIp = "localhost";
79+
/**
80+
* Creates a new Zabbix API connection and logs it in.
81+
* @param host The Zabbix API frontend host or IP address
82+
* @param port The Zabbix API frontend port
83+
* @param username The Zabbix API username (must be a valid user)
84+
* @param password The Zabbix API password for the user
85+
*/
86+
public ZabbixApi(InetAddress host, Integer port, String username, String password) {
87+
_zabbixConn = host.getHostAddress() + ":" + port;
88+
logger.debug("Initializing Zabbix API with connection: " + _zabbixConn);
8089
_requestId = 1;
8190
_authKey = login(username, password, _requestId);
8291
}
@@ -244,7 +253,7 @@ private JSONObject zabbixApiCall(String jrpc) throws IOException, JSONException
244253
BufferedReader rd;
245254
OutputStreamWriter wr;
246255
HttpURLConnection conn;
247-
URL zabbixApiUrl = new URL("http://" + _zabbixIp + "/zabbix/api_jsonrpc.php");
256+
URL zabbixApiUrl = new URL("http://" + _zabbixConn + "/zabbix/api_jsonrpc.php");
248257

249258
conn = (HttpURLConnection)zabbixApiUrl.openConnection();
250259
conn.setRequestProperty(CONTENT_TYPE, APP_JRPC);

startup.sh

100644100755
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ 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"
37+
fi
3238
if [ -n "$PID_FILE" ]; then
3339
ZABBIX_OPTIONS="$ZABBIX_OPTIONS -Dzabbix.pidFile=$PID_FILE"
3440
fi

0 commit comments

Comments
 (0)