Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
fmarslan authored Oct 2, 2019
1 parent 205e609 commit a6313fd
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 52 deletions.
85 changes: 74 additions & 11 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
</scm>
<properties>
<java.version>1.8</java.version>
<org.slf4j.version>2.0.0-alpha0</org.slf4j.version>
<org.slf4j.version>1.7.10</org.slf4j.version>
</properties>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
Expand All @@ -57,29 +57,92 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>
${project.build.directory}/libs
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<classpathPrefix>libs/</classpathPrefix>
<addClasspath>true</addClasspath>
<mainClass>com.fmarslan.properties.PingMonitor</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<!-- <plugin> -->
<!-- <groupId>org.apache.maven.plugins</groupId> -->
<!-- <artifactId>maven-assembly-plugin</artifactId> -->
<!-- <version>3.1.0</version> -->
<!-- <executions> -->
<!-- <execution> -->
<!-- <phase>package</phase> -->
<!-- <goals> -->
<!-- <goal>single</goal> -->
<!-- </goals> -->
<!-- <configuration> -->
<!-- <archive> -->
<!-- <manifest> -->
<!-- <mainClass> -->
<!-- com.fmarslan.properties.PingMonitor -->
<!-- </mainClass> -->
<!-- </manifest> -->
<!-- </archive> -->
<!-- <descriptorRefs> -->
<!-- <descriptorRef>jar-with-dependencies</descriptorRef> -->
<!-- </descriptorRefs> -->
<!-- </configuration> -->
<!-- </execution> -->
<!-- </executions> -->
<!-- </plugin> -->
</plugins>
</build>

<dependencies>
<dependency>
<groupId>com.fmarslan.tools</groupId>
<artifactId>environment-reader</artifactId>
<version>1.0.1-1</version>
<version>1.0.1-2</version>
</dependency>
<!-- <dependency> -->
<!-- <groupId>org.slf4j</groupId> -->
<!-- <artifactId>slf4j-log4j12</artifactId> -->
<!-- <version>${org.slf4j.version}</version> -->
<!-- </dependency> -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.7</version>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.7</version>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.6</version>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.6</version>
</dependency>
</dependencies>
</project>
14 changes: 11 additions & 3 deletions src/main/java/com/fmarslan/properties/OSBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@
public class OSBuilder {

private static final String PING_WINDOWS = "ping -n 1 -l %s -w %s %s";

private static final String PING_LINUX = "ping -c 1 -s %s -W %s %s";

private PingResultParser parser;
private String commandFormat;
private int timeoutFactor;

public static OSBuilder build() {
OSBuilder osb = new OSBuilder();
if (System.getProperty("os.name").startsWith("Windows")) {
osb.parser = new PingResultParser(PingResultParser.WINDOWS);
osb.commandFormat = PING_WINDOWS;
osb.timeoutFactor=1000;
} else {
// For Linux and OSX
// cmd = "ping -c 1 " + host;
osb.parser = new PingResultParser(PingResultParser.LINUX);
osb.commandFormat = PING_LINUX;
osb.timeoutFactor=1;
}
return osb;
}
Expand All @@ -29,5 +33,9 @@ public PingResultParser getParser() {
public String getCommandFormat() {
return commandFormat;
}

public int getTimeoutFactor() {
return timeoutFactor;
}

}
40 changes: 15 additions & 25 deletions src/main/java/com/fmarslan/properties/PingMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.TimeZone;

import org.apache.logging.log4j.LogManager;
Expand All @@ -38,6 +36,7 @@ public class PingMonitor {
private static OSBuilder osBuilder;
private static int timeout;
private static int packageSize;
private static int threshold;

static SimpleDateFormat sdf;

Expand All @@ -55,41 +54,32 @@ public static void sendPingRequest(String ipAddress) {
outputLines.append(line);
}
PingResult pingResult = osBuilder.getParser().parse(outputLines.toString());
LOGGER.info("time={}, host={}, responseIp={}, size={}, miliseconds={}, ttl={}, message={}, originalText={}", getTime(), pingResult.getHost(), pingResult.getIp(), pingResult.getSize(),
pingResult.getTime(), pingResult.getTtl(), pingResult.getMessage(), pingResult.getOriginalText());
if (pingResult.getTime() == null || threshold <= pingResult.getTime())
LOGGER.info(
"time={}, host={}, responseIp={}, size={}, miliseconds={}, ttl={}, message={}, originalText={}",
getTime(), pingResult.getHost(), pingResult.getIp(), pingResult.getSize(), pingResult.getTime(),
pingResult.getTtl(), pingResult.getMessage(), pingResult.getOriginalText());
} catch (Exception ex) {
ex.printStackTrace();
}

// InetAddress geek = InetAddress.getByName(ipAddress);
//
// long time = System.currentTimeMillis();
// System.out.println("Sending Ping Request to " + ipAddress);
//
// if (geek.isReachable(Integer.valueOf(EnvironmentReader.getEnvironment("app.timeout")))) {
// time = System.currentTimeMillis() - time;
// LOGGER.info("", getTime(), REACHABLE, time, ipAddress);
// if (Long.valueOf(EnvironmentReader.getEnvironment("app.threshold")) <= time) {
//
// }
// } else {
//// LOGGER.error(String.format("%s,%s,[unreachable]", "-", ipAddress));
// LOGGER.error("", getTime(), UNREACHABLE, "-", ipAddress);
// }
}

private static String getTime() {
String timezone = EnvironmentReader.getEnvironment("app.timezone");
Calendar c = Calendar
.getInstance(TimeZone.getTimeZone(timezone));
Calendar c = Calendar.getInstance(TimeZone.getTimeZone(timezone));
return sdf.format(c.getTime());
}

public static void main(String[] ars) {
EnvironmentReader.loadFromResource(PingMonitor.class, "app.properties");
EnvironmentReader.loadFromResource(PingMonitor.class, "/app.properties");
osBuilder = OSBuilder.build();
timeout = Integer.valueOf(EnvironmentReader.getEnvironment("app.timeout"));
packageSize = Integer.valueOf(EnvironmentReader.getEnvironment("app.packageSize"));
String t = EnvironmentReader.getEnvironment("app.timeout");
System.out.println(t);
timeout = Integer.valueOf(t) * osBuilder.getTimeoutFactor();
t = EnvironmentReader.getEnvironment("app.packageSize");
packageSize = Integer.valueOf(t);
t = EnvironmentReader.getEnvironment("app.threshold");
threshold = Integer.valueOf(t);
sdf = new SimpleDateFormat(EnvironmentReader.getEnvironment("app.dateformat"));
String ips = EnvironmentReader.getEnvironment("app.ips");
String[] ipadress = ips.split(",");
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/com/fmarslan/properties/PingResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ public class PingResult {
private String ip;
private String host;
private int size;
private int ttl;
private int time;
private Double ttl;
private Double time;
private String originalText;
private String message;

Expand Down Expand Up @@ -34,19 +34,19 @@ public void setSize(int size) {
this.size = size;
}

public int getTtl() {
public Double getTtl() {
return ttl;
}

public void setTtl(int ttl) {
public void setTtl(Double ttl) {
this.ttl = ttl;
}

public int getTime() {
public Double getTime() {
return time;
}

public void setTime(int time) {
public void setTime(Double time) {
this.time = time;
}

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/fmarslan/properties/PingResultParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
public class PingResultParser {

public static final String WINDOWS = "Pinging (?<host>[a-zA-Z0-9\\.]*)(.*)data:((Reply from (?<ip>[0-9\\.]*): bytes=(?<size>[0-9]*) time(=|<|>)(?<time>[0-9]*)ms TTL(=|>|<)(?<ttl>[0-9]*))|(?<message>[a-zA-Z ]*)\\.)";
public static final String LINUX = "PING (?<host>[a-zA-Z0-9\\.]*)(.*)data.(((?<size>[0-9\\.]*) bytes from (?<ip>[0-9\\.]*)(.*) ttl(=|<|>)(?<ttl>[0-9\\.]*) time(<|=|>)(?<time>[0-9\\.]*))|(?<message>[a-zA-Z-]*))";

private String regex;

Expand All @@ -30,9 +31,9 @@ public PingResult parse(String text) {
if (size != null)
pr.setSize(Integer.valueOf(size));
if (time != null)
pr.setTime(Integer.valueOf(time));
pr.setTime(Double.valueOf(time));
if (ttl != null)
pr.setTtl(Integer.valueOf(ttl));
pr.setTtl(Double.valueOf(ttl));
pr.setOriginalText(text);
}
return pr;
Expand Down
6 changes: 3 additions & 3 deletions src/main/resources/app.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
app.timeout=1000
app.timeout=1
app.timezone=GMT+3
app.threshold=0
app.threshold=100
app.ips="127.0.0.1","207.244.126.7","111.11.11.11"
app.tick=1000
app.tick=10000
app.packageSize=1024
app.dateformat=dd-MM-yyyy hh:mm:ss
6 changes: 4 additions & 2 deletions src/main/resources/log4j2.properties
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,7 @@ appender.console.layout.pattern = %m%n
#appender.console.filter.threshold.level = debug

rootLogger.level = debug
rootLogger.appenderRefs = console
rootLogger.appenderRef.csvFile.ref =STDOUT
rootLogger.appenderRefs = csvFile
rootLogger.appenderRef.csvFile.ref =CSVFILE
#rootLogger.appenderRefs = console
#rootLogger.appenderRef.csvFile.ref =STDOUT

0 comments on commit a6313fd

Please sign in to comment.