|
| 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 | +} |
0 commit comments