blob: df1dd221eb78377d52de210496226da537592993 [file] [log] [blame]
Stephan Pleinesb1177672024-05-27 17:48:32 -07001#include <stddef.h>
Austin Schuhc979f7c2024-03-02 16:49:20 -08002#include <sys/statvfs.h>
3
Stephan Pleinesb1177672024-05-27 17:48:32 -07004#include <algorithm>
5#include <chrono>
6#include <istream>
7#include <optional>
8#include <string>
9#include <string_view>
10#include <vector>
Austin Schuhc979f7c2024-03-02 16:49:20 -080011
Stephan Pleinesb1177672024-05-27 17:48:32 -070012#include "absl/strings/str_split.h"
13#include "flatbuffers/buffer.h"
14#include "flatbuffers/flatbuffer_builder.h"
15#include "flatbuffers/string.h"
16#include "flatbuffers/vector.h"
17#include "gflags/gflags.h"
18#include "glog/logging.h"
19
20#include "aos/configuration.h"
21#include "aos/events/event_loop.h"
Austin Schuhc979f7c2024-03-02 16:49:20 -080022#include "aos/events/shm_event_loop.h"
Stephan Pleinesb1177672024-05-27 17:48:32 -070023#include "aos/flatbuffers.h"
Austin Schuhc979f7c2024-03-02 16:49:20 -080024#include "aos/init.h"
25#include "aos/util/filesystem_generated.h"
26
27DEFINE_string(config, "aos_config.json", "File path of aos configuration");
28
29namespace aos::util {
30namespace {
31std::optional<std::string> ReadShortFile(std::string_view file_name) {
32 // Open as input and seek to end immediately.
33 std::ifstream file(std::string(file_name), std::ios_base::in);
34 if (!file.good()) {
35 VLOG(1) << "Can't read " << file_name;
36 return std::nullopt;
37 }
38 const size_t kMaxLineLength = 4096;
39 char buffer[kMaxLineLength];
40 file.read(buffer, kMaxLineLength);
41 if (!file.eof()) {
42 return std::nullopt;
43 }
44 return std::string(buffer, file.gcount());
45}
46} // namespace
47
48// Periodically sends out the Filesystems message with filesystem utilization
49// info.
50class FilesystemMonitor {
51 public:
52 FilesystemMonitor(aos::EventLoop *event_loop)
53 : event_loop_(event_loop),
54 sender_(event_loop_->MakeSender<FilesystemStatus>("/aos")) {
55 periodic_timer_ =
56 event_loop_->AddTimer([this]() { PublishFilesystemStatus(); });
57 event_loop_->OnRun([this]() {
58 periodic_timer_->Schedule(event_loop_->monotonic_now(),
59 std::chrono::seconds(5));
60 });
61 }
62
63 private:
64 void PublishFilesystemStatus() {
65 aos::Sender<FilesystemStatus>::Builder builder = sender_.MakeBuilder();
66
67 std::optional<std::string> contents = ReadShortFile("/proc/self/mountinfo");
68
69 CHECK(contents.has_value());
70
71 std::vector<flatbuffers::Offset<Filesystem>> filesystems;
72
73 // Iterate through /proc/self/mounts to find all the filesystems.
74 for (std::string_view line :
75 absl::StrSplit(std::string_view(contents->c_str(), contents->size()),
76 '\n', absl::SkipWhitespace())) {
77 // See https://www.kernel.org/doc/Documentation/filesystems/proc.txt for
78 // the format.
79 std::vector<std::string_view> elements =
80 absl::StrSplit(line, ' ', absl::SkipWhitespace());
81
82 // First thing after - is the filesystem type.
83 size_t i = 6;
84 while (elements[i] != "-") {
85 ++i;
86 CHECK_LT(i + 1, elements.size());
87 }
88
89 // Mount point is the 4th element.
90 std::string mount_point(elements[4]);
91 std::string_view type = elements[i + 1];
92
93 // Ignore filesystems without reasonable types.
94 if (type != "ext2" && type != "xfs" && type != "vfat" && type != "ext3" &&
95 type != "ext4" && type != "tmpfs" && type != "devtmpfs") {
96 continue;
97 }
98 VLOG(1) << mount_point << ", type " << type;
99
100 struct statvfs info;
101
102 PCHECK(statvfs(mount_point.c_str(), &info) == 0);
103
104 VLOG(1) << "overall size: " << info.f_frsize * info.f_blocks << ", free "
105 << info.f_bfree * info.f_bsize << ", inodes " << info.f_files
106 << ", free " << info.f_ffree;
107
108 flatbuffers::Offset<flatbuffers::String> path_offset =
109 builder.fbb()->CreateString(mount_point);
110 flatbuffers::Offset<flatbuffers::String> type_offset =
111 builder.fbb()->CreateString(type);
112 Filesystem::Builder filesystem_builder =
113 builder.MakeBuilder<Filesystem>();
114 filesystem_builder.add_path(path_offset);
115 filesystem_builder.add_type(type_offset);
116 filesystem_builder.add_overall_space(info.f_frsize * info.f_blocks);
117 filesystem_builder.add_free_space(info.f_bfree * info.f_bsize);
118 filesystem_builder.add_overall_inodes(info.f_files);
119 filesystem_builder.add_free_inodes(info.f_ffree);
120
121 filesystems.emplace_back(filesystem_builder.Finish());
122 }
123
124 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Filesystem>>>
125 filesystems_offset = builder.fbb()->CreateVector(filesystems);
126
127 FilesystemStatus::Builder filesystem_status_builder =
128 builder.MakeBuilder<FilesystemStatus>();
129
130 filesystem_status_builder.add_filesystems(filesystems_offset);
131
132 (void)builder.Send(filesystem_status_builder.Finish());
133 }
134
135 aos::EventLoop *event_loop_;
136
137 aos::Sender<FilesystemStatus> sender_;
138
139 aos::TimerHandler *periodic_timer_;
140};
141
142} // namespace aos::util
143
144int main(int argc, char **argv) {
145 aos::InitGoogle(&argc, &argv);
146
147 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
148 aos::configuration::ReadConfig(FLAGS_config);
149
150 aos::ShmEventLoop shm_event_loop(&config.message());
151
152 aos::util::FilesystemMonitor filesystem_monitor(&shm_event_loop);
153
154 shm_event_loop.Run();
155
156 return 0;
157}