Skip to content
Snippets Groups Projects
Commit ef45052e authored by Ramaguru Radhakrishnan's avatar Ramaguru Radhakrishnan :speech_balloon:
Browse files

Update src/main/java/com/rubix/WAMPAC/NMSCollection/DescriptorThread.java,...

Update src/main/java/com/rubix/WAMPAC/NMSCollection/DescriptorThread.java, src/main/java/com/rubix/WAMPAC/NMSCollection/DetectUSB.java, src/main/java/com/rubix/WAMPAC/NMSCollection/DiskStorage.java, src/main/java/com/rubix/WAMPAC/NMSCollection/DiskThread.java, src/main/java/com/rubix/WAMPAC/NMSCollection/FileDescriptor.java, src/main/java/com/rubix/WAMPAC/NMSCollection/FileSystem.java, src/main/java/com/rubix/WAMPAC/NMSCollection/FileSystemThread.java, src/main/java/com/rubix/WAMPAC/NMSCollection/Functions.java, src/main/java/com/rubix/WAMPAC/NMSCollection/MemoryInfo.java, src/main/java/com/rubix/WAMPAC/NMSCollection/MemoryThread.java, src/main/java/com/rubix/WAMPAC/NMSCollection/NetworkInfo.java, src/main/java/com/rubix/WAMPAC/NMSCollection/Paths.java, src/main/java/com/rubix/WAMPAC/NMSCollection/PowerInfo.java, src/main/java/com/rubix/WAMPAC/NMSCollection/ProcessInfo.java, src/main/java/com/rubix/WAMPAC/NMSCollection/Sessions.java, src/main/java/com/rubix/WAMPAC/NMSCollection/TCPandUDPStats.java
parent 4a5add71
No related branches found
No related tags found
No related merge requests found
Showing
with 855 additions and 0 deletions
package com.rubix.WAMPAC.NMSCollection;
import org.json.JSONException;
import java.io.IOException;
import static com.rubix.WAMPAC.NMSCollection.FileDescriptor.DescriptorMain;
public class DescriptorThread implements Runnable {
@Override
public void run() {
while (true) {
try {
DescriptorMain();
Thread.sleep( 60000 );
} catch (JSONException | IOException | InterruptedException e) {
e.printStackTrace();
}
}
}
}
package com.rubix.WAMPAC.NMSCollection;
/**
* The code is automation of USB detection and Removal
* Runs in loop continuously
* Pushes output to console
*/
import org.json.JSONArray;
import org.json.JSONObject;
import oshi.SystemInfo;
import oshi.hardware.HardwareAbstractionLayer;
import oshi.hardware.UsbDevice;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
public final class DetectUSB {
private static List<String> devicesToDetect = new ArrayList<>();
private static boolean anyDeviceFound = false;
private static JSONArray resultArray = new JSONArray( );
public static JSONArray usbDetectMain () throws Throwable {
devicesToDetect.clear();
resultArray = new JSONArray( );
devicesToDetect.add("USB Mass Storage Device");
SystemInfo si = new SystemInfo();
HardwareAbstractionLayer hal = si.getHardware();
List<UsbDevice> USBData;
USBData = hal.getUsbDevices( true );
anyDeviceFound = false;
for( UsbDevice dev : USBData) {
usbDetect( dev, false );
}
if (!anyDeviceFound) {
JSONArray resultArray = new JSONArray( );
JSONObject object = new JSONObject( );
object.put( "time" , LocalDateTime.now() );
object.put( "detected", "no" );
object.put( "devicename", "" );
resultArray.put( object );
}
return resultArray;
}
private static void usbDetect (UsbDevice usbDev, boolean devDetected) throws Throwable {
boolean devDetectedLoc = false;
if (devicesToDetect.contains( usbDev.getName() )) {
devDetectedLoc = true;
}
if (devDetected) {
anyDeviceFound = true;
usbDisconnect( usbDev );
} else {
if (!usbDev.getConnectedDevices().toString().equals( "[]" )) {
for (UsbDevice dev : usbDev.getConnectedDevices()) {
usbDetect( dev, devDetectedLoc );
}
}
}
}
private static String usbDisconnect (UsbDevice usbDev) throws Throwable {
JSONObject object = new JSONObject( );
object.put( "time" , LocalDateTime.now() );
object.put( "detected", "yes" );
object.put( "devicename", usbDev.getName() );
resultArray.put( object );
//outp.add( LocalDateTime.now() + " [Detected Device [" + usbDev.getName() + "]]");
// System.out.println(LocalDateTime.now() + " [Disconnecting [" + usbDev.getName() + "]]");
// //logic to disconnect
// if(true) {
// System.out.println( LocalDateTime.now() + " [Disconnected [" + usbDev.getName() + "]]" );
// }
// else {
// System.out.println( LocalDateTime.now() + " [Could not disconnect [" + usbDev.getName() + "]]" );
// }
return resultArray.toString();
}
}
package com.rubix.WAMPAC.NMSCollection;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import oshi.SystemInfo;
import java.time.LocalDateTime;
import static com.rubix.Resources.Functions.readFile;
import static com.rubix.Resources.Functions.writeToFile;
import static com.rubix.WAMPAC.NMSCollection.Paths.DiskFile;
import static com.rubix.WAMPAC.NMSCollection.Paths.DiskFolder;
public class DiskStorage {
public static void DiskStorageMain() throws JSONException{
String readFile = readFile(DiskFolder + DiskFile);
JSONArray array = new JSONArray(readFile);
array.put(collectDiskStorage());
writeToFile(DiskFolder + DiskFile, array.toString(), false);
}
public static JSONObject collectDiskStorage() throws JSONException {
JSONObject object = new JSONObject();
SystemInfo si = new SystemInfo();
object.put("time", LocalDateTime.now());
if (si.getHardware().getDiskStores().get( 0 ).getSize() == 0)
object.put( "message", "Disk storage is 0" );
else
object.put( "message", "" );
object.put("diskName", si.getHardware().getDiskStores().get(0).getName());
object.put("size", si.getHardware().getDiskStores().get(0).getSize());
object.put("readBytes", si.getHardware().getDiskStores().get(0).getReadBytes());
object.put("writeBytes", si.getHardware().getDiskStores().get(0).getWriteBytes());
return object;
}
}
package com.rubix.WAMPAC.NMSCollection;
import org.json.JSONException;
import java.io.IOException;
public class DiskThread implements Runnable {
@Override
public void run() {
while (true) {
try {
DiskStorage.DiskStorageMain();
Thread.sleep( 60000);
} catch (JSONException | InterruptedException e) {
e.printStackTrace();
}
}
}
}
package com.rubix.WAMPAC.NMSCollection;
/**
* The code is automation File Descriptor Info in the System
* Runs every 6 minutes
* Pushes output to console
*/
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import oshi.SystemInfo;
import java.io.IOException;
import java.time.LocalDateTime;
import static com.rubix.Resources.Functions.readFile;
import static com.rubix.Resources.Functions.writeToFile;
import static com.rubix.WAMPAC.NMSCollection.Paths.DescriptorFile;
import static com.rubix.WAMPAC.NMSCollection.Paths.DescriptorFolder;
public class FileDescriptor {
private static JSONObject object = new JSONObject( );
public static void DescriptorMain() throws JSONException, IOException {
String readFile = readFile(DescriptorFolder + DescriptorFile);
JSONArray array = new JSONArray(readFile);
array.put(fileDescriptor());
writeToFile(DescriptorFolder + DescriptorFile, array.toString(), false);
}
public static JSONObject fileDescriptor() throws JSONException {
SystemInfo si = new SystemInfo();
object.put( "time" , LocalDateTime.now() );
object.put( "openDescriptors" , si.getOperatingSystem().getFileSystem().getOpenFileDescriptors() );
object.put("MaxDescriptors", si.getOperatingSystem().getFileSystem().getMaxFileDescriptors());
if(si.getOperatingSystem().getFileSystem().getOpenFileDescriptors()==(si.getOperatingSystem().getFileSystem().getMaxFileDescriptors()))
object.put( "message", "Maximum and open file descriptors are same" );
else
object.put( "message", "" );
return object;
}
}
package com.rubix.WAMPAC.NMSCollection;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import oshi.SystemInfo;
import oshi.software.os.OSFileStore;
import java.io.IOException;
import static com.rubix.Resources.Functions.readFile;
import static com.rubix.Resources.Functions.writeToFile;
import static com.rubix.WAMPAC.NMSCollection.Paths.FileSystemFile;
import static com.rubix.WAMPAC.NMSCollection.Paths.FileSystemFolder;
public class FileSystem {
public static void FileSystemMain() throws JSONException, IOException {
String readFile = readFile(FileSystemFolder + FileSystemFile);
JSONArray array = new JSONArray(readFile);
array.put(fileSystem());
writeToFile(FileSystemFolder + FileSystemFile, array.toString(), false);
}
public static JSONObject fileSystem() throws JSONException {
SystemInfo si = new SystemInfo();
JSONObject object = new JSONObject();
oshi.software.os.FileSystem fileSys = si.getOperatingSystem().getFileSystem();
for (OSFileStore store : fileSys.getFileStores()) {
object.put("fileStoreName", store.getName());
object.put("freeSpace", store.getFreeSpace());
object.put("usableSpace", store.getUsableSpace());
object.put("totalSpace", store.getTotalSpace());
if ((store.getFreeSpace() < store.getUsableSpace()) || (store.getFreeSpace() > store.getTotalSpace()) || (store.getUsableSpace() > store.getTotalSpace())) {
object.put("message", "Error occurred for drive storage");
}
else
object.put("message", "");
}
return object;
}
}
package com.rubix.WAMPAC.NMSCollection;
import org.json.JSONException;
import java.io.IOException;
import static com.rubix.WAMPAC.NMSCollection.FileSystem.FileSystemMain;
public class FileSystemThread implements Runnable {
@Override
public void run() {
while (true) {
try {
FileSystemMain();
Thread.sleep( 60000);
} catch (JSONException | IOException | InterruptedException e) {
e.printStackTrace();
}
}
}
}
package com.rubix.WAMPAC.NMSCollection;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.*;
import java.nio.charset.StandardCharsets;
import static com.rubix.Resources.Functions.*;
import static com.rubix.Resources.IPFSNetwork.executeIPFSCommands;
public class Functions {
public static JSONArray readDataFromCSV(String fileName) {
String nameString="", vendorString="";
JSONArray jsonArray = new JSONArray();
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
String line = "";
br.readLine();
br.readLine();
br.readLine();
while ((line = br.readLine()) != null) {
line = line.trim();
if (!line.equals("")) {
String[] attributes = line.split(",");
String name = attributes[1];
String vendor = attributes[2];
nameString = new String(name.getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_16);
vendorString = new String(vendor.getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_16);
JSONObject jsonObject = new JSONObject();
jsonObject.put("name", nameString.substring(0, nameString.length()-1));
jsonObject.put("vendor", vendorString);
jsonArray.put(jsonObject);
}
}
} catch (IOException | JSONException ioe) {
ioe.printStackTrace();
}
return jsonArray;
}
public static void createDIR(JSONArray array) throws JSONException {
for(int i = 0; i < array.length(); i++){
File file = new File(array.getString(i));
if(!file.exists())
file.mkdir();
}
}
}
package com.rubix.WAMPAC.NMSCollection;
/**
* The code is automation Memory Info in the System
* Runs every 6 minutes
* Pushes output to console
*/
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import oshi.SystemInfo;
import oshi.hardware.HardwareAbstractionLayer;
import java.io.IOException;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.time.LocalDateTime;
import static com.rubix.Resources.Functions.readFile;
import static com.rubix.Resources.Functions.writeToFile;
import static com.rubix.WAMPAC.NMSCollection.Paths.MemoryFile;
import static com.rubix.WAMPAC.NMSCollection.Paths.MemoryFolder;
public class MemoryInfo {
private static final DecimalFormat df2 = new DecimalFormat("#.##");
public static void MemoryMain() throws JSONException, IOException {
String readFile = readFile(MemoryFolder + MemoryFile);
JSONArray array = new JSONArray(readFile);
array.put(memoryTest());
writeToFile(MemoryFolder + MemoryFile, array.toString(), false);
}
public static JSONObject memoryTest() throws JSONException {
JSONObject object = new JSONObject();
SystemInfo si = new SystemInfo();
HardwareAbstractionLayer hal = si.getHardware();
df2.setRoundingMode(RoundingMode.UP);
long totalMem = hal.getMemory().getTotal();
long maxvm = hal.getMemory().getVirtualMemory().getVirtualMax();
//threshold for Memory and Virtual Memory
double twentyPTotal = 0.10 * totalMem;
double twentypvm = 0.10 * maxvm;
object.put("time", LocalDateTime.now());
long availableMem = hal.getMemory().getAvailable();
object.put("availableMem", availableMem);
object.put("totalMemory", si.getHardware().getMemory().getTotal());
long availableVM = hal.getMemory().getVirtualMemory().getVirtualInUse();
object.put("usedVirtualMem", maxvm - availableVM);
object.put("totalVirtualMemory", si.getHardware().getMemory().getVirtualMemory().getVirtualMax());
//checking if Memory and Virtual memory is less
if (availableMem < twentyPTotal) {
object.put("Message", "Available memory is less than 10%");
}
if (availableVM < twentypvm) {
object.put("Message", "Available virtual memory is less than 10%");
}
return object;
}
}
package com.rubix.WAMPAC.NMSCollection;
import org.json.JSONException;
import java.io.IOException;
public class MemoryThread implements Runnable {
@Override
public void run() {
while (true) {
try {
MemoryInfo.MemoryMain();
Thread.sleep( 60000 );
} catch (JSONException | IOException | InterruptedException e) {
e.printStackTrace();
}
}
}
}
package com.rubix.WAMPAC.NMSCollection;
import org.json.JSONException;
import org.json.JSONObject;
import oshi.hardware.platform.windows.WindowsNetworkIF;
import java.net.*;
import java.time.LocalDateTime;
import java.util.Arrays;
public class NetworkInfo {
private static JSONObject object = new JSONObject( );
public static JSONObject networkInfo() throws UnknownHostException, SocketException, JSONException {
InetAddress ip = Inet4Address.getLocalHost();
NetworkInterface nifByIp = NetworkInterface.getByInetAddress( ip );
WindowsNetworkIF windowsNetworkIF = new WindowsNetworkIF( nifByIp );
object.put("time", LocalDateTime.now() );
object.put("interface", nifByIp );
object.put("macAddress", windowsNetworkIF.getMacaddr());
object.put("ipAddress", ip);
object.put("displayName", windowsNetworkIF.getDisplayName());
object.put("packetsReceived", windowsNetworkIF.getPacketsRecv());
object.put("bytesReceived", windowsNetworkIF.getBytesRecv());
object.put("packetsSent", windowsNetworkIF.getPacketsSent());
object.put("bytesSent", windowsNetworkIF.getBytesSent());
object.put("inputDrops", windowsNetworkIF.getInDrops());
object.put("inputErrors", windowsNetworkIF.getInErrors());
object.put("outErrors", windowsNetworkIF.getOutErrors() );
object.put("speed", windowsNetworkIF.getSpeed());
object.put("mtu", windowsNetworkIF.getMTU());
String ipv4Addr = Arrays.toString( windowsNetworkIF.getIPv4addr());
//triggering SC if any of the below conditions are not met
if(windowsNetworkIF.queryNetworkInterface() == null)
object.put( "message", "Interface queried cannot be null" );
else
object.put( "message", "" );
if(ipv4Addr.contains( "127.0.0.1" ))
object.put( "message", "Interface is localhost, please check network connectivity" );
else
object.put( "message", "" );
if(windowsNetworkIF.getInErrors() >= 5){
object.put( "message", "More than 5 input errors present" );
object.put( "value", windowsNetworkIF.getInErrors() );
//trigger SC to check
}
else
object.put( "message", "" );
if(windowsNetworkIF.getOutErrors() >= 5)
{
object.put( "message", "More than 5 output errors present" );
object.put( "value", windowsNetworkIF.getOutErrors() );
//Trigger SC to check
}
else
object.put( "message", "" );
return object;
}
}
package com.rubix.WAMPAC.NMSCollection;
import org.json.JSONArray;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import static com.rubix.Resources.Functions.*;
public class Paths {
public static String CollectionFolder = getCollectionFolderPath();
private static String getCollectionFolderPath() {
if(getOsName().contains("Linux"))
return "/home/" + getSystemUser() + "/Rubix/Collection/";
else if(getOsName().contains("Mac"))
return "/Applications/Rubix/Collection/";
else if(getOsName().contains("Windows"))
return "C:\\Rubix\\Collection\\";
return null;
}
public static String MemoryFolder = getMemoryFolderPath();
private static String getMemoryFolderPath() {
if(getOsName().contains("Linux"))
return "/home/" + getSystemUser() + "/Rubix/Collection/Memory/";
else if(getOsName().contains("Mac"))
return "/Applications/Rubix/Collection/Memory/";
else if(getOsName().contains("Windows"))
return "C:\\Rubix\\Collection\\Memory\\";
return null;
}
public static String DiskFolder = getDiskFolderPath();
private static String getDiskFolderPath() {
if(getOsName().contains("Linux"))
return "/home/" + getSystemUser() + "/Rubix/Collection/DiskStorage/";
else if(getOsName().contains("Mac"))
return "/Applications/Rubix/Collection/DiskStorage/";
else if(getOsName().contains("Windows"))
return "C:\\Rubix\\Collection\\DiskStorage\\";
return null;
}
public static String DescriptorFolder = getDescriptorFolderPath();
private static String getDescriptorFolderPath() {
if(getOsName().contains("Linux"))
return "/home/" + getSystemUser() + "/Rubix/Collection/Descriptors/";
else if(getOsName().contains("Mac"))
return "/Applications/Rubix/Collection/Descriptors/";
else if(getOsName().contains("Windows"))
return "C:\\Rubix\\Collection\\Descriptors\\";
return null;
}
public static String FileSystemFolder = getFileSystemFolderPath();
private static String getFileSystemFolderPath() {
if(getOsName().contains("Linux"))
return "/home/" + getSystemUser() + "/Rubix/Collection/FileSystem/";
else if(getOsName().contains("Mac"))
return "/Applications/Rubix/Collection/FileSystem/";
else if(getOsName().contains("Windows"))
return "C:\\Rubix\\Collection\\FileSystem\\";
return null;
}
public static String MemoryFile = getMemoryFilePath();
private static String getMemoryFilePath() {
String day, month, year;
day = String.format("%2d", LocalDateTime.now().getDayOfMonth()).replace(' ', '0');
month = String.format("%2d", LocalDateTime.now().getMonthValue()).replace(' ', '0');
year = String.valueOf(LocalDateTime.now().getYear());
return day.concat(month).concat(year).concat("_").concat("Memory").concat(".json");
}
public static String DiskFile = getDiskFilePath();
private static String getDiskFilePath() {
String day, month, year;
day = String.format("%2d", LocalDateTime.now().getDayOfMonth()).replace(' ', '0');
month = String.format("%2d", LocalDateTime.now().getMonthValue()).replace(' ', '0');
year = String.valueOf(LocalDateTime.now().getYear());
return day.concat(month).concat(year).concat("_").concat("DiskStorage").concat(".json");
}
public static String DescriptorFile = getDescriptorFilePath();
private static String getDescriptorFilePath() {
String day, month, year;
day = String.format("%2d", LocalDateTime.now().getDayOfMonth()).replace(' ', '0');
month = String.format("%2d", LocalDateTime.now().getMonthValue()).replace(' ', '0');
year = String.valueOf(LocalDateTime.now().getYear());
return day.concat(month).concat(year).concat("_").concat("Descriptor").concat(".json");
}
public static String FileSystemFile = getFileSystemFilePath();
private static String getFileSystemFilePath() {
String day, month, year;
day = String.format("%2d", LocalDateTime.now().getDayOfMonth()).replace(' ', '0');
month = String.format("%2d", LocalDateTime.now().getMonthValue()).replace(' ', '0');
year = String.valueOf(LocalDateTime.now().getYear());
return day.concat(month).concat(year).concat("_").concat("FileSystem").concat(".json");
}
public static void createCollectionFolder() throws IOException {
File collectionFolder = new File(CollectionFolder);
if(!collectionFolder.exists())
collectionFolder.mkdir();
File memoryFolder = new File(MemoryFolder);
if(!memoryFolder.exists())
memoryFolder.mkdir();
File diskFolder = new File(DiskFolder);
if(!diskFolder.exists())
diskFolder.mkdir();
File descriptorFolder = new File(DescriptorFolder);
if(!descriptorFolder.exists())
descriptorFolder.mkdir();
File fileSystemFolder = new File(FileSystemFolder);
if(!fileSystemFolder.exists())
fileSystemFolder.mkdir();
File memoryFile = new File(MemoryFolder + MemoryFile);
if(!memoryFile.exists()) {
memoryFile.createNewFile();
writeToFile(MemoryFolder + MemoryFile, "[]", false);
}
File diskFile = new File(DiskFolder + DiskFile);
if(!diskFile.exists()) {
diskFile.createNewFile();
writeToFile(DiskFolder + DiskFile, "[]", false);
}
File descriptorFile = new File(DescriptorFolder + DescriptorFile);
if(!descriptorFile.exists()) {
descriptorFile.createNewFile();
writeToFile(DescriptorFolder + DescriptorFile, "[]", false);
}
File fileSystemFile = new File(FileSystemFolder + FileSystemFile);
if(!fileSystemFile.exists()) {
fileSystemFile.createNewFile();
writeToFile(FileSystemFolder + FileSystemFile, "[]", false);
}
}
}
package com.rubix.WAMPAC.NMSCollection;
/**
* The code is run to check if its a Desktop or Battery powered system
* Runs when system switches on
* Pushes output to console
* If a battery powered system, trigger SC to alert system is not DESKTOP
*/
import org.json.JSONException;
import org.json.JSONObject;
import oshi.SystemInfo;
import oshi.hardware.HardwareAbstractionLayer;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.time.LocalDateTime;
public class PowerInfo {
public static DecimalFormat df2 = new DecimalFormat("#.##");
private static JSONObject object = new JSONObject( );
public static JSONObject powerInfo() throws JSONException {
SystemInfo si = new SystemInfo();
HardwareAbstractionLayer hal = si.getHardware();
df2.setRoundingMode( RoundingMode.UP );
if((hal.getPowerSources().get( 0 ).isPowerOnLine()==false) && (hal.getPowerSources().get( 0 ).isCharging()==(false))) {
//System is on battery and not charging for eg: laptop/tablet etc
object.put( "time" , LocalDateTime.now() );
object.put( "capacity", df2.format( hal.getPowerSources().get( 0 ).getRemainingCapacityPercent()) );
object.put( "Message", "System is on battery power" );
object.put( "condition", "Informational" );
// resultArray.put( object );
}
else if((hal.getPowerSources().get( 0 ).isPowerOnLine()==true) && (hal.getPowerSources().get( 0 ).isCharging()==(false))){
//System is connected to charger but 100 percent charged - All sorts of systems
object.put( "time" , LocalDateTime.now() );
object.put( "capacity", df2.format( hal.getPowerSources().get( 0 ).getRemainingCapacityPercent()) );
object.put( "Message", "System is not charging" );
object.put( "condition", "Informational" );
// resultArray.put( object );
}
else if((hal.getPowerSources().get( 0 ).isPowerOnLine()==true) && (hal.getPowerSources().get( 0 ).isCharging()==(true))) {
//System is connected to charger and charging - All sorts of systems
object.put( "time" , LocalDateTime.now() );
object.put( "capacity", df2.format( hal.getPowerSources().get( 0 ).getRemainingCapacityPercent()) );
object.put( "Message", "System is charging" );
object.put( "condition", "Informational" );
//resultArray.put( object );
}
if(hal.getPowerSources().get( 0 ).getRemainingCapacityPercent()<=(0.10))
{
object.put( "time" , LocalDateTime.now() );
object.put( "capacity", df2.format( hal.getPowerSources().get( 0 ).getRemainingCapacityPercent()) );
object.put( "Message", "Battery power is less than 10 percent and needs attention" );
object.put( "condition", "Critical" );
// resultArray.put( object );
}
return object;
}
}
package com.rubix.WAMPAC.NMSCollection;
import org.json.JSONException;
import org.json.JSONObject;
import oshi.SystemInfo;
import oshi.software.os.OSProcess;
public class ProcessInfo {
private static JSONObject object = new JSONObject();
public static JSONObject processInfo() throws JSONException {
SystemInfo si = new SystemInfo();
int countingprocessminor = 0;
int countProc = 0;
for (OSProcess proc : si.getOperatingSystem().getProcesses()) {
if (proc.getUserID() == null) {
object.put( "process", proc.getProcessID() );
object.put( "user", proc.getUser() );
//let the SC know this
} else if (proc.getUser().equals( " " )) {
object.put( "process", proc.getProcessID() );
object.put( "user", proc.getUser() );
} else if (proc.getMajorFaults() != 0) {
object.put( "process", proc.getProcessID() );
object.put( "fault", proc.getMajorFaults() );
//let the SC know this
} else if (proc.getMinorFaults() >= 100000) {
countingprocessminor++;
//let the SC know this
} else if ((proc.getBitness() != (64)) && (proc.getBitness() != (32)) && (proc.getBitness() != (0))) {
object.put( "process", proc.getProcessID() );
object.put( "bitness", proc.getBitness() );
//let the SC know this
} else if (proc.getState().equals( "INVALID" )) {
object.put( "process", proc.getProcessID() );
object.put( "state", proc.getState() );
//let the SC know this
} else if (!(proc.getPriority() >= (-20) && proc.getPriority() <= 128)) {
object.put( "process", proc.getProcessID() );
object.put( "priority", proc.getPriority() );
//let the SC know
} else if (proc.getCurrentWorkingDirectory() == null) {
countProc++;
} else { }
}
object.put( "processCount", si.getOperatingSystem().getProcessCount() );
//below are the errors to be reported to SC for further analysis - cross check with the learning layer
if (countingprocessminor >= 0) {
object.put( "moinorErrors", countingprocessminor );
object.put( "message", "Processes with minor faults greater than 100000" );
} else { }
if (countProc >= 0) {
object.put( "nullDirectory", countProc );
object.put( "message", "Processes with current directory as null" );
} else { }
return object;
}
}
package com.rubix.WAMPAC.NMSCollection;
/**
* The code is run to check if its a Desktop or Battery powered system
* Runs when system switches on
* Pushes output to console
* If a battery powered system, trigger SC to alert system is not DESKTOP
*/
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import oshi.SystemInfo;
import oshi.software.os.OSSession;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Date;
public class Sessions {
public static DecimalFormat df2 = new DecimalFormat( "#.##" );
private static JSONArray resultArray = new JSONArray( );
private static final DateTimeFormatter LOGIN_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
public static JSONArray sessionInfo() throws JSONException {
resultArray = new JSONArray( );
SystemInfo si = new SystemInfo();
df2.setRoundingMode( RoundingMode.UP );
for (OSSession sess : si.getOperatingSystem().getSessions()) {
Date date = new Date(sess.getLoginTime());
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
JSONObject object = new JSONObject( );
object.put( "userName", sess.getUserName() );
object.put( "hostname", sess.getHost() );
if(sess.getLoginTime()!=0)
{
object.put("login", dateFormat.format( date ) );
object.put("category", "1");
}else{
object.put( "login", "No Login" );
object.put( "category", "0" );
}
resultArray.put( object );
}
return resultArray;
}
}
package com.rubix.WAMPAC.NMSCollection;
import org.json.JSONException;
import org.json.JSONObject;
import oshi.software.os.InternetProtocolStats;
import oshi.software.os.windows.WindowsInternetProtocolStats;
public class TCPandUDPStats {
private static JSONObject object = new JSONObject( );
public static JSONObject tcpudpStats() throws JSONException {
WindowsInternetProtocolStats winISP = new WindowsInternetProtocolStats();
InternetProtocolStats.TcpStats Tcp = winISP.getTCPv4Stats();
InternetProtocolStats.UdpStats Udp = winISP.getUDPv4Stats();
object.put( "tcpInErrors", Tcp.getInErrors() );
object.put("tcpOutResets", Tcp.getOutResets());
object.put( "tcpConnectionFailures", Tcp.getConnectionFailures() );
object.put( "tcpConnectionReset", Tcp.getConnectionsReset() );
object.put( "tcpActiveConnection", Tcp.getConnectionsActive() );
object.put( "tcpEstablishedConnection", Tcp.getConnectionsEstablished() );
object.put( "tcpPassiveConnection", Tcp.getConnectionsPassive() );
object.put( "tcpSegmentReceived", Tcp.getSegmentsReceived() );
object.put( "tcpSegmentSent", Tcp.getSegmentsSent() );
object.put( "tcpSegmentRetransmitted", Tcp.getSegmentsRetransmitted() );
object.put( "udpDatagramReceived", Udp.getDatagramsReceived() );
object.put( "udpDatagramSent", Udp.getDatagramsSent() );
object.put( "udpDatagramReceivedError", Udp.getDatagramsReceivedErrors() );
object.put( "udpDatagramNoPort", Udp.getDatagramsNoPort() );
return object;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment