|
| 1 | +/* |
| 2 | + * Copyright 2025 iLogtail Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include "host_monitor/collector/CPUCollector.h" |
| 18 | + |
| 19 | +#include <string> |
| 20 | + |
| 21 | +#include "boost/algorithm/string.hpp" |
| 22 | +#include "boost/algorithm/string/split.hpp" |
| 23 | + |
| 24 | +#include "MetricValue.h" |
| 25 | +#include "common/StringTools.h" |
| 26 | +#include "host_monitor/Constants.h" |
| 27 | +#include "host_monitor/SystemInformationTools.h" |
| 28 | +#include "logger/Logger.h" |
| 29 | + |
| 30 | +namespace logtail { |
| 31 | + |
| 32 | +const std::string CPUCollector::sName = "cpu"; |
| 33 | +const std::string kMetricLabelCPU = "cpu"; |
| 34 | +const std::string kMetricLabelMode = "mode"; |
| 35 | + |
| 36 | +bool CPUCollector::Collect(const HostMonitorTimerEvent::CollectConfig& collectConfig, PipelineEventGroup* group) { |
| 37 | + if (group == nullptr) { |
| 38 | + return false; |
| 39 | + } |
| 40 | + std::vector<CPUStat> cpus; |
| 41 | + if (!GetHostSystemCPUStat(cpus)) { |
| 42 | + return false; |
| 43 | + } |
| 44 | + const time_t now = time(nullptr); |
| 45 | + constexpr struct MetricDef { |
| 46 | + const char* name; |
| 47 | + const char* mode; |
| 48 | + double CPUStat::*value; |
| 49 | + } metrics[] = { |
| 50 | + {"node_cpu_seconds_total", "user", &CPUStat::user}, |
| 51 | + {"node_cpu_seconds_total", "nice", &CPUStat::nice}, |
| 52 | + {"node_cpu_seconds_total", "system", &CPUStat::system}, |
| 53 | + {"node_cpu_seconds_total", "idle", &CPUStat::idle}, |
| 54 | + {"node_cpu_seconds_total", "iowait", &CPUStat::iowait}, |
| 55 | + {"node_cpu_seconds_total", "irq", &CPUStat::irq}, |
| 56 | + {"node_cpu_seconds_total", "softirq", &CPUStat::softirq}, |
| 57 | + {"node_cpu_seconds_total", "steal", &CPUStat::steal}, |
| 58 | + {"node_cpu_guest_seconds_total", "user", &CPUStat::guest}, |
| 59 | + {"node_cpu_guest_seconds_total", "nice", &CPUStat::guestNice}, |
| 60 | + }; |
| 61 | + for (const auto& cpu : cpus) { |
| 62 | + if (cpu.index == -1) { |
| 63 | + continue; |
| 64 | + } |
| 65 | + for (const auto& def : metrics) { |
| 66 | + auto* metricEvent = group->AddMetricEvent(true); |
| 67 | + if (!metricEvent) { |
| 68 | + continue; |
| 69 | + } |
| 70 | + metricEvent->SetName(def.name); |
| 71 | + metricEvent->SetTimestamp(now, 0); |
| 72 | + metricEvent->SetValue<UntypedSingleValue>(cpu.*(def.value) / SYSTEM_HERTZ); |
| 73 | + metricEvent->SetTag(kMetricLabelCPU, std::to_string(cpu.index)); |
| 74 | + metricEvent->SetTagNoCopy(kMetricLabelMode, def.mode); |
| 75 | + } |
| 76 | + } |
| 77 | + return true; |
| 78 | +} |
| 79 | + |
| 80 | +bool CPUCollector::GetHostSystemCPUStat(std::vector<CPUStat>& cpus) { |
| 81 | + std::vector<std::string> cpuLines; |
| 82 | + std::string errorMessage; |
| 83 | + if (!GetHostSystemStat(cpuLines, errorMessage)) { |
| 84 | + if (mValidState) { |
| 85 | + LOG_WARNING(sLogger, ("failed to get system cpu", "invalid CPU collector")("error msg", errorMessage)); |
| 86 | + mValidState = false; |
| 87 | + } |
| 88 | + return false; |
| 89 | + } |
| 90 | + mValidState = true; |
| 91 | + // cpu 1195061569 1728645 418424132 203670447952 14723544 0 773400 0 0 0 |
| 92 | + // cpu0 14708487 14216 4613031 2108180843 57199 0 424744 0 0 0 |
| 93 | + // ... |
| 94 | + cpus.clear(); |
| 95 | + cpus.reserve(cpuLines.size()); |
| 96 | + for (auto const& line : cpuLines) { |
| 97 | + std::vector<std::string> cpuMetric; |
| 98 | + boost::split(cpuMetric, line, boost::is_any_of(" "), boost::token_compress_on); |
| 99 | + if (cpuMetric.size() > 0 && cpuMetric[0].substr(0, 3) == "cpu") { |
| 100 | + CPUStat cpuStat{}; |
| 101 | + if (cpuMetric[0] == "cpu") { |
| 102 | + cpuStat.index = -1; |
| 103 | + } else { |
| 104 | + try { |
| 105 | + cpuStat.index = StringTo<int32_t>(cpuMetric[0].substr(3)); |
| 106 | + } catch (...) { |
| 107 | + LOG_ERROR(sLogger, ("failed to parse cpu index", "skip")("wrong cpu index", cpuMetric[0])); |
| 108 | + continue; |
| 109 | + } |
| 110 | + } |
| 111 | + cpuStat.user = ParseMetric(cpuMetric, EnumCpuKey::user); |
| 112 | + cpuStat.nice = ParseMetric(cpuMetric, EnumCpuKey::nice); |
| 113 | + cpuStat.system = ParseMetric(cpuMetric, EnumCpuKey::system); |
| 114 | + cpuStat.idle = ParseMetric(cpuMetric, EnumCpuKey::idle); |
| 115 | + cpuStat.iowait = ParseMetric(cpuMetric, EnumCpuKey::iowait); |
| 116 | + cpuStat.irq = ParseMetric(cpuMetric, EnumCpuKey::irq); |
| 117 | + cpuStat.softirq = ParseMetric(cpuMetric, EnumCpuKey::softirq); |
| 118 | + cpuStat.steal = ParseMetric(cpuMetric, EnumCpuKey::steal); |
| 119 | + cpuStat.guest = ParseMetric(cpuMetric, EnumCpuKey::guest); |
| 120 | + cpuStat.guestNice = ParseMetric(cpuMetric, EnumCpuKey::guest_nice); |
| 121 | + cpus.push_back(cpuStat); |
| 122 | + } |
| 123 | + } |
| 124 | + return true; |
| 125 | +} |
| 126 | + |
| 127 | +double CPUCollector::ParseMetric(const std::vector<std::string>& cpuMetric, EnumCpuKey key) const { |
| 128 | + try { |
| 129 | + if (cpuMetric.size() <= static_cast<size_t>(key)) { |
| 130 | + return 0; |
| 131 | + } |
| 132 | + return StringTo<double>(cpuMetric[static_cast<size_t>(key)]); |
| 133 | + } catch (...) { |
| 134 | + return 0.0; |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +} // namespace logtail |
0 commit comments