1
+ #ifndef AM_SUPER_INCLUDE_RESOURCE_MONITOR_RESOURCE_STATUS_CLASS_H_
2
+ #define AM_SUPER_INCLUDE_RESOURCE_MONITOR_RESOURCE_STATUS_CLASS_H_
3
+
4
+ #include < iostream>
5
+ #include < am_utils/am_ros2_utility.h>
6
+
7
+ #include < iostream>
8
+ #include < fstream>
9
+ #include < sstream>
10
+ #include < string>
11
+ #include < unistd.h>
12
+ #include < vb_util_lib/transformer.h>
13
+ #include < resource_monitor/resource_monitor_stats.h>
14
+ #include < std_msgs/msg/int32.hpp>
15
+ #include < sys/statvfs.h> // For statvfs
16
+ #include < iomanip> // For std::setprecision
17
+ #include < brain_box_msgs/msg/system_report.hpp>
18
+ #include < vb_util_lib/bag_logger.h>
19
+ #include < vb_util_lib/vb_main.h>
20
+ #include < brain_box_msgs/msg/log_control.hpp>
21
+
22
+ namespace am
23
+ {
24
+
25
+ struct MemoryInfo
26
+ {
27
+ unsigned long total;
28
+ unsigned long free;
29
+ unsigned long used;
30
+ unsigned long available;
31
+ int used_percent;
32
+ };
33
+
34
+ struct GpuInfo
35
+ {
36
+ std::string gpu_name;
37
+ int temp;
38
+ int load_percent;
39
+ };
40
+
41
+ struct CpuInfo
42
+ {
43
+ unsigned long long user;
44
+ unsigned long long nice;
45
+ unsigned long long system;
46
+ unsigned long long idle;
47
+ unsigned long long iowait;
48
+ unsigned long long irq;
49
+ unsigned long long softirq;
50
+ unsigned long long steal;
51
+ unsigned long long total;
52
+ };
53
+
54
+ struct DiskInfo {
55
+ unsigned long long totalSpace; // Total space in bytes
56
+ unsigned long long availableSpace; // Available space in bytes (matches `df`)
57
+ unsigned long long usedSpace; // Used space in bytes
58
+ double percentUsed; // Percentage used
59
+ };
60
+
61
+ class ResourceStatus
62
+ {
63
+ public:
64
+ ResourceStatus (std::shared_ptr<am::ResourceMonitorStats> stats);
65
+
66
+ ~ResourceStatus ();
67
+
68
+ am::MemoryInfo& getMemoryInfo ();
69
+
70
+ am::CpuInfo getCPUInfo ();
71
+
72
+ am::GpuInfo getGPUInfo ();
73
+
74
+ void getCPUInfo (std::vector<am::CpuInfo> &infos);
75
+
76
+ DiskInfo getDiskInfo (const std::string& path = " /" );
77
+
78
+ double calculateCpuLoad (const am::CpuInfo &ci, const am::CpuInfo &ci_old);
79
+
80
+ double getUpTime ();
81
+
82
+ void updateInfos (brain_box_msgs::msg::SystemReport &sys_report);
83
+
84
+ void print ();
85
+
86
+ bool isReachable (const std::string &ipAddress);
87
+
88
+ void getParams ();
89
+
90
+ std::unordered_set<std::string> getActiveIPs (const std::string& subnet = " 192.168.1.0/24" );
91
+
92
+ std::shared_ptr<am::ResourceMonitorStats> getStats ();
93
+
94
+ std::vector<std::string> getInetAddresses ();
95
+
96
+ // AMLifeCycle passthrus
97
+ bool onConfigure ();
98
+
99
+ bool onCleanup ();
100
+
101
+ void heartbeatCB ();
102
+
103
+ private:
104
+
105
+ std::shared_ptr<am::ResourceMonitorStats> stats_;
106
+
107
+ rclcpp::Subscription<std_msgs::msg::Int32>::SharedPtr status_sub_;
108
+
109
+ rclcpp::Subscription<std_msgs::msg::Int32>::SharedPtr stat_sub_;
110
+
111
+ rclcpp::Subscription<brain_box_msgs::msg::LogControl>::SharedPtr log_ctrl_sub_;
112
+
113
+ rclcpp::Publisher<brain_box_msgs::msg::SystemReport>::SharedPtr sys_rep_pub_;
114
+
115
+ void statusCB (const std_msgs::msg::Int32::SharedPtr msg);
116
+
117
+ void statCB (const std_msgs::msg::Int32::SharedPtr msg);
118
+
119
+ void logCtrlCB (const brain_box_msgs::msg::LogControl::SharedPtr msg);
120
+
121
+ int getCPUCoresCount ();
122
+
123
+ std::string readFile (const std::string& path);
124
+
125
+ am::CpuInfo parseCpuLine (const std::string &line);
126
+
127
+ int cpu_cnt_= -1 ;
128
+
129
+ double cpu_usage_;
130
+
131
+ double uptime_seconds_;
132
+
133
+ bool ip_check_ {false };
134
+
135
+ bool is_first_time_ {true };
136
+
137
+ BagLogger::BagLoggerLevel level_;
138
+
139
+ bool enabled_ {false };
140
+
141
+ std::vector<double > cpu_loads_;
142
+
143
+ std::string system_status_topic_ = " /resource_status" ;
144
+
145
+ am::MemoryInfo mi;
146
+
147
+ std::vector<am::CpuInfo> cpu_infos_;
148
+
149
+ std::vector<am::CpuInfo> cpu_infos_old_;
150
+
151
+ am::GpuInfo gpu_info_;
152
+
153
+ std::vector<std::string> sub_nets_add_;
154
+
155
+ std::map<std::string, std::string> ip_addresses_; // IPAddress, Name
156
+
157
+ /* ROS Infrastructure Checking tools*/
158
+ std::shared_ptr<am::Transformer> transformer_;
159
+
160
+ std::vector<std::pair<std::string, std::string>> transform_list_;
161
+
162
+ rclcpp::TimerBase::SharedPtr timer_;
163
+
164
+ void timerCB ();
165
+
166
+ void checkNodeNames (brain_box_msgs::msg::SystemReport &sys_report);
167
+
168
+ void checkTransforms (brain_box_msgs::msg::SystemReport &sys_report);
169
+
170
+ void checkSensorIPs (brain_box_msgs::msg::SystemReport &sys_report);
171
+ };
172
+ }
173
+
174
+ #endif /* AM_SUPER_INCLUDE_RESOURCE_MONITOR_RESOURCE_STATUS_CLASS_H_*/
0 commit comments