Autopsy  4.1
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-2015 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.logging.FileHandler;
27 import java.util.logging.Level;
28 import java.util.logging.SimpleFormatter;
29 import org.openide.util.NbBundle;
31 import javax.swing.Timer;
35 
42 public final class IngestMonitor {
43 
44  public static final int DISK_FREE_SPACE_UNKNOWN = -1;
45  private static final int INITIAL_INTERVAL_MS = 60000; //1 min.
46  private static final int MAX_LOG_FILES = 3;
47  private static final java.util.logging.Logger MONITOR_LOGGER = java.util.logging.Logger.getLogger("monitor"); //NON-NLS
48  private final Logger logger = Logger.getLogger(IngestMonitor.class.getName());
49  private Timer timer;
51 
56  IngestMonitor() {
57  /*
58  * Setup a separate memory usage logger.
59  */
60  try {
61  FileHandler monitorLogHandler = new FileHandler(PlatformUtil.getUserDirectory().getAbsolutePath() + "/var/log/monitor.log", 0, MAX_LOG_FILES); //NON-NLS
62  monitorLogHandler.setFormatter(new SimpleFormatter());
63  monitorLogHandler.setEncoding(PlatformUtil.getLogFileEncoding());
64  MONITOR_LOGGER.setUseParentHandlers(false);
65  MONITOR_LOGGER.addHandler(monitorLogHandler);
66  } catch (IOException | SecurityException ex) {
67  logger.log(Level.SEVERE, "Failed to create memory usage logger", ex); //NON-NLS
68  }
69  }
70 
74  void start() {
75  timerAction = new MonitorTimerAction();
76  timer = new Timer(INITIAL_INTERVAL_MS, timerAction);
77  timer.start();
78  }
79 
83  void stop() {
84  if (null != timer) {
85  timer.stop();
86  }
87  }
88 
94  boolean isRunning() {
95  return (null != timer && timer.isRunning());
96  }
97 
104  long getFreeSpace() {
105  try {
106  return timerAction.getFreeSpace();
107  } catch (SecurityException e) {
108  logger.log(Level.WARNING, "Error checking for free disk space on ingest data drive", e); //NON-NLS
110  }
111  }
112 
117  private class MonitorTimerAction implements ActionListener {
118 
119  private final static long MIN_FREE_DISK_SPACE = 100L * 1024 * 1024; // 100MB
120  private File root;
121 
124  Case.addEventSubscriber(Case.Events.CURRENT_CASE.toString(), (PropertyChangeEvent evt) -> {
125  if (evt instanceof AutopsyEvent) {
126  AutopsyEvent event = (AutopsyEvent) evt;
127  if (AutopsyEvent.SourceType.LOCAL == event.getSourceType() && event.getPropertyName().equals(Case.Events.CURRENT_CASE.toString())) {
128  /*
129  * The new value of the event will be non-null if a new
130  * case has been opened.
131  */
132  if (null != evt.getNewValue()) {
133  findRootDirectoryForCurrentCase((Case) evt.getNewValue());
134  }
135  }
136  }
137  });
138  }
139 
145  try {
146  Case currentCase = Case.getCurrentCase();
147  findRootDirectoryForCurrentCase(currentCase);
148  } catch (IllegalStateException unused) {
149  /*
150  * Case.getCurrentCase() throws IllegalStateException when there
151  * is no case.
152  */
153  root = new File(File.separator);
155  }
156  }
157 
164  private void findRootDirectoryForCurrentCase(Case currentCase) {
165  File curDir = new File(currentCase.getCaseDirectory());
166  File parentDir = curDir.getParentFile();
167  while (null != parentDir) {
168  curDir = parentDir;
169  parentDir = curDir.getParentFile();
170  }
171  root = curDir;
173  }
174 
179  private void logMonitoredRootDirectory() {
180  logger.log(Level.INFO, "Monitoring disk space of {0}", root.getAbsolutePath()); //NON-NLS
181  }
182 
183  @Override
184  public void actionPerformed(ActionEvent e) {
185  /*
186  * Skip monitoring if ingest is not running.
187  */
188  final IngestManager manager = IngestManager.getInstance();
189  if (manager.isIngestRunning() == false) {
190  return;
191  }
192 
193  logMemoryUsage();
194 
195  if (!enoughDiskSpace()) {
196  /*
197  * Shut down ingest by cancelling all ingest jobs.
198  */
200  String diskPath = root.getAbsolutePath();
201  IngestServices.getInstance().postMessage(IngestMessage.createManagerErrorMessage(
202  NbBundle.getMessage(this.getClass(), "IngestMonitor.mgrErrMsg.lowDiskSpace.title", diskPath),
203  NbBundle.getMessage(this.getClass(), "IngestMonitor.mgrErrMsg.lowDiskSpace.msg", diskPath)));
204  MONITOR_LOGGER.log(Level.SEVERE, "Stopping ingest due to low disk space on {0}", diskPath); //NON-NLS
205  logger.log(Level.SEVERE, "Stopping ingest due to low disk space on {0}", diskPath); //NON-NLS
206  }
207  }
208 
212  private void logMemoryUsage() {
214  }
215 
222  private boolean enoughDiskSpace() {
223  long freeSpace;
224  try {
225  freeSpace = getFreeSpace();
226  } catch (SecurityException e) {
227  logger.log(Level.WARNING, "Unable to check for free disk space (permission issue)", e); //NON-NLS
228  return true; //OK
229  }
230 
231  if (freeSpace == DISK_FREE_SPACE_UNKNOWN) {
232  return true;
233  } else {
234  return freeSpace > MIN_FREE_DISK_SPACE;
235  }
236  }
237 
244  private long getFreeSpace() throws SecurityException {
245  final long freeSpace = root.getFreeSpace();
246  if (0 == freeSpace) {
247  /*
248  * Check for a network drive, some network filesystems always
249  * return zero.
250  */
251  final String monitoredPath = root.getAbsolutePath();
252  if (monitoredPath.startsWith("\\\\") || monitoredPath.startsWith("//")) {
254  }
255  }
256  return freeSpace;
257  }
258  }
259 
260 }
static synchronized IngestManager getInstance()
Logger(String name, String resourceBundleName)
Definition: Logger.java:198
void postMessage(final IngestMessage message)
synchronized static Logger getLogger(String name)
Definition: Logger.java:161
static final java.util.logging.Logger MONITOR_LOGGER
static void addEventSubscriber(Set< String > eventNames, PropertyChangeListener subscriber)
Definition: Case.java:330
static synchronized IngestServices getInstance()

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