tpalfy commented on a change in pull request #5195:
URL: https://github.com/apache/nifi/pull/5195#discussion_r682046485
##########
File path:
nifi-commons/nifi-utils/src/main/java/org/apache/nifi/util/file/FileUtils.java
##########
@@ -575,11 +577,43 @@ public static long getContainerCapacity(final Path path) {
/**
* Returns the free capacity for a given path
+ *
* @param path path
* @return usable space
*/
public static long getContainerUsableSpace(final Path path) {
return path.toFile().getUsableSpace();
}
+ /**
+ * Returns the size for a given directory.
+ * @param path directory path
+ * @param logger logger
+ * @return the size in bytes
+ */
+ public static long getDirectorySize(Path path, final Logger logger) {
+ long size = 0;
+ try (Stream<Path> walk = Files.walk(path)) {
+ size = walk
+ .filter(Files::isRegularFile)
+ .mapToLong(getFileSizeByPathFunction(logger))
+ .sum();
+
+ } catch (IOException e) {
+ logger.error("IO exception occured while tried to get the size of
the directory {}", path, e);
+ }
+ return size;
+ }
+
+ private static ToLongFunction<Path> getFileSizeByPathFunction(Logger
logger) {
+ return path -> {
+ try {
+ return Files.size(path);
+ } catch (IOException e) {
+ logger.error("Failed to get size of directory {}", path, e);
Review comment:
```suggestion
logger.error("Failed to get size of file {}", path, e);
```
##########
File path: nifi-docs/src/main/asciidoc/administration-guide.adoc
##########
@@ -4106,3 +4112,20 @@ Example configuration:
nifi.nar.library.provider.hdfs2.implementation=org.apache.nifi.nar.hadoop.HDFSNarProvider
nifi.nar.library.provider.hdfs2.resources=/etc/hadoop/core-site.xml
nifi.nar.library.provider.hdfs2.source.directory=/other/dir/for/customNars
+
+= NiFi diagnostics
+
+It is possible to run diagnostics on NiFi with
+
+```
+$ ./bin/nifi.sh --diagnostic --verbose <dumpfilePath>
+```
+
+During the diagnostic, NiFi sends a request to an already running NiFi
instance, which collects information about clusters,
+components, part of the configuration, memory usage, etc., and writes it to
the specified file or, failing that, to the logs.
+
+The verbose switch is optional and can be used to control the level of
diagnostic detail. In case of a missing dump file path, NiFi writes the
diagnostics information to the bootstrap.log file.
+
+== Automatic diagnostics at shutdown
Review comment:
```suggestion
== Automatic diagnostics at shutdown/restart
```
##########
File path:
nifi-bootstrap/src/main/java/org/apache/nifi/bootstrap/util/DiagnosticContext.java
##########
@@ -0,0 +1,111 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+package org.apache.nifi.bootstrap.util;
+
+import org.apache.nifi.properties.BootstrapProperties;
+import org.apache.nifi.util.NiFiBootstrapUtils;
+import org.apache.nifi.util.file.FileUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.Comparator;
+import java.util.Optional;
+import java.util.stream.Stream;
+
+public class DiagnosticContext {
+
+ private static final Logger logger =
LoggerFactory.getLogger(DiagnosticContext.class);
+
+ private static final String ALLOWED_PROP_NAME = "nifi.diag.allowed";
+
+ private static final String DIR_PROP_NAME = "nifi.diag.dir";
+ private static final String DIR_DEFAULT_VALUE = "./diagnostics";
+
+ private static final String MAX_FILE_COUNT_PROP_NAME =
"nifi.diag.filecount.max";
+ private static final int MAX_FILE_COUNT_DEFAULT_VALUE = 10;
+
+ private static final String MAX_SIZE_PROP_NAME = "nifi.diag.size.max.byte";
+ private static final int MAX_SIZE_DEFAULT_VALUE = Integer.MAX_VALUE;
+
+ private static final String VERBOSE_PROP_NAME = "nifi.diag.verbose";
+
+ private final DiagnosticProperties diagnosticProperties;
+
+ public DiagnosticContext() throws IOException {
+ BootstrapProperties properties =
NiFiBootstrapUtils.loadBootstrapProperties();
+ final String dirPath = properties.getProperty(DIR_PROP_NAME,
DIR_DEFAULT_VALUE);
+ final int maxFileCount =
getPropertyAsInt(properties.getProperty(MAX_FILE_COUNT_PROP_NAME),
MAX_FILE_COUNT_DEFAULT_VALUE);
+ final int maxSizeInBytes =
getPropertyAsInt(properties.getProperty(MAX_SIZE_PROP_NAME),
MAX_SIZE_DEFAULT_VALUE);
+ final boolean verbose =
Boolean.parseBoolean(properties.getProperty(VERBOSE_PROP_NAME));
+ final boolean allowed =
Boolean.parseBoolean(properties.getProperty(ALLOWED_PROP_NAME));
+ diagnosticProperties = new DiagnosticProperties(dirPath, maxFileCount,
maxSizeInBytes, verbose, allowed);
+ createDiagnosticDirectory();
+ }
+
+ public Path getOldestFile() throws IOException {
+ Comparator<? super Path> lastModifiedComparator =
Comparator.comparingLong(p -> p.toFile().lastModified());
+
+ final Optional<Path> oldestFiles;
Review comment:
```suggestion
final Optional<Path> oldestFile;
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]