-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWebSphereAuditMonitor.java
More file actions
843 lines (705 loc) · 32.8 KB
/
WebSphereAuditMonitor.java
File metadata and controls
843 lines (705 loc) · 32.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
/*******************************************************************************
* Copyright (c) 2026 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
import java.io.*;
import java.nio.file.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.*;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
/**
* Utility method for Java 8 compatibility
*/
class StringUtils {
static String repeat(String str, int count) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < count; i++) {
sb.append(str);
}
return sb.toString();
}
}
/**
* WebSphere Configuration Audit Monitor
*
* Usage: java WebSphereAuditMonitor [config.properties]
*
* Arguments:
* - config.properties: Configuration file path (optional, defaults to config.properties)
*
* Configuration properties:
* - checkpoint.directory=/dmgr/config/temp/download/cells/was90cell/repository/checkpoints
* - wsadmin.path=/opt/IBM/WebSphere/AppServer/bin
* - audit.log.path=./audit.log
* - schedule.interval.minutes=60
* - last.processed.timestamp.file=.last_processed_timestamp
*/
public class WebSphereAuditMonitor {
private static final String DEFAULT_CONFIG = "config.properties";
private Properties config;
private String checkpointDir;
private String wsadminPath;
private String auditLogPath;
private int scheduleIntervalMinutes;
private String lastProcessedTimestampFile;
private long lastProcessedTimestamp = 0;
private long maxLogSizeMB;
private int maxLogFiles;
private String archivedLogsDir;
public WebSphereAuditMonitor(String configFile) throws IOException {
loadConfiguration(configFile);
loadLastProcessedTimestamp();
}
private void loadConfiguration(String configFile) throws IOException {
config = new Properties();
File file = new File(configFile);
if (!file.exists()) {
// Create default configuration file
createDefaultConfig(configFile);
System.out.println("Created default configuration file: " + configFile);
System.out.println("Please update the configuration and run again.");
System.exit(0);
}
try (FileInputStream fis = new FileInputStream(file)) {
config.load(fis);
}
checkpointDir = config.getProperty("checkpoint.directory",
"/dmgr/config/temp/download/cells/was90cell/repository/checkpoints");
wsadminPath = config.getProperty("wsadmin.path", "/opt/IBM/WebSphere/AppServer/bin");
auditLogPath = config.getProperty("audit.log.path", "./audit.log");
scheduleIntervalMinutes = Integer.parseInt(config.getProperty("schedule.interval.minutes", "60"));
lastProcessedTimestampFile = config.getProperty("last.processed.timestamp.file", ".last_processed_timestamp");
maxLogSizeMB = Long.parseLong(config.getProperty("audit.log.max.size.mb", "5"));
maxLogFiles = Integer.parseInt(config.getProperty("audit.log.max.files", "10"));
archivedLogsDir = config.getProperty("audit.log.archive.dir", "./archived_logs");
}
private void createDefaultConfig(String configFile) throws IOException {
Properties defaultConfig = new Properties();
defaultConfig.setProperty("checkpoint.directory", "/dmgr/config/temp/download/cells/was90cell/repository/checkpoints");
defaultConfig.setProperty("wsadmin.path", "/opt/IBM/WebSphere/AppServer/bin");
defaultConfig.setProperty("audit.log.path", "./audit.log");
defaultConfig.setProperty("schedule.interval.minutes", "60");
defaultConfig.setProperty("last.processed.timestamp.file", ".last_processed_timestamp");
defaultConfig.setProperty("audit.log.max.size.mb", "5");
defaultConfig.setProperty("audit.log.max.files", "10");
defaultConfig.setProperty("audit.log.archive.dir", "./archived_logs");
try (FileOutputStream fos = new FileOutputStream(configFile)) {
defaultConfig.store(fos, "WebSphere Audit Monitor Configuration");
}
}
private void loadLastProcessedTimestamp() {
File timestampFile = new File(lastProcessedTimestampFile);
if (timestampFile.exists()) {
try (BufferedReader reader = new BufferedReader(new FileReader(timestampFile))) {
String line = reader.readLine();
if (line != null && !line.trim().isEmpty()) {
lastProcessedTimestamp = Long.parseLong(line.trim());
System.out.println("Loaded last processed timestamp: " + lastProcessedTimestamp);
}
} catch (Exception e) {
System.err.println("Error loading last processed timestamp: " + e.getMessage());
}
}
}
private void saveLastProcessedTimestamp(long timestamp) {
try (PrintWriter writer = new PrintWriter(new FileWriter(lastProcessedTimestampFile))) {
writer.println(timestamp);
lastProcessedTimestamp = timestamp;
System.out.println("Saved last processed timestamp: " + timestamp);
} catch (IOException e) {
System.err.println("Error saving last processed timestamp: " + e.getMessage());
}
}
public void start() {
System.out.println("WebSphere Audit Monitor started");
System.out.println("Checkpoint Directory: " + checkpointDir);
System.out.println("Schedule Interval: " + scheduleIntervalMinutes + " minutes");
System.out.println("Audit Log: " + auditLogPath);
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
// Run immediately on start
processCheckpoints();
// Schedule periodic execution
scheduler.scheduleAtFixedRate(() -> {
try {
processCheckpoints();
} catch (Exception e) {
System.err.println("Error in scheduled execution: " + e.getMessage());
e.printStackTrace();
}
}, scheduleIntervalMinutes, scheduleIntervalMinutes, TimeUnit.MINUTES);
// Keep the program running
try {
Thread.currentThread().join();
} catch (InterruptedException e) {
scheduler.shutdown();
}
}
private void processCheckpoints() {
System.out.println("\n=== Processing Checkpoints at " + new Date() + " ===");
try {
// Step 1: Scan checkpoint directory
List<DeltaDirectory> deltaDirectories = scanCheckpointDirectory();
if (deltaDirectories.isEmpty()) {
System.out.println("No new delta directories found.");
return;
}
// Step 2: Filter directories based on last processed timestamp
List<DeltaDirectory> newDirectories = deltaDirectories.stream()
.filter(d -> d.timestamp > lastProcessedTimestamp)
.sorted(Comparator.comparingLong(d -> d.timestamp))
.collect(Collectors.toList());
if (newDirectories.isEmpty()) {
System.out.println("No new directories to process.");
return;
}
System.out.println("Found " + newDirectories.size() + " new delta directories to process");
// Step 3: Process each delta directory
List<AuditEntry> auditEntries = new ArrayList<>();
long maxTimestamp = lastProcessedTimestamp;
for (DeltaDirectory deltaDir : newDirectories) {
System.out.println("\nProcessing: " + deltaDir.name);
try {
// Extract checkpoint to zip file
File zipFile = extractCheckpoint(deltaDir);
if (zipFile != null && zipFile.exists()) {
// Process the zip file
List<AuditEntry> entries = processZipFile(zipFile, deltaDir);
auditEntries.addAll(entries);
// Clean up zip file
zipFile.delete();
}
maxTimestamp = Math.max(maxTimestamp, deltaDir.timestamp);
} catch (Exception e) {
System.err.println("Error processing " + deltaDir.name + ": " + e.getMessage());
e.printStackTrace();
}
}
// Step 4: Write audit log
if (!auditEntries.isEmpty()) {
writeAuditLog(auditEntries);
saveLastProcessedTimestamp(maxTimestamp);
}
System.out.println("\n=== Processing Complete ===");
} catch (Exception e) {
System.err.println("Error in processCheckpoints: " + e.getMessage());
e.printStackTrace();
}
}
private List<DeltaDirectory> scanCheckpointDirectory() throws IOException {
List<DeltaDirectory> deltaDirectories = new ArrayList<>();
File checkpointDirFile = new File(checkpointDir);
if (!checkpointDirFile.exists() || !checkpointDirFile.isDirectory()) {
System.err.println("Checkpoint directory does not exist: " + checkpointDir);
return deltaDirectories;
}
File[] files = checkpointDirFile.listFiles();
if (files == null) {
return deltaDirectories;
}
for (File file : files) {
if (file.isDirectory() && file.getName().startsWith("Delta-")) {
try {
String timestampStr = file.getName().substring(6); // Remove "Delta-"
long timestamp = Long.parseLong(timestampStr);
deltaDirectories.add(new DeltaDirectory(file.getName(), timestamp, file.getAbsolutePath()));
} catch (NumberFormatException e) {
System.err.println("Invalid delta directory name: " + file.getName());
}
}
}
return deltaDirectories;
}
private File extractCheckpoint(DeltaDirectory deltaDir) {
try {
// Create Python script
File scriptFile = createWsadminScript(deltaDir.name);
// Execute wsadmin command
String wsadminScript = wsadminPath + "/wsadmin.sh";
String zipFileName = deltaDir.name + ".zip";
System.out.println(" Python script path: " + scriptFile.getAbsolutePath());
ProcessBuilder pb = new ProcessBuilder(
wsadminScript,
"-conntype", "NONE",
"-f", scriptFile.getAbsolutePath()
);
pb.redirectErrorStream(true);
Process process = pb.start();
// Read output
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(" wsadmin: " + line);
}
int exitCode = process.waitFor();
scriptFile.delete();
if (exitCode == 0) {
File zipFile = new File(zipFileName);
if (zipFile.exists()) {
System.out.println(" Successfully extracted checkpoint to: " + zipFileName);
return zipFile;
} else {
System.err.println(" Zip file not created: " + zipFileName);
}
} else {
System.err.println(" wsadmin command failed with exit code: " + exitCode);
}
} catch (Exception e) {
System.err.println("Error extracting checkpoint: " + e.getMessage());
e.printStackTrace();
}
return null;
}
private File createWsadminScript(String checkpointName) throws IOException {
File scriptFile = File.createTempFile("extract_checkpoint_", ".py");
try (PrintWriter writer = new PrintWriter(new FileWriter(scriptFile))) {
writer.println("# WebSphere Checkpoint Extraction Script");
writer.println("checkpointName = '" + checkpointName + "'");
writer.println("zipFileName = checkpointName + '.zip'");
writer.println("AdminTask.extractRepositoryCheckpoint('[-checkpointName ' + checkpointName + ' -extractToFile ' + zipFileName + ']')");
writer.println("print 'Checkpoint extracted to: ' + zipFileName");
}
return scriptFile;
}
private List<AuditEntry> processZipFile(File zipFile, DeltaDirectory deltaDir) throws IOException {
List<AuditEntry> auditEntries = new ArrayList<>();
// Create temp directory for extraction
File tempDir = Files.createTempDirectory("checkpoint_").toFile();
try {
// Unzip the file
unzip(zipFile, tempDir);
// Look for user.id file
File userIdFile = new File(tempDir, "user.id");
String userId = "Unknown";
if (userIdFile.exists()) {
userId = new String(Files.readAllBytes(userIdFile.toPath())).trim();
}
// Process before and after directories
File beforeDir = new File(tempDir, "before");
File afterDir = new File(tempDir, "after");
if (beforeDir.exists() && afterDir.exists()) {
Map<String, File> beforeFiles = getFilesMap(beforeDir);
Map<String, File> afterFiles = getFilesMap(afterDir);
// Find all unique file paths
Set<String> allPaths = new HashSet<>();
allPaths.addAll(beforeFiles.keySet());
allPaths.addAll(afterFiles.keySet());
for (String path : allPaths) {
File beforeFile = beforeFiles.get(path);
File afterFile = afterFiles.get(path);
String changeType;
List<String> differences = new ArrayList<>();
if (beforeFile == null && afterFile != null) {
changeType = "ADDED";
differences.add("File created");
} else if (beforeFile != null && afterFile == null) {
changeType = "DELETED";
differences.add("File deleted");
} else {
differences = compareFiles(beforeFile, afterFile);
if (differences.isEmpty()) {
continue; // No actual changes
}
// Determine change type based on the differences found
changeType = determineChangeType(differences);
}
AuditEntry entry = new AuditEntry(
deltaDir.timestamp,
userId,
path,
changeType,
differences
);
auditEntries.add(entry);
}
}
} finally {
// Clean up temp directory
deleteDirectory(tempDir);
}
return auditEntries;
}
private void unzip(File zipFile, File destDir) throws IOException {
byte[] buffer = new byte[1024];
try (ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile))) {
ZipEntry zipEntry = zis.getNextEntry();
while (zipEntry != null) {
File newFile = new File(destDir, zipEntry.getName());
if (zipEntry.isDirectory()) {
newFile.mkdirs();
} else {
// Create parent directories
new File(newFile.getParent()).mkdirs();
try (FileOutputStream fos = new FileOutputStream(newFile)) {
int len;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
}
}
zipEntry = zis.getNextEntry();
}
zis.closeEntry();
}
}
private Map<String, File> getFilesMap(File directory) {
Map<String, File> filesMap = new HashMap<>();
collectFiles(directory, directory, filesMap);
return filesMap;
}
private void collectFiles(File rootDir, File currentDir, Map<String, File> filesMap) {
File[] files = currentDir.listFiles();
if (files == null) return;
for (File file : files) {
if (file.isDirectory()) {
collectFiles(rootDir, file, filesMap);
} else {
String relativePath = rootDir.toPath().relativize(file.toPath()).toString();
filesMap.put(relativePath, file);
}
}
}
/**
* Determine the overall change type based on the differences found
*/
private String determineChangeType(List<String> differences) {
boolean hasAdded = false;
boolean hasDeleted = false;
boolean hasModified = false;
for (String diff : differences) {
if (diff.contains(" added:")) {
hasAdded = true;
} else if (diff.contains(" deleted:")) {
hasDeleted = true;
} else if (diff.contains(" modified:")) {
hasModified = true;
}
}
// If only additions, mark as ADDED
if (hasAdded && !hasDeleted && !hasModified) {
return "ADDED";
}
// If only deletions, mark as DELETED
else if (hasDeleted && !hasAdded && !hasModified) {
return "DELETED";
}
// Otherwise it's a modification (or mixed changes)
else {
return "MODIFIED";
}
}
private List<String> compareFiles(File beforeFile, File afterFile) throws IOException {
List<String> differences = new ArrayList<>();
List<String> beforeLines = Files.readAllLines(beforeFile.toPath());
List<String> afterLines = Files.readAllLines(afterFile.toPath());
// Use a more sophisticated diff algorithm
List<DiffEntry> diffEntries = computeDiff(beforeLines, afterLines);
for (DiffEntry entry : diffEntries) {
switch (entry.type) {
case ADDED:
differences.add("Line " + entry.lineNumber + " added: " + entry.content);
break;
case DELETED:
differences.add("Line " + entry.lineNumber + " deleted: " + entry.content);
break;
case MODIFIED:
differences.add("Line " + entry.lineNumber + " modified:");
differences.add(" Before: " + entry.beforeContent);
differences.add(" After: " + entry.content);
break;
}
}
return differences;
}
private List<DiffEntry> computeDiff(List<String> beforeLines, List<String> afterLines) {
List<DiffEntry> diffEntries = new ArrayList<>();
// Use LCS (Longest Common Subsequence) based diff algorithm
int[][] lcs = computeLCS(beforeLines, afterLines);
// Backtrack through LCS to identify changes
int i = beforeLines.size();
int j = afterLines.size();
while (i > 0 || j > 0) {
if (i > 0 && j > 0 && beforeLines.get(i - 1).equals(afterLines.get(j - 1))) {
// Lines are the same, move diagonally
i--;
j--;
} else if (j > 0 && (i == 0 || lcs[i][j - 1] >= lcs[i - 1][j])) {
// Line was added in after
diffEntries.add(0, new DiffEntry(DiffType.ADDED, j, afterLines.get(j - 1), null));
j--;
} else if (i > 0 && (j == 0 || lcs[i][j - 1] < lcs[i - 1][j])) {
// Line was deleted from before
diffEntries.add(0, new DiffEntry(DiffType.DELETED, i, beforeLines.get(i - 1), null));
i--;
}
}
// Now check for modifications (lines at same position but different content)
// This is a second pass to catch true modifications vs add/delete pairs
List<DiffEntry> refinedEntries = new ArrayList<>();
for (int k = 0; k < diffEntries.size(); k++) {
DiffEntry current = diffEntries.get(k);
// Look for adjacent delete+add which might be a modification
if (current.type == DiffType.DELETED && k + 1 < diffEntries.size()) {
DiffEntry next = diffEntries.get(k + 1);
if (next.type == DiffType.ADDED &&
Math.abs(current.lineNumber - next.lineNumber) <= 1) {
// This is likely a modification
refinedEntries.add(new DiffEntry(DiffType.MODIFIED, next.lineNumber,
next.content, current.content));
k++; // Skip the next entry as we've combined them
continue;
}
}
refinedEntries.add(current);
}
return refinedEntries;
}
/**
* Compute Longest Common Subsequence (LCS) matrix
* This helps identify which lines are common between before and after
*/
private int[][] computeLCS(List<String> beforeLines, List<String> afterLines) {
int m = beforeLines.size();
int n = afterLines.size();
int[][] lcs = new int[m + 1][n + 1];
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (beforeLines.get(i - 1).equals(afterLines.get(j - 1))) {
lcs[i][j] = lcs[i - 1][j - 1] + 1;
} else {
lcs[i][j] = Math.max(lcs[i - 1][j], lcs[i][j - 1]);
}
}
}
return lcs;
}
enum DiffType {
ADDED, DELETED, MODIFIED
}
static class DiffEntry {
DiffType type;
int lineNumber;
String content;
String beforeContent;
DiffEntry(DiffType type, int lineNumber, String content, String beforeContent) {
this.type = type;
this.lineNumber = lineNumber;
this.content = content;
this.beforeContent = beforeContent;
}
}
private void writeAuditLog(List<AuditEntry> auditEntries) {
// Check if log rolling is needed before writing
rollLogFileIfNeeded();
// Sort by timestamp
auditEntries.sort(Comparator.comparingLong(e -> e.timestamp));
try (PrintWriter writer = new PrintWriter(new FileWriter(auditLogPath, true))) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
writer.println("\n" + StringUtils.repeat("=", 80));
writer.println("Audit Log Entry - " + sdf.format(new Date()));
writer.println(StringUtils.repeat("=", 80));
for (AuditEntry entry : auditEntries) {
writer.println("\nTimestamp: " + sdf.format(new Date(entry.timestamp)));
writer.println("User: " + entry.userId);
writer.println("Cell: " + entry.cellName);
writer.println("File: " + entry.filePath);
writer.println("Change Type: " + entry.changeType);
writer.println("Changes:");
for (String diff : entry.differences) {
writer.println(" " + diff);
}
writer.println(StringUtils.repeat("-", 80));
}
System.out.println("Audit log updated: " + auditLogPath);
System.out.println("Total changes logged: " + auditEntries.size());
} catch (IOException e) {
System.err.println("Error writing audit log: " + e.getMessage());
e.printStackTrace();
}
}
private void rollLogFileIfNeeded() {
File logFile = new File(auditLogPath);
// Check if file exists and size exceeds limit
if (!logFile.exists() || logFile.length() < maxLogSizeMB * 1024 * 1024) {
return; // No rolling needed
}
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd-HHmmss");
String timestamp = sdf.format(new Date());
// Count existing rolled log files
File logDir = logFile.getParentFile();
if (logDir == null) {
logDir = new File(".");
}
String logBaseName = logFile.getName();
File[] existingLogs = logDir.listFiles((dir, name) ->
name.startsWith(logBaseName + ".") && !name.endsWith(".zip"));
int currentLogCount = existingLogs != null ? existingLogs.length : 0;
// Roll current log file with timestamp
String rolledLogName = logBaseName + "." + timestamp;
File rolledLog = new File(logDir, rolledLogName);
if (logFile.renameTo(rolledLog)) {
System.out.println("Rolled audit log: " + logFile.getName() + " -> " + rolledLogName);
// Check if we've reached the max file count
if (currentLogCount >= maxLogFiles) {
System.out.println("Maximum log files (" + maxLogFiles + ") reached. Archiving old logs...");
archiveOldLogs(logDir, logBaseName);
}
} else {
System.err.println("Failed to roll audit log file");
}
} catch (Exception e) {
System.err.println("Error during log rolling: " + e.getMessage());
e.printStackTrace();
}
}
private void archiveOldLogs(File logDir, String logBaseName) {
try {
// Create archive directory if it doesn't exist
File archiveDir = new File(archivedLogsDir);
if (!archiveDir.exists()) {
archiveDir.mkdirs();
}
// Get all rolled log files (excluding already zipped ones)
File[] rolledLogs = logDir.listFiles((dir, name) ->
name.startsWith(logBaseName + ".") && !name.endsWith(".zip"));
if (rolledLogs == null || rolledLogs.length == 0) {
return;
}
// Sort by last modified time (oldest first)
Arrays.sort(rolledLogs, Comparator.comparingLong(File::lastModified));
// Create timestamped archive folder
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd-HHmmss");
String archiveTimestamp = sdf.format(new Date());
File archiveFolder = new File(archiveDir, "audit_logs_" + archiveTimestamp);
archiveFolder.mkdirs();
// Move old log files to archive folder
int filesToArchive = rolledLogs.length - maxLogFiles + 1;
if (filesToArchive <= 0) {
return;
}
List<File> archivedFiles = new ArrayList<>();
for (int i = 0; i < filesToArchive && i < rolledLogs.length; i++) {
File oldLog = rolledLogs[i];
File archivedLog = new File(archiveFolder, oldLog.getName());
if (oldLog.renameTo(archivedLog)) {
archivedFiles.add(archivedLog);
System.out.println("Moved to archive: " + oldLog.getName());
}
}
// Zip the archive folder
if (!archivedFiles.isEmpty()) {
String zipFileName = "audit_logs_" + archiveTimestamp + ".zip";
File zipFile = new File(archiveDir, zipFileName);
zipDirectory(archiveFolder, zipFile);
// Delete the archive folder after zipping
deleteDirectory(archiveFolder);
System.out.println("Created archive: " + zipFileName + " (" + archivedFiles.size() + " files)");
}
} catch (Exception e) {
System.err.println("Error archiving old logs: " + e.getMessage());
e.printStackTrace();
}
}
private void zipDirectory(File sourceDir, File zipFile) throws IOException {
try (FileOutputStream fos = new FileOutputStream(zipFile);
java.util.zip.ZipOutputStream zos = new java.util.zip.ZipOutputStream(fos)) {
File[] files = sourceDir.listFiles();
if (files != null) {
for (File file : files) {
if (file.isFile()) {
addFileToZip(file, file.getName(), zos);
}
}
}
}
}
private void addFileToZip(File file, String fileName, java.util.zip.ZipOutputStream zos) throws IOException {
try (FileInputStream fis = new FileInputStream(file)) {
java.util.zip.ZipEntry zipEntry = new java.util.zip.ZipEntry(fileName);
zos.putNextEntry(zipEntry);
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) > 0) {
zos.write(buffer, 0, length);
}
zos.closeEntry();
}
}
private void deleteDirectory(File directory) {
File[] files = directory.listFiles();
if (files != null) {
for (File file : files) {
if (file.isDirectory()) {
deleteDirectory(file);
} else {
file.delete();
}
}
}
directory.delete();
}
// Inner classes
private static class DeltaDirectory {
String name;
long timestamp;
String path;
DeltaDirectory(String name, long timestamp, String path) {
this.name = name;
this.timestamp = timestamp;
this.path = path;
}
}
private static class AuditEntry {
long timestamp;
String userId;
String filePath;
String cellName;
String changeType;
List<String> differences;
AuditEntry(long timestamp, String userId, String filePath, String changeType, List<String> differences) {
this.timestamp = timestamp;
this.userId = userId;
this.filePath = filePath;
this.cellName = extractCellName(filePath);
this.changeType = changeType;
this.differences = differences;
}
private static String extractCellName(String filePath) {
// Extract cell name from path like: cells/was90cell/nodes/...
// Cell name is between first and second /
if (filePath != null && filePath.startsWith("cells/")) {
String[] parts = filePath.split("/");
if (parts.length >= 2) {
return parts[1]; // Return was90cell
}
}
return "Unknown";
}
}
public static void main(String[] args) {
String configFile = args.length > 0 ? args[0] : DEFAULT_CONFIG;
try {
WebSphereAuditMonitor monitor = new WebSphereAuditMonitor(configFile);
monitor.start();
} catch (Exception e) {
System.err.println("Error starting WebSphere Audit Monitor: " + e.getMessage());
e.printStackTrace();
System.exit(1);
}
}
}
// Made with Bob