DiskSpaceMonitor.java revision a89f7014aeb71dba5c94404dfea7eb89e7eeee74
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at legal-notices/CDDLv1_0.txt.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information:
* Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*
*
* Copyright 2010 Sun Microsystems, Inc.
* Portions Copyright 2014-2015 ForgeRock AS
*/
/**
* This class provides an application-wide disk space monitoring service.
* It provides the ability for registered handlers to receive notifications
* when the free disk space falls below a certain threshold.
*
* The handler will only be notified once when when the free space
* have dropped below any of the thresholds. Once the "full" threshold
* have been reached, the handler will not be notified again until the
* free space raises above the "low" threshold.
*/
public class DiskSpaceMonitor extends MonitorProvider<MonitorProviderCfg> implements Runnable, AlertGenerator,
{
/**
* Helper class for each requestor for use with cn=monitor reporting and users of a spcific mountpoint.
*/
{
private volatile long lowThreshold;
private volatile long fullThreshold;
private final DiskSpaceMonitorHandler handler;
private final String instanceName;
private int lastState;
private MonitoredDirectory(File directory, String instanceName, String baseName, DiskSpaceMonitorHandler handler)
{
this.instanceName = instanceName;
}
/** {@inheritDoc} */
public String getMonitorInstanceName() {
}
/** {@inheritDoc} */
throws ConfigException, InitializationException {
}
/** {@inheritDoc} */
return monitorAttrs;
}
private File getDirectory() {
return directory;
}
private long getFreeSpace() {
return directory.getUsableSpace();
}
private long getFullThreshold() {
return fullThreshold;
}
private long getLowThreshold() {
return lowThreshold;
}
private void setFullThreshold(long fullThreshold) {
this.fullThreshold = fullThreshold;
}
private void setLowThreshold(long lowThreshold) {
this.lowThreshold = lowThreshold;
}
{
}
{
switch(lastState)
{
case NORMAL:
return "normal";
case LOW:
return "low";
case FULL:
return "full";
default:
return null;
}
}
}
/**
* Helper class for building temporary list of handlers to notify on threshold hits.
* One object per directory per state will hold all the handlers matching directory and state.
*/
private class HandlerNotifier {
private int state;
/** printable list of handlers names, for reporting backend names in alert messages */
{
}
private void notifyHandlers()
{
{
switch (state)
{
case FULL:
break;
case LOW:
break;
case NORMAL:
break;
}
}
}
private boolean isEmpty()
{
}
{
{
}
}
{
{
}
}
}
private static final int NORMAL = 0;
private static final int LOW = 1;
private static final int FULL = 2;
/**
* Constructs a new DiskSpaceMonitor that will notify registered DiskSpaceMonitorHandler objects when filesystems
* on which configured directories reside, fall below the provided thresholds.
*/
public DiskSpaceMonitor()
{
}
/**
* Starts periodic monitoring of all registered directories.
*/
public void startDiskSpaceMonitor()
{
}
/**
* Registers or reconfigures a directory for monitoring.
* If possible, we will try to get and use the mountpoint where the directory resides and monitor it instead.
* If the directory is already registered for the same <code>handler</code>, simply change its configuration.
* @param instanceName A name for the handler, as used by cn=monitor
* @param directory The directory to monitor
* @param lowThresholdBytes Disk slow threshold expressed in bytes
* @param fullThresholdBytes Disk full threshold expressed in bytes
* @param handler The class requesting to be called when a transition in disk space occurs
*/
{
try
{
}
catch (IOException ioe)
{
logger.warn(ERR_DISK_SPACE_GET_MOUNT_POINT, directory.getAbsolutePath(), ioe.getLocalizedMessage());
}
synchronized (monitoredDirs)
{
if (diskHelpers == null)
{
}
else
{
{
{
return;
}
}
}
}
}
{
/*
* Since there is no concept of mount point in the APIs, iterate on all parents of
* the given directory until the FileSystem Store changes (hint of a different
* device, hence a mount point) or we get to root, which works too.
*/
{
{
return mountPoint.toFile();
}
}
return mountPoint.toFile();
}
/**
* Removes a directory from the set of monitored directories.
*
* @param directory The directory to stop monitoring on
* @param handler The class that requested monitoring
*/
{
synchronized (monitoredDirs)
{
if (directories != null)
{
{
{
}
}
if (directories.isEmpty())
{
}
}
}
}
/** {@inheritDoc} */
throws ConfigException, InitializationException {
// Not used...
}
/** {@inheritDoc} */
public String getMonitorInstanceName() {
return INSTANCENAME;
}
/** {@inheritDoc} */
return new ArrayList<>();
}
/** {@inheritDoc} */
public void run()
{
synchronized (monitoredDirs)
{
{
try
{
{
{
}
{
}
{
}
}
}
catch(Exception e)
{
logger.traceException(e);
}
}
}
// It is probably better to notify handlers outside of the synchronized section.
}
{
{
}
}
{
{
{
}
{
}
else
{
}
}
}
/** {@inheritDoc} */
public DN getComponentEntryDN()
{
try
{
}
catch (DirectoryException de)
{
}
}
/** {@inheritDoc} */
public String getClassName()
{
return DiskSpaceMonitor.class.getName();
}
/** {@inheritDoc} */
{
return alerts;
}
/** {@inheritDoc} */
public String getShutdownListenerName()
{
return INSTANCENAME;
}
/** {@inheritDoc} */
{
synchronized (monitoredDirs)
{
{
{
}
}
}
}
}