Change classes, make it work

This commit is contained in:
Mans Ziesel 2024-02-03 16:25:40 +01:00
parent 1a20bfc01c
commit af880fff0f
7 changed files with 89 additions and 21 deletions

View File

@ -0,0 +1,8 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="AutoCloseableResource" enabled="true" level="WARNING" enabled_by_default="true">
<option name="METHOD_MATCHER_CONFIG" value="java.util.Formatter,format,java.io.Writer,append,com.google.common.base.Preconditions,checkNotNull,org.hibernate.Session,close,java.io.PrintWriter,printf,java.io.PrintStream,printf,java.util.concurrent.Executors,newVirtualThreadPerTaskExecutor" />
</inspection_tool>
</profile>
</component>

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="21" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="openjdk-21" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

View File

@ -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);
}
}
}
}

View File

@ -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;
}
}

View File

@ -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() {
}
}

View File

@ -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);
}
}
}