Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
78d7fe9c
Commit
78d7fe9c
authored
Jul 27, 2014
by
Mattias Severson
Committed by
Andy Wilkinson
Oct 08, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add HealthIndicator that checks free disk space
See gh-1297
parent
1dbc94d0
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
171 additions
and
0 deletions
+171
-0
DiskSpaceHealthIndicator.java
...amework/boot/actuate/health/DiskSpaceHealthIndicator.java
+82
-0
DiskSpaceHealthIndicatorTests.java
...rk/boot/actuate/health/DiskSpaceHealthIndicatorTests.java
+89
-0
No files found.
spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/DiskSpaceHealthIndicator.java
0 → 100644
View file @
78d7fe9c
/*
* Copyright 2014 the original author or authors.
*
* Licensed 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
.
springframework
.
boot
.
actuate
.
health
;
import
java.io.File
;
import
org.apache.commons.logging.Log
;
import
org.apache.commons.logging.LogFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.stereotype.Component
;
import
org.springframework.util.Assert
;
/**
* A {@link HealthIndicator} that checks for available disk space.
*
* @author Mattias Severson
* @since 1.2.0
*/
@Component
public
class
DiskSpaceHealthIndicator
extends
AbstractHealthIndicator
{
private
static
Log
logger
=
LogFactory
.
getLog
(
DiskSpaceHealthIndicator
.
class
);
private
final
File
path
;
private
final
long
thresholdBytes
;
/**
* Constructor.
* @param path The path to a directory for checking available disk space. The
* application must have read access to this path. Additionally, the
* {@link java.lang.SecurityManager#checkRead(String)} will be called
* if a security manager is used.
* By default, it uses the system property {@code user.dir}, i.e. the
* current directory when the JVM was started.
* @param thresholdBytes The threshold (in Bytes) of remaining disk space that will
* trigger this health indicator when exceeded.
* Default value is 10485760 Bytes (10 MB).
*/
@Autowired
public
DiskSpaceHealthIndicator
(
@Value
(
"${health.path?:${user.dir}}"
)
String
path
,
@Value
(
"${health.threshold.bytes:10485760}"
)
long
thresholdBytes
)
{
this
.
path
=
new
File
(
path
);
this
.
thresholdBytes
=
thresholdBytes
;
verifyPathIsAccessible
(
this
.
path
);
Assert
.
isTrue
(
thresholdBytes
>=
0
,
"thresholdBytes must be greater than 0"
);
}
@Override
protected
void
doHealthCheck
(
Health
.
Builder
builder
)
throws
Exception
{
long
diskFreeInBytes
=
this
.
path
.
getFreeSpace
();
if
(
diskFreeInBytes
>=
this
.
thresholdBytes
)
{
builder
.
up
();
}
else
{
logger
.
warn
(
String
.
format
(
"Free disk space threshold exceeded. "
+
"Available: %d bytes (threshold: %d bytes)."
,
diskFreeInBytes
,
this
.
thresholdBytes
));
builder
.
down
();
}
}
private
static
void
verifyPathIsAccessible
(
File
path
)
{
if
(!
path
.
exists
())
{
throw
new
IllegalArgumentException
(
String
.
format
(
"Path does not exist: %s"
,
path
));
}
if
(!
path
.
canRead
())
{
throw
new
IllegalStateException
(
String
.
format
(
"Path cannot be read: %s"
,
path
));
}
}
}
spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/DiskSpaceHealthIndicatorTests.java
0 → 100644
View file @
78d7fe9c
/*
* Copyright 2014 the original author or authors.
*
* Licensed 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
.
springframework
.
boot
.
actuate
.
health
;
import
java.io.File
;
import
org.junit.Before
;
import
org.junit.Rule
;
import
org.junit.Test
;
import
org.junit.rules.ExpectedException
;
import
org.junit.runner.RunWith
;
import
org.mockito.Mock
;
import
org.mockito.runners.MockitoJUnitRunner
;
import
org.springframework.test.util.ReflectionTestUtils
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
mockito
.
Mockito
.
when
;
/**
* Tests for {@link DiskSpaceHealthIndicator}.
*
* @author Mattias Severson
*/
@RunWith
(
MockitoJUnitRunner
.
class
)
public
class
DiskSpaceHealthIndicatorTests
{
static
final
long
THRESHOLD_BYTES
=
1024
;
@Rule
public
ExpectedException
exception
=
ExpectedException
.
none
();
@Mock
File
fileMock
;
HealthIndicator
healthIndicator
;
@Before
public
void
setUp
()
throws
Exception
{
this
.
healthIndicator
=
new
DiskSpaceHealthIndicator
(
DiskSpaceHealthIndicatorTests
.
class
.
getResource
(
""
).
getPath
(),
THRESHOLD_BYTES
);
ReflectionTestUtils
.
setField
(
this
.
healthIndicator
,
"path"
,
this
.
fileMock
);
}
@Test
public
void
diskSpaceIsUp
()
throws
Exception
{
when
(
this
.
fileMock
.
getFreeSpace
()).
thenReturn
(
THRESHOLD_BYTES
+
10
);
Health
health
=
this
.
healthIndicator
.
health
();
assertEquals
(
Status
.
UP
,
health
.
getStatus
());
}
@Test
public
void
diskSpaceIsDown
()
throws
Exception
{
when
(
this
.
fileMock
.
getFreeSpace
()).
thenReturn
(
THRESHOLD_BYTES
-
10
);
Health
health
=
this
.
healthIndicator
.
health
();
assertEquals
(
Status
.
DOWN
,
health
.
getStatus
());
}
@Test
public
void
throwsExceptionForUnknownPath
()
{
exception
.
expect
(
IllegalArgumentException
.
class
);
exception
.
expectMessage
(
"Path does not exist: an_path_that_does_not_exist"
);
new
DiskSpaceHealthIndicator
(
"an_path_that_does_not_exist"
,
THRESHOLD_BYTES
);
}
@Test
public
void
throwsExceptionForNegativeThreshold
()
{
exception
.
expect
(
IllegalArgumentException
.
class
);
exception
.
expectMessage
(
"thresholdBytes must be greater than 0"
);
new
DiskSpaceHealthIndicator
(
DiskSpaceHealthIndicatorTests
.
class
.
getResource
(
""
).
getPath
(),
-
1
);
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment