11package io .sentrius .agent .analysis .agents .sessions ;
22
33import java .sql .Timestamp ;
4+ import java .util .ArrayList ;
45import java .util .Arrays ;
56import java .util .List ;
67import java .util .Set ;
8+ import java .util .regex .Matcher ;
9+ import java .util .regex .Pattern ;
710import java .util .stream .Collectors ;
811import io .sentrius .sso .core .model .metadata .AnalyticsTracking ;
912import io .sentrius .sso .core .model .metadata .TerminalBehaviorMetrics ;
@@ -55,13 +58,13 @@ public void processSessions() {
5558 try {
5659 processSession (session );
5760 // ACTIVE -> INACTIVE -> CLOSED -> PROCESSED
58- saveToTracking (session .getId (), "PROCESSED" );
61+ // saveToTracking(session.getId(), "PROCESSED");
5962 } catch (Exception e ) {
6063 log .error ("Error processing session {}: {}" , session .getId (), e .getMessage (), e );
6164 saveToTracking (session .getId (), "ERROR" );
6265 }
63- session .setSessionStatus ("PROCESSED" );
64- sessionMetadataService .saveSession (session );
66+ // session.setSessionStatus("PROCESSED");
67+ // sessionMetadataService.saveSession(session);
6568 }
6669 }
6770
@@ -72,8 +75,10 @@ private void processSession(TerminalSessionMetadata session) {
7275 terminalLogs = List .of (); // Ensure it's not null
7376 }
7477
78+ TerminalLogs previousLog = null ;
7579 for (TerminalLogs terminalLog : terminalLogs ) {
76- parseAndSaveCommands (terminalLog , session );
80+ parseAndSaveCommands (previousLog , terminalLog , session );
81+ previousLog = terminalLog ;
7782 }
7883
7984 List <TerminalCommand > commands = commandService .getCommandsBySessionId (session .getId ());
@@ -99,22 +104,77 @@ private void saveToTracking(Long sessionId, String status) {
99104 trackingRepository .save (tracking );
100105 }
101106
102- public List <TerminalCommand > parseAndSaveCommands (
103- TerminalLogs terminalLog ,
104- TerminalSessionMetadata sessionMetadata ) {
107+ public List <TerminalCommand > parseAndSaveCommands (
108+ TerminalLogs previousLog ,
109+ TerminalLogs terminalLog , TerminalSessionMetadata sessionMetadata ) {
110+ SessionAnalyticsAgent .log .info ("Parsing and saving commands from terminal log: {}" , terminalLog .getOutput ());
105111 // Split output into individual commands (Assume each command ends with a newline or specific delimiter)
106- String [] commands = terminalLog .getOutput ().split ("\r \n |\r |\n " );
112+ //String[] commands = terminalLog.getOutput().split("\r\n|\r|\n");
113+ String [] commands = terminalLog .getOutput ().split ("\r " );
107114
108115 // Parse each command
116+ List <TerminalCommand > terminalCommands = new ArrayList <>();
117+ for (int i = 0 ; i < commands .length ; i ++) {
118+ var command = commands [i ];
119+ var cmd = extractCommand (i == 0 ? previousLog : null , command .trim ());
120+ if (!cmd .isEmpty ()) {
121+ terminalCommands .add (createTerminalCommand (cmd , terminalLog , sessionMetadata ));
122+ }
123+ }
124+ /*
109125 List<TerminalCommand> terminalCommands = Arrays.stream(commands)
110- .filter (command -> !command .trim ().isEmpty ()) // Skip empty lines
111- .map (command -> createTerminalCommand (command , terminalLog , sessionMetadata ))
126+ .filter(command -> !extractCommand(previousLog, command.trim() ).isEmpty()) // Skip empty lines
127+ .map(command -> createTerminalCommand(extractCommand(previousLog, command) , terminalLog, sessionMetadata))
112128 .collect(Collectors.toList());
113-
129+ */
114130 // Save commands to the database
115131 return commandService .saveAll (terminalCommands );
116132 }
117133
134+ public static String extractCommand (TerminalLogs previousLog , String logLine ) {
135+ // Remove ANSI escape sequences
136+ log .info ("Cleaning log line: {}" , logLine );
137+ String cleanedLog = logLine .replaceAll ("\u001B \\ [[;\\ d]*m" , "" ).replaceAll ("\u001B \\ [\\ ?\\ d+h" , "" );
138+
139+ // Define regex to match the prompt and capture the command
140+ // This assumes the prompt ends with `$` or `#`, followed by a space and the command
141+ Pattern pattern = Pattern .compile (".*[#$] (.+)$" );
142+ Matcher matcher = pattern .matcher (cleanedLog );
143+
144+ if (matcher .find ()) {
145+ log .info ("Extracted command: {}" , matcher .group (1 ).trim ());
146+ return matcher .group (1 ).trim ();
147+ } else {
148+ if (null != previousLog ) {
149+ log .info ("Previous log: {}" , previousLog .getOutput ());
150+ // it could be that we are at the beginning of the log set.
151+ String lastLogLine = getLastLogLine (previousLog );
152+ if (!lastLogLine .isEmpty ()) {
153+ log .info ("Last log line: {}" , lastLogLine );
154+ logLine = lastLogLine + logLine ;
155+ matcher = pattern .matcher (logLine );
156+ if (matcher .find ()) {
157+ log .info ("Extracted command from last log line: {}" , matcher .group (1 ).trim ());
158+ return matcher .group (1 ).trim ();
159+ }
160+ }
161+
162+ }
163+ log .info ("No command found in log line: {}" , logLine );
164+ // Return an empty string if no command is found
165+ return "" ;
166+ }
167+ }
168+
169+ private static String getLastLogLine (TerminalLogs previousLog ) {
170+ if (previousLog == null ) {
171+ return "" ;
172+ }
173+
174+ String [] lines = previousLog .getOutput ().split ("\r " );
175+ return lines [lines .length - 1 ];
176+ }
177+
118178 private TerminalCommand createTerminalCommand (String command , TerminalLogs terminalLog , TerminalSessionMetadata sessionMetadata ) {
119179 TerminalCommand terminalCommand = new TerminalCommand ();
120180 terminalCommand .setCommand (command .trim ());
0 commit comments