init commit
This commit is contained in:
+116
@@ -0,0 +1,116 @@
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.net.Inet4Address;
|
||||
import java.util.Scanner;
|
||||
import java.util.Vector;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class App {
|
||||
public static int pingAmount = 5;
|
||||
public static int threadCount = 4;
|
||||
|
||||
public static boolean killLoggingThread = false;
|
||||
|
||||
public static ExecutorService workerThread = Executors.newFixedThreadPool(threadCount);
|
||||
public static BlockingQueue<String> printQueue = new ArrayBlockingQueue<String>(1024);
|
||||
public static BlockingQueue<String> fileQueue = new ArrayBlockingQueue<String>(1024);
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
//Var dec
|
||||
Vector<String[]> requestedPings = new Vector<String[]>();
|
||||
File pingRequests = new File("pingRequests.csv");
|
||||
File pingResults = new File("pingResults.csv");
|
||||
FileWriter pingResultsWriter = new FileWriter(pingResults);
|
||||
Scanner FScanner = new Scanner(pingRequests);
|
||||
|
||||
System.out.println("Reading file...");
|
||||
// Populate vector
|
||||
do{
|
||||
requestedPings.add(FScanner.nextLine().split(","));
|
||||
} while (FScanner.hasNextLine());
|
||||
FScanner.close();
|
||||
|
||||
System.out.println("Setting up output file...");
|
||||
// See if the file exists
|
||||
if(!pingResults.exists())
|
||||
pingResults.createNewFile();
|
||||
|
||||
// Make sure I can read/write to file
|
||||
if(!pingResults.canRead() && !pingResults.canWrite()) {
|
||||
System.out.println("Can't read/write to file. Aborting");
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
// Clear file and set title line
|
||||
pingResultsWriter.write("device,ip,status\n");
|
||||
|
||||
System.out.println("Pinging...\n---");
|
||||
// Ping and append to file
|
||||
requestedPings.forEach((x) -> {
|
||||
// Build runnable
|
||||
Runnable rPing = () -> {
|
||||
try {
|
||||
float passCount = 0;
|
||||
for(int i = 0; i < pingAmount; i++)
|
||||
if(Inet4Address.getByName(x[1]).isReachable(5000))
|
||||
passCount++;
|
||||
if(passCount/pingAmount > 0.5) {
|
||||
printQueue.add(x[0] + " is up");
|
||||
fileQueue.add(x[0] + "," + x[1] + ",up\n");
|
||||
} else {
|
||||
printQueue.add(x[0] + " is \u001B[31mdown\u001B[0m");
|
||||
fileQueue.add(x[0] + "," + x[1] + ",down\n");
|
||||
}
|
||||
} catch (IOException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
};
|
||||
// Add to the execute stack and execute it
|
||||
workerThread.execute(rPing);
|
||||
});
|
||||
|
||||
Runnable rLog = () -> {
|
||||
try {
|
||||
while(!killLoggingThread){
|
||||
if(!printQueue.isEmpty())
|
||||
System.out.println(printQueue.take());
|
||||
if(!fileQueue.isEmpty())
|
||||
pingResultsWriter.append(fileQueue.take());
|
||||
}
|
||||
} catch(IOException ioe){
|
||||
ioe.printStackTrace();
|
||||
} catch(InterruptedException ie){
|
||||
ie.printStackTrace();
|
||||
}
|
||||
};
|
||||
|
||||
// Create logging thread
|
||||
Thread logThread = new Thread(rLog);
|
||||
logThread.start();
|
||||
|
||||
// Tell the worker queue we're ready to shutdown
|
||||
workerThread.shutdown();
|
||||
try {
|
||||
// Wait one hour for the threads to finish
|
||||
if (!workerThread.awaitTermination(1, TimeUnit.HOURS)) {
|
||||
workerThread.shutdownNow();
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
workerThread.shutdownNow();
|
||||
}
|
||||
|
||||
// Interrupt the log thread
|
||||
killLoggingThread = true;
|
||||
logThread.join();
|
||||
|
||||
// Write and save file
|
||||
System.out.println("---\nWriting to file...");
|
||||
pingResultsWriter.close();
|
||||
System.out.print("Complete! Exiting\n");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user