diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..6aa5079 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,8 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 6f29fee..69ace3f 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/BaseMonitor.java b/src/BaseMonitor.java index da54160..b31d664 100644 --- a/src/BaseMonitor.java +++ b/src/BaseMonitor.java @@ -1,16 +1,23 @@ import java.io.IOException; -public abstract class BaseMonitor { +public abstract class BaseMonitor implements Runnable { private final String friendlyName; private final String target; private final int interval; private final int timeout; + private MonitorStatus prevCheckStatus; + public enum MonitorStatus { + ONLINE, + OFFLINE, + UNKNOWN + } public BaseMonitor(String name, String target) { this.friendlyName = name; this.target = target; - this.interval = 120; - this.timeout = 5000; + this.interval = 5000; + this.timeout = 15000; + this.prevCheckStatus = MonitorStatus.UNKNOWN; } public String getFriendlyName() { @@ -29,13 +36,34 @@ public abstract class BaseMonitor { return timeout; } - public abstract boolean check() throws IOException; + public void setPrevCheckStatus(MonitorStatus newStatus) { + this.prevCheckStatus = newStatus; + } + public abstract MonitorStatus check() throws IOException, InterruptedException; - 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!"); +// public void checkCmd() throws IOException, InterruptedException { +// MonitorStatus currStatus = this.check(); +// +// if (currStatus == MonitorStatus.ONLINE) { +// System.out.println(this.getFriendlyName() + ", with address '" + this.getTarget() + "' is online!"); +// } else if (currStatus == MonitorStatus.OFFLINE) { +// System.out.println(this.getFriendlyName() + ", with address '" + this.getTarget() + "' is offline!"); +// } +// } + @Override + public void run() { + System.out.println("Monitor '" + this.friendlyName + "' start status: " + this.prevCheckStatus); + while (true) { + try { + MonitorStatus prevStatus = this.prevCheckStatus; + MonitorStatus checkStatus = this.check(); + if (prevStatus != checkStatus) { + System.out.println("Monitor '" + this.getFriendlyName() + "', changed, new status: " + checkStatus); + } + Thread.sleep(this.getInterval()); + } catch (IOException | InterruptedException e) { + throw new RuntimeException(e); + } } } } \ No newline at end of file diff --git a/src/HTTPMonitor.java b/src/HTTPMonitor.java index be8aee5..bc0fbc4 100644 --- a/src/HTTPMonitor.java +++ b/src/HTTPMonitor.java @@ -8,9 +8,12 @@ public class HTTPMonitor extends BaseMonitor{ } @Override - public boolean check() throws IOException { + public MonitorStatus check() throws IOException { URI uri = URI.create(super.getTarget()); HttpsURLConnection conn = (HttpsURLConnection) uri.toURL().openConnection(); - return conn.getResponseCode() == 200; + MonitorStatus checkStatus = (conn.getResponseCode() == 200) ? MonitorStatus.ONLINE : MonitorStatus.OFFLINE; + + super.setPrevCheckStatus(checkStatus); + return checkStatus; } } diff --git a/src/ICMPMonitor.java b/src/ICMPMonitor.java index 6d50c31..0a32eab 100644 --- a/src/ICMPMonitor.java +++ b/src/ICMPMonitor.java @@ -1,5 +1,4 @@ import java.io.IOException; -import java.net.InetAddress; public class ICMPMonitor extends BaseMonitor{ public ICMPMonitor(String name, String target) { @@ -7,8 +6,20 @@ public class ICMPMonitor extends BaseMonitor{ } @Override - public boolean check() throws IOException { - InetAddress address = InetAddress.getByName(super.getTarget()); - return address.isReachable(super.getTimeout()); + public MonitorStatus check() throws IOException, InterruptedException { + Process p1 = java.lang.Runtime.getRuntime().exec("ping -c 1 " + this.getTarget()); + int returnVal = p1.waitFor(); + if (returnVal==0) { + return MonitorStatus.ONLINE; + } else { + return MonitorStatus.OFFLINE; + } +// InetAddress address = InetAddress.getByName(super.getTarget()); +// return address.isReachable(super.getTimeout()); + } + + @Override + public void run() { + } } \ No newline at end of file diff --git a/src/Main.java b/src/Main.java index 726527f..2c1df4f 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,11 +1,23 @@ import java.io.IOException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; 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"); + public static void main(String[] args) throws InterruptedException { +// ICMPMonitor cloudFlareIcmpMonitor = new ICMPMonitor("Oracle Server 01", "srv01.oci.mzsl.nl"); + HTTPMonitor mzieselHttpMonit = new HTTPMonitor("Website van Mans", "https://mziesel.nl/"); + HTTPMonitor testHttpMonit = new HTTPMonitor("Test website", "https://whoami.mzsl.nl/"); + HTTPMonitor mcaHttpMonit = new HTTPMonitor("Microcenter website", "https://microcenter.nl/"); - cloudFlareIcmpMonitor.checkCmd(); - mzieselHttpMonit.checkCmd(); + ExecutorService executorService = Executors.newVirtualThreadPerTaskExecutor(); + + executorService.execute(mzieselHttpMonit); + executorService.execute(testHttpMonit); + executorService.execute(mcaHttpMonit); + + while (!executorService.isTerminated()) { + Thread.sleep(1000); + } } } \ No newline at end of file