pingCheck method
Returns a map with the status of the specific component. Throws an exception if the component is critically failing.
Implementation
@override
Future<HealthIndicatorCheck> pingCheck(RequestContext _) async {
try {
ProcessResult result;
if (Platform.isWindows) {
result = await Process.run('wmic', [
'logicaldisk',
'get',
'size,freespace',
]);
} else {
result = await Process.run('df', ['-k', p.normalize(p.absolute(path))]);
}
if (result.exitCode != 0) {
throw Exception('Process failed with exit code ${result.exitCode}');
}
final lines = result.stdout.toString().trim().split('\n');
if (lines.length < 2) {
throw Exception('Unrecognized output format');
}
final columns = lines[1].trim().split(RegExp(r'\s+'));
final usePercentString = Platform.isWindows
? ((int.parse(columns[0]) / int.parse(columns[1])) * 100)
.toStringAsFixed(0)
: columns[4].replaceAll('%', '');
final usePercent = int.parse(usePercentString) / 100;
final formattedUsed = '${(usePercent * 100).toStringAsFixed(0)}%';
final formattedThreshold =
'${(thresholdPercent * 100).toStringAsFixed(0)}%';
if (usePercent < thresholdPercent) {
return (status: HealthStatus.up, details: {'used': formattedUsed});
} else {
return (
status: HealthStatus.down,
details: {
'error': 'Used disk storage exceeded the set threshold',
'used': formattedUsed,
'limit': formattedThreshold,
},
);
}
} catch (e) {
return (
status: HealthStatus.down,
details: {'error': 'Failed to read disk space: $e'},
);
}
}