commit 1a20bfc01c5d4314d8d42276ef85a8344e5ae411 Author: Mans Ziesel Date: Sat Feb 3 13:29:23 2024 +0100 first commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..6f29fee --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..1cc9d9b --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..599afa1 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# jmonit +Java Monitor, a simple monitoring application inspired by uptime kuma. diff --git a/hello-world.iml b/hello-world.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/hello-world.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/BaseMonitor.java b/src/BaseMonitor.java new file mode 100644 index 0000000..da54160 --- /dev/null +++ b/src/BaseMonitor.java @@ -0,0 +1,41 @@ +import java.io.IOException; + +public abstract class BaseMonitor { + private final String friendlyName; + private final String target; + private final int interval; + private final int timeout; + + public BaseMonitor(String name, String target) { + this.friendlyName = name; + this.target = target; + this.interval = 120; + this.timeout = 5000; + } + + public String getFriendlyName() { + return friendlyName; + } + + public String getTarget() { + return target; + } + + public int getInterval() { + return interval; + } + + public int getTimeout() { + return timeout; + } + + public abstract boolean check() throws IOException; + + public void checkCmd() throws IOException { + if (this.check()) { + System.out.println(this.getFriendlyName() + ", with address '" + this.getTarget() + "' is online!"); + } else { + System.out.println(this.getFriendlyName() + ", with address '" + this.getTarget() + "' is offline!"); + } + } +} \ No newline at end of file diff --git a/src/HTTPMonitor.java b/src/HTTPMonitor.java new file mode 100644 index 0000000..be8aee5 --- /dev/null +++ b/src/HTTPMonitor.java @@ -0,0 +1,16 @@ +import javax.net.ssl.HttpsURLConnection; +import java.io.IOException; +import java.net.URI; + +public class HTTPMonitor extends BaseMonitor{ + public HTTPMonitor(String name, String target) { + super(name, target); + } + + @Override + public boolean check() throws IOException { + URI uri = URI.create(super.getTarget()); + HttpsURLConnection conn = (HttpsURLConnection) uri.toURL().openConnection(); + return conn.getResponseCode() == 200; + } +} diff --git a/src/ICMPMonitor.java b/src/ICMPMonitor.java new file mode 100644 index 0000000..6d50c31 --- /dev/null +++ b/src/ICMPMonitor.java @@ -0,0 +1,14 @@ +import java.io.IOException; +import java.net.InetAddress; + +public class ICMPMonitor extends BaseMonitor{ + public ICMPMonitor(String name, String target) { + super(name, target); + } + + @Override + public boolean check() throws IOException { + InetAddress address = InetAddress.getByName(super.getTarget()); + return address.isReachable(super.getTimeout()); + } +} \ No newline at end of file diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..726527f --- /dev/null +++ b/src/Main.java @@ -0,0 +1,11 @@ +import java.io.IOException; + +public class Main { + public static void main(String[] args) throws IOException { + ICMPMonitor cloudFlareIcmpMonitor = new ICMPMonitor("Oracle Server 01", "srv01.oci.mzsl.nl"); + HTTPMonitor mzieselHttpMonit = new HTTPMonitor("Website van Mans", "https://mziesel.nl/bestaat-niet"); + + cloudFlareIcmpMonitor.checkCmd(); + mzieselHttpMonit.checkCmd(); + } +} \ No newline at end of file