Skip to content

Commit d93ef19

Browse files
committed
parsing syslog levels - done
1 parent 2a54960 commit d93ef19

File tree

7 files changed

+133
-5
lines changed

7 files changed

+133
-5
lines changed

pom.xml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@
7474
<groupId>io.quarkus</groupId>
7575
<artifactId>quarkus-elytron-security-properties-file</artifactId>
7676
</dependency>
77+
<dependency>
78+
<groupId>org.antlr</groupId>
79+
<artifactId>antlr4-runtime</artifactId>
80+
<version>4.13.0</version>
81+
</dependency>
7782
<!-- SMB Support -->
7883
<dependency>
7984
<groupId>eu.agno3.jcifs</groupId>
@@ -101,6 +106,28 @@
101106

102107
<build>
103108
<plugins>
109+
<plugin>
110+
<groupId>org.antlr</groupId>
111+
<artifactId>antlr4-maven-plugin</artifactId>
112+
<version>4.13.0</version>
113+
<executions>
114+
<execution>
115+
<id>generate-antlr-sources</id>
116+
<phase>generate-sources</phase>
117+
<goals>
118+
<goal>antlr4</goal>
119+
</goals>
120+
<configuration>
121+
<outputDirectory>${project.build.directory}/generated-sources/antlr</outputDirectory>
122+
<addOutputDirectory>true</addOutputDirectory>
123+
<arguments>
124+
<argument>-package</argument>
125+
<argument>com.krusty84.syslog.antlr</argument>
126+
</arguments>
127+
</configuration>
128+
</execution>
129+
</executions>
130+
</plugin>
104131
<plugin>
105132
<groupId>${quarkus.platform.group-id}</groupId>
106133
<artifactId>quarkus-maven-plugin</artifactId>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
lexer grammar TcPulseSyslogLexer;
2+
3+
LEVEL : 'INFO' | 'DEBUG' | 'NOTE' | 'WARN' | 'ERROR' | 'FATAL';
4+
NEWLINE : '\r'? '\n' -> skip;
5+
WS : [ \t]+ -> skip;
6+
OTHER : . -> skip;

src/main/java/com/krusty84/DatabaseService.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class DatabaseService {
2525

2626
private final PmProcessListRepository processListRepo;
2727
private final PpomSessionRepository sessionRepo;
28+
private final SyslogLevelCounter syslogLevelCounter;
2829

2930
@ConfigProperty(name = "syslog.base.path", defaultValue = "/")
3031
String syslogBasePath;
@@ -47,9 +48,12 @@ public class DatabaseService {
4748
@ConfigProperty(name = "syslog.smb.password", defaultValue = "")
4849
String smbPassword;
4950

50-
public DatabaseService(PmProcessListRepository processListRepo, PpomSessionRepository sessionRepo) {
51+
public DatabaseService(PmProcessListRepository processListRepo,
52+
PpomSessionRepository sessionRepo,
53+
SyslogLevelCounter syslogLevelCounter) {
5154
this.processListRepo = processListRepo;
5255
this.sessionRepo = sessionRepo;
56+
this.syslogLevelCounter = syslogLevelCounter;
5357
}
5458

5559
@Transactional
@@ -81,8 +85,22 @@ private LoggedUserData loggedJoinedData(Object[] row) {
8185
String syslogPath = (String) row[4];
8286

8387
String loginDateIso = loginDate != null ? DateTimeFormatter.ISO_INSTANT.format(loginDate.toInstant()) : null;
88+
SyslogLevelStats levels = resolveSyslogLevels(syslogPath);
8489

85-
return new LoggedUserData(processId, userName, nodeName, loginDateIso, syslogPath);
90+
return new LoggedUserData(processId, userName, nodeName, loginDateIso, syslogPath, levels);
91+
}
92+
93+
private SyslogLevelStats resolveSyslogLevels(String syslogPath) {
94+
if (syslogPath == null || syslogPath.isBlank()) {
95+
return SyslogLevelStats.empty();
96+
}
97+
98+
try (InputStream stream = downloadSyslogFile(syslogPath)) {
99+
return syslogLevelCounter.countLevels(stream);
100+
} catch (Exception e) {
101+
System.out.println("[DatabaseService] Unable to analyze syslog '" + syslogPath + "': " + e.getMessage());
102+
return SyslogLevelStats.empty();
103+
}
86104
}
87105

88106
public InputStream getFileStream(String pmSyslogPath, long start, long length) throws Exception {

src/main/java/com/krusty84/LoggedUserData.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ public record LoggedUserData(
55
String userName,
66
String nodeName,
77
String loginDateUtc,
8-
String syslogPath
8+
String syslogPath,
9+
SyslogLevelStats levels
910
) {
10-
}
11+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.krusty84;
2+
3+
import com.krusty84.syslog.antlr.TcPulseSyslogLexer;
4+
import jakarta.enterprise.context.ApplicationScoped;
5+
import org.antlr.v4.runtime.CharStream;
6+
import org.antlr.v4.runtime.CharStreams;
7+
import org.antlr.v4.runtime.CommonTokenStream;
8+
import org.antlr.v4.runtime.Token;
9+
10+
import java.io.IOException;
11+
import java.io.InputStream;
12+
import java.nio.charset.StandardCharsets;
13+
import java.util.List;
14+
15+
@ApplicationScoped
16+
public class SyslogLevelCounter {
17+
18+
public SyslogLevelStats countLevels(InputStream stream) {
19+
if (stream == null) {
20+
return SyslogLevelStats.empty();
21+
}
22+
23+
try {
24+
CharStream charStream = CharStreams.fromStream(stream, StandardCharsets.UTF_8);
25+
TcPulseSyslogLexer lexer = new TcPulseSyslogLexer(charStream);
26+
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
27+
tokenStream.fill();
28+
29+
long info = 0;
30+
long debug = 0;
31+
long note = 0;
32+
long warn = 0;
33+
long error = 0;
34+
long fatal = 0;
35+
36+
List<Token> tokens = tokenStream.getTokens();
37+
for (Token token : tokens) {
38+
if (token.getType() == TcPulseSyslogLexer.LEVEL) {
39+
String text = token.getText();
40+
if (text == null) {
41+
continue;
42+
}
43+
switch (text) {
44+
case "INFO" -> info++;
45+
case "DEBUG" -> debug++;
46+
case "NOTE" -> note++;
47+
case "WARN" -> warn++;
48+
case "ERROR" -> error++;
49+
case "FATAL" -> fatal++;
50+
default -> {
51+
// ignore unexpected values
52+
}
53+
}
54+
}
55+
}
56+
57+
return new SyslogLevelStats(info, debug, note, warn, error, fatal);
58+
} catch (IOException e) {
59+
System.out.println("[TCPulse] Failed to parse syslog: " + e.getMessage());
60+
return SyslogLevelStats.empty();
61+
}
62+
}
63+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.krusty84;
2+
3+
public record SyslogLevelStats(long info,
4+
long debug,
5+
long note,
6+
long warn,
7+
long error,
8+
long fatal) {
9+
10+
public static SyslogLevelStats empty() {
11+
return new SyslogLevelStats(0, 0, 0, 0, 0, 0);
12+
}
13+
}

src/main/resources/application.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ quarkus.http.auth.basic=true
1111

1212
# Teamcenter based on PostgreSQL DB
1313
quarkus.datasource.db-kind=postgresql
14-
quarkus.datasource.jdbc.url=jdbc:postgresql://51.250.111.246:5432/tc
14+
quarkus.datasource.jdbc.url=jdbc:postgresql://89.169.183.86:5432/tc
1515
quarkus.datasource.username=infodba
1616
quarkus.datasource.password=infodba
1717
# Minimum server version Quarkus expects (override QUARKUS_DB_VERSION env var to match your server or switch to Oracle)

0 commit comments

Comments
 (0)