Autopsy  4.7.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
IngestMonitor.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-2018 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.ingest;
20 
21 import java.awt.event.ActionEvent;
22 import java.awt.event.ActionListener;
23 import java.beans.PropertyChangeEvent;
24 import java.io.File;
25 import java.io.IOException;
26 import java.util.EnumSet;
27 import java.util.logging.FileHandler;
28 import java.util.logging.Level;
29 import java.util.logging.SimpleFormatter;
30 import org.openide.util.NbBundle;
32 import javax.swing.Timer;
37 
44 public final class IngestMonitor {
45 
46  public static final int DISK_FREE_SPACE_UNKNOWN = -1;
47  private static final int INITIAL_INTERVAL_MS = 60000; //1 min.
48  private static final int MAX_LOG_FILES = 3;
49 
50  /*
51  * The monitorLogger used the standard Java Logger type for compact logs
52  * without the stack trace.
53  */
54  private static final java.util.logging.Logger monitorLogger = java.util.logging.Logger.getLogger("monitor"); //NON-NLS
55  private final Logger logger = Logger.getLogger(IngestMonitor.class.getName());
56  private Timer timer;
58 
63  IngestMonitor() {
64  /*
65  * Setup a separate memory usage logger.
66  */
67  try {
68  FileHandler monitorLogHandler = new FileHandler(PlatformUtil.getUserDirectory().getAbsolutePath() + "/var/log/monitor.log", 0, MAX_LOG_FILES); //NON-NLS
69  monitorLogHandler.setFormatter(new SimpleFormatter());
70  monitorLogHandler.setEncoding(PlatformUtil.getLogFileEncoding());
71  monitorLogger.setUseParentHandlers(false);
72  monitorLogger.addHandler(monitorLogHandler);
73  } catch (IOException | SecurityException ex) {
74  logger.log(Level.SEVERE, "Failed to create memory usage logger", ex); //NON-NLS
75  }
76  }
77 
81  void start() {
82  timerAction = new MonitorTimerAction();
83  timer = new Timer(INITIAL_INTERVAL_MS, timerAction);
84  timer.start();
85  }
86 
90  void stop() {
91  if (null != timer) {
92  timer.stop();
93  }
94  }
95 
101  boolean isRunning() {
102  return (null != timer && timer.isRunning());
103  }
104 
111  long getFreeSpace() {
112  try {
113  return timerAction.getFreeSpace();
114  } catch (SecurityException e) {
115  logger.log(Level.WARNING, "Error checking for free disk space on ingest data drive", e); //NON-NLS
117  }
118  }
119 
124  private class MonitorTimerAction implements ActionListener {
125 
126  private final static long MIN_FREE_DISK_SPACE = 100L * 1024 * 1024; // 100MB
127  private File root;
128 
131  Case.addEventTypeSubscriber(EnumSet.of(Case.Events.CURRENT_CASE), (PropertyChangeEvent evt) -> {
132  if (evt instanceof AutopsyEvent) {
133  AutopsyEvent event = (AutopsyEvent) evt;
134  if (AutopsyEvent.SourceType.LOCAL == event.getSourceType() && event.getPropertyName().equals(Case.Events.CURRENT_CASE.toString())) {
135  /*
136  * The new value of the event will be non-null if a new
137  * case has been opened.
138  */
139  if (null != evt.getNewValue()) {
140  findRootDirectoryForCurrentCase((Case) evt.getNewValue());
141  }
142  }
143  }
144  });
145  }
146 
152  try {
153  Case currentCase = Case.getCurrentCaseThrows();
154  findRootDirectoryForCurrentCase(currentCase);
155  } catch (NoCurrentCaseException unused) {
156  /*
157  * Case.getCurrentOpenCase() throws NoCurrentCaseException when there
158  * is no case.
159  */
160  root = new File(File.separator);
162  }
163  }
164 
171  private void findRootDirectoryForCurrentCase(Case currentCase) {
172  File curDir = new File(currentCase.getCaseDirectory());
173  File parentDir = curDir.getParentFile();
174  while (null != parentDir) {
175  curDir = parentDir;
176  parentDir = curDir.getParentFile();
177  }
178  root = curDir;
180  }
181 
186  private void logMonitoredRootDirectory() {
187  logger.log(Level.INFO, "Monitoring disk space of {0}", root.getAbsolutePath()); //NON-NLS
188  }
189 
190  @Override
191  public void actionPerformed(ActionEvent e) {
192  /*
193  * Skip monitoring if ingest is not running.
194  */
195  final IngestManager manager = IngestManager.getInstance();
196  if (manager.isIngestRunning() == false) {
197  return;
198  }
199 
200  logMemoryUsage();
201  if (!enoughDiskSpace()) {
202  /*
203  * Shut down ingest by cancelling all ingest jobs.
204  */
206  String diskPath = root.getAbsolutePath();
207  IngestServices.getInstance().postMessage(IngestMessage.createManagerErrorMessage(
208  NbBundle.getMessage(this.getClass(), "IngestMonitor.mgrErrMsg.lowDiskSpace.title", diskPath),
209  NbBundle.getMessage(this.getClass(), "IngestMonitor.mgrErrMsg.lowDiskSpace.msg", diskPath)));
210  monitorLogger.log(Level.SEVERE, "Stopping ingest due to low disk space on {0}", diskPath); //NON-NLS
211  logger.log(Level.SEVERE, "Stopping ingest due to low disk space on {0}", diskPath); //NON-NLS
212  }
213  }
214 
218  private void logMemoryUsage() {
219  monitorLogger.log(Level.INFO, PlatformUtil.getAllMemUsageInfo());
220  }
221 
226  private void logDiskSpaceUsage() {
227  final long freeSpace = root.getFreeSpace();
228  logger.log(Level.INFO, "Available disk space on drive where case dir resides is {0} (bytes)", freeSpace); //NON-NLS
229  }
230 
237  private boolean enoughDiskSpace() {
238  long freeSpace;
239  try {
240  freeSpace = getFreeSpace();
241  } catch (SecurityException e) {
242  logger.log(Level.WARNING, "Unable to check for free disk space (permission issue)", e); //NON-NLS
243  return true; //OK
244  }
245 
246  if (freeSpace == DISK_FREE_SPACE_UNKNOWN) {
247  return true;
248  } else {
249  return freeSpace > MIN_FREE_DISK_SPACE;
250  }
251  }
252 
259  private long getFreeSpace() throws SecurityException {
260  // always return "UNKNOWN", see note below
262 
263  /*
264  * NOTE: use and accuracy of this code for network drives needs to
265  * be investigated and validated final long freeSpace =
266  * root.getFreeSpace(); if (0 == freeSpace) { // Check for a network
267  * drive, some network filesystems always // return zero. final
268  * String monitoredPath = root.getAbsolutePath(); if
269  * (monitoredPath.startsWith("\\\\") ||
270  * monitoredPath.startsWith("//")) { return DISK_FREE_SPACE_UNKNOWN;
271  * } } return freeSpace;
272  */
273  }
274  }
275 
276 }
static synchronized IngestManager getInstance()
static final java.util.logging.Logger monitorLogger
Logger(String name, String resourceBundleName)
Definition: Logger.java:160
void postMessage(final IngestMessage message)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124
static void addEventTypeSubscriber(Set< Events > eventTypes, PropertyChangeListener subscriber)
Definition: Case.java:420
void cancelAllIngestJobs(IngestJob.CancellationReason reason)
static synchronized IngestServices getInstance()

Copyright © 2012-2016 Basis Technology. Generated on: Mon Jun 18 2018
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.