Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zabbix agent: set panic on not existed provider optionally #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
build
**/out/
.gradle
.classpath
.project
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public class ZabbixAgent {
* @throws Exception when a problem occurs creating the agent.
*/
public ZabbixAgent() throws Exception {
failOnNotExistedMetricProvider = true;

metricsContainer = new MetricsContainer();

enablePassive = true;
Expand Down Expand Up @@ -307,6 +309,17 @@ public void setPsk(String psk) {
this.psk = psk;
}

/**
* Return the value of property <code>failOnNotExistedMetricProvider</code>.
* @return the current value of the <code>failOnNotExistedMetricProvider</code> property. Defaults to <code>true</code>.
*/
public boolean isFailOnNotExistedMetricProvider() {
return failOnNotExistedMetricProvider;
}
public void setFailOnNotExistedMetricProvider(boolean failOnNotExistedMetricProvider) {
this.failOnNotExistedMetricProvider = failOnNotExistedMetricProvider;
}

/**
* Add a MetricsProvider to the agent.
* @param name bind the provider to this name.
Expand All @@ -325,6 +338,7 @@ public void setProviders(Map<String, MetricsProvider> providers) {
private int listenPort;

private boolean enableActive;
private boolean failOnNotExistedMetricProvider;
private String hostName;
private String serverAddress;
private int serverPort;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,10 @@ private void sendMetrics(int delay, List<String> keyList) throws Exception {
key.put("key", keyName);
try {
Object value = metricsContainer.getMetric(keyName);
if (value == null) {
// skip null values, it means not need report ZBX_NOTSUPPORTED to zabbix
continue;
}
key.put("value", value.toString());

} catch(Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@

public class MetricsContainer {
public MetricsContainer() {
container = new HashMap<String, MetricsProvider>();
this(true);
}

public MetricsContainer(boolean failOnNotExistedMetricProvider) {
this.container = new HashMap<String, MetricsProvider>();
this.failOnNotExistedMetricProvider = failOnNotExistedMetricProvider;
}

/**
Expand Down Expand Up @@ -76,8 +81,10 @@ public void addProviders(Map<String, MetricsProvider> providers) {
public MetricsProvider getProvider(String name) throws MetricsException {
if(container.containsKey(name)) {
return (MetricsProvider) container.get(name);
} else {
} else if (failOnNotExistedMetricProvider) {
throw new MetricsException("No MetricsProvider with name: " + name);
} else {
return null;
}
}

Expand All @@ -90,10 +97,12 @@ public MetricsProvider getProvider(String name) throws MetricsException {
public Object getMetric(String key) throws MetricsException {
MetricsKey mk = new MetricsKey(key);
MetricsProvider provider = getProvider(mk.getProvider());
return provider.getValue(mk);
return provider == null? null : provider.getValue(mk);
}

private Map<String, MetricsProvider> container;

private boolean failOnNotExistedMetricProvider;

private static Logger log = LoggerFactory.getLogger(MetricsProvider.class);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.quigley.zabbixj.metrics;

import org.junit.Test;

public class MetricsContainerTest {

@Test(expected = MetricsException.class)
public void testGetProvider_1() {
MetricsContainer c = new MetricsContainer();
c.getMetric("provider.some_key");
}

@Test
public void testGetProvider_2() {
MetricsContainer c = new MetricsContainer(false);
c.getMetric("provider.some_key");
}

@Test
public void testGetMetric_1() {
MetricsContainer c = new MetricsContainer(false);
c.addProvider("provider", new MetricsProvider() {
@Override
public Object getValue(MetricsKey mKey) throws MetricsException {
return null;
}
});
c.getMetric("provider.some_key");
c.getMetric("other.some_key");
}

@Test(expected = MetricsException.class)
public void testGetMetric_2() {
MetricsContainer c = new MetricsContainer();
c.addProvider("provider", new MetricsProvider() {
@Override
public Object getValue(MetricsKey mKey) throws MetricsException {
throw new MetricsException();
}
});
c.getMetric("provider.some_key");
}
}