Apollo 10.0
自动驾驶开放平台
process_monitor.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2017 The Apollo Authors. All Rights Reserved.
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
18
19#include "gflags/gflags.h"
20
21#include "cyber/common/file.h"
22#include "cyber/common/log.h"
26
27DEFINE_string(process_monitor_name, "ProcessMonitor",
28 "Name of the process monitor.");
29
30DEFINE_double(process_monitor_interval, 1.5,
31 "Process status checking interval in seconds.");
32
33namespace apollo {
34namespace monitor {
35
37 : RecurrentRunner(FLAGS_process_monitor_name,
38 FLAGS_process_monitor_interval) {}
39
40void ProcessMonitor::RunOnce(const double current_time) {
41 // Get running processes.
42 std::vector<std::string> running_processes;
43 for (const auto& cmd_file : cyber::common::Glob("/proc/*/cmdline")) {
44 // Get process command string.
45 std::string cmd_string;
46 if (cyber::common::GetContent(cmd_file, &cmd_string) &&
47 !cmd_string.empty()) {
48 // In /proc/<PID>/cmdline, the parts are separated with \0, which will be
49 // converted back to whitespaces here.
50 std::replace(cmd_string.begin(), cmd_string.end(), '\0', ' ');
51 running_processes.push_back(cmd_string);
52 }
53 }
54
55 auto manager = MonitorManager::Instance();
56 const auto& mode = manager->GetHMIMode();
57
58 // Check HMI modules.
59 auto* hmi_modules = manager->GetStatus()->mutable_hmi_modules();
60 for (const auto& iter : mode.modules()) {
61 const std::string& module_name = iter.first;
62 const auto& config = iter.second.process_monitor_config();
63 UpdateStatus(running_processes, config, &hmi_modules->at(module_name));
64 }
65
66 // Check monitored components.
67 auto* components = manager->GetStatus()->mutable_components();
68 for (const auto& iter : mode.monitored_components()) {
69 const std::string& name = iter.first;
70 if (iter.second.has_process() &&
71 apollo::common::util::ContainsKey(*components, name)) {
72 const auto& config = iter.second.process();
73 auto* status = components->at(name).mutable_process_status();
74 UpdateStatus(running_processes, config, status);
75 }
76 }
77
78 // Check other components.
79 auto* other_components = manager->GetStatus()->mutable_other_components();
80 for (const auto& iter : mode.other_components()) {
81 const std::string& name = iter.first;
82 const auto& config = iter.second;
83 UpdateStatus(running_processes, config, &other_components->at(name));
84 }
85
86 // Check global components.
87 auto* global_components = manager->GetStatus()->mutable_global_components();
88 for (const auto& iter : mode.global_components()) {
89 const std::string& name = iter.first;
90 if (iter.second.has_process() &&
91 apollo::common::util::ContainsKey(*global_components, name)) {
92 const auto& config = iter.second.process();
93 auto* status = global_components->at(name).mutable_process_status();
94 UpdateStatus(running_processes, config, status);
95 }
96 }
97}
98
99void ProcessMonitor::UpdateStatus(
100 const std::vector<std::string>& running_processes,
102 ComponentStatus* status) {
103 status->clear_status();
104 for (const std::string& command : running_processes) {
105 bool all_keywords_matched = true;
106 for (const std::string& keyword : config.command_keywords()) {
107 if (command.find(keyword) == std::string::npos) {
108 all_keywords_matched = false;
109 break;
110 }
111 }
112 if (all_keywords_matched) {
113 // Process command keywords are all matched. The process is running.
114 SummaryMonitor::EscalateStatus(ComponentStatus::OK, command, status);
115 return;
116 }
117 }
118 SummaryMonitor::EscalateStatus(ComponentStatus::FATAL, "", status);
119}
120
121} // namespace monitor
122} // namespace apollo
void RunOnce(const double current_time) override
static void EscalateStatus(const ComponentStatus::Status new_status, const std::string &message, ComponentStatus *current_status)
Some map util functions.
std::vector< std::string > Glob(const std::string &pattern)
Expand path pattern to matched paths.
Definition file.cc:212
bool GetContent(const std::string &file_name, std::string *content)
Get file content as string.
Definition file.cc:167
class register implement
Definition arena_queue.h:37
DEFINE_string(process_monitor_name, "ProcessMonitor", "Name of the process monitor.")
DEFINE_double(process_monitor_interval, 1.5, "Process status checking interval in seconds.")