From 006c3af3a946b0de8b7c143cc457b460e41ae2fd Mon Sep 17 00:00:00 2001 From: Bernardo-Gabriel Date: Wed, 4 Sep 2024 17:15:07 +0100 Subject: [PATCH 1/2] Monitors/Uptime: added task to periodically read CPU uptime. --- src/Monitors/Uptime/Task.cmake | 0 src/Monitors/Uptime/Task.cpp | 94 ++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 src/Monitors/Uptime/Task.cmake create mode 100644 src/Monitors/Uptime/Task.cpp diff --git a/src/Monitors/Uptime/Task.cmake b/src/Monitors/Uptime/Task.cmake new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/Monitors/Uptime/Task.cpp b/src/Monitors/Uptime/Task.cpp new file mode 100644 index 0000000000..0c689ce257 --- /dev/null +++ b/src/Monitors/Uptime/Task.cpp @@ -0,0 +1,94 @@ +//*************************************************************************** +// Copyright 2007-2024 Universidade do Porto - Faculdade de Engenharia * +// Laboratório de Sistemas e Tecnologia Subaquática (LSTS) * +//*************************************************************************** +// This file is part of DUNE: Unified Navigation Environment. * +// * +// Commercial Licence Usage * +// Licencees holding valid commercial DUNE licences may use this file in * +// accordance with the commercial licence agreement provided with the * +// Software or, alternatively, in accordance with the terms contained in a * +// written agreement between you and Faculdade de Engenharia da * +// Universidade do Porto. For licensing terms, conditions, and further * +// information contact lsts@fe.up.pt. * +// * +// Modified European Union Public Licence - EUPL v.1.1 Usage * +// Alternatively, this file may be used under the terms of the Modified * +// EUPL, Version 1.1 only (the "Licence"), appearing in the file LICENCE.md * +// included in the packaging of this file. You may not use this work * +// except in compliance with the Licence. Unless required by applicable * +// law or agreed to in writing, software distributed under the Licence is * +// distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF * +// ANY KIND, either express or implied. See the Licence for the specific * +// language governing permissions and limitations at * +// https://github.com/LSTS/dune/blob/master/LICENCE.md and * +// http://ec.europa.eu/idabc/eupl.html. * +//*************************************************************************** +// Author: Bernardo Gabriel * +//*************************************************************************** + +// DUNE headers. +#include + +namespace Monitors +{ + //! Task to periodically read CPU uptime. + //! + //! @author Bernardo Gabriel + namespace Uptime + { + using DUNE_NAMESPACES; + + static const char c_file_name[13] = "/proc/uptime"; + + struct Task: public Tasks::Periodic + { + char m_message[64]; + + Task(const std::string& name, Tasks::Context& ctx): + Tasks::Periodic(name, ctx) + { } + + void + checkUptime() + { + std::ifstream f(c_file_name); + + if (f.is_open()) + { + double sec; + f >> sec; + f.close(); + + int days = static_cast(sec / 86400); + sec -= days * 86400; + int hours = static_cast(sec / 3600); + sec -= hours * 3600; + int mins = static_cast(sec / 60); + sec -= mins * 60; + + std::memset(&m_message, '\0', sizeof(m_message)); + sprintf(m_message, "active - %2dd:%02dh:%02dm:%02.0fs", days, hours, mins, sec); + setEntityState(IMC::EntityState::ESTA_NORMAL, Utils::String::str(DTR(m_message))); + } + else + throw FileReadError(c_file_name); + } + + void + task() + { + try + { + checkUptime(); + } + catch(FileReadError& e) + { + spew("%s", DTR(e.what())); + } + } + }; + } +} + +DUNE_TASK From 7defc380025b8155c57b5f9c28495ec72f3902e2 Mon Sep 17 00:00:00 2001 From: Bernardo-Gabriel Date: Wed, 4 Sep 2024 17:15:41 +0100 Subject: [PATCH 2/2] Config: auv/monitors: added Uptime monitor. --- etc/auv/monitors.ini | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/etc/auv/monitors.ini b/etc/auv/monitors.ini index ad8022d06e..ed3ee23c08 100644 --- a/etc/auv/monitors.ini +++ b/etc/auv/monitors.ini @@ -215,3 +215,8 @@ Entity Label - Current 0 = Servo Controller 0 Entity Label - Current 1 = Servo Controller 1 Entity Label - Current 2 = Servo Controller 2 Entity Label - Current 3 = Servo Controller 3 + +[Monitors.Uptime] +Enabled = Always +Entity Label = CPU Uptime +Debug Level = None \ No newline at end of file