31 if (disk_conf.device().size() == 0) {
35 std::vector<std::regex> regex_list;
36 static const std::string device_prefix =
"/dev/";
37 for (
auto& pattern : disk_conf.device()) {
38 if (pattern.empty()) {
41 auto pattern_ = pattern;
42 if (pattern[0] !=
'/') {
44 pattern_ = device_prefix + pattern;
47 regex_list.emplace_back(std::regex(pattern_));
48 }
catch (std::regex_error& e) {
49 AERROR <<
"error disk conf: " << pattern <<
", regex_error caught: " << e.what();
54 static const std::string mounts_file =
"/proc/mounts";
55 static constexpr size_t kMaxMounts = 500, kDevice = 0, kPath = 1, kSize = 6;
56 const auto mount_lines =
GetStatsLines(mounts_file, kMaxMounts);
57 for (
const auto& line : mount_lines) {
58 const std::vector<std::string> mounts = absl::StrSplit(line,
' ', absl::SkipWhitespace());
60 if (mounts.size() < kSize || mounts[kDevice].substr(0, device_prefix.size()) != device_prefix) {
63 for (
auto& regex_ : regex_list) {
64 if (std::regex_match(mounts[kDevice], regex_)) {
65 auto device = mounts[kDevice].substr(device_prefix.size());
66 disk_load_device_set_.insert(
device);
67 disk_usage_path_map_.insert({
device, mounts[kPath]});
74 static const std::string disks_stat_file =
"/proc/diskstats";
75 const int device_index = 2, in_out_ms = 12;
76 static constexpr int kDiskInfo = 128;
78 auto devices = disk_metric->mutable_devices();
79 const auto stat_lines =
GetStatsLines(disks_stat_file, kDiskInfo);
80 uint64_t disk_stats = 0;
81 for (
const auto& line : stat_lines) {
82 const std::vector<std::string> stats = absl::StrSplit(line,
' ', absl::SkipWhitespace());
83 auto&
device = stats[device_index];
84 if (disk_load_device_set_.find(
device) == disk_load_device_set_.end()) {
87 disk_stats = std::stoll(stats[in_out_ms]);
88 if (disk_load_pre_stat_map_.find(
device) == disk_load_pre_stat_map_.end()) {
89 disk_load_pre_stat_map_[
device] = disk_stats;
93 auto metric = devices->Add();
98 * (
static_cast<float>(disk_stats - disk_load_pre_stat_map_[
device]) /
static_cast<float>(interval_)));
99 disk_load_pre_stat_map_[
device] = disk_stats;
101 const auto space = boost::filesystem::space(disk_usage_path_map_[
device]);
102 const auto used_gb =
static_cast<uint32_t
>((space.capacity - space.available) >> 30);
103 const auto total_gb =
static_cast<uint32_t
>(space.capacity >> 30);
104 metric->set_used_gb(used_gb);
105 metric->set_available_gb(
static_cast<uint32_t
>(space.available >> 30));
106 metric->set_total_gb(total_gb);
108 metric->set_usage(100.f * used_gb / total_gb);