diff --git a/gradle.properties b/gradle.properties index c54020f..900db79 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,3 +1,3 @@ id = hmci group = biz.nellemann.hmci -version = 1.2.1 +version = 1.2.2 diff --git a/src/main/java/biz/nellemann/hmci/Application.java b/src/main/java/biz/nellemann/hmci/Application.java index bb1a26a..aa02695 100644 --- a/src/main/java/biz/nellemann/hmci/Application.java +++ b/src/main/java/biz/nellemann/hmci/Application.java @@ -37,6 +37,7 @@ public class Application implements Callable { @Option(names = { "-d", "--debug" }, description = "Enable debugging [default: 'false'].") private boolean[] enableDebug = new boolean[0]; + public static void main(String... args) { int exitCode = new CommandLine(new Application()).execute(args); System.exit(exitCode); diff --git a/src/main/java/biz/nellemann/hmci/Configuration.java b/src/main/java/biz/nellemann/hmci/Configuration.java index 893460a..7b447a4 100644 --- a/src/main/java/biz/nellemann/hmci/Configuration.java +++ b/src/main/java/biz/nellemann/hmci/Configuration.java @@ -91,6 +91,12 @@ public final class Configuration { c.unsafe = false; } + if(hmcTable.contains(key+".trace")) { + c.trace = hmcTable.getString(key+".trace"); + } else { + c.trace = null; + } + list.add(c); } } @@ -179,6 +185,7 @@ public final class Configuration { String username; String password; Boolean unsafe = false; + String trace; Long update = 30L; Long rescan = 60L; diff --git a/src/main/java/biz/nellemann/hmci/HmcInstance.java b/src/main/java/biz/nellemann/hmci/HmcInstance.java index 4af15e0..e2a4a0c 100644 --- a/src/main/java/biz/nellemann/hmci/HmcInstance.java +++ b/src/main/java/biz/nellemann/hmci/HmcInstance.java @@ -19,6 +19,9 @@ import biz.nellemann.hmci.Configuration.HmcObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; import java.io.IOException; import java.time.Duration; import java.time.Instant; @@ -42,6 +45,8 @@ class HmcInstance implements Runnable { private final InfluxClient influxClient; private final AtomicBoolean keepRunning = new AtomicBoolean(true); + private File traceDir; + private Boolean doTrace = false; HmcInstance(HmcObject configHmc, InfluxClient influxClient) { this.hmcId = configHmc.name; @@ -50,6 +55,20 @@ class HmcInstance implements Runnable { this.influxClient = influxClient; hmcRestClient = new HmcRestClient(configHmc.url, configHmc.username, configHmc.password, configHmc.unsafe); log.debug(String.format("HmcInstance() - id: %s, update: %s, refresh %s", hmcId, updateValue, rescanValue)); + + if(configHmc.trace != null) { + try { + traceDir = new File(configHmc.trace); + traceDir.mkdirs(); + if(traceDir.canWrite()) { + doTrace = true; + } else { + log.warn("HmcInstance() - can't write to trace dir: " + traceDir.toString()); + } + } catch (Exception e) { + log.error("HmcInstance() - trace error: " + e.getMessage()); + } + } } @@ -171,8 +190,12 @@ class HmcInstance implements Runnable { if(tmpJsonString != null && !tmpJsonString.isEmpty()) { system.processMetrics(tmpJsonString); + if(doTrace) { + writeTraceFile(systemId, tmpJsonString); + } } + }); } @@ -194,6 +217,9 @@ class HmcInstance implements Runnable { } if(tmpJsonString2 != null && !tmpJsonString2.isEmpty()) { partition.processMetrics(tmpJsonString2); + if(doTrace) { + writeTraceFile(partitionId, tmpJsonString2); + } } }); @@ -252,4 +278,18 @@ class HmcInstance implements Runnable { } + private void writeTraceFile(String id, String json) { + + String fileName = String.format("%s-%s.json", id, Instant.now().toString()); + try { + log.debug("Writing trace file: " + fileName); + File traceFile = new File(traceDir, fileName); + BufferedWriter writer = new BufferedWriter(new FileWriter(traceFile)); + writer.write(json); + writer.close(); + } catch (IOException e) { + log.warn("writeTraceFile() - " + e.getMessage()); + } + } + } diff --git a/src/test/resources/hmci.toml b/src/test/resources/hmci.toml index a5c87f5..6cd785c 100644 --- a/src/test/resources/hmci.toml +++ b/src/test/resources/hmci.toml @@ -30,3 +30,4 @@ database = "hmci" #username = "viewer" #password = "someSecret" #unsafe = false + #trace = "/tmp/pcm-files"