use of fvarrui.sysadmin.challenger.utils.Chronometer in project Challenger4SysAdmins by fvarrui.
the class Command method execute.
public ExecutionResult execute(boolean waitFor, String... params) {
final ExecutionResult result = new ExecutionResult();
result.setExecutionTime(LocalDateTime.now());
result.setExecutedCommand(prepareCommand(params));
result.setParams(StringUtils.join(params, " "));
try {
Chronometer chrono = new Chronometer();
String[] splittedCommand = result.getExecutedCommand().split("[ ]+");
ProcessBuilder pb = new ProcessBuilder();
pb.command(splittedCommand);
pb.redirectErrorStream(true);
Process p = pb.start();
result.setOutputStream(p.getInputStream());
result.setErrorStream(p.getErrorStream());
if (waitFor) {
result.setOutput(IOUtils.toString(p.getInputStream(), Charset.defaultCharset()).trim());
result.setError(IOUtils.toString(p.getErrorStream(), Charset.defaultCharset()).trim());
result.setReturnValue(p.waitFor());
} else {
new Thread(() -> {
try {
result.setReturnValue(p.waitFor());
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
}
p.getOutputStream().flush();
p.getOutputStream().close();
chrono.stop();
result.setDuration(Duration.ofMillis(chrono.getDiff()));
} catch (Exception e) {
result.setError(e.getMessage());
result.setReturnValue(-1);
e.printStackTrace();
} finally {
this.result.set(result);
}
return result;
}
use of fvarrui.sysadmin.challenger.utils.Chronometer in project Challenger4SysAdmins by fvarrui.
the class PSMonitor method doWork.
@Override
public void doWork() {
String resolveUsernameCommand = "";
Command resolveUsername = new PSCommand("(Get-LocalUser | Where SID -eq '%s').Name");
ZonedDateTime dateTime = ZonedDateTime.now(ZoneOffset.UTC);
Chronometer chrono = new Chronometer();
do {
chrono.init();
ExecutionResult result = command.execute(dateTime.toString());
if (!result.getOutput().isEmpty()) {
String xml = "<Events>" + result.getOutput() + "</Events>";
Document doc = XMLUtils.stringToDocument(xml);
NodeList nodes = doc.getElementsByTagName("Event");
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
String command = XMLUtils.searchText(node, "EventData/Data[@Name='ScriptBlockText']");
String userId = XMLUtils.searchAttribute(node, "System/Security", "UserID");
String xmlDateTime = XMLUtils.searchAttribute(node, "System/TimeCreated", "SystemTime");
ZonedDateTime timestamp = DateTimeUtils.xmlInstantToZonedDateTime(xmlDateTime);
if (!getExcludedCommands().contains(command) && !command.equals(resolveUsernameCommand)) {
ExecutionResult usernameResult = resolveUsername.execute(userId);
resolveUsernameCommand = usernameResult.getParams();
String username = usernameResult.getOutput();
Map<String, Object> data = new HashMap<>();
data.put(COMMAND, command);
data.put(USERNAME, username);
data.put(TIMESTAMP, LocalDateTime.ofInstant(timestamp.toInstant(), ZoneId.systemDefault()));
notifyAll(data);
}
dateTime = timestamp;
}
}
chrono.stop();
Sleep.millis(delay - chrono.getDiff());
} while (!isStopped());
}
Aggregations