diff --git a/.gitignore b/.gitignore index 41280ef..a46d60f 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ /MonitoringProbes.iml /.idea/ /zabbix-probes/mesos-zabbix-probe/mesos-zabbix-probe.iml +/zabbix-probes/zabbix-probes-common/zabbix-probes-common.iml diff --git a/zabbix-probes/heapster-zabbix-probe/heapster-zabbix-probe-1.01.deb b/zabbix-probes/heapster-zabbix-probe/heapster-zabbix-probe-1.01.deb new file mode 100644 index 0000000..0d9bbad Binary files /dev/null and b/zabbix-probes/heapster-zabbix-probe/heapster-zabbix-probe-1.01.deb differ diff --git a/zabbix-probes/mesos-zabbix-probe/mesos-zabbix-probe.iml b/zabbix-probes/mesos-zabbix-probe/mesos-zabbix-probe.iml index 2b4f2e1..01b763d 100644 --- a/zabbix-probes/mesos-zabbix-probe/mesos-zabbix-probe.iml +++ b/zabbix-probes/mesos-zabbix-probe/mesos-zabbix-probe.iml @@ -1,6 +1,6 @@ - + @@ -16,9 +16,18 @@ + + - + + + + + + + + diff --git a/zabbix-probes/mesos-zabbix-probe/pom.xml b/zabbix-probes/mesos-zabbix-probe/pom.xml index 91055a6..6d3bc80 100644 --- a/zabbix-probes/mesos-zabbix-probe/pom.xml +++ b/zabbix-probes/mesos-zabbix-probe/pom.xml @@ -7,7 +7,7 @@ org.indigo zabbix-probes-base ../zabbix-probes-base - 0.95 + 1.01 @@ -52,12 +52,6 @@ 0.3.0 - - org.apache.commons - commons-configuration2 - 2.0 - - org.apache.commons commons-lang3 @@ -151,7 +145,15 @@ - + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + \ No newline at end of file diff --git a/zabbix-probes/mesos-zabbix-probe/src/main/java/com/indigo/mesosprobe/ChronosClient.java b/zabbix-probes/mesos-zabbix-probe/src/main/java/com/indigo/mesosprobe/ChronosClient.java index 8c7dad1..00f61bb 100644 --- a/zabbix-probes/mesos-zabbix-probe/src/main/java/com/indigo/mesosprobe/ChronosClient.java +++ b/zabbix-probes/mesos-zabbix-probe/src/main/java/com/indigo/mesosprobe/ChronosClient.java @@ -1,5 +1,7 @@ package com.indigo.mesosprobe; +import com.indigo.zabbix.utils.PropertiesManager; + import it.infn.ba.indigo.chronos.client.Chronos; import it.infn.ba.indigo.chronos.client.model.v1.Container; import it.infn.ba.indigo.chronos.client.model.v1.Job; @@ -20,6 +22,7 @@ public class ChronosClient { /** * Test that chronos is running ok. + * * @return the chronos status. */ diff --git a/zabbix-probes/mesos-zabbix-probe/src/main/java/com/indigo/mesosprobe/MarathonClient.java b/zabbix-probes/mesos-zabbix-probe/src/main/java/com/indigo/mesosprobe/MarathonClient.java index 3db034e..5347b1a 100644 --- a/zabbix-probes/mesos-zabbix-probe/src/main/java/com/indigo/mesosprobe/MarathonClient.java +++ b/zabbix-probes/mesos-zabbix-probe/src/main/java/com/indigo/mesosprobe/MarathonClient.java @@ -1,6 +1,8 @@ package com.indigo.mesosprobe; +import com.indigo.zabbix.utils.PropertiesManager; + import mesosphere.marathon.client.Marathon; import mesosphere.marathon.client.model.v2.App; import mesosphere.marathon.client.model.v2.Container; @@ -13,7 +15,6 @@ import org.apache.commons.logging.LogFactory; - public class MarathonClient { private static final Log logger = LogFactory.getLog(MarathonClient.class); @@ -21,6 +22,7 @@ public class MarathonClient { /** * Test that Marathon installation is working. + * * @return result of the test. */ public boolean testMarathon() { @@ -47,7 +49,7 @@ public boolean testMarathon() { } } catch (MarathonException e) { - logger.error("Error creating app " + APP_NAME,e); + logger.error("Error creating app " + APP_NAME, e); } diff --git a/zabbix-probes/mesos-zabbix-probe/src/main/java/com/indigo/mesosprobe/MesosClientFactory.java b/zabbix-probes/mesos-zabbix-probe/src/main/java/com/indigo/mesosprobe/MesosClientFactory.java new file mode 100644 index 0000000..81b0ea3 --- /dev/null +++ b/zabbix-probes/mesos-zabbix-probe/src/main/java/com/indigo/mesosprobe/MesosClientFactory.java @@ -0,0 +1,15 @@ +package com.indigo.mesosprobe; + +import com.indigo.mesosprobe.mesos.MesosClient; +import com.indigo.zabbix.utils.ProbeClientFactory; + +/** + * Created by jose on 4/10/16. + */ +public class MesosClientFactory extends ProbeClientFactory { + + public static MesosClient getMesosClient(String endpoint) { + return getClient(MesosClient.class, endpoint); + } + +} diff --git a/zabbix-probes/mesos-zabbix-probe/src/main/java/com/indigo/mesosprobe/MesosCollector.java b/zabbix-probes/mesos-zabbix-probe/src/main/java/com/indigo/mesosprobe/MesosCollector.java new file mode 100644 index 0000000..dad807e --- /dev/null +++ b/zabbix-probes/mesos-zabbix-probe/src/main/java/com/indigo/mesosprobe/MesosCollector.java @@ -0,0 +1,80 @@ +package com.indigo.mesosprobe; + +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; + +import com.indigo.mesosprobe.mesos.MesosClient; +import com.indigo.mesosprobe.mesos.beans.MesosMasterInfoBean; +import com.indigo.zabbix.utils.MetricsCollector; +import com.indigo.zabbix.utils.PropertiesManager; +import com.indigo.zabbix.utils.ZabbixMetrics; + +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Created by jose on 4/10/16. + */ +public class MesosCollector implements MetricsCollector { + + private Map readMetrics(JsonObject metrics, List properties) { + Map result = new HashMap<>(); + properties.forEach(property -> { + JsonElement elem = metrics.get(property); + if (elem != null && elem.isJsonPrimitive()) { + result.put(property.replace("/", "."), elem.getAsString()); + } + }); + return result; + } + + private String findLeader() { + String masterEndpoint = PropertiesManager.getProperty(MesosProbeTags.MESOS_MASTER_ENDPOINT); + if (masterEndpoint != null) { + MesosClient client = MesosClientFactory.getMesosClient(masterEndpoint); + MesosMasterInfoBean redirect = client.getInfo(); + String leader = redirect.getLeader(); + if (leader != null) { + String[] leaderInfo = leader.split("@"); + if (leaderInfo.length == 2) { + return leaderInfo[1]; + } + } + } + return null; + } + + @Override + public ZabbixMetrics getMetrics() { + String leader = findLeader(); + if (leader != null) { + + List properties = PropertiesManager.getListProperty( + MesosProbeTags.MESOS_METRIC); + + if (properties != null && !properties.isEmpty()) { + MesosClient mesosClient = MesosClientFactory.getMesosClient("http://" + leader); + + MesosMasterInfoBean leaderInfo = mesosClient.getInfo(); + if (leaderInfo.getHostname() != null) { + + ZabbixMetrics result = new ZabbixMetrics(); + + result.setHostName(leaderInfo.getHostname()); + + JsonObject metrics = mesosClient.getMetrics(); + result.setMetrics(readMetrics(metrics, properties)); + + result.setTimestamp(new Date().getTime()); + + return result; + } + } + } + + return null; + } + +} diff --git a/zabbix-probes/mesos-zabbix-probe/src/main/java/com/indigo/mesosprobe/MesosProbeTags.java b/zabbix-probes/mesos-zabbix-probe/src/main/java/com/indigo/mesosprobe/MesosProbeTags.java index 6b1cf0e..45ba6df 100644 --- a/zabbix-probes/mesos-zabbix-probe/src/main/java/com/indigo/mesosprobe/MesosProbeTags.java +++ b/zabbix-probes/mesos-zabbix-probe/src/main/java/com/indigo/mesosprobe/MesosProbeTags.java @@ -9,8 +9,6 @@ public interface MesosProbeTags { String CHRONOS_ENDPOINT = "chronos.endpoint"; String MARATHON_ENDPOINT = "marathon.endpoint"; - String ZABBIX_WRAPPER_ENDPOINT = "zabbix.wrapper.location"; - String CHRONOS_USERNAME = "chronos.username"; String CHRONOS_PASSWORD = "chronos.password"; @@ -19,4 +17,6 @@ public interface MesosProbeTags { String CONFIG_FILE = "mesosprobe.properties"; String MESOS_METRIC = "mesos.metric"; + + } diff --git a/zabbix-probes/mesos-zabbix-probe/src/main/java/com/indigo/mesosprobe/ProbeClient.java b/zabbix-probes/mesos-zabbix-probe/src/main/java/com/indigo/mesosprobe/ProbeClient.java deleted file mode 100644 index 07407da..0000000 --- a/zabbix-probes/mesos-zabbix-probe/src/main/java/com/indigo/mesosprobe/ProbeClient.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.indigo.mesosprobe; - -import feign.Feign; -import feign.gson.GsonDecoder; -import feign.gson.GsonEncoder; - -/** - * Created by jose on 12/08/16. - */ -public class ProbeClient { - - private static T getClient(Class clientClass, String baseUrl) { - return Feign.builder() - .decoder(new GsonDecoder()) - .encoder(new GsonEncoder()) - .target(clientClass,baseUrl); - } - - public static MesosClient getMesosClient(String endpoint) { - return getClient(MesosClient.class, endpoint); - } - - public static ZabbixClient getZabbixClient(String endpoint) { - return getClient(ZabbixClient.class, endpoint); - } - - public static ChronosClient getChronosClient(String endpoint) { - return getClient(ChronosClient.class, endpoint); - } - - public static MarathonClient getMarathonClient(String endpoint) { - return getClient(MarathonClient.class, endpoint); - } -} diff --git a/zabbix-probes/mesos-zabbix-probe/src/main/java/com/indigo/mesosprobe/ProbeThread.java b/zabbix-probes/mesos-zabbix-probe/src/main/java/com/indigo/mesosprobe/ProbeThread.java new file mode 100644 index 0000000..3318733 --- /dev/null +++ b/zabbix-probes/mesos-zabbix-probe/src/main/java/com/indigo/mesosprobe/ProbeThread.java @@ -0,0 +1,43 @@ +package com.indigo.mesosprobe; + +import com.indigo.zabbix.utils.PropertiesManager; +import com.indigo.zabbix.utils.ZabbixClient; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.io.IOException; + +/** + * Created by jose on 22/09/16. + */ +public class ProbeThread { + + private static final Log logger = LogFactory.getLog(ProbeThread.class); + + private ZabbixClient zabbixClient = new ZabbixClient(); + + private void startMonitoring() { + MesosCollector collector = new MesosCollector(); + zabbixClient.sendMetrics(collector.getMetrics()); + } + + + + /** + * Start monitoring process. + * @param args Arguments will be ignored. + */ + public static void main(String[] args) { + + try { + PropertiesManager.loadProperties(MesosProbeTags.CONFIG_FILE); + ProbeThread thread = new ProbeThread(); + thread.startMonitoring(); + } catch (IOException e) { + logger.error("Error reading configuration file", e); + } + + } + +} diff --git a/zabbix-probes/mesos-zabbix-probe/src/main/java/com/indigo/mesosprobe/ZabbixClient.java b/zabbix-probes/mesos-zabbix-probe/src/main/java/com/indigo/mesosprobe/ZabbixClient.java deleted file mode 100644 index 64c8f8a..0000000 --- a/zabbix-probes/mesos-zabbix-probe/src/main/java/com/indigo/mesosprobe/ZabbixClient.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.indigo.mesosprobe; - -import javax.ws.rs.Consumes; -import javax.ws.rs.GET; -import javax.ws.rs.PUT; -import javax.ws.rs.Path; -import javax.ws.rs.PathParam; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; - -/** - * Created by jose on 16/08/16. - */ -@Path("/monitoring/adapters/zabbix/zones/indigo/types/service/groups/") -public interface ZabbixClient { - - @PUT - @Path("Containers/hosts/{hostName}") - @Consumes(MediaType.APPLICATION_JSON) - @Produces(MediaType.APPLICATION_JSON) - Response registerContainerHost( - @PathParam("hostName") - String hostName, - ZabbixHost host - ); - - - @PUT - @Path("Pods/hosts/{hostName}") - @Consumes(MediaType.APPLICATION_JSON) - @Produces(MediaType.APPLICATION_JSON) - Response registerPodHost( - @PathParam("hostName") - String hostName, - ZabbixHost host - ); - - @GET - @Path("Containers/hosts/{hostName}") - @Produces(MediaType.APPLICATION_JSON) - Response isContainerRegistered( - @PathParam("hostName") - String hostName - ); - - - @GET - @Path("Pods/hosts/{hostName}") - @Produces(MediaType.APPLICATION_JSON) - Response isPodRegistered( - @PathParam("hostName") - String hostName - ); -} diff --git a/zabbix-probes/mesos-zabbix-probe/src/main/java/com/indigo/mesosprobe/MesosClient.java b/zabbix-probes/mesos-zabbix-probe/src/main/java/com/indigo/mesosprobe/mesos/MesosClient.java similarity index 55% rename from zabbix-probes/mesos-zabbix-probe/src/main/java/com/indigo/mesosprobe/MesosClient.java rename to zabbix-probes/mesos-zabbix-probe/src/main/java/com/indigo/mesosprobe/mesos/MesosClient.java index 76d7cf1..a789ecd 100644 --- a/zabbix-probes/mesos-zabbix-probe/src/main/java/com/indigo/mesosprobe/MesosClient.java +++ b/zabbix-probes/mesos-zabbix-probe/src/main/java/com/indigo/mesosprobe/mesos/MesosClient.java @@ -1,7 +1,9 @@ -package com.indigo.mesosprobe; +package com.indigo.mesosprobe.mesos; import com.google.gson.JsonObject; +import com.indigo.mesosprobe.mesos.beans.MesosMasterInfoBean; + import feign.RequestLine; /** @@ -12,4 +14,7 @@ public interface MesosClient { @RequestLine("GET /metrics/snapshot") JsonObject getMetrics(); + @RequestLine("GET /state") + MesosMasterInfoBean getInfo(); + } diff --git a/zabbix-probes/mesos-zabbix-probe/src/main/java/com/indigo/mesosprobe/mesos/beans/MesosMasterInfoBean.java b/zabbix-probes/mesos-zabbix-probe/src/main/java/com/indigo/mesosprobe/mesos/beans/MesosMasterInfoBean.java new file mode 100644 index 0000000..831d9e9 --- /dev/null +++ b/zabbix-probes/mesos-zabbix-probe/src/main/java/com/indigo/mesosprobe/mesos/beans/MesosMasterInfoBean.java @@ -0,0 +1,37 @@ +package com.indigo.mesosprobe.mesos.beans; + +/** + * Created by jose on 22/09/16. + */ +public class MesosMasterInfoBean { + + private String leader; + + private String pid; + + private String hostname; + + public String getLeader() { + return leader; + } + + public void setLeader(String leader) { + this.leader = leader; + } + + public String getPid() { + return pid; + } + + public void setPid(String pid) { + this.pid = pid; + } + + public String getHostname() { + return hostname; + } + + public void setHostname(String hostname) { + this.hostname = hostname; + } +} diff --git a/zabbix-probes/mesos-zabbix-probe/src/main/resources/mesosprobe.properties b/zabbix-probes/mesos-zabbix-probe/src/main/resources/mesosprobe.properties index fe9205e..6cf2b0b 100644 --- a/zabbix-probes/mesos-zabbix-probe/src/main/resources/mesosprobe.properties +++ b/zabbix-probes/mesos-zabbix-probe/src/main/resources/mesosprobe.properties @@ -1,6 +1,4 @@ -mesos.master.endpoint=http://90.147.170.116:5050 -mesos.master.endpoint=http://90.147.170.10:5050 -mesos.master.endpoint=http://90.147.170.122:5050 +mesos.master.endpoint= mesos.metric=master/uptime_secs mesos.metric=master/tasks_lost @@ -11,12 +9,15 @@ mesos.metric=master/dropped_messages mesos.metric=master/frameworks_inactive mesos.metric=master/frameworks_disconnected -chronos.endpoint=http://mesos-m0.cloud.ba.infn.it:4400 +chronos.endpoint= chronos.username= chronos.password= -marathon.endpoint=http://mesos-m0.cloud.ba.infn.it:8080 +marathon.endpoint= marathon.username= marathon.password= -zabbix.wrapper.location=http://90.147.170.168:32640 \ No newline at end of file +zabbix.wrapper.location= +zabbix.ip= +zabbix.category=IaaS +zabbix.group=Mesos \ No newline at end of file diff --git a/zabbix-probes/mesos-zabbix-probe/src/test/java/com/indigo/mesosprobe/MesosClientTest.java b/zabbix-probes/mesos-zabbix-probe/src/test/java/com/indigo/mesosprobe/MesosClientTest.java index 52b80b6..567179e 100644 --- a/zabbix-probes/mesos-zabbix-probe/src/test/java/com/indigo/mesosprobe/MesosClientTest.java +++ b/zabbix-probes/mesos-zabbix-probe/src/test/java/com/indigo/mesosprobe/MesosClientTest.java @@ -1,15 +1,11 @@ package com.indigo.mesosprobe; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; +import com.indigo.zabbix.utils.PropertiesManager; import org.junit.Before; import java.io.IOException; import java.io.InputStreamReader; -import java.util.List; - -import javax.ws.rs.core.Response; /** * Created by jose on 16/08/16. @@ -25,55 +21,5 @@ public void prepare() throws IOException { ); } - //@Test - public void testClient() { - List endpoints = PropertiesManager - .getListProperty(MesosProbeTags.MESOS_MASTER_ENDPOINT); - - List metrics = PropertiesManager - .getListProperty(MesosProbeTags.MESOS_METRIC); - - for (String endpoint : endpoints) { - MesosClient client = ProbeClient.getMesosClient(endpoint); - - JsonObject result = client.getMetrics(); - assert result != null; - assert !result.isJsonNull(); - - for (String metric : metrics) { - JsonElement element = result.get(metric); - if (element.isJsonPrimitive()) { - String value = element.getAsString(); - - assert value != null; - assert !value.isEmpty(); - - } - } - } - - } - - //@Test - public void testChronosClient() { - ChronosClient client = new ChronosClient(); - assert client.testChronos(); - } - - //@Test - public void testMarathonClient() { - MarathonClient client = new MarathonClient(); - assert client.testMarathon(); - } - - //@Test - public void testZabbixClient() { - ZabbixClient client = ProbeClient.getZabbixClient( - PropertiesManager.getProperty(MesosProbeTags.ZABBIX_WRAPPER_ENDPOINT) - ); - - Response response = client.isContainerRegistered("PruHost"); - assert response.getStatus() == 200; - } } diff --git a/zabbix-probes/mesos-zabbix-probe/src/test/resources/mesosprobe.properties b/zabbix-probes/mesos-zabbix-probe/src/test/resources/mesosprobe.properties index fe9205e..a619f80 100644 --- a/zabbix-probes/mesos-zabbix-probe/src/test/resources/mesosprobe.properties +++ b/zabbix-probes/mesos-zabbix-probe/src/test/resources/mesosprobe.properties @@ -1,7 +1,4 @@ -mesos.master.endpoint=http://90.147.170.116:5050 -mesos.master.endpoint=http://90.147.170.10:5050 -mesos.master.endpoint=http://90.147.170.122:5050 - +mesos.master.endpoint= mesos.metric=master/uptime_secs mesos.metric=master/tasks_lost mesos.metric=master/slaves_active @@ -11,12 +8,13 @@ mesos.metric=master/dropped_messages mesos.metric=master/frameworks_inactive mesos.metric=master/frameworks_disconnected -chronos.endpoint=http://mesos-m0.cloud.ba.infn.it:4400 +chronos.endpoint= chronos.username= chronos.password= -marathon.endpoint=http://mesos-m0.cloud.ba.infn.it:8080 +marathon.endpoint= marathon.username= marathon.password= -zabbix.wrapper.location=http://90.147.170.168:32640 \ No newline at end of file +zabbix.wrapper.location= +zabbix.ip= \ No newline at end of file diff --git a/zabbix-probes/occi-zabbix-probe/occi-zabbix-probe-1.01.deb b/zabbix-probes/occi-zabbix-probe/occi-zabbix-probe-1.01.deb new file mode 100644 index 0000000..2cdd5dc Binary files /dev/null and b/zabbix-probes/occi-zabbix-probe/occi-zabbix-probe-1.01.deb differ diff --git a/zabbix-probes/pom.xml b/zabbix-probes/pom.xml index ebd358b..b6aa8ad 100644 --- a/zabbix-probes/pom.xml +++ b/zabbix-probes/pom.xml @@ -26,6 +26,7 @@ + zabbix-probes-common zabbix-probes-base heapster-zabbix-probe occi-zabbix-probe diff --git a/zabbix-probes/zabbix-probes-base/pom.xml b/zabbix-probes/zabbix-probes-base/pom.xml index 4a5ca99..80d793b 100644 --- a/zabbix-probes/zabbix-probes-base/pom.xml +++ b/zabbix-probes/zabbix-probes-base/pom.xml @@ -33,6 +33,12 @@ + + org.indigo + zabbix-probes-common + 1.0-SNAPSHOT + + org.glassfish.jersey.core jersey-client @@ -74,6 +80,8 @@ test + + diff --git a/zabbix-probes/zabbix-probes-common/pom.xml b/zabbix-probes/zabbix-probes-common/pom.xml new file mode 100644 index 0000000..e1741e4 --- /dev/null +++ b/zabbix-probes/zabbix-probes-common/pom.xml @@ -0,0 +1,75 @@ + + 4.0.0 + org.indigo + zabbix-probes-common + Common utilities for zabbix probes + 1.0-SNAPSHOT + + + + + org.apache.commons + commons-configuration2 + 2.0 + + + + com.netflix.feign + feign-gson + 8.18.0 + + + + com.netflix.feign + feign-core + 8.18.0 + + + + io.github.hengyunabc + zabbix-sender + 0.0.3 + + + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + org.apache.maven.plugins + maven-checkstyle-plugin + 2.17 + + + validate + validate + + google_checks.xml + UTF-8 + true + true + false + + + check + + + + + + + + + + \ No newline at end of file diff --git a/zabbix-probes/zabbix-probes-common/src/main/java/com/indigo/zabbix/utils/MetricsCollector.java b/zabbix-probes/zabbix-probes-common/src/main/java/com/indigo/zabbix/utils/MetricsCollector.java new file mode 100644 index 0000000..ed2dcfb --- /dev/null +++ b/zabbix-probes/zabbix-probes-common/src/main/java/com/indigo/zabbix/utils/MetricsCollector.java @@ -0,0 +1,10 @@ +package com.indigo.zabbix.utils; + +/** + * Created by jose on 4/10/16. + */ +public interface MetricsCollector { + + ZabbixMetrics getMetrics(); + +} diff --git a/zabbix-probes/zabbix-probes-common/src/main/java/com/indigo/zabbix/utils/ProbeClientFactory.java b/zabbix-probes/zabbix-probes-common/src/main/java/com/indigo/zabbix/utils/ProbeClientFactory.java new file mode 100644 index 0000000..986dab0 --- /dev/null +++ b/zabbix-probes/zabbix-probes-common/src/main/java/com/indigo/zabbix/utils/ProbeClientFactory.java @@ -0,0 +1,22 @@ +package com.indigo.zabbix.utils; + +import feign.Feign; +import feign.gson.GsonDecoder; +import feign.gson.GsonEncoder; + +/** + * Created by jose on 12/08/16. + */ +public class ProbeClientFactory { + + protected static T getClient(Class clientClass, String baseUrl) { + return Feign.builder() + .decoder(new GsonDecoder()) + .encoder(new GsonEncoder()) + .target(clientClass, baseUrl); + } + + public static ZabbixWrapperClient getZabbixWrapperClient(String endpoint) { + return getClient(ZabbixWrapperClient.class, endpoint); + } +} diff --git a/zabbix-probes/zabbix-probes-common/src/main/java/com/indigo/zabbix/utils/ProbesTags.java b/zabbix-probes/zabbix-probes-common/src/main/java/com/indigo/zabbix/utils/ProbesTags.java new file mode 100644 index 0000000..f7dcafe --- /dev/null +++ b/zabbix-probes/zabbix-probes-common/src/main/java/com/indigo/zabbix/utils/ProbesTags.java @@ -0,0 +1,14 @@ +package com.indigo.zabbix.utils; + +/** + * Created by jose on 4/10/16. + */ +public interface ProbesTags { + + String ZABBIX_WRAPPER_ENDPOINT = "zabbix.wrapper.location"; + String ZABBIX_HOST = "zabbix.ip"; + String ZABBIX_PORT = "zabbix.port"; + String ZABBIX_CATEGORY = "zabbix.category"; + String ZABBIX_GROUP = "zabbix.group"; + +} diff --git a/zabbix-probes/mesos-zabbix-probe/src/main/java/com/indigo/mesosprobe/PropertiesManager.java b/zabbix-probes/zabbix-probes-common/src/main/java/com/indigo/zabbix/utils/PropertiesManager.java similarity index 57% rename from zabbix-probes/mesos-zabbix-probe/src/main/java/com/indigo/mesosprobe/PropertiesManager.java rename to zabbix-probes/zabbix-probes-common/src/main/java/com/indigo/zabbix/utils/PropertiesManager.java index ca5086e..6b5b467 100644 --- a/zabbix-probes/mesos-zabbix-probe/src/main/java/com/indigo/mesosprobe/PropertiesManager.java +++ b/zabbix-probes/zabbix-probes-common/src/main/java/com/indigo/zabbix/utils/PropertiesManager.java @@ -1,4 +1,4 @@ -package com.indigo.mesosprobe; +package com.indigo.zabbix.utils; import org.apache.commons.configuration2.PropertiesConfiguration; import org.apache.commons.configuration2.ex.ConfigurationException; @@ -18,9 +18,10 @@ public class PropertiesManager { /** * Loads the configuration file from the default location. + * * @throws IOException Exception in case of fail. */ - public static void loadProperties() throws IOException { + public static void loadProperties(String configFileName) throws IOException { String location = ""; String opSystem = System.getProperty("os.name").toLowerCase(); @@ -30,7 +31,7 @@ public static void loadProperties() throws IOException { location = "/etc/zabbix/"; } - File configFile = new File(location + "/" + MesosProbeTags.CONFIG_FILE); + File configFile = new File(location + "/" + configFileName); loadProperties(new FileReader(configFile)); @@ -38,6 +39,7 @@ public static void loadProperties() throws IOException { /** * Load a configuration file into properties. + * * @param confFile Configuration file reader. * @throws IOException Exception if some I/O error occurs. */ @@ -45,16 +47,41 @@ public static void loadProperties(Reader confFile) throws IOException { try { props.read(confFile); } catch (ConfigurationException e) { - throw new IOException("Error reading configuration file",e); + throw new IOException("Error reading configuration file", e); } } + /** + * Get an individual property value. + * @param property The property name. + * @return The property value. + */ public static String getProperty(String property) { return props.getString(property); } + /** + * Get a property value providing a default in case it's not found. + * @param property The property name. + * @param defaultValue The property default value. + * @return The property value. + */ + public static String getProperty(String property, String defaultValue) { + String value = getProperty(property); + if (value != null) { + return value; + } else { + return defaultValue; + } + } + + /** + * Get a property list value. + * @param property The property name. + * @return The property value. + */ public static List getListProperty(String property) { - return props.getList(String .class, property); + return props.getList(String.class, property); } } diff --git a/zabbix-probes/zabbix-probes-common/src/main/java/com/indigo/zabbix/utils/ZabbixClient.java b/zabbix-probes/zabbix-probes-common/src/main/java/com/indigo/zabbix/utils/ZabbixClient.java new file mode 100644 index 0000000..52c70c0 --- /dev/null +++ b/zabbix-probes/zabbix-probes-common/src/main/java/com/indigo/zabbix/utils/ZabbixClient.java @@ -0,0 +1,118 @@ +package com.indigo.zabbix.utils; + +import feign.Response; + +import io.github.hengyunabc.zabbix.sender.DataObject; +import io.github.hengyunabc.zabbix.sender.SenderResult; +import io.github.hengyunabc.zabbix.sender.ZabbixSender; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.io.IOException; +import java.util.List; +import java.util.stream.Collectors; + +/** + * Created by jose on 22/09/16. + */ +public class ZabbixClient { + + public static final Integer ZABBIX_DEFAULT_PORT = 10051; + + private String zabbixCategory; + private String zabbixGroup; + + private static final Log logger = LogFactory.getLog(ZabbixClient.class); + + private ZabbixWrapperClient wrapperClient; + private ZabbixSender sender; + + /** + * Default constructor that will read the information from the configuration properties. + */ + public ZabbixClient() { + this(PropertiesManager.getProperty(ProbesTags.ZABBIX_WRAPPER_ENDPOINT), + PropertiesManager.getProperty(ProbesTags.ZABBIX_CATEGORY), + PropertiesManager.getProperty(ProbesTags.ZABBIX_GROUP), + PropertiesManager.getProperty(ProbesTags.ZABBIX_HOST), + new Integer(PropertiesManager.getProperty(ProbesTags.ZABBIX_PORT, + ZABBIX_DEFAULT_PORT.toString()))); + } + + /** + * Default constructor. + */ + public ZabbixClient(String wrapperEndpoint, String category, String group, + String zabbixHost, Integer zabbixPort) { + + wrapperClient = ProbeClientFactory.getZabbixWrapperClient(wrapperEndpoint); + + Integer port = (zabbixPort != null) ? zabbixPort : ZABBIX_DEFAULT_PORT; + + sender = new ZabbixSender(zabbixHost, port); + + zabbixCategory = category; + zabbixGroup = group; + } + + /** + * Ensure that a host is registered and if not, register it. + * + * @param host The host name. + * @return The registration status. + */ + public boolean ensureRegistration(String host) { + Response hostInfo = wrapperClient.getHostInfo(host, zabbixGroup); + if (hostInfo.status() < 300 && hostInfo.status() >= 200) { + return true; + } else { + Response registrationResult = wrapperClient.registerHost(host, zabbixGroup, + new ZabbixHost(host, zabbixCategory, zabbixGroup)); + if (registrationResult.status() < 300 && registrationResult.status() >= 200) { + return true; + } else { + return false; + } + } + } + + /** + * Sends a series of metrics associated to a host. + * + * @param metrics The metrics to send. + */ + public void sendMetrics(ZabbixMetrics metrics) { + if (ensureRegistration(metrics.getHostName())) { + long timeSecs = metrics.getTimestamp() / 1000; + String zabbixHost = PropertiesManager.getProperty(ProbesTags.ZABBIX_HOST); + if (zabbixHost != null) { + + List toSend = metrics.getMetrics().entrySet().stream().map(entry -> { + + DataObject dataObject = new DataObject(); + dataObject.setHost(metrics.getHostName()); + dataObject.setKey(entry.getKey()); + dataObject.setValue(entry.getValue()); + dataObject.setClock(timeSecs); + + return dataObject; + }) + .collect(Collectors.toList()); + + try { + SenderResult sendResult = sender.send(toSend); + if (!sendResult.success()) { + logger.error("Error sending values: " + + "\nTotal: " + sendResult.getTotal() + + "\nProcessed: " + sendResult.getProcessed() + + "\nFailed: " + sendResult.getFailed()); + } + } catch (IOException e) { + logger.error("Error sending values", e); + } + } + } + } + +} diff --git a/zabbix-probes/mesos-zabbix-probe/src/main/java/com/indigo/mesosprobe/ZabbixHost.java b/zabbix-probes/zabbix-probes-common/src/main/java/com/indigo/zabbix/utils/ZabbixHost.java similarity index 55% rename from zabbix-probes/mesos-zabbix-probe/src/main/java/com/indigo/mesosprobe/ZabbixHost.java rename to zabbix-probes/zabbix-probes-common/src/main/java/com/indigo/zabbix/utils/ZabbixHost.java index cfb2d89..63d3e81 100644 --- a/zabbix-probes/mesos-zabbix-probe/src/main/java/com/indigo/mesosprobe/ZabbixHost.java +++ b/zabbix-probes/zabbix-probes-common/src/main/java/com/indigo/zabbix/utils/ZabbixHost.java @@ -1,24 +1,25 @@ /** -Copyright 2016 ATOS SPAIN S.A. - -Licensed under the Apache License, Version 2.0 (the License); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - -http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. - -Authors Contact: -Francisco Javier Nieto. Atos Research and Innovation, Atos SPAIN SA -@email francisco.nieto@atos.net -**/ + * Copyright 2016 ATOS SPAIN S.A. + * + *

Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at

+ * + *

http://www.apache.org/licenses/LICENSE-2.0

+ * + *

Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License.

+ * + *

Authors Contact: + * Francisco Javier Nieto. Atos Research and Innovation, Atos SPAIN SA

+ * + * @email francisco.nieto@atos.net + **/ -package com.indigo.mesosprobe; +package com.indigo.zabbix.utils; import java.io.Serializable; @@ -28,7 +29,7 @@ * */ public class ZabbixHost implements Serializable { - + private static final long serialVersionUID = 1L; private String ip; private String uuid; @@ -36,20 +37,30 @@ public class ZabbixHost implements Serializable { private String serviceId; private String[] atomicServices; private boolean activeMode; - + + /** + * Default constructor that will read the information from the configuration properties. + * @param hostName The hostname to use. + */ + public ZabbixHost(String hostName) { + this(hostName, + PropertiesManager.getProperty(ProbesTags.ZABBIX_CATEGORY), + PropertiesManager.getProperty(ProbesTags.ZABBIX_GROUP)); + } + /** * This is the constructor of the class, setting by default most of the properties. * @param hostName Identifier of the host */ - public ZabbixHost(String hostName) { - ip = "127.0.0.1"; + public ZabbixHost(String hostName, String category, String group) { + ip = "127.0.0.1"; uuid = hostName; - serviceCategory = "PaaS"; + serviceCategory = category; serviceId = "serviceId"; - atomicServices = new String[]{"Heapster"}; + atomicServices = new String[]{group}; activeMode = false; } - + /** * It retrieves the IP. * @return Default IP: 127.0.0.1 @@ -57,7 +68,7 @@ public ZabbixHost(String hostName) { public String getIp() { return ip; } - + /** * It retrieves the Uuid. * @return It corresponds to the Host Name @@ -65,7 +76,7 @@ public String getIp() { public String getUuid() { return uuid; } - + /** * It retrieves the service category. * @return The default value is PaaS @@ -73,7 +84,7 @@ public String getUuid() { public String getServiceCategory() { return serviceCategory; } - + /** * It retrieves the service Identifier. * @return It is serviceId by default. @@ -81,7 +92,7 @@ public String getServiceCategory() { public String getServiceId() { return serviceId; } - + /** * It retrieves the list of atomic services. * @return Set to Heapster by default. @@ -89,7 +100,7 @@ public String getServiceId() { public String[] getAtomicServices() { return atomicServices; } - + /** * It retrieves the mode. * @return Set to false by default. diff --git a/zabbix-probes/zabbix-probes-common/src/main/java/com/indigo/zabbix/utils/ZabbixMetrics.java b/zabbix-probes/zabbix-probes-common/src/main/java/com/indigo/zabbix/utils/ZabbixMetrics.java new file mode 100644 index 0000000..c4913fc --- /dev/null +++ b/zabbix-probes/zabbix-probes-common/src/main/java/com/indigo/zabbix/utils/ZabbixMetrics.java @@ -0,0 +1,39 @@ +package com.indigo.zabbix.utils; + +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +/** + * Created by jose on 4/10/16. + */ +public class ZabbixMetrics { + + private String hostName; + private Map metrics = new HashMap<>(); + private long timestamp = new Date().getTime(); + + public String getHostName() { + return hostName; + } + + public void setHostName(String hostName) { + this.hostName = hostName; + } + + public Map getMetrics() { + return metrics; + } + + public void setMetrics(Map metrics) { + this.metrics = metrics; + } + + public long getTimestamp() { + return timestamp; + } + + public void setTimestamp(long timestamp) { + this.timestamp = timestamp; + } +} diff --git a/zabbix-probes/zabbix-probes-common/src/main/java/com/indigo/zabbix/utils/ZabbixWrapperClient.java b/zabbix-probes/zabbix-probes-common/src/main/java/com/indigo/zabbix/utils/ZabbixWrapperClient.java new file mode 100644 index 0000000..decb8a7 --- /dev/null +++ b/zabbix-probes/zabbix-probes-common/src/main/java/com/indigo/zabbix/utils/ZabbixWrapperClient.java @@ -0,0 +1,25 @@ +package com.indigo.zabbix.utils; + +import feign.Headers; +import feign.Param; +import feign.RequestLine; +import feign.Response; + +/** + * Created by jose on 21/09/16. + */ +public interface ZabbixWrapperClient { + + static String ZABBIX_BASE_PATH = + "/monitoring/adapters/zabbix/zones/indigo/types/infrastructure/groups"; + + @RequestLine("POST " + ZABBIX_BASE_PATH + "/{group}/hosts/{hostName}") + @Headers("Content-Type: application/json") + Response registerHost(@Param("hostName") String hostName, @Param("group") String group, + ZabbixHost content); + + @RequestLine("GET " + ZABBIX_BASE_PATH + "/{group}/hosts/{hostName}") + @Headers("Content-Type: application/json") + Response getHostInfo(@Param("hostName") String hostName, @Param("group") String group); + +} diff --git a/zabbix-probes/zabbix-probes-common/zabbix-probes-common.iml b/zabbix-probes/zabbix-probes-common/zabbix-probes-common.iml new file mode 100644 index 0000000..a38b39a --- /dev/null +++ b/zabbix-probes/zabbix-probes-common/zabbix-probes-common.iml @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file