From 53f781f52738b415b465dc97145421285871df85 Mon Sep 17 00:00:00 2001 From: Michael Carlberg Date: Fri, 30 Dec 2016 22:44:28 +0100 Subject: [PATCH] feat(fs): Add option to remove invalid/unmounted endpoints --- include/modules/fs.hpp | 7 ++++--- src/modules/fs.cpp | 30 ++++++++++++++++++++++++------ 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/include/modules/fs.hpp b/include/modules/fs.hpp index 2155ec01..45cbc1e3 100644 --- a/include/modules/fs.hpp +++ b/include/modules/fs.hpp @@ -61,11 +61,12 @@ namespace modules { vector m_mountpoints; vector m_mounts; - bool m_fixed = false; - int m_spacing = 2; + bool m_fixed{false}; + bool m_remove_unmounted{false}; + int m_spacing{2}; // used while formatting output - size_t m_index = 0; + size_t m_index{0_z}; }; } diff --git a/src/modules/fs.cpp b/src/modules/fs.cpp index d71219ad..9d7d26bb 100644 --- a/src/modules/fs.cpp +++ b/src/modules/fs.cpp @@ -23,9 +23,10 @@ namespace modules { */ fs_module::fs_module(const bar_settings& bar, string name_) : timer_module(bar, move(name_)) { m_mountpoints = m_conf.get_list(name(), "mount"); - m_fixed = m_conf.get(name(), "fixed-values", m_fixed); - m_spacing = m_conf.get(name(), "spacing", m_spacing); - m_interval = chrono::duration(m_conf.get(name(), "interval", 30)); + m_remove_unmounted = m_conf.get(name(), "remove-unmounted", m_remove_unmounted); + m_fixed = m_conf.get(name(), "fixed-values", m_fixed); + m_spacing = m_conf.get(name(), "spacing", m_spacing); + m_interval = m_conf.get(name(), "interval", 30s); // Add formats and elements m_formatter->add( @@ -47,6 +48,12 @@ namespace modules { if (m_formatter->has(TAG_RAMP_CAPACITY)) { m_rampcapacity = load_ramp(m_conf, name(), TAG_RAMP_CAPACITY); } + + // Warn about "unreachable" format tag + if (m_formatter->has(TAG_LABEL_UNMOUNTED) && m_remove_unmounted) { + m_log.warn("%s: Defined format tag \"%s\" will never be used (reason: `remove-unmounted = true`)", name(), + TAG_LABEL_UNMOUNTED); + } } /** @@ -88,14 +95,25 @@ namespace modules { mount->bytes_free = b_free; mount->bytes_used = b_used; - mount->percentage_free = math_util::percentage(b_avail, 0, b_total); - mount->percentage_used = math_util::percentage(b_used, 0, b_total); + mount->percentage_free = math_util::percentage(b_avail, 0UL, b_total); + mount->percentage_used = math_util::percentage(b_used, 0UL, b_total); mount->percentage_free_s = string_util::floatval(mount->percentage_free, 2, m_fixed, m_bar.locale); mount->percentage_used_s = string_util::floatval(mount->percentage_used, 2, m_fixed, m_bar.locale); } } + if (m_remove_unmounted) { + for (auto&& mount : m_mounts) { + if (!mount->mounted) { + m_log.info("%s: Removing mountpoint \"%s\" (reason: `remove-unmounted = true`)", name(), mount->mountpoint); + m_mountpoints.erase( + std::remove(m_mountpoints.begin(), m_mountpoints.end(), mount->mountpoint), m_mountpoints.end()); + m_mounts.erase(std::remove(m_mounts.begin(), m_mounts.end(), mount), m_mounts.end()); + } + } + } + return true; } @@ -105,7 +123,7 @@ namespace modules { string fs_module::get_output() { string output; - for (m_index = 0; m_index < m_mounts.size(); ++m_index) { + for (m_index = 0_z; m_index < m_mounts.size(); ++m_index) { if (!output.empty()) { m_builder->space(m_spacing); }