Skip to content

Commit

Permalink
json monitoring API added (C++, Python)
Browse files Browse the repository at this point in the history
  • Loading branch information
rex-schilasky committed Aug 8, 2024
1 parent 72a8b5c commit 0f51c70
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 7 deletions.
12 changes: 11 additions & 1 deletion ecal/core/include/ecal/ecal_monitoring.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* ========================= eCAL LICENSE =================================
*
* Copyright (C) 2016 - 2019 Continental Corporation
* Copyright (C) 2016 - 2024 Continental Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -93,6 +93,16 @@ namespace eCAL
ECAL_API int GetMonitoring(eCAL::Monitoring::SMonitoring& mon_, unsigned int entities_ = Entity::All);


/**
* @brief Get monitoring subset as JSON string.
*
* @param [out] mon_ String to store the monitoring information.
* @param entities_ Entities to get.
*
* @return Zero if succeeded.
**/
ECAL_API int GetMonitoringJSON(std::string& mon_, unsigned int entities_ = Entity::All);

/**
* @brief Get logging as serialized protobuf string.
*
Expand Down
13 changes: 12 additions & 1 deletion ecal/core/src/monitoring/ecal_monitoring_def.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* ========================= eCAL LICENSE =================================
*
* Copyright (C) 2016 - 2019 Continental Corporation
* Copyright (C) 2016 - 2024 Continental Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -74,6 +74,11 @@ namespace eCAL
m_monitoring_impl->GetMonitoringStructs(monitoring_, entities_);
}

void CMonitoring::GetMonitoringJSON(std::string& monitoring_, unsigned int entities_)
{
m_monitoring_impl->GetMonitoringJSON(monitoring_, entities_);
}

namespace Monitoring
{
////////////////////////////////////////////////////////
Expand Down Expand Up @@ -125,6 +130,12 @@ namespace eCAL
return(0);
}

ECAL_API int GetMonitoringJSON(std::string& mon_, unsigned int entities_)
{
if (g_monitoring() != nullptr) g_monitoring()->GetMonitoringJSON(mon_, entities_);
return((int)mon_.size());
}

int GetLogging(std::string& log_)
{
eCAL::pb::Logging logging;
Expand Down
3 changes: 2 additions & 1 deletion ecal/core/src/monitoring/ecal_monitoring_def.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* ========================= eCAL LICENSE =================================
*
* Copyright (C) 2016 - 2019 Continental Corporation
* Copyright (C) 2016 - 2024 Continental Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -57,6 +57,7 @@ namespace eCAL

void GetMonitoring(eCAL::pb::Monitoring& monitoring_, unsigned int entities_ = Monitoring::Entity::All);
void GetMonitoring(eCAL::Monitoring::SMonitoring& monitoring_, unsigned int entities_ = Monitoring::Entity::All);
void GetMonitoringJSON(std::string& monitoring_, unsigned int entities_);

protected:
std::unique_ptr<CMonitoringImpl> m_monitoring_impl;
Expand Down
28 changes: 27 additions & 1 deletion ecal/core/src/monitoring/ecal_monitoring_impl.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* ========================= eCAL LICENSE =================================
*
* Copyright (C) 2016 - 2019 Continental Corporation
* Copyright (C) 2016 - 2024 Continental Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -38,6 +38,20 @@

#include "registration/ecal_registration_receiver.h"

#ifdef _MSC_VER
#pragma warning(push, 0) // disable proto warnings
#endif
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#endif
#include <google/protobuf/util/json_util.h>
#ifdef _MSC_VER
#pragma warning(pop)
#endif
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif

namespace eCAL
{
Expand Down Expand Up @@ -553,6 +567,18 @@ namespace eCAL
return(pHostMap);
}

void CMonitoringImpl::GetMonitoringJSON(std::string& monitoring_, unsigned int entities_)
{
monitoring_.clear();

eCAL::pb::Monitoring monitoring_pb;
GetMonitoringPb(monitoring_pb, entities_);

google::protobuf::util::JsonPrintOptions options;
options.add_whitespace = true; // enables pretty printing with whitespace
google::protobuf::util::MessageToJsonString(monitoring_pb, &monitoring_, options);
}

void CMonitoringImpl::GetMonitoringPb(eCAL::pb::Monitoring& monitoring_, unsigned int entities_)
{
// clear protobuf object
Expand Down
3 changes: 2 additions & 1 deletion ecal/core/src/monitoring/ecal_monitoring_impl.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* ========================= eCAL LICENSE =================================
*
* Copyright (C) 2016 - 2019 Continental Corporation
* Copyright (C) 2016 - 2024 Continental Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -61,6 +61,7 @@ namespace eCAL
void SetInclFilter(const std::string& filter_);
void SetFilterState(bool state_);

void GetMonitoringJSON(std::string& monitoring_, unsigned int entities_);
void GetMonitoringPb(eCAL::pb::Monitoring& monitoring_, unsigned int entities_);
void GetMonitoringStructs(eCAL::Monitoring::SMonitoring& monitoring_, unsigned int entities_);

Expand Down
6 changes: 5 additions & 1 deletion lang/python/core/ecal/core/core.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# ========================= eCAL LICENSE =================================
#
# Copyright (C) 2016 - 2019 Continental Corporation
# Copyright (C) 2016 - 2024 Continental Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -569,6 +569,10 @@ def mon_monitoring():
"""
return _ecal.mon_monitoring()

def mon_monitoring_json():
""" get all host, process and topic information as json string
"""
return _ecal.mon_monitoring_json()

def mon_logging():
""" get list of ecal log messages
Expand Down
13 changes: 12 additions & 1 deletion lang/python/core/src/ecal_wrap.cxx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* ========================= eCAL LICENSE =================================
*
* Copyright (C) 2016 - 2019 Continental Corporation
* Copyright (C) 2016 - 2024 Continental Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -1525,6 +1525,16 @@ PyObject* mon_monitoring(PyObject* /*self*/, PyObject* /*args*/)
return(Py_BuildValue("iO", 0, retDict));
}

/****************************************/
/* mon_monitoring_json */
/****************************************/
PyObject* mon_monitoring_json(PyObject* /*self*/, PyObject* /*args*/)
{
std::string monitoring_s;
eCAL::Monitoring::GetMonitoringJSON(monitoring_s);
return(Py_BuildValue("s", ecal_getversion()));
}

/****************************************/
/* mon_logging */
/****************************************/
Expand Down Expand Up @@ -1679,6 +1689,7 @@ static PyMethodDef _ecal_methods[] =
{"mon_setfilterstate", mon_setfilterstate, METH_VARARGS, "mon_setfilterstate(state)"},

{"mon_monitoring", mon_monitoring, METH_NOARGS, "mon_monitoring()"},
{"mon_monitoring_json", mon_monitoring_json, METH_NOARGS, "mon_monitoring_json()"},
{"mon_logging", mon_logging, METH_NOARGS, "mon_logging()"},

{"mon_pubmonitoring", mon_pubmonitoring, METH_VARARGS, "mon_pubmonitoring(state, name)"},
Expand Down
1 change: 1 addition & 0 deletions samples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ add_subdirectory(cpp/monitoring/monitoring_get_services)
add_subdirectory(cpp/monitoring/monitoring_get_topics)
add_subdirectory(cpp/monitoring/monitoring_performance)
add_subdirectory(cpp/monitoring/monitoring_rec)
add_subdirectory(cpp/monitoring/monitoring_rec_json)
add_subdirectory(cpp/monitoring/monitoring_reg)
add_subdirectory(cpp/orchestration/component1)
add_subdirectory(cpp/orchestration/component2)
Expand Down
41 changes: 41 additions & 0 deletions samples/cpp/monitoring/monitoring_rec_json/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# ========================= eCAL LICENSE =================================
#
# Copyright (C) 2016 - 2024 Continental Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# ========================= eCAL LICENSE =================================

cmake_minimum_required(VERSION 3.10)

set(CMAKE_FIND_PACKAGE_PREFER_CONFIG ON)

project(monitoring_rec_json)

find_package(eCAL REQUIRED)

set(monitoring_rec_json_src
src/monitoring_rec_json.cpp
)

ecal_add_sample(${PROJECT_NAME} ${monitoring_rec_json_src})

target_link_libraries(${PROJECT_NAME} eCAL::core)

target_link_libraries(${PROJECT_NAME} eCAL::core_pb)

target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_14)

ecal_install_sample(${PROJECT_NAME})

set_property(TARGET ${PROJECT_NAME} PROPERTY FOLDER samples/cpp/monitoring)
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* ========================= eCAL LICENSE =================================
*
* Copyright (C) 2016 - 2024 Continental Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ========================= eCAL LICENSE =================================
*/

#include <ecal/ecal.h>
#include <iostream>

int main(int argc, char** argv)
{
// initialize eCAL core API
eCAL::Initialize(argc, argv, "monitoring", eCAL::Init::All);

// monitor for ever
while (eCAL::Ok())
{
// take a snapshot as json string
std::string monitoring_s;
eCAL::Monitoring::GetMonitoringJSON(monitoring_s, eCAL::Monitoring::Entity::All);

// log it
std::cout << monitoring_s;

// sleep few milliseconds
eCAL::Process::SleepMS(1000);
}

// finalize eCAL API
eCAL::Finalize();

return(0);
}

0 comments on commit 0f51c70

Please sign in to comment.