diff --git a/userspace/libscap/metrics_v2.h b/userspace/libscap/metrics_v2.h index 10f17b1958..49dd91343e 100644 --- a/userspace/libscap/metrics_v2.h +++ b/userspace/libscap/metrics_v2.h @@ -39,6 +39,7 @@ extern "C" { #define METRICS_V2_STATE_COUNTERS (1 << 3) #define METRICS_V2_RULE_COUNTERS (1 << 4) #define METRICS_V2_MISC (1 << 5) +#define METRICS_V2_PLUGINS (1 << 6) typedef union metrics_v2_value { uint32_t u32; diff --git a/userspace/libsinsp/metrics_collector.cpp b/userspace/libsinsp/metrics_collector.cpp index 179da24999..00d7d3dfd6 100644 --- a/userspace/libsinsp/metrics_collector.cpp +++ b/userspace/libsinsp/metrics_collector.cpp @@ -20,6 +20,7 @@ limitations under the License. #include #include +#include #include #include #include @@ -456,6 +457,19 @@ void libs_metrics_collector::snapshot() return; } + /* + * plugins metrics + */ + + if(m_metrics_flags & METRICS_V2_PLUGINS) + { + for (auto& p : m_inspector->get_plugin_manager()->plugins()) + { + std::vector plugin_metrics = p->get_metrics(); + m_metrics.insert(m_metrics.end(), plugin_metrics.begin(), plugin_metrics.end()); + } + } + /* * libscap metrics */ diff --git a/userspace/libsinsp/metrics_collector.h b/userspace/libsinsp/metrics_collector.h index c816bf1bf4..147ac0aeef 100644 --- a/userspace/libsinsp/metrics_collector.h +++ b/userspace/libsinsp/metrics_collector.h @@ -271,7 +271,7 @@ class libs_metrics_collector private: sinsp* m_inspector; - uint32_t m_metrics_flags = METRICS_V2_KERNEL_COUNTERS | METRICS_V2_LIBBPF_STATS | METRICS_V2_RESOURCE_UTILIZATION | METRICS_V2_STATE_COUNTERS; + uint32_t m_metrics_flags = METRICS_V2_KERNEL_COUNTERS | METRICS_V2_LIBBPF_STATS | METRICS_V2_RESOURCE_UTILIZATION | METRICS_V2_STATE_COUNTERS | METRICS_V2_PLUGINS; std::vector m_metrics; void get_rss_vsz_pss_total_memory_and_open_fds(uint32_t &rss, uint32_t &vsz, uint32_t &pss, uint64_t &host_memory_used, uint64_t &host_open_fds); diff --git a/userspace/libsinsp/plugin.cpp b/userspace/libsinsp/plugin.cpp index d9ca41b655..d7f57b1a64 100755 --- a/userspace/libsinsp/plugin.cpp +++ b/userspace/libsinsp/plugin.cpp @@ -886,6 +886,77 @@ std::vector sinsp_plugin::list_open_params() const return list; } +static void set_plugin_metric_value(metrics_v2& metric, metrics_v2_value_type type, ss_plugin_metric_value val) +{ + switch (type) + { + case METRIC_VALUE_TYPE_U32: + metric.value.u32 = val.u32; + break; + case METRIC_VALUE_TYPE_S32: + metric.value.s32 = val.s32; + break; + case METRIC_VALUE_TYPE_U64: + metric.value.u64 = val.u64; + break; + case METRIC_VALUE_TYPE_S64: + metric.value.s64 = val.s64; + break; + case METRIC_VALUE_TYPE_D: + metric.value.d = val.d; + break; + case METRIC_VALUE_TYPE_F: + metric.value.f = val.f; + break; + case METRIC_VALUE_TYPE_I: + metric.value.i = val.i; + break; + default: + break; + } +} + +std::vector sinsp_plugin::get_metrics() const +{ + if(!m_inited) + { + throw sinsp_exception(std::string(s_not_init_err) + ": " + m_name); + } + + std::vector metrics; + uint32_t num_metrics = 0; + + if(!m_handle->api.get_metrics) + { + return metrics; + } + + ss_plugin_metric *plugin_metrics = m_handle->api.get_metrics(m_state, &num_metrics); + for (uint32_t i = 0; i < num_metrics; i++) + { + ss_plugin_metric *plugin_metric = plugin_metrics + i; + + metrics_v2 metric; + + //copy plugin name + int s = strlcpy(metric.name, m_name.c_str(), METRIC_NAME_MAX); + //copy dot + strlcpy(metric.name + s, ".", METRIC_NAME_MAX); + //copy metric name + strlcpy(metric.name + s + 1, plugin_metric->name, METRIC_NAME_MAX); + + metric.flags = METRICS_V2_PLUGINS; + metric.unit = METRIC_VALUE_UNIT_COUNT; + metric.type = static_cast(plugin_metric->value_type); + metric.metric_type = static_cast(plugin_metric->type); + set_plugin_metric_value(metric, metric.type, plugin_metric->value); + + metrics.emplace_back(metric); + } + + return metrics; +} + /** End of Event Source CAP **/ /** Field Extraction CAP **/ diff --git a/userspace/libsinsp/plugin.h b/userspace/libsinsp/plugin.h index cc93ccb624..49f2e3784f 100755 --- a/userspace/libsinsp/plugin.h +++ b/userspace/libsinsp/plugin.h @@ -165,6 +165,7 @@ class sinsp_plugin std::string get_last_error() const; std::string get_init_schema(ss_plugin_schema_type& schema_type) const; bool set_config(const std::string& config); + std::vector get_metrics() const; /** Event Sourcing **/ inline uint32_t id() const