|
| 1 | +/* |
| 2 | + * Copyright (C) Canonical, Ltd. |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation; version 3. |
| 7 | + * |
| 8 | + * This program is distributed in the hope that it will be useful, |
| 9 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | + * GNU General Public License for more details. |
| 12 | + * |
| 13 | + * You should have received a copy of the GNU General Public License |
| 14 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | + * |
| 16 | + */ |
| 17 | + |
| 18 | +#include "runtime_instance_info_helper.h" |
| 19 | + |
| 20 | +#include <multipass/format.h> |
| 21 | +#include <multipass/rpc/multipass.grpc.pb.h> |
| 22 | +#include <multipass/utils.h> |
| 23 | +#include <multipass/virtual_machine.h> |
| 24 | + |
| 25 | +#include <yaml-cpp/yaml.h> |
| 26 | + |
| 27 | +#include <array> |
| 28 | + |
| 29 | +namespace mp = multipass; |
| 30 | + |
| 31 | +namespace |
| 32 | +{ |
| 33 | + |
| 34 | +struct Keys |
| 35 | +{ |
| 36 | +public: |
| 37 | + static constexpr auto loadavg_key = "loadavg"; |
| 38 | + static constexpr auto mem_usage_key = "mem_usage"; |
| 39 | + static constexpr auto mem_total_key = "mem_total"; |
| 40 | + static constexpr auto disk_usage_key = "disk_usage"; |
| 41 | + static constexpr auto disk_total_key = "disk_total"; |
| 42 | + static constexpr auto cpus_key = "cpus"; |
| 43 | + static constexpr auto current_release_key = "current_release"; |
| 44 | +}; |
| 45 | + |
| 46 | +struct Cmds |
| 47 | +{ |
| 48 | +private: |
| 49 | + static constexpr auto key_val_cmd = R"(echo {}: \"$(eval "{}")\")"; |
| 50 | + static constexpr std::array key_cmds_pairs{ |
| 51 | + std::pair{Keys::loadavg_key, "cat /proc/loadavg | cut -d ' ' -f1-3"}, |
| 52 | + std::pair{Keys::mem_usage_key, R"(free -b | grep 'Mem:' | awk '{printf \$3}')"}, |
| 53 | + std::pair{Keys::mem_total_key, R"(free -b | grep 'Mem:' | awk '{printf \$2}')"}, |
| 54 | + std::pair{Keys::disk_usage_key, "df -t ext4 -t vfat --total -B1 --output=used | tail -n 1"}, |
| 55 | + std::pair{Keys::disk_total_key, "df -t ext4 -t vfat --total -B1 --output=size | tail -n 1"}, |
| 56 | + std::pair{Keys::cpus_key, "nproc"}, |
| 57 | + std::pair{Keys::current_release_key, R"(cat /etc/os-release | grep 'PRETTY_NAME' | cut -d \\\" -f2)"}}; |
| 58 | + |
| 59 | + inline static const std::array cmds = [] { |
| 60 | + constexpr auto n = key_cmds_pairs.size(); |
| 61 | + std::array<std::string, key_cmds_pairs.size()> ret; |
| 62 | + for (std::size_t i = 0; i < n; ++i) |
| 63 | + { |
| 64 | + const auto [key, cmd] = key_cmds_pairs[i]; |
| 65 | + ret[i] = fmt::format(key_val_cmd, key, cmd); |
| 66 | + } |
| 67 | + |
| 68 | + return ret; |
| 69 | + }(); |
| 70 | + |
| 71 | +public: |
| 72 | + inline static const std::string sequential_composite_cmd = fmt::to_string(fmt::join(cmds, "; ")); |
| 73 | + inline static const std::string parallel_composite_cmd = fmt::format("{} & wait", fmt::join(cmds, "& ")); |
| 74 | +}; |
| 75 | +} // namespace |
| 76 | + |
| 77 | +void mp::RuntimeInstanceInfoHelper::populate_runtime_info(mp::VirtualMachine& vm, |
| 78 | + mp::DetailedInfoItem* info, |
| 79 | + mp::InstanceDetails* instance_info, |
| 80 | + const std::string& original_release, |
| 81 | + bool parallelize) |
| 82 | +{ |
| 83 | + const auto& cmd = parallelize ? Cmds::parallel_composite_cmd : Cmds::sequential_composite_cmd; |
| 84 | + auto results = YAML::Load(vm.ssh_exec(cmd)); |
| 85 | + |
| 86 | + instance_info->set_load(results[Keys::loadavg_key].as<std::string>()); |
| 87 | + instance_info->set_memory_usage(results[Keys::mem_usage_key].as<std::string>()); |
| 88 | + info->set_memory_total(results[Keys::mem_total_key].as<std::string>()); |
| 89 | + instance_info->set_disk_usage(results[Keys::disk_usage_key].as<std::string>()); |
| 90 | + info->set_disk_total(results[Keys::disk_total_key].as<std::string>()); |
| 91 | + info->set_cpu_count(results[Keys::cpus_key].as<std::string>()); |
| 92 | + |
| 93 | + auto current_release = results[Keys::current_release_key].as<std::string>(); |
| 94 | + instance_info->set_current_release(!current_release.empty() ? current_release : original_release); |
| 95 | + |
| 96 | + std::string management_ip = vm.management_ipv4(); |
| 97 | + auto all_ipv4 = vm.get_all_ipv4(); |
| 98 | + |
| 99 | + if (MP_UTILS.is_ipv4_valid(management_ip)) |
| 100 | + instance_info->add_ipv4(management_ip); |
| 101 | + else if (all_ipv4.empty()) |
| 102 | + instance_info->add_ipv4("N/A"); |
| 103 | + |
| 104 | + for (const auto& extra_ipv4 : all_ipv4) |
| 105 | + if (extra_ipv4 != management_ip) |
| 106 | + instance_info->add_ipv4(extra_ipv4); |
| 107 | +} |
0 commit comments