Autopsy  3.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 2012 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.beans.PropertyChangeListener;
25 import java.io.File;
26 import java.io.IOException;
27 import java.util.logging.FileHandler;
28 import java.util.logging.Level;
29 import java.util.logging.SimpleFormatter;
30 
31 import org.openide.util.NbBundle;
33 import javax.swing.Timer;
34 import org.openide.util.Exceptions;
37 
41 public final class IngestMonitor {
42 
43  public static final int DISK_FREE_SPACE_UNKNOWN = -1;
44  private static final int INITIAL_INTERVAL_MS = 60000; //1 min.
45  private final Logger logger = Logger.getLogger(IngestMonitor.class.getName());
46  private Timer timer;
47  private static final java.util.logging.Logger MONITOR_LOGGER = java.util.logging.Logger.getLogger("monitor"); //NON-NLS
49 
50  IngestMonitor() {
51 
52  //setup the custom memory logger
53  try {
54  final int MAX_LOG_FILES = 3;
55  FileHandler monitorLogHandler = new FileHandler(PlatformUtil.getUserDirectory().getAbsolutePath() + "/var/log/monitor.log", //NON-NLS
56  0, MAX_LOG_FILES);
57  monitorLogHandler.setFormatter(new SimpleFormatter());
58  monitorLogHandler.setEncoding(PlatformUtil.getLogFileEncoding());
59  MONITOR_LOGGER.addHandler(monitorLogHandler);
60  //do not forward to the parent autopsy logger
61  MONITOR_LOGGER.setUseParentHandlers(false);
62  } catch (IOException ex) {
63  Exceptions.printStackTrace(ex);
64  } catch (SecurityException ex) {
65  Exceptions.printStackTrace(ex);
66  }
67 
68  }
69 
73  void start() {
74  monitor = new MonitorAction();
75  timer = new Timer(INITIAL_INTERVAL_MS, monitor);
76  timer.start();
77  }
78 
82  void stop() {
83  if (timer != null) {
84  timer.stop();
85  }
86  }
87 
93  boolean isRunning() {
94  return timer != null && timer.isRunning();
95  }
96 
102  long getFreeSpace() {
103  try {
104  return monitor.getFreeSpace();
105  } catch (SecurityException e) {
106  logger.log(Level.WARNING, "Error checking for free disk space on ingest data drive", e); //NON-NLS
108  }
109  }
110 
111  //TODO add support to monitor multiple drives, e.g. user dir drive in addition to Case drive
112  private class MonitorAction implements ActionListener {
113 
114  private final static long MIN_FREE_DISK_SPACE = 100L * 1024 * 1024; //100MB
115  private File root = new File(File.separator); //default, root dir where autopsy runs
116 
117  MonitorAction() {
118  //find drive where case is located
119  setMonitorDir();
120 
121  //update monitor dir if case changed
122  Case.addPropertyChangeListener(new PropertyChangeListener() {
123  @Override
124  public void propertyChange(PropertyChangeEvent evt) {
125  String changed = evt.getPropertyName();
126  Object newValue = evt.getNewValue();
127 
128  if (changed.equals(Case.Events.CURRENT_CASE.toString())) {
129  if (newValue != null) {
130  setMonitorDir();
131  }
132 
133  }
134 
135 
136  }
137  });
138 
139  }
140 
141  private void setMonitorDir() {
142  String caseDir = Case.getCurrentCase().getCaseDirectory();
143  File curDir = new File(caseDir);
144  File tempF = null;
145  while ((tempF = curDir.getParentFile()) != null) {
146  curDir = tempF;
147  }
148  root = curDir;
149  logger.log(Level.INFO, "Monitoring disk space of case root: " + curDir.getAbsolutePath()); //NON-NLS
150  }
151 
152  @Override
153  public void actionPerformed(ActionEvent e) {
154  final IngestManager manager = IngestManager.getInstance();
155 
156  //runs checks only if ingest is running
157  if (manager.isIngestRunning() == false) {
158  return;
159  }
160 
161  monitorMemory();
162 
163  if (checkDiskSpace() == false) {
164  //stop ingest if running
165  final String diskPath = root.getAbsolutePath();
166  MONITOR_LOGGER.log(Level.SEVERE, "Stopping ingest due to low disk space on disk {0}", diskPath); //NON-NLS
167  logger.log(Level.SEVERE, "Stopping ingest due to low disk space on disk {0}", diskPath); //NON-NLS
168  manager.cancelAllIngestJobs();
169  IngestServices.getInstance().postMessage(IngestMessage.createManagerErrorMessage(
170  NbBundle.getMessage(this.getClass(), "IngestMonitor.mgrErrMsg.lowDiskSpace.title", diskPath),
171  NbBundle.getMessage(this.getClass(), "IngestMonitor.mgrErrMsg.lowDiskSpace.msg", diskPath)));
172  }
173  }
174 
181  private long getFreeSpace() throws SecurityException {
182  final long freeSpace = root.getFreeSpace();
183 
184  if (freeSpace == 0) {
185  //check if network drive, some network filesystems always return 0
186  final String monitoredPath = root.getAbsolutePath();
187  if (monitoredPath.startsWith("\\\\") || monitoredPath.startsWith("//")) {
189 
190  }
191  }
192 
193  return freeSpace;
194 
195  }
196 
202  private boolean checkDiskSpace() {
203  long freeSpace;
204  try {
205  freeSpace = getFreeSpace();
206  } catch (SecurityException e) {
207  logger.log(Level.WARNING, "Unable to check for free disk space (permission issue)", e); //NON-NLS
208  return true; //OK
209  }
210 
211  if (freeSpace == DISK_FREE_SPACE_UNKNOWN) {
212  return true;
213  } else {
214  //logger.log(Level.INFO, "Checking free disk apce: " + freeSpace + " need: " + Long.toString(MIN_FREE_DISK_SPACE));
215  return freeSpace > MIN_FREE_DISK_SPACE;
216  }
217  }
218 
222  private void monitorMemory() {
224  }
225  }
226 }
static synchronized IngestManager getInstance()
Logger(String name, String resourceBundleName)
Definition: Logger.java:151
void postMessage(final IngestMessage message)
static synchronized void addPropertyChangeListener(PropertyChangeListener listener)
Definition: Case.java:833
static Logger getLogger(String name)
Definition: Logger.java:131
static final java.util.logging.Logger MONITOR_LOGGER
static synchronized IngestServices getInstance()

Copyright © 2012-2015 Basis Technology. Generated on: Mon Oct 19 2015
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.