Skip to content

Commit 24680ae

Browse files
health
1 parent e32f023 commit 24680ae

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.health;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.beans.factory.annotation.Value;
5+
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
6+
import org.springframework.boot.actuate.health.Health;
7+
import org.springframework.stereotype.Component;
8+
9+
import java.io.IOException;
10+
import java.nio.file.FileStore;
11+
import java.nio.file.Files;
12+
import java.nio.file.Paths;
13+
14+
/**
15+
* Created by zhuzhengping on 2017/3/25.
16+
*/
17+
@Component
18+
public class CusDiskSpaceHealthIndicator extends AbstractHealthIndicator {
19+
20+
private final FileStore fileStore;
21+
private final long thresholdBytes;
22+
23+
@Autowired
24+
public CusDiskSpaceHealthIndicator(
25+
@Value("${health.filestore.path:/}") String path,
26+
@Value("${health.filestore.bytes:10485760}") long thresholdBytes
27+
) throws IOException {
28+
fileStore = Files.getFileStore(Paths.get(path));
29+
this.thresholdBytes = thresholdBytes;
30+
}
31+
32+
@Override
33+
protected void doHealthCheck(Health.Builder builder) throws Exception {
34+
long diskFreeInBytes = fileStore.getUnallocatedSpace();
35+
if(diskFreeInBytes >= thresholdBytes){
36+
builder.up();
37+
} else{
38+
builder.down();
39+
}
40+
41+
long totalSpaceInBytes = fileStore.getTotalSpace();
42+
builder.withDetail("disk.free",diskFreeInBytes);
43+
builder.withDetail("disk.totol",totalSpaceInBytes);
44+
}
45+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.health;
2+
3+
import org.springframework.boot.actuate.health.Health;
4+
import org.springframework.boot.actuate.health.HealthIndicator;
5+
import org.springframework.http.HttpStatus;
6+
import org.springframework.stereotype.Component;
7+
8+
/**
9+
* Created by zhuzhengping on 2017/3/25.
10+
*/
11+
@Component
12+
public class CusStatusHealthIndicator implements HealthIndicator {
13+
14+
@Override
15+
public Health health() {
16+
int errCode = check();
17+
if(errCode != 0){
18+
return Health.down()
19+
.withDetail("status",errCode)
20+
.withDetail("message","http error")
21+
.build();
22+
}
23+
return Health.up().build();
24+
}
25+
26+
private int check(){
27+
return HttpStatus.NOT_FOUND.value();
28+
}
29+
}

0 commit comments

Comments
 (0)